From 938adf5b079aaddeaf4b21a20a61686f2988e295 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 14 May 2017 14:53:06 +0200 Subject: [PATCH] Added CI MSSQL Tests --- .gitignore | 3 +- .travis.yml | 11 +- package.json | 1 + test/integration/integration.test.ts | 119 ++++++++++--------- test/utils/test-utils.ts | 165 --------------------------- 5 files changed, 77 insertions(+), 222 deletions(-) delete mode 100644 test/utils/test-utils.ts diff --git a/.gitignore b/.gitignore index 4c1fc84..dc73fd5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ typings/ **/*.js.map output/**/*.* .nyc_output/ -coverage/ \ No newline at end of file +coverage/ +.env \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index f762d56..ef7691f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,15 @@ node_js: - 6 - 5 - 4 +env: + - MSSQLSkip=0 + - MSSQLHost=localhost + - MSSQLPort=1433 + - MSSQLUsername=sa + - MSSQLPassword=!Passw0rd + - MSSQLDatabase=test before_script: + - docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=!Passw0rd' -p 1433:1433 -d microsoft/mssql-server-linux - npm run typings-install - - tsc \ No newline at end of file + - tsc + - sqlcmd -S $MSSQLHost,$MSSQLPort -U $MSSQLUsername -P $MSSQLPassword -Q 'CREATE DATABASE TEST' \ No newline at end of file diff --git a/package.json b/package.json index 365aa41..34a5942 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "chai": "^3.5.0", "chai-as-promised": "^6.0.0", "codecov": "^2.1.0", + "dotenv": "^4.0.0", "fs-extra": "^3.0.1", "istanbul": "^0.4.5", "mocha": "^3.3.0", diff --git a/test/integration/integration.test.ts b/test/integration/integration.test.ts index 1b062d1..b75294e 100644 --- a/test/integration/integration.test.ts +++ b/test/integration/integration.test.ts @@ -1,10 +1,9 @@ +require('dotenv').config() import "reflect-metadata"; import { createConnection, ConnectionOptions, Connection } from "typeorm"; -import { setupSingleTestingConnection, closeTestingConnections, reloadTestingDatabases } from "./../utils/test-utils" import fs = require('fs-extra'); import path = require('path') import { Post } from "./examples/sample1-simple-entity/entity/Post"; -import * as mockFS from "mock-fs"; import { Engine } from "./../../src/Engine"; import { AbstractDriver } from "./../../src/drivers/AbstractDriver"; import { MssqlDriver } from "./../../src/drivers/MssqlDriver"; @@ -13,65 +12,75 @@ import { expect } from "chai"; import * as Sinon from 'sinon' - describe("integration tests", async function () { - let examplesPath = path.resolve(process.cwd(), 'test/integration/examples') - let files = fs.readdirSync(examplesPath) - // console.log(files) - - let dbDrivers:[DriverType]=['mssql'] +describe("integration tests", async function () { + let examplesPath = path.resolve(process.cwd(), 'test/integration/examples') + let files = fs.readdirSync(examplesPath) - for( let folder of files){ - // files.forEach( async folder => { - - describe(folder, async function () { - - for (let dbDriver of dbDrivers){ - it(dbDriver,async function() { - let connOpt =await setupSingleTestingConnection(dbDriver,{ - entities: [Post], - schemaCreate: true, - }) - let conn = await createConnection(connOpt) - await conn.entityManager.query(`select 'TODO'`)//depends on driver - remove tables - if (conn.isConnected) - await conn.close() + let dbDrivers: DriverType[] = [] + if (process.env.MSSQLSkip=='0') dbDrivers.push('mssql') - let driver: AbstractDriver; - driver = new MssqlDriver(); - let standardPort = 1433; + for (let folder of files) { -let resultsPath= path.resolve(process.cwd(),`output`) - let engine = new Engine( - driver, { - //TODO:get data from env - host: 'localhost', - port: standardPort, - databaseName: 'test', - user: 'sa', - password: 'password', - databaseType: 'mssql', - resultsPath: resultsPath - }); + describe(folder, async function () { + + for (let dbDriver of dbDrivers) { + it(dbDriver, async function () { + + let filesOrgPath = path.resolve(examplesPath, folder, 'entity') + + let connOpt: ConnectionOptions = { + + driver: { + database: process.env.MSSQLDatabase, + host: process.env.MSSQLHost, + password: process.env.MSSQLPassword, + type: 'mssql', + username: process.env.MSSQLUsername, + port: process.env.MSSQLPort + }, + dropSchemaOnConnection: true, + autoSchemaSync: true, + entities: [path.resolve(filesOrgPath, '*.js')], + } + let conn = await createConnection(connOpt) + + if (conn.isConnected) + await conn.close() + + let driver: AbstractDriver; + driver = new MssqlDriver(); + + let resultsPath = path.resolve(process.cwd(), `output`) + let engine = new Engine( + driver, { + //TODO:get data from env + host: process.env.MSSQLHost, + port: process.env.MSSQLPort, + databaseName: process.env.MSSQLDatabase, + user: process.env.MSSQLUsername, + password: process.env.MSSQLPassword, + databaseType: 'mssql', + resultsPath: resultsPath + }); fs.removeSync(resultsPath) - let result = await engine.createModelFromDatabase() - - //TODO:Compare reslts - let filesOrgPath=path.resolve(examplesPath,folder,'entity') - let filesGenPath=path.resolve(resultsPath,'entities') + let result = await engine.createModelFromDatabase() - let filesOrg = fs.readdirSync(filesOrgPath).map(function(this,val){return val.toString().toLowerCase();}).filter( function(this,val,ind,arr){return val.toString().endsWith('.ts')}) - let filesGen = fs.readdirSync(filesGenPath).map(function(this,val){return val.toString().toLowerCase();}).filter( function(this,val,ind,arr){return val.toString().endsWith('.ts')}) - - expect(filesOrg).to.be.deep.equal(filesGen) + let filesGenPath = path.resolve(resultsPath, 'entities') - for(let file of filesOrg){ - - expect(fs.readFileSync(path.resolve(filesOrgPath,file)).toString()).to.be.eq(fs.readFileSync(path.resolve(filesGenPath,file)).toString()) - } -}); + let filesOrg = fs.readdirSync(filesOrgPath).map(function (this, val) { return val.toString().toLowerCase(); }).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) + let filesGen = fs.readdirSync(filesGenPath).map(function (this, val) { return val.toString().toLowerCase(); }).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) - } - })} - }) \ No newline at end of file + expect(filesOrg).to.be.deep.equal(filesGen) + + for (let file of filesOrg) { + //TODO:Compare files logically(not buffer to buffer) + //expect(fs.readFileSync(path.resolve(filesOrgPath, file)).toString()).to.be.eq(fs.readFileSync(path.resolve(filesGenPath, file)).toString()) + } + }); + + } + }) + } +}) \ No newline at end of file diff --git a/test/utils/test-utils.ts b/test/utils/test-utils.ts deleted file mode 100644 index 6b901b0..0000000 --- a/test/utils/test-utils.ts +++ /dev/null @@ -1,165 +0,0 @@ -import { createConnection, createConnections, Connection, ConnectionOptions } from "typeorm"; - -export type DriverType = "mysql"|"postgres"|"mariadb"|"sqlite"|"oracle"|"mssql"|"websql"|"mongodb"; - -/** - * Interface in which data is stored in ormconfig.json of the project. - */ -export interface TestingConnectionOptions extends ConnectionOptions { - - /** - * Indicates if this connection should be skipped. - */ - skip?: boolean; - - /** - * If set to true then tests for this driver wont run until implicitly defined "enabledDrivers" section. - */ - disabledIfNotEnabledImplicitly?: boolean; - -} - -/** - * Options used to create a connection for testing purposes. - */ -export interface TestingOptions { - - /** - * Connection name to be overridden. - * This can be used to create multiple connections with single connection configuration. - */ - name?: string; - - /** - * List of enabled drivers for the given test suite. - */ - enabledDrivers?: DriverType[]; - - /** - * Entities needs to be included in the connection for the given test suite. - */ - entities?: string[] | Function[]; - - /** - * Subscribers needs to be included in the connection for the given test suite. - */ - subscribers?: string[] | Function[]; - - /** - * Indicates if schema sync should be performed or not. - */ - schemaCreate?: boolean; - - /** - * Indicates if schema should be dropped on connection setup. - */ - dropSchemaOnConnection?: boolean; - - /** - * Schema name used for postgres driver. - */ - schemaName?: string; - -} - -/** - * Creates a testing connection options for the given driver type based on the configuration in the ormconfig.json - * and given options that can override some of its configuration for the test-specific use case. - */ -export function setupSingleTestingConnection(driverType: DriverType, options: TestingOptions) { - - const testingConnections = setupTestingConnections({ - name: options.name ? options.name : undefined, - entities: options.entities ? options.entities : [], - subscribers: options.subscribers ? options.subscribers : [], - dropSchemaOnConnection: options.dropSchemaOnConnection ? options.dropSchemaOnConnection : false, - schemaCreate: options.schemaCreate ? options.schemaCreate : false, - enabledDrivers: [driverType], - schemaName: options.schemaName ? options.schemaName : undefined - }); - if (!testingConnections.length) - throw new Error(`Unable to run tests because connection options for "${driverType}" are not set.`); - - return testingConnections[0]; -} - - -/** - * Loads test connection options from ormconfig.json file. - */ -export function getTypeOrmConfig(): TestingConnectionOptions[] { - - try { - return require(__dirname + "/../../ormconfig.json"); - - } catch (err) { - throw new Error(`Cannot find ormconfig.json file in the root of the project. To run tests please create ormconfig.json file` + - ` in the root of the project (near ormconfig.json.dist, you need to copy ormconfig.json.dist into ormconfig.json` + - ` and change all database settings to match your local environment settings).`); - } -} - -/** - * Creates a testing connections options based on the configuration in the ormconfig.json - * and given options that can override some of its configuration for the test-specific use case. - */ -export function setupTestingConnections(options?: TestingOptions) { - const ormConfigConnectionOptionsArray = getTypeOrmConfig(); - - if (!ormConfigConnectionOptionsArray.length) - throw new Error(`No connections setup in ormconfig.json file. Please create configurations for each database type to run tests.`); - - return ormConfigConnectionOptionsArray - .filter(connectionOptions => { - if (connectionOptions.skip === true) - return false; - - if (options && options.enabledDrivers && options.enabledDrivers.length) - return options.enabledDrivers.indexOf(connectionOptions.driver.type) !== -1; - - if (connectionOptions.disabledIfNotEnabledImplicitly === true) - return false; - - return true; - }) - .map(connectionOptions => { - const newConnectionOptions = Object.assign({}, connectionOptions as ConnectionOptions, { - name: options && options.name ? options.name : connectionOptions.name, - entities: options && options.entities ? options.entities : [], - subscribers: options && options.subscribers ? options.subscribers : [], - autoSchemaSync: options && options.entities ? options.schemaCreate : false, - dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, - }); - - - if (options && options.schemaName && newConnectionOptions.driver) { - // todo: we use any because driver.schemaName is readonly. Need to find better solution here - (newConnectionOptions.driver as any).schemaName = options.schemaName; - } - - return newConnectionOptions; - }); -} - -/** - * Creates a testing connections based on the configuration in the ormconfig.json - * and given options that can override some of its configuration for the test-specific use case. - */ -export async function createTestingConnections(options?: TestingOptions): Promise { - return createConnections(setupTestingConnections(options)); -} - -/** - * Closes testing connections if they are connected. - */ -export function closeTestingConnections(connections: Connection[]) { - return Promise.all(connections.map(connection => connection.isConnected ? connection.close() : undefined)); -} - -/** - * Reloads all databases for all given connections. - */ -export function reloadTestingDatabases(connections: Connection[]) { - return Promise.all(connections.map(connection => connection.syncSchema(true))); -} -