added partial entity comparision in integration tests
This commit is contained in:
parent
2adce2f54f
commit
5e307aca65
@ -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": {
|
||||
|
@ -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)
|
||||
}
|
||||
});
|
||||
|
||||
|
115
test/utils/EntityFileToJson.ts
Normal file
115
test/utils/EntityFileToJson.ts
Normal file
@ -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[] = <EntityColumn[]>[];
|
||||
}
|
||||
class EntityColumn {
|
||||
columnName: string
|
||||
columnType: string
|
||||
}
|
Loading…
Reference in New Issue
Block a user