From fd04324cf8215c1ff869e6b982dcdc24eed604de Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 26 Nov 2017 18:58:32 +0100 Subject: [PATCH] tests for #12 --- test/integration/entityTypes.test.ts | 112 +++++++++--------- .../github-issues/12/entity/Post.ts | 16 +++ .../github-issues/12/entity/PostAuthor.ts | 16 +++ test/integration/githubIssues.test.ts | 100 ++++++++++++++++ test/integration/integration.test.ts | 9 +- 5 files changed, 194 insertions(+), 59 deletions(-) create mode 100644 test/integration/github-issues/12/entity/Post.ts create mode 100644 test/integration/github-issues/12/entity/PostAuthor.ts create mode 100644 test/integration/githubIssues.test.ts diff --git a/test/integration/entityTypes.test.ts b/test/integration/entityTypes.test.ts index 0f9c1fb..6102403 100644 --- a/test/integration/entityTypes.test.ts +++ b/test/integration/entityTypes.test.ts @@ -15,12 +15,9 @@ import { Engine } from "./../../src/Engine"; chai.use(chaiSubset); -describe("platform specyfic types", async function () { +describe("Platform specyfic types", async function () { this.timeout(20000) this.slow(5000)//compiling created models takes time - let examplesPathJS = path.resolve(process.cwd(), 'dist/test/integration/entityTypes') - let examplesPathTS = path.resolve(process.cwd(), 'test/integration/entityTypes') - let files = fs.readdirSync(examplesPathTS) let dbDrivers: string[] = [] if (process.env.POSTGRES_Skip == '0') dbDrivers.push('postgres') @@ -28,70 +25,75 @@ describe("platform specyfic types", async function () { if (process.env.MARIADB_Skip == '0') dbDrivers.push('mariadb') if (process.env.MSSQL_Skip == '0') dbDrivers.push('mssql') + + let examplesPathJS = path.resolve(process.cwd(), 'dist/test/integration/entityTypes') + let examplesPathTS = path.resolve(process.cwd(), 'test/integration/entityTypes') + let files = fs.readdirSync(examplesPathTS) + for (let folder of files) { - for (let dbDriver of dbDrivers) { - if (dbDriver == folder) { - it(dbDriver, async function () { + for (let dbDriver of dbDrivers) { + if (dbDriver == folder) { + it(dbDriver, async function () { - let filesOrgPathJS = path.resolve(examplesPathJS, folder, 'entity') - let filesOrgPathTS = path.resolve(examplesPathTS, folder, 'entity') - let resultsPath = path.resolve(process.cwd(), `output`) - fs.removeSync(resultsPath) + let filesOrgPathJS = path.resolve(examplesPathJS, folder, 'entity') + let filesOrgPathTS = path.resolve(examplesPathTS, folder, 'entity') + let resultsPath = path.resolve(process.cwd(), `output`) + fs.removeSync(resultsPath) - let engine: Engine; - switch (dbDriver) { - case 'mssql': - engine = await GTU.createMSSQLModels(filesOrgPathJS, resultsPath) - break; - case 'postgres': - engine = await GTU.createPostgresModels(filesOrgPathJS, resultsPath) - break; - case 'mysql': - engine = await GTU.createMysqlModels(filesOrgPathJS, resultsPath) - break; - case 'mariadb': - engine = await GTU.createMariaDBModels(filesOrgPathJS, resultsPath) - break; + let engine: Engine; + switch (dbDriver) { + case 'mssql': + engine = await GTU.createMSSQLModels(filesOrgPathJS, resultsPath) + break; + case 'postgres': + engine = await GTU.createPostgresModels(filesOrgPathJS, resultsPath) + break; + case 'mysql': + engine = await GTU.createMysqlModels(filesOrgPathJS, resultsPath) + break; + case 'mariadb': + engine = await GTU.createMariaDBModels(filesOrgPathJS, resultsPath) + break; - default: - console.log(`Unknown engine type`); - engine = {} - break; - } + default: + console.log(`Unknown engine type`); + engine = {} + break; + } - let result = await engine.createModelFromDatabase() + let result = await engine.createModelFromDatabase() - let filesGenPath = path.resolve(resultsPath, 'entities') + let filesGenPath = path.resolve(resultsPath, 'entities') - let filesOrg = fs.readdirSync(filesOrgPathTS).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) - let filesGen = fs.readdirSync(filesGenPath).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) + let filesOrg = fs.readdirSync(filesOrgPathTS).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) + let filesGen = fs.readdirSync(filesGenPath).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) - expect(filesOrg, 'Errors detected in model comparision').to.be.deep.equal(filesGen) + expect(filesOrg, 'Errors detected in model comparision').to.be.deep.equal(filesGen) - for (let file of filesOrg) { - let entftj = new EntityFileToJson(); - let jsonEntityOrg = entftj.convert(fs.readFileSync(path.resolve(filesOrgPathTS, file))) - let jsonEntityGen = entftj.convert(fs.readFileSync(path.resolve(filesGenPath, file))) - expect(jsonEntityGen, `Error in file ${file}`).to.containSubset(jsonEntityOrg) - } - const currentDirectoryFiles = fs.readdirSync(filesGenPath). - filter(fileName => fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts").map(v => { - return path.resolve(filesGenPath, v) - }) - let compileErrors = GTU.compileTsFiles(currentDirectoryFiles, { + for (let file of filesOrg) { + let entftj = new EntityFileToJson(); + let jsonEntityOrg = entftj.convert(fs.readFileSync(path.resolve(filesOrgPathTS, file))) + let jsonEntityGen = entftj.convert(fs.readFileSync(path.resolve(filesGenPath, file))) + expect(jsonEntityGen, `Error in file ${file}`).to.containSubset(jsonEntityOrg) + } + const currentDirectoryFiles = fs.readdirSync(filesGenPath). + filter(fileName => fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts").map(v => { + return path.resolve(filesGenPath, v) + }) + let compileErrors = GTU.compileTsFiles(currentDirectoryFiles, { - experimentalDecorators: true, - sourceMap: false, - emitDecoratorMetadata: true, - target: ts.ScriptTarget.ES2016, - moduleResolution: ts.ModuleResolutionKind.NodeJs, - module: ts.ModuleKind.CommonJS - }); - expect(compileErrors, 'Errors detected while compiling generated model').to.be.false; + experimentalDecorators: true, + sourceMap: false, + emitDecoratorMetadata: true, + target: ts.ScriptTarget.ES2016, + moduleResolution: ts.ModuleResolutionKind.NodeJs, + module: ts.ModuleKind.CommonJS }); - } + expect(compileErrors, 'Errors detected while compiling generated model').to.be.false; + }); } + } } }) diff --git a/test/integration/github-issues/12/entity/Post.ts b/test/integration/github-issues/12/entity/Post.ts new file mode 100644 index 0000000..79d2472 --- /dev/null +++ b/test/integration/github-issues/12/entity/Post.ts @@ -0,0 +1,16 @@ +import { PrimaryGeneratedColumn, Column, Entity, OneToOne, OneToMany, ManyToOne, JoinColumn } from "typeorm"; +import { PostAuthor } from "./PostAuthor"; + +@Entity("Post") +export class Post { + + @PrimaryGeneratedColumn() + id: number; + + @ManyToOne(type => PostAuthor, author => author.posts) + author: PostAuthor; + + @ManyToOne(type => PostAuthor, author => author.posts2) + author2: PostAuthor; + +} \ No newline at end of file diff --git a/test/integration/github-issues/12/entity/PostAuthor.ts b/test/integration/github-issues/12/entity/PostAuthor.ts new file mode 100644 index 0000000..9d5a433 --- /dev/null +++ b/test/integration/github-issues/12/entity/PostAuthor.ts @@ -0,0 +1,16 @@ +import { PrimaryGeneratedColumn, Column, Entity, OneToOne, OneToMany, ManyToOne, JoinColumn } from "typeorm"; +import { Post } from "./Post"; + +@Entity("PostAuthor") +export class PostAuthor { + + @PrimaryGeneratedColumn() + id: number; + + @OneToMany(type => Post, post => post.author) + posts: Post[]; + + @OneToMany(type => Post, post => post.author2) + posts2: Post[]; + +} \ No newline at end of file diff --git a/test/integration/githubIssues.test.ts b/test/integration/githubIssues.test.ts new file mode 100644 index 0000000..8266e65 --- /dev/null +++ b/test/integration/githubIssues.test.ts @@ -0,0 +1,100 @@ +require('dotenv').config() +import "reflect-metadata"; +import { createConnection, ConnectionOptions, Connection } from "typeorm"; +import fs = require('fs-extra'); +import path = require('path') +import { Engine } from "./../../src/Engine"; +import { expect } from "chai"; +import * as Sinon from 'sinon' +import { EntityFileToJson } from "../utils/EntityFileToJson"; +var chai = require('chai'); +var chaiSubset = require('chai-subset'); +import * as yn from "yn" +import * as ts from "typescript"; +import * as GTU from "../utils/GeneralTestUtils" + +chai.use(chaiSubset); + + +describe("GitHub issues", async function () { + this.timeout(20000) + this.slow(5000)//compiling created models takes time + + let dbDrivers: string[] = [] + if (process.env.POSTGRES_Skip == '0') dbDrivers.push('postgres') + if (process.env.MYSQL_Skip == '0') dbDrivers.push('mysql') + if (process.env.MARIADB_Skip == '0') dbDrivers.push('mariadb') + if (process.env.MSSQL_Skip == '0') dbDrivers.push('mssql') + + let examplesPathJS = path.resolve(process.cwd(), 'dist/test/integration/github-issues') + let examplesPathTS = path.resolve(process.cwd(), 'test/integration/github-issues') + let files = fs.readdirSync(examplesPathTS) + + for (let folder of files) { + + describe(`#${folder}`, async function () { + for (let dbDriver of dbDrivers) { + it(dbDriver, async function () { + + let filesOrgPathJS = path.resolve(examplesPathJS, folder, 'entity') + let filesOrgPathTS = path.resolve(examplesPathTS, folder, 'entity') + let resultsPath = path.resolve(process.cwd(), `output`) + fs.removeSync(resultsPath) + + let engine: Engine; + switch (dbDriver) { + case 'mssql': + engine = await GTU.createMSSQLModels(filesOrgPathJS, resultsPath) + break; + case 'postgres': + engine = await GTU.createPostgresModels(filesOrgPathJS, resultsPath) + break; + case 'mysql': + engine = await GTU.createMysqlModels(filesOrgPathJS, resultsPath) + break; + case 'mariadb': + engine = await GTU.createMariaDBModels(filesOrgPathJS, resultsPath) + break; + + default: + console.log(`Unknown engine type`); + engine = {} + break; + } + + + let result = await engine.createModelFromDatabase() + + let filesGenPath = path.resolve(resultsPath, 'entities') + + let filesOrg = fs.readdirSync(filesOrgPathTS).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) + let filesGen = fs.readdirSync(filesGenPath).filter(function (this, val, ind, arr) { return val.toString().endsWith('.ts') }) + + expect(filesOrg, 'Errors detected in model comparision').to.be.deep.equal(filesGen) + + for (let file of filesOrg) { + let entftj = new EntityFileToJson(); + let jsonEntityOrg = entftj.convert(fs.readFileSync(path.resolve(filesOrgPathTS, file))) + let jsonEntityGen = entftj.convert(fs.readFileSync(path.resolve(filesGenPath, file))) + expect(jsonEntityGen, `Error in file ${file}`).to.containSubset(jsonEntityOrg) + } + const currentDirectoryFiles = fs.readdirSync(filesGenPath). + filter(fileName => fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts").map(v => { + return path.resolve(filesGenPath, v) + }) + let compileErrors = GTU.compileTsFiles(currentDirectoryFiles, { + + experimentalDecorators: true, + sourceMap: false, + emitDecoratorMetadata: true, + target: ts.ScriptTarget.ES2016, + moduleResolution: ts.ModuleResolutionKind.NodeJs, + module: ts.ModuleKind.CommonJS + }); + expect(compileErrors, 'Errors detected while compiling generated model').to.be.false; + }); + + } + }) + } +}) diff --git a/test/integration/integration.test.ts b/test/integration/integration.test.ts index b6c30c3..1a9395d 100644 --- a/test/integration/integration.test.ts +++ b/test/integration/integration.test.ts @@ -16,12 +16,9 @@ import * as GTU from "../utils/GeneralTestUtils" chai.use(chaiSubset); -describe("integration tests - examples", async function () { +describe("TypeOrm examples", async function () { this.timeout(20000) this.slow(5000)//compiling created models takes time - let examplesPathJS = path.resolve(process.cwd(), 'dist/test/integration/examples') - let examplesPathTS = path.resolve(process.cwd(), 'test/integration/examples') - let files = fs.readdirSync(examplesPathTS) let dbDrivers: string[] = [] if (process.env.POSTGRES_Skip == '0') dbDrivers.push('postgres') @@ -29,6 +26,10 @@ describe("integration tests - examples", async function () { if (process.env.MARIADB_Skip == '0') dbDrivers.push('mariadb') if (process.env.MSSQL_Skip == '0') dbDrivers.push('mssql') + let examplesPathJS = path.resolve(process.cwd(), 'dist/test/integration/examples') + let examplesPathTS = path.resolve(process.cwd(), 'test/integration/examples') + let files = fs.readdirSync(examplesPathTS) + for (let folder of files) { describe(folder, async function () {