From d68b2cf3ed3f68bf6906439cc4d5ae872a923324 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Wed, 3 May 2017 19:12:25 +0200 Subject: [PATCH] first tests created --- package.json | 11 ++++- src/models/ColumnInfo.ts | 14 +++--- test/drivers/MssqlDriver.ts | 97 +++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 +- 4 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 test/drivers/MssqlDriver.ts diff --git a/package.json b/package.json index 1490288..6f5c93b 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ }, "homepage": "https://github.com/Kononnable/typeorm-model-generator#readme", "dependencies": { - "@types/node": "^7.0.10", "mssql": "^3.3.0", "mustache": "^2.3.0", "reflect-metadata": "^0.1.10", @@ -28,6 +27,16 @@ "yargs": "^7.0.2" }, "devDependencies": { + "@types/chai": "^3.5.2", + "@types/chai-as-promised": "0.0.30", + "@types/mocha": "^2.2.41", + "@types/node": "^7.0.10", + "@types/sinon": "^2.1.3", + "chai": "^3.5.0", + "chai-as-promised": "^6.0.0", + "mocha": "^3.3.0", + "sinon": "^2.2.0", + "sinon-chai": "^2.10.0", "typings": "^2.1.0" } } diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index 6eaa028..aa49e79 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -2,16 +2,16 @@ * ColumnInfo */ export class ColumnInfo { - name: string; - default: string|null; - is_nullable: boolean; + name: string=''; + default: string|null=null; + is_nullable: boolean=false; ts_type: 'number' | 'string' | 'boolean'; sql_type: "string" | "text" | "number" | "integer" | "int" | "smallint" | "bigint" | "float" | "double" | "decimal" | "date" | "time" | "datetime" | "boolean" | "json"; - char_max_lenght: number|null; - isPrimary:boolean; - numericPrecision:number|null; - numericScale:number|null; + char_max_lenght: number|null=null; + isPrimary:boolean=false; + numericPrecision:number|null=null; + numericScale:number|null=null; relations:RelationInfo[]; constructor() { diff --git a/test/drivers/MssqlDriver.ts b/test/drivers/MssqlDriver.ts new file mode 100644 index 0000000..954d4bd --- /dev/null +++ b/test/drivers/MssqlDriver.ts @@ -0,0 +1,97 @@ +import { expect } from "chai"; +import { MssqlDriver } from './../../src/drivers/MssqlDriver' +import * as Sinon from 'sinon' +import * as MSSQL from 'mssql' +import { EntityInfo } from './../../src/models/EntityInfo' +import { ColumnInfo } from './../../src/models/ColumnInfo' + + +describe('MssqlDriver', function () { + let driver: MssqlDriver + let sandbox = Sinon.sandbox.create() + + beforeEach(() => { + driver = new MssqlDriver(); + // sandbox.mock() + // sandbox.stub( (driver).Connection,) + // driver = Sinon.createStubInstance(MssqlDriver); + + // sandbox.stub(MSSQL,'Connection') + // .callsFake( (a,b)=>{ + // console.log(a) + // b({message:'a'}) + // }) + // sandbox.stub(MSSQL.) + }) + + afterEach(() => { + sandbox.restore() + }) + + it('should get tables info', async () => { + sandbox.stub(MSSQL, 'Request') + .returns( + { + query: (q) => { + let response = <{ TABLE_SCHEMA: string, TABLE_NAME: string }[]>[]; + response.push({ TABLE_SCHEMA: 'schema', TABLE_NAME: 'name' }) + return response; + } + } + ) + let result = await driver.GetAllTables() + let expectedResult = []; + let y = new EntityInfo(); + y.EntityName = 'name' + y.Columns = []; + y.Indexes = []; + expectedResult.push(y) + expect(result).to.be.deep.equal(expectedResult) + }) + it('should get columns info', async () => { + sandbox.stub(MSSQL, 'Request') + .returns( + { + query: (q) => { + let response = <{ + TABLE_NAME: string, COLUMN_NAME: string, COLUMN_DEFAULT: string, + IS_NULLABLE: string, DATA_TYPE: string, CHARACTER_MAXIMUM_LENGTH: number, + NUMERIC_PRECISION: number, NUMERIC_SCALE: number + }[]>[] + response.push({ + TABLE_NAME: 'name', CHARACTER_MAXIMUM_LENGTH: 0, + COLUMN_DEFAULT: 'a', COLUMN_NAME: 'name', DATA_TYPE: 'int', + IS_NULLABLE: 'YES', NUMERIC_PRECISION: 0, NUMERIC_SCALE: 0 + }) + return response; + } + } + ) + + + let entities = []; + let y = new EntityInfo(); + y.EntityName = 'name' + y.Columns = []; + y.Indexes = []; + entities.push(y) + var expected: EntityInfo[] = JSON.parse(JSON.stringify(entities)); + expected[0].Columns.push({ + char_max_lenght: null, + default: 'a', + is_nullable: true, + isPrimary: false, + name: 'name', + numericPrecision: null, + numericScale: null, + sql_type: 'int', + ts_type: 'number', + relations: [] + }) + let result = await driver.GetCoulmnsFromEntity(entities); + expect(result).to.be.deep.equal(expected) + }) + it('should find primary indexes') + it('should get indexes info') + it('should get relations info') +}) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 204461f..7f2781c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,7 @@ "strictNullChecks": true, "moduleResolution": "node" }, "include": [ - "src" + "src", + "test" ] } \ No newline at end of file