Skip to content

Commit

Permalink
refactor: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Jun 6, 2018
1 parent c7b1fa5 commit 1d7abb1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 88 deletions.
5 changes: 2 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.tabCompletion": true,
"editor.tabSize": 2,
"editor.trimAutoWhitespace": true,
"editor.formatOnSave": false,
"editor.formatOnSave": true,
"[html]": {
"editor.formatOnSave": false
},
Expand All @@ -25,6 +25,5 @@

"tslint.ignoreDefinitionFiles": true,
"tslint.nodePath": "./packages/build/node_modules",
"typescript.tsdk": "./packages/build/node_modules/typescript/lib",
"eslint.enable": false
"typescript.tsdk": "./packages/build/node_modules/typescript/lib"
}
10 changes: 3 additions & 7 deletions packages/rest/test/unit/coercion/paramStringToBoolean.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {runTests} from './utils';
import {test} from './utils';

describe('coerce param from string to boolean', () => {
/*tslint:disable:max-line-length*/
const testCases = [
['false', {type: 'boolean'}, 'false', false, new Error().stack],
['true', {type: 'boolean'}, 'true', true, new Error().stack],
];

runTests(testCases);
test(['false', {type: 'boolean'}, 'false', false]);
test(['true', {type: 'boolean'}, 'true', true]);
});
8 changes: 2 additions & 6 deletions packages/rest/test/unit/coercion/paramStringToBuffer.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {runTests} from './utils';
import {test} from './utils';

describe('coerce param from string to buffer', () => {
const testValues = {
base64: Buffer.from('Hello World').toString('base64')
}
/*tslint:disable:max-line-length*/
const testCases = [
['base64', {type: 'string', format: 'byte'}, testValues.base64, Buffer.from(testValues.base64, 'base64'), new Error().stack],
];

runTests(testCases);
test(['base64', {type: 'string', format: 'byte'}, testValues.base64, Buffer.from(testValues.base64, 'base64')]);
});
8 changes: 2 additions & 6 deletions packages/rest/test/unit/coercion/paramStringToDate.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {runTests} from './utils';
import {test} from './utils';

describe('coerce param from string to date', () => {
/*tslint:disable:max-line-length*/
const testCases = [
['date', {type: 'string', format: 'date'}, '2015-03-01', new Date('2015-03-01'), new Error().stack],
];

runTests(testCases);
test(['date', {type: 'string', format: 'date'}, '2015-03-01', new Date('2015-03-01')]);
});
9 changes: 3 additions & 6 deletions packages/rest/test/unit/coercion/paramStringToInteger.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {runTests} from './utils';
import {test} from './utils';

describe('coerce param from string to integer', () => {
/*tslint:disable:max-line-length*/
const testCases = [
['integer', {type: 'integer', format: 'int32'}, '100', 100, new Error().stack],
['long', {type: 'integer', format: 'int64'}, '9223372036854775807', 9223372036854775807, new Error().stack],
]
runTests(testCases);
test(['integer', {type: 'integer', format: 'int32'}, '100', 100]);
test(['long', {type: 'integer', format: 'int64'}, '9223372036854775807', 9223372036854775807]);
});
73 changes: 25 additions & 48 deletions packages/rest/test/unit/coercion/paramStringToNumber.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {runTests, ERROR_BAD_REQUEST} from './utils';
import {test, ERROR_BAD_REQUEST} from './utils';

const NUMBER_SCHEMA = {type: 'number'};
const NUMBER_SCHEMA_REQUIRED = {type: 'number', required: true};
Expand All @@ -13,80 +13,57 @@ const DOUBLE_SCHEMA = {type: 'number', format: 'double'};
/*tslint:disable:max-line-length*/
describe('coerce param from string to number - required', () => {
context('valid values', () => {
const tests = [
['0', NUMBER_SCHEMA_REQUIRED, '0', 0, new Error().stack],
['1', NUMBER_SCHEMA_REQUIRED, '1', 1, new Error().stack],
['-1', NUMBER_SCHEMA_REQUIRED, '-1', -1, new Error().stack],
];
runTests(tests);
test(['0', NUMBER_SCHEMA_REQUIRED, '0', 0]);
test(['1', NUMBER_SCHEMA_REQUIRED, '1', 1]);
test(['-1', NUMBER_SCHEMA_REQUIRED, '-1', -1]);
})

context('empty values trigger ERROR_BAD_REQUEST', () => {
const tests = [
// null, ''
['empty string', NUMBER_SCHEMA_REQUIRED, '', ERROR_BAD_REQUEST, new Error().stack, true],
];
runTests(tests);
// null, '' are converted to ''
test(['empty string', NUMBER_SCHEMA_REQUIRED, '', ERROR_BAD_REQUEST, true]);
})
});

describe('coerce param from string to number - optional', () => {
context('valid values', () => {
const tests = [
['0', NUMBER_SCHEMA, '0', 0, new Error().stack],
['1', NUMBER_SCHEMA, '1', 1, new Error().stack],
['-1', NUMBER_SCHEMA, '-1', -1, new Error().stack],
['1.2', NUMBER_SCHEMA, '1.2', 1.2, new Error().stack],
['-1.2', NUMBER_SCHEMA, '-1.2', -1.2, new Error().stack],
];
runTests(tests);
test(['0', NUMBER_SCHEMA, '0', 0]);
test(['1', NUMBER_SCHEMA, '1', 1]);
test(['-1', NUMBER_SCHEMA, '-1', -1]);
test(['1.2', NUMBER_SCHEMA, '1.2', 1.2]);
test(['-1.2', NUMBER_SCHEMA, '-1.2', -1.2]);
})

context('numbers larger than MAX_SAFE_INTEGER get trimmed', () => {
const tests = [
['positive large number', NUMBER_SCHEMA, '2343546576878989879789', 2.34354657687899e+21, new Error().stack],
['negative large number', NUMBER_SCHEMA, '-2343546576878989879789', -2.34354657687899e+21, new Error().stack],
];
runTests(tests);
test(['positive large number', NUMBER_SCHEMA, '2343546576878989879789', 2.34354657687899e+21]);
test(['negative large number', NUMBER_SCHEMA, '-2343546576878989879789', -2.34354657687899e+21]);
})

context('scientific notations', () => {
const tests = [
['positive number', NUMBER_SCHEMA, '1.234e+30', 1.234e+30, new Error().stack],
['negative number', NUMBER_SCHEMA, '-1.234e+30', -1.234e+30, new Error().stack],
];
runTests(tests);
test(['positive number', NUMBER_SCHEMA, '1.234e+30', 1.234e+30]);
test(['negative number', NUMBER_SCHEMA, '-1.234e+30', -1.234e+30]);
})

context('empty value converts to undefined', () => {
const tests = [
// [], {} are converted to undefined
['empty value as undefined', NUMBER_SCHEMA, undefined, undefined, new Error().stack]
]
runTests(tests);
// [], {} are converted to undefined
test(['empty value as undefined', NUMBER_SCHEMA, undefined, undefined]);
})

context('All other non-number values trigger ERROR_BAD_REQUEST', () => {
const tests = [
// 'false', false, 'true', true, 'text', null, '' are convert to a string
['invalid value as string', NUMBER_SCHEMA, 'text', ERROR_BAD_REQUEST, new Error().stack, true],
// {a: true}, [1,2] are convert to object
['invalid value as object', NUMBER_SCHEMA, {a: true}, ERROR_BAD_REQUEST, new Error().stack, true],
]
runTests(tests);
// 'false', false, 'true', true, 'text', null, '' are convert to a string
test(['invalid value as string', NUMBER_SCHEMA, 'text', ERROR_BAD_REQUEST, true]);
// {a: true}, [1,2] are convert to object
test(['invalid value as object', NUMBER_SCHEMA, {a: true}, ERROR_BAD_REQUEST, true]);
})
});

describe('OAI3 primitive types', () => {
const testCases = [
['float', FLOAT_SCHEMA, '3.333333', 3.333333, new Error().stack],
['double', DOUBLE_SCHEMA, '3.3333333333', 3.3333333333, new Error().stack],
];
runTests(testCases);
test(['float', FLOAT_SCHEMA, '3.333333', 3.333333]);
test(['double', DOUBLE_SCHEMA, '3.3333333333', 3.3333333333]);
})

context('Number-like string values trigger ERROR_BAD_REQUEST', () => {
// this has to be in serialization acceptance tests
// Copied from strong-remoting
// these have to be in serialization acceptance tests
// [{arg: '0'}, ERROR_BAD_REQUEST],
// [{arg: '1'}, ERROR_BAD_REQUEST],
// [{arg: '-1'}, ERROR_BAD_REQUEST],
Expand Down
24 changes: 12 additions & 12 deletions packages/rest/test/unit/coercion/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ export async function testCoercion<T>(config: TestArgs<T>) {
expect(args).to.eql([config.expectedResult]);
}
} catch (err) {
throw new Error(`${err} \n Failed ${config.caller.split(/\n/)[1]}`);
err.stack += config.caller;
throw err;
}
}

// tslint:disable-next-line:no-any
export function runTests(tests: any[][]) {
for (let t of tests) {
it(t[0] as string, async () => {
await testCoercion({
schema: t[1] as SchemaObject,
valueFromReq: t[2] as string,
expectedResult: t[3],
caller: t[4] as string,
expectError: t.length > 5 ? t[5] : false,
});
export function test(t: any[]) {
const caller: string = new Error().stack!;
it(t[0] as string, async () => {
await testCoercion({
schema: t[1] as SchemaObject,
valueFromReq: t[2] as string,
expectedResult: t[3],
caller,
expectError: t.length > 4 ? t[4] : false,
});
}
});
}

export const ERROR_BAD_REQUEST = new HttpErrors['400']();

0 comments on commit 1d7abb1

Please sign in to comment.