Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cliff0412 committed Feb 26, 2019
1 parent a1cf558 commit 2f7f9df
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 60 deletions.
37 changes: 27 additions & 10 deletions src/services/MarketDataService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test('retry within short time', () => {

test('launchSource fail windows', () => {
osUtil.isWindows = jest.fn(() => true);
child_process.exec = jest.fn() as any;
child_process.exec = jest.fn(() => false) as any;
util.getUTCNowTimestamp = jest.fn(() => 1234567890);
marketDataService.retry = jest.fn();
marketDataService.subProcesses['source'] = {
Expand All @@ -79,9 +79,14 @@ test('launchSource fail windows', () => {
dynamo: true,
azure: true
} as any);
const originalLaunchSource = marketDataService.launchSource;
marketDataService.launchSource = jest.fn();
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
expect((marketDataService.retry as jest.Mock<void>).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect((marketDataService.launchSource as jest.Mock).mock.calls).toMatchSnapshot();
marketDataService.launchSource = originalLaunchSource;
});

test('launchSource success windows', () => {
Expand All @@ -106,12 +111,17 @@ test('launchSource success windows', () => {
live: false,
aws: true
} as any);
const originalLaunchSource = marketDataService.launchSource;
marketDataService.launchSource = jest.fn();
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
expect(execOn.mock.calls).toMatchSnapshot();
execOn.mock.calls[0][1]();
execOn.mock.calls[0][1](1);
expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect((marketDataService.launchSource as jest.Mock).mock.calls).toMatchSnapshot();
marketDataService.launchSource = originalLaunchSource;
});

test('launchSource forceREST windows', () => {
Expand Down Expand Up @@ -320,20 +330,24 @@ test('launchEvent azure', () => {
event: 'event',
azure: true
} as any);
const originalLaunchEvent = marketDataService.launchEvent;
marketDataService.launchEvent = jest.fn();
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot();
(on as jest.Mock).mock.calls[0][1]('');
(on as jest.Mock).mock.calls[0][1]();
(on as jest.Mock).mock.calls[0][1]('code');
expect((on as jest.Mock).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect(marketDataService.subProcesses).toMatchSnapshot();
expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot();
marketDataService.launchEvent = originalLaunchEvent;
});

test('launchEvent with existingInstance, no windows', () => {
const on = jest.fn();
osUtil.isWindows = jest.fn(() => true);
child_process.exec = jest.fn(() => ({
on: on
})) as any;
child_process.exec = jest.fn(() => null) as any;
util.getUTCNowTimestamp = jest.fn(() => 1234567890);
marketDataService.retry = jest.fn();
marketDataService.subProcesses['Others'] = {
source: 'Others',
lastFailTimestamp: 0,
Expand All @@ -348,10 +362,13 @@ test('launchEvent with existingInstance, no windows', () => {
server: true,
aws: true
} as any);
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
const originalLaunchEvent = marketDataService.launchEvent;
marketDataService.launchEvent = jest.fn();
expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
(on as jest.Mock).mock.calls[0][1]('code');
expect((on as jest.Mock).mock.calls).toMatchSnapshot();
marketDataService.launchEvent = originalLaunchEvent;
});

test('startFetching no source', async () => {
Expand Down
135 changes: 85 additions & 50 deletions src/services/__snapshots__/MarketDataService.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ Array [
]
`;

exports[`launchEvent azure 2`] = `
exports[`launchEvent azure 2`] = `Array []`;

exports[`launchEvent azure 3`] = `
Array [
Array [
"exit",
[Function],
],
]
`;

exports[`launchEvent azure 4`] = `
Object {
"event": Object {
"failCount": 0,
Expand Down Expand Up @@ -75,46 +86,53 @@ Object {
}
`;

exports[`launchEvent azure 3`] = `Array []`;

exports[`launchEvent azure 4`] = `
exports[`launchEvent azure 5`] = `
Array [
Array [
"exit",
[Function],
"tool",
"event",
Object {
"azure": true,
"debug": false,
"event": "event",
"forceREST": false,
"live": true,
},
],
]
`;

exports[`launchEvent with existingInstance, no windows 1`] = `
Array [
Array [
"npm run tool event=Others dynamo aws server>> tool.Others.log",
Object {},
"Others",
[Function],
],
]
`;

exports[`launchEvent with existingInstance, no windows 2`] = `
Array [
Array [
"tool",
"Others",
Object {
"aws": true,
"debug": false,
"event": "Others",
"forceREST": false,
"live": true,
"server": true,
},
],
]
`;

exports[`launchEvent with existingInstance, no windows 3`] = `
Object {
"Others": Object {
"failCount": 0,
"instance": Object {
"on": [MockFunction] {
"calls": Array [
Array [
"exit",
[Function],
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
},
},
"instance": null,
"lastFailTimestamp": 1234567890,
"source": "Others",
},
Expand Down Expand Up @@ -148,15 +166,6 @@ Object {
}
`;

exports[`launchEvent with existingInstance, no windows 3`] = `
Array [
Array [
"exit",
[Function],
],
]
`;

exports[`launchSource debug not windows 1`] = `
Array [
Array [
Expand Down Expand Up @@ -243,7 +252,7 @@ exports[`launchSource fail windows 2`] = `
Object {
"source": Object {
"failCount": 0,
"instance": undefined,
"instance": false,
"lastFailTimestamp": 1234567890,
"source": "source",
},
Expand All @@ -259,6 +268,27 @@ Array [
]
`;

exports[`launchSource fail windows 4`] = `
Array [
Array [
"tool",
"source",
Array [
"asset1",
"asset2",
"asset3",
],
Object {
"azure": true,
"debug": false,
"dynamo": true,
"forceREST": false,
"live": false,
},
],
]
`;

exports[`launchSource forceREST not windows 1`] = `
Array [
Array [
Expand Down Expand Up @@ -442,6 +472,26 @@ Array [
]
`;

exports[`launchSource success windows 5`] = `
Array [
Array [
"tool",
"source",
Array [
"asset1",
"asset2",
"asset3",
],
Object {
"aws": true,
"debug": false,
"forceREST": false,
"live": false,
},
],
]
`;

exports[`retry after long enought time 1`] = `
Object {
"failCount": 1,
Expand Down Expand Up @@ -793,22 +843,7 @@ exports[`startFetchingEvent no event 2`] = `
Object {
"Others": Object {
"failCount": 0,
"instance": Object {
"on": [MockFunction] {
"calls": Array [
Array [
"exit",
[Function],
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
},
},
"instance": null,
"lastFailTimestamp": 1234567890,
"source": "Others",
},
Expand Down

0 comments on commit 2f7f9df

Please sign in to comment.