-
-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: test import template command handler #1599
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa8fd8e
test: test import template command handler
WanShufen caeea1f
test: test import template command handler
WanShufen 72ee6ff
Merge branch 'develop' into import-template-command-handler-test
WanShufen 2baf662
feat: add creatTestBase and createTestTemplate functionalities
Sophie4140 5fc9380
chore: code optimization
Sophie4140 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/core/src/base/fixtures/__snapshots__/base.fixture.test.ts.snap
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`base fixture test > create test base success 1`] = ` | ||
Base { | ||
"id": BaseId { | ||
"props": { | ||
"value": "baseId", | ||
}, | ||
}, | ||
"name": BaseName { | ||
"props": { | ||
"value": "name", | ||
}, | ||
}, | ||
} | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createTestBase } from './base.fixture' | ||
|
||
describe('base fixture test', () => { | ||
test('create test base success', () => { | ||
const base = createTestBase() | ||
|
||
expect(base).toMatchSnapshot() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BaseFactory } from '../base.factory' | ||
import type { BaseSpecification } from '../interface' | ||
import { WithBaseId, WithBaseName } from '../specifications' | ||
|
||
export const createTestBase = (...specs: BaseSpecification[]) => { | ||
let spec = WithBaseId.fromString('baseId').and(WithBaseName.fromString('name')) | ||
|
||
for (const s of specs) { | ||
spec = spec.and(s) | ||
} | ||
|
||
return BaseFactory.create(spec) | ||
} |
132 changes: 132 additions & 0 deletions
132
packages/cqrs/src/commands/import-template/import-template.command.handler.test.ts
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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { ClsStore, IClsService, IRecordRepository, ITableRepository, UserId } from '@undb/core' | ||
import { ITemplateSchema, TemplateFactory } from '@undb/template' | ||
import { MockProxy, mock } from 'vitest-mock-extended' | ||
import { ImportTemplateCommand } from './import-template.command' | ||
import { ImportTemplateCommandHandler } from './import-template.command.handler' | ||
import { SpyInstance } from 'vitest' | ||
|
||
describe('import template command hanlder test ', () => { | ||
let repo: MockProxy<ITableRepository> | ||
let recordRepo: MockProxy<IRecordRepository> | ||
let cls: MockProxy<IClsService<ClsStore>> | ||
let handler: ImportTemplateCommandHandler | ||
const userId: UserId = new UserId('user1') | ||
let spy: SpyInstance | ||
let tabSpy: SpyInstance | ||
const templateInput: ITemplateSchema = { | ||
id: 'tplt1wjru6o', | ||
name: 'testTable', | ||
export: { | ||
tables: [ | ||
{ | ||
id: 'tblsj55f6su', | ||
name: 'testTable', | ||
schema: [ | ||
{ | ||
id: 'fldn2672c7n', | ||
name: 'id', | ||
display: false, | ||
required: false, | ||
type: 'id', | ||
}, | ||
{ | ||
id: 'fld6elro1gn', | ||
name: 'name', | ||
display: true, | ||
required: false, | ||
type: 'string', | ||
}, | ||
{ | ||
id: 'fldkek4etmf', | ||
name: 'createdAt', | ||
display: false, | ||
required: false, | ||
type: 'created-at', | ||
format: 'yyyy-MM-dd', | ||
timeFormat: null, | ||
}, | ||
{ | ||
id: 'fldx2ekp10u', | ||
name: 'createdBy', | ||
display: false, | ||
required: false, | ||
type: 'created-by', | ||
}, | ||
{ | ||
id: 'fldmvgkyxkj', | ||
name: 'updatedAt', | ||
display: false, | ||
required: false, | ||
type: 'updated-at', | ||
format: 'yyyy-MM-dd', | ||
timeFormat: null, | ||
}, | ||
{ | ||
id: 'fld9v4iiuys', | ||
name: 'updatedBy', | ||
display: false, | ||
required: false, | ||
type: 'updated-by', | ||
}, | ||
], | ||
views: [ | ||
{ | ||
id: 'viwryu43tdx', | ||
name: 'testTable', | ||
showSystemFields: false, | ||
dashboard: { | ||
widgets: [], | ||
}, | ||
displayType: 'grid', | ||
fieldOptions: {}, | ||
rowHeight: 'short', | ||
}, | ||
], | ||
viewsOrder: ['viwryu43tdx'], | ||
records: [ | ||
{ | ||
id: 'recidpnmpp6', | ||
values: { | ||
fld6elro1gn: 'test data', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
} | ||
const template = TemplateFactory.fromJSON(templateInput) | ||
|
||
beforeEach(() => { | ||
repo = mock<ITableRepository>() | ||
recordRepo = mock<IRecordRepository>() | ||
cls = mock<IClsService<ClsStore>>() | ||
spy = vi.spyOn(TemplateFactory, 'fromJSON').mockReturnValue(template) | ||
handler = new ImportTemplateCommandHandler(repo, recordRepo, cls) | ||
tabSpy = vi.spyOn(template.export, 'toTables') | ||
cls.get.calledWith('user.userId').mockReturnValue(userId.value) | ||
}) | ||
|
||
test('import template success and includeRecords is false', async () => { | ||
const command = new ImportTemplateCommand({ template: templateInput }) | ||
|
||
await handler.execute(command) | ||
|
||
expect(tabSpy).toHaveBeenCalledOnce() | ||
expect(repo.insert).toHaveBeenCalledWith(tabSpy.mock.results[0].value[0].table) | ||
expect(recordRepo.insertMany).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('import template success includeRecords is true', async () => { | ||
const command = new ImportTemplateCommand({ template: templateInput, includeRecords: true }) | ||
|
||
await handler.execute(command) | ||
|
||
expect(tabSpy).toHaveBeenCalledOnce() | ||
expect(repo.insert).toHaveBeenCalledWith(tabSpy.mock.results[0].value[0].table) | ||
expect(recordRepo.insertMany).toHaveBeenCalledWith( | ||
tabSpy.mock.results[0].value[0].table, | ||
tabSpy.mock.results[0].value[0].records, | ||
) | ||
}) | ||
}) |
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
88 changes: 88 additions & 0 deletions
88
packages/template/src/fixtures/__snapshots__/template.fixture.test.ts.snap
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`test createTestTemplate > create default test template success 1`] = ` | ||
{ | ||
"export": { | ||
"tables": [ | ||
{ | ||
"id": "tblh5muva7x", | ||
"name": "testTable", | ||
"records": [ | ||
{ | ||
"id": "recnrwbwi0h", | ||
"values": { | ||
"fldy08vpazg": "test data", | ||
}, | ||
}, | ||
], | ||
"schema": [ | ||
{ | ||
"display": false, | ||
"id": "fldlvkhvhqz", | ||
"name": "id", | ||
"required": false, | ||
"type": "id", | ||
}, | ||
{ | ||
"display": true, | ||
"id": "fldy08vpazg", | ||
"name": "name", | ||
"required": false, | ||
"type": "string", | ||
}, | ||
{ | ||
"display": false, | ||
"format": "yyyy-MM-dd", | ||
"id": "fld2p5gpabk", | ||
"name": "createdAt", | ||
"required": false, | ||
"timeFormat": null, | ||
"type": "created-at", | ||
}, | ||
{ | ||
"display": false, | ||
"id": "fldzpsh2eed", | ||
"name": "createdBy", | ||
"required": false, | ||
"type": "created-by", | ||
}, | ||
{ | ||
"display": false, | ||
"format": "yyyy-MM-dd", | ||
"id": "fldeswxg3sh", | ||
"name": "updatedAt", | ||
"required": false, | ||
"timeFormat": null, | ||
"type": "updated-at", | ||
}, | ||
{ | ||
"display": false, | ||
"id": "fldyl028jku", | ||
"name": "updatedBy", | ||
"required": false, | ||
"type": "updated-by", | ||
}, | ||
], | ||
"views": [ | ||
{ | ||
"dashboard": { | ||
"widgets": [], | ||
}, | ||
"displayType": "grid", | ||
"fieldOptions": {}, | ||
"id": "viw1x8vbpmb", | ||
"name": "testTable", | ||
"rowHeight": "short", | ||
"showSystemFields": false, | ||
}, | ||
], | ||
"viewsOrder": [ | ||
"viw1x8vbpmb", | ||
], | ||
}, | ||
], | ||
}, | ||
"id": "tplt1wjru6o", | ||
"name": "testTable", | ||
} | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createTestTemplate } from './template.fixture' | ||
|
||
describe('test createTestTemplate', () => { | ||
test('create default test template success', () => { | ||
const template = createTestTemplate() | ||
|
||
expect(template).toMatchSnapshot() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type { ITemplateSchema } from '../template.schema' | ||
import { TemplateFactory } from '../template.factory' | ||
|
||
export const createTestTemplate = () => { | ||
const templateInput: ITemplateSchema = { | ||
id: 'tplt1wjru6o', | ||
name: 'testTable', | ||
export: { | ||
tables: [ | ||
{ | ||
id: 'tblsj55f6su', | ||
name: 'testTable', | ||
schema: [ | ||
{ | ||
id: 'fldn2672c7n', | ||
name: 'id', | ||
display: false, | ||
required: false, | ||
type: 'id', | ||
}, | ||
{ | ||
id: 'fld6elro1gn', | ||
name: 'name', | ||
display: true, | ||
required: false, | ||
type: 'string', | ||
}, | ||
{ | ||
id: 'fldkek4etmf', | ||
name: 'createdAt', | ||
display: false, | ||
required: false, | ||
type: 'created-at', | ||
format: 'yyyy-MM-dd', | ||
timeFormat: null, | ||
}, | ||
{ | ||
id: 'fldx2ekp10u', | ||
name: 'createdBy', | ||
display: false, | ||
required: false, | ||
type: 'created-by', | ||
}, | ||
{ | ||
id: 'fldmvgkyxkj', | ||
name: 'updatedAt', | ||
display: false, | ||
required: false, | ||
type: 'updated-at', | ||
format: 'yyyy-MM-dd', | ||
timeFormat: null, | ||
}, | ||
{ | ||
id: 'fld9v4iiuys', | ||
name: 'updatedBy', | ||
display: false, | ||
required: false, | ||
type: 'updated-by', | ||
}, | ||
], | ||
views: [ | ||
{ | ||
id: 'viwryu43tdx', | ||
name: 'testTable', | ||
showSystemFields: false, | ||
dashboard: { | ||
widgets: [], | ||
}, | ||
displayType: 'grid', | ||
fieldOptions: {}, | ||
rowHeight: 'short', | ||
}, | ||
], | ||
viewsOrder: ['viwryu43tdx'], | ||
records: [ | ||
{ | ||
id: 'recidpnmpp6', | ||
values: { | ||
fld6elro1gn: 'test data', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
} | ||
|
||
return TemplateFactory.fromJSON(templateInput) | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/template/src/specifications/template-export.specification.ts
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
2 changes: 1 addition & 1 deletion
2
packages/template/src/specifications/template-id.specification.ts
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
2 changes: 1 addition & 1 deletion
2
packages/template/src/specifications/template-name.specification.ts
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你这里倒是替换一下你新写的那个呀