forked from nocobase/nocobase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(action-import): skip reset sequence if no auto-increment primar…
…y is imported (nocobase#4631) * chore: auto incr primary key imported test * chore: skip reset sequence if no auto increment primary imported
- Loading branch information
Showing
2 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,7 +206,94 @@ describe('xlsx importer', () => { | |
expect(user2Json['boolean']).toBe(false); | ||
}); | ||
|
||
it('should reset id seq after import', async () => { | ||
it('should not reset id seq if not import id field', async () => { | ||
const User = app.db.collection({ | ||
name: 'users', | ||
autoGenId: false, | ||
fields: [ | ||
{ | ||
type: 'bigInt', | ||
name: 'id', | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
{ | ||
type: 'string', | ||
name: 'name', | ||
}, | ||
{ | ||
type: 'string', | ||
name: 'email', | ||
}, | ||
], | ||
}); | ||
|
||
await app.db.sync(); | ||
|
||
const templateCreator = new TemplateCreator({ | ||
collection: User, | ||
columns: [ | ||
{ | ||
dataIndex: ['name'], | ||
defaultTitle: '姓名', | ||
}, | ||
{ | ||
dataIndex: ['email'], | ||
defaultTitle: '邮箱', | ||
}, | ||
], | ||
}); | ||
|
||
const template = await templateCreator.run(); | ||
|
||
const worksheet = template.Sheets[template.SheetNames[0]]; | ||
|
||
XLSX.utils.sheet_add_aoa( | ||
worksheet, | ||
[ | ||
['User1', '[email protected]'], | ||
['User2', '[email protected]'], | ||
], | ||
{ | ||
origin: 'A2', | ||
}, | ||
); | ||
|
||
const importer = new XlsxImporter({ | ||
collectionManager: app.mainDataSource.collectionManager, | ||
collection: User, | ||
columns: [ | ||
{ | ||
dataIndex: ['name'], | ||
defaultTitle: '姓名', | ||
}, | ||
{ | ||
dataIndex: ['email'], | ||
defaultTitle: '邮箱', | ||
}, | ||
], | ||
workbook: template, | ||
}); | ||
|
||
const testFn = vi.fn(); | ||
importer.on('seqReset', testFn); | ||
|
||
await importer.run(); | ||
|
||
expect(await User.repository.count()).toBe(2); | ||
|
||
const user3 = await User.repository.create({ | ||
values: { | ||
name: 'User3', | ||
email: '[email protected]', | ||
}, | ||
}); | ||
|
||
expect(user3.get('id')).toBe(3); | ||
expect(testFn).not.toBeCalled(); | ||
}); | ||
|
||
it('should reset id seq after import id field', async () => { | ||
const User = app.db.collection({ | ||
name: 'users', | ||
autoGenId: false, | ||
|
@@ -293,6 +380,9 @@ describe('xlsx importer', () => { | |
workbook: template, | ||
}); | ||
|
||
const testFn = vi.fn(); | ||
importer.on('seqReset', testFn); | ||
|
||
await importer.run(); | ||
|
||
expect(await User.repository.count()).toBe(2); | ||
|
@@ -305,6 +395,8 @@ describe('xlsx importer', () => { | |
}); | ||
|
||
expect(user3.get('id')).toBe(3); | ||
|
||
expect(testFn).toBeCalled(); | ||
}); | ||
|
||
it('should validate workbook with error', async () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters