-
Notifications
You must be signed in to change notification settings - Fork 99
feat: local provider scheduled command adapter unit test #1095
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
Closed
hackykitty
wants to merge
3
commits into
boostercloud:main
from
hackykitty:feat/local-scheduled-unit-test
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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
102 changes: 102 additions & 0 deletions
102
packages/framework-provider-local-infrastructure/test/scheduler.test.ts
This file contains hidden or 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,102 @@ | ||
| import { BoosterConfig } from '@boostercloud/framework-types' | ||
| import { restore, SinonStubbedInstance, createStubInstance, replace, fake } from 'sinon' | ||
| import { configureScheduler } from '../src/scheduler' | ||
| import { expect } from './expect' | ||
| import { describe } from 'mocha' | ||
| import { random } from 'faker' | ||
| import * as scheduler from 'node-schedule' | ||
|
|
||
| const rewire = require('rewire') | ||
| const schedule = rewire('../src/scheduler') | ||
| const createCronExpression = schedule.__get__('createCronExpression') | ||
| const buildScheduledCommandInfo = schedule.__get__('buildScheduledCommandInfo') | ||
| interface ScheduledCommandInfo { | ||
| typeName: string | ||
| } | ||
| declare class UserProject { | ||
| constructor() | ||
| boosterTriggerScheduledCommand: (rawRequest: unknown) => void | ||
| } | ||
|
|
||
| describe('Local Scheduler', () => { | ||
| let mockUserProjectStub: SinonStubbedInstance<UserProject> | ||
| let mockConfig: BoosterConfig | ||
| let mockScheduledCommandName: string | ||
|
|
||
| beforeEach(() => { | ||
| class CheckCart { | ||
| public static async handle(): Promise<void> { | ||
| console.log('handle') | ||
| } | ||
| } | ||
| class UserProject { | ||
| boosterTriggerScheduledCommand(rawRequest: unknown): void { | ||
| console.log('rawRequest: ', rawRequest) | ||
| } | ||
| } | ||
|
|
||
| mockScheduledCommandName = random.word() | ||
|
|
||
| mockUserProjectStub = createStubInstance(UserProject) | ||
|
|
||
| mockConfig = buildConfig() | ||
| mockConfig.scheduledCommandHandlers[mockScheduledCommandName] = { | ||
| class: CheckCart, | ||
| scheduledOn: {}, | ||
| } | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| restore() | ||
| }) | ||
|
|
||
| describe('buildScheduledCommandInfo', () => { | ||
| it('should return expected scheduled command info', async () => { | ||
| const result = await buildScheduledCommandInfo(mockConfig, mockScheduledCommandName) | ||
| expect(result).to.be.deep.equal({ | ||
| name: mockScheduledCommandName, | ||
| metadata: mockConfig.scheduledCommandHandlers[mockScheduledCommandName], | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('createCronExpression', () => { | ||
| it('should return default cron expression', async () => { | ||
| const result = await createCronExpression({ scheduledOn: {} }) | ||
| expect(result).to.be.equal('* * * * * *') | ||
hackykitty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
|
|
||
| it('should return expected cron expression', async () => { | ||
| const result = await createCronExpression({ | ||
| scheduledOn: { | ||
| minute: '30', | ||
| hour: '14', | ||
| weekDay: '0', | ||
| }, | ||
| }) | ||
| expect(result).to.be.equal('* 30 14 * * 0') | ||
| }) | ||
| }) | ||
|
|
||
| describe('configureScheduler', () => { | ||
| it('should call scedule job', async () => { | ||
| const fakeScheduleJob = fake((name: string, cronExpression: string, scheduledFunction: () => void) => { | ||
| scheduledFunction() | ||
| }) | ||
| const fakeTriggerScheduleCommand = fake((command: ScheduledCommandInfo) => {}) | ||
| replace(scheduler, 'scheduleJob', fakeScheduleJob) | ||
| replace(mockUserProjectStub, 'boosterTriggerScheduledCommand', fakeTriggerScheduleCommand as any) | ||
|
|
||
| configureScheduler(mockConfig, mockUserProjectStub) | ||
|
|
||
| expect(scheduler.scheduleJob).to.have.been.calledWith(mockScheduledCommandName, '* * * * * *') | ||
| expect(mockUserProjectStub.boosterTriggerScheduledCommand).to.have.been.calledWith({ | ||
| typeName: mockScheduledCommandName, | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| function buildConfig(): BoosterConfig { | ||
| return new BoosterConfig('test') | ||
| } | ||
| }) | ||
2 changes: 1 addition & 1 deletion
2
packages/framework-provider-local/src/library/scheduled-adapter.ts
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
packages/framework-provider-local/test/helpers/scheduled-helper.ts
This file contains hidden or 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,11 @@ | ||
| import { random } from 'faker' | ||
|
|
||
| interface LocalScheduleCommandEnvelope { | ||
| typeName: string | ||
| } | ||
|
|
||
| export function createMockLocalScheduleCommandEnvelope(): Partial<LocalScheduleCommandEnvelope> { | ||
| return { | ||
| typeName: random.word() | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
packages/framework-provider-local/test/library/scheduled-adapter.test.ts
This file contains hidden or 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,65 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import { SinonStub, stub, replace, restore } from 'sinon' | ||
| import { rawScheduledInputToEnvelope, LocalScheduleCommandEnvelope } from '../../src/library/scheduled-adapter' | ||
| import { createMockLocalScheduleCommandEnvelope } from '../helpers/scheduled-helper' | ||
| import { UUID } from '@boostercloud/framework-types' | ||
| import { expect } from '../expect' | ||
| import { random } from 'faker' | ||
|
|
||
| describe('Local provider scheduled-adapter', () => { | ||
| describe('rawScheduledInputToEnvelope', () => { | ||
| let mockScheduledEnvelop: Partial<LocalScheduleCommandEnvelope> | ||
| let mockEmptyScheduledEnvelop: Partial<unknown> | ||
| let mockUuid: string | ||
|
|
||
| let debugStub: SinonStub | ||
| let generateStub: SinonStub | ||
|
|
||
| let logger: any | ||
|
|
||
| beforeEach(() => { | ||
| mockUuid = random.uuid() | ||
|
|
||
| mockScheduledEnvelop = createMockLocalScheduleCommandEnvelope() | ||
| mockEmptyScheduledEnvelop = {} | ||
|
|
||
| debugStub = stub() | ||
| generateStub = stub().returns(mockUuid) | ||
|
|
||
| logger = { | ||
| debug: debugStub, | ||
| } | ||
| replace(UUID, 'generate', generateStub) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| restore() | ||
| }) | ||
|
|
||
| it('should call logger.debug', async () => { | ||
| await rawScheduledInputToEnvelope(mockScheduledEnvelop, logger) | ||
|
|
||
| expect(debugStub).to.have.been.calledOnceWith( | ||
| 'Received LocalScheduleCommand request: ', | ||
| mockScheduledEnvelop | ||
| ) | ||
| }) | ||
|
|
||
| it('should thrown an exception for empty typeName', async () => { | ||
| const error = new Error( | ||
| `typeName is not defined or empty, scheduled command envelope should have the structure {typeName: string }, but you gave ${JSON.stringify( | ||
| mockEmptyScheduledEnvelop | ||
| )}` | ||
| ) | ||
| expect(rawScheduledInputToEnvelope(mockEmptyScheduledEnvelop, logger)).to.be.rejectedWith(error) | ||
| }) | ||
|
|
||
| it('should generate expected envelop', async () => { | ||
| const result = await rawScheduledInputToEnvelope(mockScheduledEnvelop, logger) | ||
| expect(result).to.be.deep.equal({ | ||
| requestID: mockUuid, | ||
| typeName: mockScheduledEnvelop.typeName | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.