Skip to content

Commit

Permalink
Merge pull request #74 from mbc-net/feat/sync-processing
Browse files Browse the repository at this point in the history
[core]: support for synchronous and asynchronous processing
  • Loading branch information
koichimurakami authored Nov 27, 2024
2 parents 9071c2d + f2de2a6 commit 79b3b3e
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"rootDir": "packages",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
"^.+\\.ts$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ DATABASE_URL="mysql://root:[email protected]:3306/cqrs?schema=public&connection_l

# serverless dynamodb local stream

LOCAL_DDB_TESTING-TABLE_STREAM=arn:aws:dynamodb:ddblocal:000000000000:table/local-demo-testing-table-command/stream/2024-09-20T07:06:05.837
LOCAL_DDB_TESTING_TABLE_STREAM=arn:aws:dynamodb:ddblocal:000000000000:table/local-demo-testing_table-command/stream/2024-09-20T07:06:05.837
141 changes: 115 additions & 26 deletions packages/core/src/commands/command.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { CommandService } from './command.service'
import { DataService } from './data.service'
import { addSortKeyVersion } from '../helpers/key'
import { BadRequestException } from '@nestjs/common'
import { ICommandOptions } from '../interfaces/command.options.interface'

const moduleMocker = new ModuleMocker(global)

Expand All @@ -24,30 +23,30 @@ function buildItem(key: DetailKey, version = 0, attributes = {}) {
}
}

const dataSet: {
command: CommandModel[]
data: DataModel[]
} = {
command: [
buildItem({ pk: 'master', sk: 'max_value@1' }, 1),
buildItem({ pk: 'master', sk: 'max_value@2' }, 2),
buildItem({ pk: 'master', sk: 'max_value@3' }, 3),
buildItem({ pk: 'master', sk: 'max_value@4' }, 3),
buildItem({ pk: 'master', sk: 'max_value@5' }, 5),
buildItem({ pk: 'master', sk: 'max_value@6' }, 6),
buildItem({ pk: 'master', sk: 'max_value@7' }, 7),
buildItem({ pk: 'master', sk: 'max_value@8' }, 8),
buildItem({ pk: 'master', sk: 'max_value@9' }, 9),
buildItem({ pk: 'master', sk: 'max_value@10' }, 10),
buildItem({ pk: 'master', sk: 'max_value@11' }, 11),
],
data: [buildItem({ pk: 'master', sk: 'max_value' }, 5)],
}

describe('CommandService', () => {
let commandService: CommandService

beforeEach(async () => {
const dataSet: {
command: CommandModel[]
data: DataModel[]
} = {
command: [
buildItem({ pk: 'master', sk: 'max_value@1' }, 1),
buildItem({ pk: 'master', sk: 'max_value@2' }, 2),
buildItem({ pk: 'master', sk: 'max_value@3' }, 3),
buildItem({ pk: 'master', sk: 'max_value@4' }, 3),
buildItem({ pk: 'master', sk: 'max_value@5' }, 5),
buildItem({ pk: 'master', sk: 'max_value@6' }, 6),
buildItem({ pk: 'master', sk: 'max_value@7' }, 7),
buildItem({ pk: 'master', sk: 'max_value@8' }, 8),
buildItem({ pk: 'master', sk: 'max_value@9' }, 9),
buildItem({ pk: 'master', sk: 'max_value@10' }, 10),
buildItem({ pk: 'master', sk: 'max_value@11' }, 11),
],
data: [buildItem({ pk: 'master', sk: 'max_value' }, 5)],
}

const moduleRef = await Test.createTestingModule({
providers: [CommandService, DataService],
})
Expand Down Expand Up @@ -118,7 +117,7 @@ describe('CommandService', () => {
})
})

describe('publishPartialUpdate', () => {
describe('publishPartialUpdateAsync', () => {
it('should update with the latest item', async () => {
const key = {
pk: 'master',
Expand All @@ -130,7 +129,7 @@ describe('CommandService', () => {
name: '-1',
}
const latestItem = await commandService.getLatestItem(key)
const item = await commandService.publishPartialUpdate(inputItem, {
const item = await commandService.publishPartialUpdateAsync(inputItem, {
invokeContext: {},
})
expect(item).toBeDefined()
Expand All @@ -147,7 +146,7 @@ describe('CommandService', () => {
version: -1,
name: '-1',
}
const call = commandService.publishPartialUpdate(inputItem, {
const call = commandService.publishPartialUpdateAsync(inputItem, {
invokeContext: {},
})
expect(call).rejects.toThrow(
Expand Down Expand Up @@ -175,15 +174,15 @@ describe('CommandService', () => {
).toBe(false)
})

it('should return true if input atributes is class instance', () => {
it('should return true if input attributes is class instance', () => {
const item = buildItem({ pk: 'master', sk: 'max_value@5' }, 5, {
name: 'test',
})
const input = { ...item, attributes: new MasterAttributes('test') }
expect(commandService.isNotCommandDirty(item, input)).toBe(true)
})

it('should return false if input atributes is class instance and not dirty', () => {
it('should return false if input attributes is class instance and not dirty', () => {
const item = buildItem({ pk: 'master', sk: 'max_value@5' }, 5, {
name: 'test',
})
Expand All @@ -192,4 +191,94 @@ describe('CommandService', () => {
expect(commandService.isNotCommandDirty(item, input)).toBe(false)
})
})

describe('publishPartialUpdateSync', () => {
it('should update with the latest version item', async () => {
const key = {
pk: 'master',
sk: 'max_value',
}
const inputItem = {
...key,
version: 5,
name: '-1',
}
const item = await commandService.publishPartialUpdateSync(inputItem, {
invokeContext: {},
})
expect(item).toBeDefined()
expect(item).toMatchObject({
...inputItem,
version: 6,
})
})

it('should raise error with invalid version', async () => {
const key = {
pk: 'master',
sk: 'max_value',
}
const inputItem = {
...key,
version: 4,
name: '4',
}
const res = commandService.publishPartialUpdateSync(inputItem, {
invokeContext: {},
})
expect(res).rejects.toThrow(
new BadRequestException(
'The input is not a valid, item not found or version not match',
),
)
})

it('should raise error with item not found', async () => {
const key = {
pk: 'master',
sk: 'max_value_',
}
const inputItem = {
...key,
version: 4,
name: '4',
}
const res = commandService.publishPartialUpdateSync(inputItem, {
invokeContext: {},
})
expect(res).rejects.toThrow(
new BadRequestException(
'The input is not a valid, item not found or version not match',
),
)
})
})

describe('publishSync', () => {
it('should update with the latest version item', async () => {
const inputItem = buildItem({ pk: 'master', sk: 'max_value' }, -1)

const item = await commandService.publishSync(inputItem, {
invokeContext: {},
})
console.log('$@#$@', item)
expect(item).toBeDefined()
expect(item).toMatchObject({
...inputItem,
version: 6,
})
})

it('should raise error with invalid input version', async () => {
const inputItem = buildItem({ pk: 'master', sk: 'max_value' }, 4)
const res = commandService.publishSync(inputItem, {
invokeContext: {},
})
expect(res).rejects.toThrow(
new BadRequestException(
'Invalid input version. The input version must be equal to the latest version',
),
)
})
})
})
Loading

0 comments on commit 79b3b3e

Please sign in to comment.