From c3e17548b8324a93b05a19f3941e098a14c53b07 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Tue, 13 Feb 2018 13:45:26 +0200 Subject: [PATCH] refactoring --- test/Main.test.js | 28 ++-- .../{DataTypesTest.js => ColumnTypesTest.js} | 37 +++--- test/TestModules/DataContentTest.js | 123 ++++++++++++------ test/TestModules/SchemaProcessorTest.js | 27 ++-- 4 files changed, 123 insertions(+), 92 deletions(-) rename test/TestModules/{DataTypesTest.js => ColumnTypesTest.js} (79%) diff --git a/test/Main.test.js b/test/Main.test.js index 5910a44..1cab201 100644 --- a/test/Main.test.js +++ b/test/Main.test.js @@ -20,10 +20,11 @@ */ 'use strict'; +const test = require('tape'); const TestSchemaProcessor = require('./TestModules/TestSchemaProcessor'); const testSchema = require('./TestModules/SchemaProcessorTest'); const testDataContent = require('./TestModules/DataContentTest'); -const testDataTypes = require('./TestModules/DataTypesTest'); +const testColumnTypes = require('./TestModules/ColumnTypesTest'); /** * Runs test suites. @@ -34,15 +35,22 @@ const testDataTypes = require('./TestModules/DataTypesTest'); */ const runTestSuites = testSchemaProcessor => { return () => { - Promise.all([ - // A list of test suite modules. - // Each test suite module must export a function, that returns a promise. - testSchema(testSchemaProcessor), - testDataContent(testSchemaProcessor), - testDataTypes(testSchemaProcessor), - ]) - .then(() => testSchemaProcessor.removeTestResources()) - .then(() => process.exit()); + test.onFinish(() => { + testSchemaProcessor.removeTestResources() + .then(() => process.exit()); + }); + + test('Test schema should be created', tapeTestSchema => { + testSchema(testSchemaProcessor, tapeTestSchema); + }); + + test('Test the data content', tapeTestDataContent => { + testDataContent(testSchemaProcessor, tapeTestDataContent); + }); + + test('Test column types', tapeTestColumnTypes => { + testColumnTypes(testSchemaProcessor, tapeTestColumnTypes); + }); }; }; diff --git a/test/TestModules/DataTypesTest.js b/test/TestModules/ColumnTypesTest.js similarity index 79% rename from test/TestModules/DataTypesTest.js rename to test/TestModules/ColumnTypesTest.js index bb54e9b..fb1c07f 100644 --- a/test/TestModules/DataTypesTest.js +++ b/test/TestModules/ColumnTypesTest.js @@ -20,8 +20,6 @@ */ 'use strict'; -const { test } = require('tape'); - /** * Returns `table_a` column types. * @@ -94,29 +92,26 @@ const getExpectedColumnTypes = () => { * The data content testing. * * @param {TestSchemaProcessor} testSchemaProcessor + * @param {Tape} tape * - * @returns {Promise} + * @returns {undefined} */ -module.exports = testSchemaProcessor => { - return new Promise(resolve => { - getColumnTypes(testSchemaProcessor).then(data => { - test('Test the data types', tape => { - const expectedColumnTypes = getExpectedColumnTypes(); - const autoTimeoutMs = 3 * 1000; // 3 seconds. - const numberOfPlannedAssertions = data.length; +module.exports = (testSchemaProcessor, tape) => { + getColumnTypes(testSchemaProcessor).then(data => { + const expectedColumnTypes = getExpectedColumnTypes(); + const autoTimeoutMs = 3 * 1000; // 3 seconds. + const numberOfPlannedAssertions = data.length; - tape.plan(numberOfPlannedAssertions); - tape.timeoutAfter(autoTimeoutMs); + tape.plan(numberOfPlannedAssertions); + tape.timeoutAfter(autoTimeoutMs); - for (let i = 0; i < numberOfPlannedAssertions; ++i) { - const columnName = data[i].column_name; - const actualColumnType = data[i].data_type; - const expectedColumnType = expectedColumnTypes[columnName]; - tape.equal(actualColumnType, expectedColumnType); - } + for (let i = 0; i < numberOfPlannedAssertions; ++i) { + const columnName = data[i].column_name; + const actualColumnType = data[i].data_type; + const expectedColumnType = expectedColumnTypes[columnName]; - resolve(); - }); - }); + tape.comment(`Test ${ columnName } column type`); + tape.equal(actualColumnType, expectedColumnType); + } }); }; diff --git a/test/TestModules/DataContentTest.js b/test/TestModules/DataContentTest.js index 1d8dab1..69fe061 100644 --- a/test/TestModules/DataContentTest.js +++ b/test/TestModules/DataContentTest.js @@ -20,8 +20,6 @@ */ 'use strict'; -const { test } = require('tape'); - /** * Retrieve a data from `table_a`. * @@ -42,52 +40,91 @@ const retrieveData = testSchemaProcessor => { * The data content testing. * * @param {TestSchemaProcessor} testSchemaProcessor + * @param {Tape} tape * - * @returns {Promise} + * @returns {undefined} */ -module.exports = testSchemaProcessor => { - return new Promise(resolve => { - retrieveData(testSchemaProcessor).then(data => { - test('Test the data content', tape => { - const autoTimeoutMs = 3 * 1000; // 3 seconds. - const numberOfPlannedAssertions = 24; - const originalTestBlobText = testSchemaProcessor - .getTestBlob(testSchemaProcessor._conversion) - .toString(); +module.exports = (testSchemaProcessor, tape) => { + retrieveData(testSchemaProcessor).then(data => { + const autoTimeoutMs = 3 * 1000; // 3 seconds. + const numberOfPlannedAssertions = 24; + const originalTestBlobText = testSchemaProcessor.getTestBlob(testSchemaProcessor._conversion).toString(); - tape.plan(numberOfPlannedAssertions); - tape.timeoutAfter(autoTimeoutMs); + tape.plan(numberOfPlannedAssertions); + tape.timeoutAfter(autoTimeoutMs); - tape.equal(data.blob_text, originalTestBlobText); - tape.equal(data.bit, '1'); // BIT is actually a "bit string", for example: '1110' -> 14 - tape.equal(data.id_test_unique_index, 7384); - tape.equal(data.id_test_composite_unique_index_1, 125); - tape.equal(data.id_test_composite_unique_index_2, 234); - tape.equal(data.id_test_index, 123); - tape.equal(data.int_test_not_null, 123); - tape.equal(data.id_test_composite_index_1, 11); - tape.equal(data.id_test_composite_index_2, 22); - tape.equal(JSON.stringify(data.json_test_comment), '{"prop1":"First","prop2":2}'); - tape.equal(data.year, 1984); - tape.equal(data.bigint, '1234567890123456800'); - tape.equal(data.float, 12345.5); - tape.equal(data.double, 123456789.23); - tape.equal(data.numeric, '1234567890'); - tape.equal(data.decimal, '1234567890'); - tape.equal(data.char_5, 'fghij'); - tape.equal(data.varchar_5, 'abcde'); - tape.equal(`${ data.date.getFullYear() }-${ data.date.getMonth() + 1 }-${ data.date.getDate() }`, '1984-11-30'); - tape.equal(data.time, '21:12:33'); - tape.equal(data.text, 'Test text'); - tape.equal(data.enum, 'e1'); - tape.equal(data.set, 's2'); + tape.comment('Test blob_text column value'); + tape.equal(data.blob_text, originalTestBlobText); - const date = `${ data.timestamp.getFullYear() }-${ data.timestamp.getMonth() + 1 }-${ data.timestamp.getDate() }`; - const time = `${ data.timestamp.getHours() }:${ data.timestamp.getMinutes() }:${ data.timestamp.getSeconds() }`; - tape.equal(`${ date } ${ time }`, '2018-11-11 22:21:20'); + tape.comment('Test bit column value'); + tape.equal(data.bit, '1'); // BIT is actually a "bit string", for example: '1110' -> 14 - resolve(); - }); - }); + tape.comment('Test id_test_unique_index column value'); + tape.equal(data.id_test_unique_index, 7384); + + tape.comment('Test id_test_composite_unique_index_1 column value'); + tape.equal(data.id_test_composite_unique_index_1, 125); + + tape.comment('Test id_test_composite_unique_index_2 column value'); + tape.equal(data.id_test_composite_unique_index_2, 234); + + tape.comment('Test id_test_index column value'); + tape.equal(data.id_test_index, 123); + + tape.comment('Test int_test_not_null column value'); + tape.equal(data.int_test_not_null, 123); + + tape.comment('Test id_test_composite_index_1 column value'); + tape.equal(data.id_test_composite_index_1, 11); + + tape.comment('Test id_test_composite_index_2 column value'); + tape.equal(data.id_test_composite_index_2, 22); + + tape.comment('Test json_test_comment column value'); + tape.equal(JSON.stringify(data.json_test_comment), '{"prop1":"First","prop2":2}'); + + tape.comment('Test year column value'); + tape.equal(data.year, 1984); + + tape.comment('Test bigint column value'); + tape.equal(data.bigint, '1234567890123456800'); + + tape.comment('Test float column value'); + tape.equal(data.float, 12345.5); + + tape.comment('Test double column value'); + tape.equal(data.double, 123456789.23); + + tape.comment('Test numeric column value'); + tape.equal(data.numeric, '1234567890'); + + tape.comment('Test decimal column value'); + tape.equal(data.decimal, '1234567890'); + + tape.comment('Test char_5 column value'); + tape.equal(data.char_5, 'fghij'); + + tape.comment('Test varchar_5 column value'); + tape.equal(data.varchar_5, 'abcde'); + + tape.comment('Test date column value'); + tape.equal(`${ data.date.getFullYear() }-${ data.date.getMonth() + 1 }-${ data.date.getDate() }`, '1984-11-30'); + + tape.comment('Test time column value'); + tape.equal(data.time, '21:12:33'); + + tape.comment('Test text column value'); + tape.equal(data.text, 'Test text'); + + tape.comment('Test enum column value'); + tape.equal(data.enum, 'e1'); + + tape.comment('Test set column value'); + tape.equal(data.set, 's2'); + + const date = `${ data.timestamp.getFullYear() }-${ data.timestamp.getMonth() + 1 }-${ data.timestamp.getDate() }`; + const time = `${ data.timestamp.getHours() }:${ data.timestamp.getMinutes() }:${ data.timestamp.getSeconds() }`; + tape.comment('Test timestamp column value'); + tape.equal(`${ date } ${ time }`, '2018-11-11 22:21:20'); }); }; diff --git a/test/TestModules/SchemaProcessorTest.js b/test/TestModules/SchemaProcessorTest.js index 68e550d..ea91714 100644 --- a/test/TestModules/SchemaProcessorTest.js +++ b/test/TestModules/SchemaProcessorTest.js @@ -20,8 +20,6 @@ */ 'use strict'; -const { test } = require('tape'); - /** * Checks if the schema exists. * @@ -42,24 +40,17 @@ const hasSchemaCreated = testSchemaProcessor => { * Schema creation testing. * * @param {TestSchemaProcessor} testSchemaProcessor + * @param {Tape} tape * - * @returns {Promise} + * @returns {undefined} */ -module.exports = testSchemaProcessor => { - return new Promise(resolve => { - test('Test schema should be created', tape => { - const numberOfPlannedAssertions = 2; - const autoTimeoutMs = 3 * 1000; // 3 seconds. +module.exports = (testSchemaProcessor, tape) => { + hasSchemaCreated(testSchemaProcessor).then(schemaExists => { + const numberOfPlannedAssertions = 1; + const autoTimeoutMs = 3 * 1000; // 3 seconds. - tape.plan(numberOfPlannedAssertions); - tape.timeoutAfter(autoTimeoutMs); - - hasSchemaCreated(testSchemaProcessor).then(schemaExists => { - tape.equal(typeof schemaExists, 'boolean'); - tape.equal(schemaExists, true); - - resolve(); - }); - }); + tape.plan(numberOfPlannedAssertions); + tape.timeoutAfter(autoTimeoutMs); + tape.equal(schemaExists, true); }); };