Skip to content
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 5 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
},
},
}
`;
9 changes: 9 additions & 0 deletions packages/core/src/base/fixtures/base.fixture.test.ts
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()
})
})
13 changes: 13 additions & 0 deletions packages/core/src/base/fixtures/base.fixture.ts
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)
}
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 = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你这里倒是替换一下你新写的那个呀

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,
)
})
})
3 changes: 2 additions & 1 deletion packages/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"clean": "rimraf dist",
"coverage": "vitest run --coverage",
"dev": "tsup --watch",
"prebuild": "pnpm run clean"
"prebuild": "pnpm run clean",
"test":"vitest run"
},
"type": "module",
"types": "dist/index.d",
Expand Down
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",
}
`;
9 changes: 9 additions & 0 deletions packages/template/src/fixtures/template.fixture.test.ts
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()
})
})
89 changes: 89 additions & 0 deletions packages/template/src/fixtures/template.fixture.ts
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)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CompositeSpecification } from '@undb/domain'
import { Ok, type Result } from 'oxide.ts'
import type { ITemplateExportSchema } from 'src/value-objects/template-export.vo.js'
import { TemplateExport } from 'src/value-objects/template-export.vo.js'
import type { ITemplateExportSchema } from '../value-objects/template-export.vo.js'
import { TemplateExport } from '../value-objects/template-export.vo.js'
import type { ITemplateVisitor } from '../interface.js'
import type { Template } from '../template.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CompositeSpecification } from '@undb/domain'
import { Ok, type Result } from 'oxide.ts'
import { TemplateID } from 'src/value-objects/template-id.vo.js'
import { TemplateID } from '../value-objects/template-id.vo.js'
import type { ITemplateVisitor } from '../interface.js'
import type { Template } from '../template.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CompositeSpecification } from '@undb/domain'
import { Ok, type Result } from 'oxide.ts'
import { TemplateName } from 'src/value-objects/template-name.vo.js'
import { TemplateName } from '../value-objects/template-name.vo.js'
import type { ITemplateVisitor } from '../interface.js'
import type { Template } from '../template.js'

Expand Down
Loading
Loading