diff --git a/package.json b/package.json index f1342b2..365aa41 100644 --- a/package.json +++ b/package.json @@ -32,16 +32,17 @@ "devDependencies": { "@types/chai": "^3.5.2", "@types/chai-as-promised": "0.0.30", + "@types/fs-extra": "^3.0.0", "@types/mocha": "^2.2.41", - "@types/mock-fs": "^3.6.30", + "@types/mssql": "^3.3.0", "@types/node": "^7.0.10", "@types/sinon": "^2.1.3", "chai": "^3.5.0", "chai-as-promised": "^6.0.0", "codecov": "^2.1.0", + "fs-extra": "^3.0.1", "istanbul": "^0.4.5", "mocha": "^3.3.0", - "mock-fs": "^4.3.0", "remap-istanbul": "^0.9.5", "sinon": "^2.2.0", "sinon-chai": "^2.10.0", diff --git a/src/Engine.ts b/src/Engine.ts index 94f5d42..76b2c53 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -25,9 +25,9 @@ export class Engine { } private createModelFromMetadata(databaseModel: DatabaseModel) { - let templatePath = path.resolve(__dirname, 'entity.mst') + let templatePath = path.resolve(process.cwd(), 'src/entity.mst') let template = fs.readFileSync(templatePath, 'UTF-8'); - let resultPath = path.resolve(__dirname, '../'+this.Options.resultsPath) + let resultPath = this.Options.resultsPath if (!fs.existsSync(resultPath)) fs.mkdirSync(resultPath); this.createTsConfigFile(resultPath) diff --git a/src/index.ts b/src/index.ts index 9dc7206..a11cd0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import * as Mustache from 'mustache' import { Engine } from './Engine' import * as Yargs from 'yargs' import { AbstractDriver } from "./drivers/AbstractDriver"; +import path = require('path') // var x = Mustache.render("{{a}}", { a: 'test' }); // console.log(x); @@ -43,7 +44,7 @@ var argv = Yargs .option('o', { alias: 'output', describe: 'Where to place generated models.', - default: './output' + default: path.resolve(process.cwd(),'output') }) .argv; diff --git a/test/drivers/MssqlDriver.ts b/test/drivers/MssqlDriver.test.ts similarity index 100% rename from test/drivers/MssqlDriver.ts rename to test/drivers/MssqlDriver.test.ts diff --git a/test/integration/examples/sample1-simple-entity/entity/Post.ts b/test/integration/examples/sample1-simple-entity/entity/Post.ts index 3aa5f65..87857ac 100644 --- a/test/integration/examples/sample1-simple-entity/entity/Post.ts +++ b/test/integration/examples/sample1-simple-entity/entity/Post.ts @@ -1,6 +1,6 @@ import {Column, Entity,PrimaryColumn,Index} from "typeorm"; -@Entity("sample01_post") +@Entity() export class Post { @PrimaryColumn("int", { generated: true }) diff --git a/test/integration/integration.test.ts b/test/integration/integration.test.ts index cdbf7b8..1b062d1 100644 --- a/test/integration/integration.test.ts +++ b/test/integration/integration.test.ts @@ -1,85 +1,77 @@ import "reflect-metadata"; import { createConnection, ConnectionOptions, Connection } from "typeorm"; -import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "./../utils/test-utils" -import fs = require('fs'); +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"; +import { DriverType } from "typeorm/driver/DriverOptions"; +import { expect } from "chai"; +import * as Sinon from 'sinon' -describe("integration tests", function () { - let connections: Connection[]; - describe('should ...', async () => { - let examplesPath = path.resolve(__dirname, 'examples') + describe("integration tests", async function () { + let examplesPath = path.resolve(process.cwd(), 'test/integration/examples') let files = fs.readdirSync(examplesPath) // console.log(files) - files.forEach(folder => { - it(folder, async () => { - connections = await createTestingConnections({ + + let dbDrivers:[DriverType]=['mssql'] + + 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, }) - await connections.forEach(async conn => { + let conn = await createConnection(connOpt) + await conn.entityManager.query(`select 'TODO'`)//depends on driver - remove tables + if (conn.isConnected) + await conn.close() - // conn.s - console.log('aa') - //TODO get model from db - await conn.entityManager.query(`select 'TODO'`)//depends on driver - remove tables - //compare models - if (conn.isConnected) - await conn.close() - let q=conn.isConnected - console.log(q) + let driver: AbstractDriver; + driver = new MssqlDriver(); + let standardPort = 1433; + +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 + }); + fs.removeSync(resultsPath) - let resultPath = path.resolve(__dirname, '../model') - mockFS({ resultPath: {} }) + let result = await engine.createModelFromDatabase() + + //TODO:Compare reslts + let filesOrgPath=path.resolve(examplesPath,folder,'entity') + let filesGenPath=path.resolve(resultsPath,'entities') + 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) - var driver: AbstractDriver; - - driver = new MssqlDriver(); - let standardPort = 1433; - - let engine = new Engine( - driver, { - host: 'localhost', - port: standardPort, - databaseName: 'test', - user: 'sa', - password: 'password', - databaseType: 'mssql', - resultsPath: `test/model` - }); - - - let result = await engine.createModelFromDatabase() - console.log(result); - }); - - }) - - }); - }) - - // describe("sample1", async function () { - // connections = await createTestingConnections({ - // //entities: [Post], - // schemaCreate: false, - // }) - // await connections.forEach( async conn => { - // await conn.syncSchema() - // conn.entityManager.query('TODO')//depends on - // }); - // closeTestingConnections(connections) - - // //foreach driver - // //create model from db - // //compare models - - // }) + for(let file of filesOrg){ + + expect(fs.readFileSync(path.resolve(filesOrgPath,file)).toString()).to.be.eq(fs.readFileSync(path.resolve(filesGenPath,file)).toString()) + } }); + } + })} + }) \ No newline at end of file