Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #160

Merged
merged 16 commits into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
branches: [ master, develop ]

jobs:
build:
Expand All @@ -24,10 +24,11 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- uses: borales/actions-yarn@v2.0.0
- uses: borales/actions-yarn@v2.3.0
with:
cmd: install
- run: npm config set unsafe-perm=true
- run: sudo chmod -R 777 ./dist
- run: npm set unsafe-perm true
- run: npm run build --if-present
- run: npm run lint
- run: npm run test
Expand Down
69 changes: 35 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
"scripts": {
"postinstall": "opencollective-postinstall",
"build": "npm run clean && tsc -p tsconfig.json",
"prepare": "npm run build",
"clean": "trash dist",
"lint:fix": "prettier src/**/*.ts --write && tslint --fix --project .",
"lint": "tslint --project . && prettier src/**/*.ts --list-different",
"test": "nyc mocha",
"test": "nyc mocha 'test/**/*.ts'",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"commit": "npx git-cz"
Expand All @@ -38,58 +39,58 @@
"homepage": "https://github.com/RobinCK/typeorm-fixtures#readme",
"license": "MIT",
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@types/chai": "^4.2.11",
"@types/chai-as-promised": "^7.1.2",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/chai": "^4.2.14",
"@types/chai-as-promised": "^7.1.3",
"@types/chalk": "^2.2.0",
"@types/cli-progress": "^3.7.0",
"@types/cli-progress": "^3.8.0",
"@types/commander": "^2.12.2",
"@types/ejs": "^3.0.4",
"@types/faker": "^4.1.11",
"@types/glob": "^7.1.1",
"@types/js-yaml": "^3.12.3",
"@types/lodash": "^4.14.150",
"@types/mocha": "^7.0.2",
"@types/ejs": "^3.0.5",
"@types/faker": "^5.1.5",
"@types/glob": "^7.1.3",
"@types/js-yaml": "^3.12.5",
"@types/lodash": "^4.14.166",
"@types/mocha": "^8.2.0",
"@types/resolve-from": "^5.0.0",
"@types/yargs-parser": "^15.0.0",
"@types/yargs-parser": "^20.2.0",
"chai-as-promised": "^7.1.1",
"commitizen": "^4.1.2",
"conventional-changelog-cli": "^2.0.21",
"commitizen": "^4.2.2",
"conventional-changelog-cli": "^2.1.1",
"coveralls": "^3.0.4",
"cz-conventional-changelog": "^3.2.0",
"husky": "^4.2.5",
"cz-conventional-changelog": "^3.3.0",
"husky": "^4.3.6",
"jsonlint": "^1.6.3",
"lint-staged": "^10.2.6",
"mocha": "^7.1.1",
"nyc": "^15.0.1",
"prettier": "^2.0.5",
"trash-cli": "^3.0.0",
"ts-node": "^8.3.0",
"tslint": "^6.1.2",
"lint-staged": "^10.5.3",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"trash-cli": "^4.0.0",
"ts-node": "^9.1.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"tslint-immutable": "^6.0.1",
"typeorm": "^0.2.18",
"typescript": "^3.5.3",
"typeorm": "^0.2.29",
"typescript": "^4.1.3",
"yaml-lint": "^1.2.4"
},
"dependencies": {
"@hapi/joi": "^17.1.1",
"@types/hapi__joi": "^17.1.0",
"chai": "^4.2.0",
"chalk": "^4.0.0",
"chalk": "^4.1.0",
"class-transformer": "^0.3.1",
"cli-progress": "^3.0.0",
"commander": "^5.1.0",
"ejs": "^3.1.3",
"faker": "^4.1.0",
"commander": "^6.2.1",
"ejs": "^3.1.5",
"faker": "^5.1.0",
"glob": "^7.1.6",
"js-yaml": "3.14.0",
"lodash": "^4.17.14",
"opencollective-postinstall": "^2.0.2",
"js-yaml": "3.14.1",
"lodash": "^4.17.20",
"opencollective-postinstall": "^2.0.3",
"reflect-metadata": "^0.1.13",
"resolve-from": "^5.0.0",
"yargs-parser": "^18.1.3"
"yargs-parser": "^20.2.4"
},
"peerDependencies": {
"typeorm": "^0.2.11"
Expand Down
14 changes: 10 additions & 4 deletions src/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { isObject, isArray } from 'lodash';
import { Connection } from 'typeorm';
import { IDataParser, IFixture } from './interface';
import { IDataParser, IEntity, IFixture } from './interface';
import { plainToClassFromExist } from 'class-transformer';

export class Builder {
Expand All @@ -16,7 +16,7 @@ export class Builder {
*/
async build(fixture: IFixture) {
const repository = this.connection.getRepository(fixture.entity);
let entity = repository.create();
let entity: IEntity = repository.create() as IEntity;
let data = this.parser.parse(fixture.data, fixture, this.entities);
let call: object;

Expand Down Expand Up @@ -67,18 +67,24 @@ export class Builder {
data = await processorInstance.preProcess(fixture.name, data);
}

entity = plainToClassFromExist(entity, data, { ignoreDecorators: true });
entity = plainToClassFromExist(entity, data, { ignoreDecorators: false });
await callExecutors();

/* istanbul ignore else */
if (typeof processorInstance.postProcess === 'function') {
await processorInstance.postProcess(fixture.name, entity);
}
} else {
entity = plainToClassFromExist(entity, data, { ignoreDecorators: true });
entity = plainToClassFromExist(entity, data, { ignoreDecorators: false });
await callExecutors();
}

if (fixture.resolvedFields && Array.isArray(fixture.resolvedFields)) {
fixture.resolvedFields.forEach((propertyName) => {
entity[propertyName] = Promise.resolve(data[propertyName]);
});
}

this.entities[fixture.name] = entity;

return entity;
Expand Down
3 changes: 2 additions & 1 deletion src/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Resolver {
* @return {IFixture[]}
*/
resolve(fixtureConfigs: IFixturesConfig[]): IFixture[] {
for (const { entity, items, parameters, processor } of fixtureConfigs) {
for (const { entity, items, parameters, processor, resolvedFields } of fixtureConfigs) {
for (const [mainReferenceName, propertyList] of Object.entries(items)) {
const rangeRegExp = /^([\w-_]+)\{(\d+)\.\.(\d+)\}$/gm;
let referenceNames: string[] = [];
Expand All @@ -37,6 +37,7 @@ export class Resolver {
processor,
entity: entity,
name: name,
resolvedFields,
data,
dependencies: this.resolveDependencies(name, data),
});
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ createConnection(

try {
bar.increment(1, { name: fixture.name });
await connection.getRepository(fixture.entity).save(entity);
await connection.getRepository(fixture.entity)
.save(entity);
} catch (e) {
bar.stop();

throw e;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/interface/IEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IEntity {
[key: string]: any;
}
1 change: 1 addition & 0 deletions src/interface/IFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export interface IFixture {
entity: string;
name: string;
dependencies: string[];
resolvedFields?: string[];
data: any;
}
1 change: 1 addition & 0 deletions src/interface/IFixturesConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export interface IFixturesConfig {
locale?: string;
parameters?: { [key: string]: any };
processor?: string;
resolvedFields?: string[];
items: { [key: string]: any };
}
1 change: 1 addition & 0 deletions src/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './IDataParser';
export * from './IEntity';
export * from './IFixture';
export * from './IFixturesConfig';
export * from './ILoader';
Expand Down
2 changes: 1 addition & 1 deletion src/loaders/YamlLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export class YamlLoader implements ILoader {
* @return {IFixturesConfig}
*/
load(filePath: string): IFixturesConfig {
return yaml.safeLoad(fs.readFileSync(filePath).toString());
return yaml.safeLoad(fs.readFileSync(filePath).toString()) as IFixturesConfig;
}
}
1 change: 1 addition & 0 deletions src/schema/jFixturesSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ export const jFixturesSchema = Joi.object().keys({
),
parameters: Joi.object(),
processor: Joi.string(),
resolvedFields: Joi.array().items(Joi.string()),
items: Joi.object().required(),
});
48 changes: 48 additions & 0 deletions test/unit/Builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Connection as MockConnection } from './assets/mock/Connection';
import { Connection } from 'typeorm';
import { User } from './assets/entity/User';
import { Listing } from './assets/entity/Listing';
import { Post } from './assets/entity/Post';

chai.use(chaiAsPromised);

Expand All @@ -22,6 +23,7 @@ describe('Builder', () => {
name: 'user1',
processor: undefined,
dependencies: [],
resolvedFields: undefined,
data: {
firstName: 'firstName',
lastName: 'lastName',
Expand Down Expand Up @@ -49,6 +51,7 @@ describe('Builder', () => {
name: 'listing1',
processor: undefined,
dependencies: [],
resolvedFields: undefined,
data: {
location: {
type: 'Point',
Expand All @@ -74,6 +77,7 @@ describe('Builder', () => {
name: 'user1',
processor: path.join(__dirname, 'assets/processor/UserProcessor.ts'),
dependencies: [],
resolvedFields: undefined,
data: {
firstName: 'firstName',
lastName: 'lastName',
Expand Down Expand Up @@ -101,6 +105,7 @@ describe('Builder', () => {
name: 'user1',
processor: path.join(__dirname, 'assets/processor/UserProcessor.ts'),
dependencies: [],
resolvedFields: undefined,
data: {
firstName: 'firstName',
lastName: 'lastName',
Expand Down Expand Up @@ -129,6 +134,7 @@ describe('Builder', () => {
name: 'user1',
processor: 'assets/processor/UserProcessor.ts',
dependencies: [],
resolvedFields: undefined,
data: {
firstName: 'firstName',
lastName: 'lastName',
Expand All @@ -148,11 +154,53 @@ describe('Builder', () => {
parameters: {},
entity: 'User',
name: 'user1',
resolvedFields: undefined,
dependencies: [],
data: {
__call: [],
},
}),
).to.be.rejectedWith(`invalid "__call" parameter format`);
});

it('should be resolved entity field as promised', async () => {
const connection = new MockConnection();
const parser = new Parser();
const builder = new Builder(<Connection>connection, parser);
builder.entities = {
user1: Object.assign(new User(), {
firstName: 'foo',
lastName: 'boo',
email: 'email',
}),
};

const result = await builder.build({
parameters: {},
entity: 'Post',
name: 'post1',
dependencies: ['user1'],
processor: undefined,
resolvedFields: ['user'],
data: {
title: 'A Post',
description: 'A description',
user: '@user1',
},
});

chai.expect(result).to.be.instanceOf(Post);
const post = result as Post;
const awaitedResult = {
title: post.title,
description: post.description,
user: await post.user,
};

chai.expect(awaitedResult).to.be.deep.equal({
title: 'A Post',
description: 'A description',
user: builder.entities['user1'],
});
});
});
Loading