From 5e307aca65201dd9112502dfe17b5b764862c371 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sat, 20 May 2017 00:31:26 +0200 Subject: [PATCH] added partial entity comparision in integration tests --- package.json | 2 +- test/integration/integration.test.ts | 7 +- test/utils/EntityFileToJson.ts | 115 +++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 test/utils/EntityFileToJson.ts diff --git a/package.json b/package.json index 2184447..aae8988 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "typings-install": "typings install", "setup": "npm install && npm run typings-install", "start": "tsc && node ./src/index.js", - "test": "istanbul cover ./node_modules/mocha/bin/_mocha test/**/*.test.js --reporter lcovonly -- -R spec", + "test": "istanbul cover ./node_modules/mocha/bin/_mocha test/**/*.test.js -- -R spec", "posttest": "remap-istanbul -i ./coverage/coverage.json -o ./coverage/coverage-remapped.json && codecov --file=./coverage/coverage-remapped.json " }, "repository": { diff --git a/test/integration/integration.test.ts b/test/integration/integration.test.ts index 85aad36..f5666fb 100644 --- a/test/integration/integration.test.ts +++ b/test/integration/integration.test.ts @@ -10,6 +10,7 @@ import { MssqlDriver } from "./../../src/drivers/MssqlDriver"; import { DriverType } from "typeorm/driver/DriverOptions"; import { expect } from "chai"; import * as Sinon from 'sinon' +import { EntityFileToJson } from "../utils/EntityFileToJson"; describe("integration tests", async function () { @@ -75,8 +76,10 @@ describe("integration tests", async function () { 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()) + let entftj = new EntityFileToJson(); + let jsonEntityOrg= entftj.convert(fs.readFileSync(path.resolve(filesOrgPath, file))) + let jsonEntityGen= entftj.convert(fs.readFileSync(path.resolve(filesGenPath, file))) + expect(jsonEntityGen).to.be.deep.eq(jsonEntityOrg) } }); diff --git a/test/utils/EntityFileToJson.ts b/test/utils/EntityFileToJson.ts new file mode 100644 index 0000000..3256012 --- /dev/null +++ b/test/utils/EntityFileToJson.ts @@ -0,0 +1,115 @@ +export class EntityFileToJson { + + convert(entityFile: Buffer): EntityJson { + let retVal = new EntityJson(); + + let isInClassBody = false; + let isMultilineStatement = false; + let priorPartOfMultilineStatement = ''; + + let lines = entityFile.toString().replace('\r','').split('\n'); + for (let line of lines) { + let trimmedLine = line.trim(); + if (isMultilineStatement) + trimmedLine = priorPartOfMultilineStatement + ' ' + trimmedLine + if (trimmedLine.length == 0) + continue;//empty line + + else if (!isInClassBody) { + if (trimmedLine.startsWith('import')) { + continue; //import statement is not part of entity definition + } else if (trimmedLine.startsWith('@Entity')) { + continue; //TODO:entity options + } else if (trimmedLine.startsWith('export class')) { + retVal.entityName = trimmedLine.substring(trimmedLine.indexOf('class') + 5, trimmedLine.lastIndexOf('{')).trim().toLowerCase() + isInClassBody = true; + continue; + } + } else { + if (trimmedLine.startsWith('@Column')) { + if (this.isPartOfMultilineStatement(trimmedLine)) { + isMultilineStatement = true; + priorPartOfMultilineStatement = trimmedLine; + continue; + } else { + isMultilineStatement = false; + retVal.columns.push(new EntityColumn()) + //TODO:Options, column type if declared + continue; + } + + } else if (trimmedLine.startsWith('@PrimaryColumn')) { + if (this.isPartOfMultilineStatement(trimmedLine)) { + isMultilineStatement = true; + priorPartOfMultilineStatement = trimmedLine; + continue; + } else { + isMultilineStatement = false; + retVal.columns.push(new EntityColumn()) + //TODO:Options, column type if declared + continue; + } + } else if (trimmedLine.startsWith('@PrimaryGeneratedColumn')) { + if (this.isPartOfMultilineStatement(trimmedLine)) { + isMultilineStatement = true; + priorPartOfMultilineStatement = trimmedLine; + continue; + } else { + isMultilineStatement = false; + retVal.columns.push(new EntityColumn()) + //TODO:Options, column type if declared + continue; + } + } else if (trimmedLine.startsWith('@ManyToOne')) { + if (this.isPartOfMultilineStatement(trimmedLine)) { + isMultilineStatement = true; + priorPartOfMultilineStatement = trimmedLine; + continue; + } else { + isMultilineStatement = false; + retVal.columns.push(new EntityColumn()) + //TODO:Options,relation options if declared + continue; + } + } else if (trimmedLine.startsWith('@OneToMany')) { + if (this.isPartOfMultilineStatement(trimmedLine)) { + isMultilineStatement = true; + priorPartOfMultilineStatement = trimmedLine; + continue; + } else { + isMultilineStatement = false; + retVal.columns.push(new EntityColumn()) + //TODO:Options, relation options if declared + continue; + } + } else if (trimmedLine.split(':').length - 1 > 0) { + retVal.columns[retVal.columns.length-1].columnName=trimmedLine.split(':')[0].trim(); + retVal.columns[retVal.columns.length-1].columnType=trimmedLine.split(':')[1].split(';')[0].trim(); + continue + }else if(trimmedLine='}'){ + isInClassBody=false; + continue; //class declaration end + } + } + + console.log(`[EntityFileToJson:convert] Line not recognized: ${trimmedLine}`) + } + return retVal; + } + isPartOfMultilineStatement(statement: string) { + let matchStarting = statement.split('(').length - 1 + let matchEnding = statement.split(')').length - 1 + + return !(matchStarting == matchEnding) + } +} +class EntityJson { + entityName: string + entityOptions: any = {} + + columns: EntityColumn[] = []; +} +class EntityColumn { + columnName: string + columnType: string +} \ No newline at end of file