test(collection-manager): migration - 20221104151410-update-collections-hidden test optimize (#1040)

This commit is contained in:
lyf-coder 2022-11-04 20:44:12 +08:00 committed by GitHub
parent dd20951323
commit 8996dc6899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import { Database, MigrationContext, mockDatabase } from '@nocobase/database'; import { Database, MigrationContext} from '@nocobase/database';
import UpdateCollectionsHiddenMigration from '../../migrations/20221104151410-update-collections-hidden'; import UpdateCollectionsHiddenMigration from '../../migrations/20221104151410-update-collections-hidden';
import { createApp } from '../index'; import { createApp } from '../index';
@ -25,23 +25,44 @@ describe('migration 20221104151410-update-collections-hidden test', () => {
}, },
hidden: false, hidden: false,
}); });
await db.getCollection('collections').model.create({
name: 'test1',
hidden: false,
});
await db.getCollection('collections').model.create({
name: 'test2',
options: {
isThrough: true,
},
hidden: false,
});
const result = await db.getRepository('collections').findOne({ const result = await db.getRepository('collections').find({
filter: { filter: {
name: 'test', name: 'test',
}, },
}); });
expect(result.get('hidden')).toBeFalsy(); expect(result.length).toBe(1);
expect(result[0].get('hidden')).toBeFalsy();
const migration = new UpdateCollectionsHiddenMigration({ db } as MigrationContext); const migration = new UpdateCollectionsHiddenMigration({ db } as MigrationContext);
migration.context.app = app; migration.context.app = app;
await migration.up(); await migration.up();
const upResult = await db.getRepository('collections').findOne({
const upResult = await db.getRepository('collections').find({
filter: { filter: {
name: 'test', name: 'test',
}, },
}); });
expect(upResult.get('hidden')).toBeTruthy();
expect(upResult[0].get('hidden')).toBeTruthy();
const hiddenResult = await db.getRepository('collections').find({
filter: {
hidden: true,
},
});
expect(hiddenResult.length).toBe(1);
}); });
}); });

View File

@ -6,10 +6,8 @@ export default class UpdateCollectionsHiddenMigration extends Migration {
if (!result) { if (!result) {
return; return;
} }
const db = this.app.db;
const transaction = await db.sequelize.transaction();
try { try {
await db.getRepository('collections').update({ await this.app.db.getRepository('collections').update({
filter: { filter: {
options: { options: {
autoCreate: true, autoCreate: true,
@ -19,13 +17,9 @@ export default class UpdateCollectionsHiddenMigration extends Migration {
values: { values: {
hidden: true, hidden: true,
}, },
transaction,
}); });
await transaction.commit();
} catch (error) { } catch (error) {
console.log(error); console.error(error);
await transaction.rollback();
} }
} }
} }