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

fix(builder): fix loads for relation with lazy loading set to true #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"prettier": "^2.0.0",
"sqlite3": "^5.0.8",
"trash-cli": "^5.0.0",
"ts-node": "^10.7.0",
"typeorm": "^0.3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export class Builder {
return entity;
}

private async buildEntity(fixture: IFixture, data: any): Promise<IEntity> {
private buildEntity(fixture: IFixture, data: any): IEntity {
const repository = this.dataSource.getRepository(fixture.entity);
const entity: IEntity = repository.create() as IEntity;
const entity: IEntity = repository.create(data) as IEntity;

// exclude prefixes to ignore __call methods
return plainToClassFromExist(entity, data, {
Expand Down
57 changes: 37 additions & 20 deletions test/unit/Builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as path from 'path';
import * as chaiAsPromised from 'chai-as-promised';
import * as chai from 'chai';
import { Builder, Parser } from '../../src';
import { Connection as MockConnection } from './assets/mock/Connection';
import { DataSource } from 'typeorm';
import { User } from './assets/entity/User';
import { Listing } from './assets/entity/Listing';
Expand All @@ -12,10 +11,27 @@ import { Post } from './assets/entity/Post';
chai.use(chaiAsPromised);

describe('Builder', () => {
let dataSource: DataSource;

beforeEach(() => {
dataSource = new DataSource({
database: ':memory:',
dropSchema: true,
entities: [Listing, Post, User],
synchronize: true,
logging: false,
type: 'sqlite',
});
});

afterEach(async () => {
await dataSource.destroy();
});

it('should be build entity', async () => {
const connection = new MockConnection();
await dataSource.initialize();
const parser = new Parser();
const builder = new Builder(<DataSource>connection, parser, false);
const builder = new Builder(<DataSource>dataSource, parser, false);

const result = await builder.build({
parameters: {},
Expand All @@ -40,10 +56,10 @@ describe('Builder', () => {
);
});

it('should be build and transformed entity', async () => {
const connection = new MockConnection();
it('should be build and transformed entity with class-transformer', async () => {
await dataSource.initialize();
const parser = new Parser();
const builder = new Builder(<DataSource>connection, parser, false);
const builder = new Builder(<DataSource>dataSource, parser, false);

const result = await builder.build({
parameters: {},
Expand All @@ -67,9 +83,9 @@ describe('Builder', () => {
});

it('should be processed entity', async () => {
const connection = new MockConnection();
await dataSource.initialize();
const parser = new Parser();
const builder = new Builder(<DataSource>connection, parser, false);
const builder = new Builder(<DataSource>dataSource, parser, false);

const result = await builder.build({
parameters: {},
Expand All @@ -95,9 +111,9 @@ describe('Builder', () => {
});

it('should be call method ', async () => {
const connection = new MockConnection();
await dataSource.initialize();
const parser = new Parser();
const builder = new Builder(<DataSource>connection, parser, false);
const builder = new Builder(<DataSource>dataSource, parser, false);

const result: any = await builder.build({
parameters: {},
Expand All @@ -122,10 +138,10 @@ describe('Builder', () => {
chai.expect(result.firstName).to.be.equal('emaNtsrif');
});

it('should be processor not found', () => {
const connection = new MockConnection();
it('should be processor not found', async () => {
await dataSource.initialize();
const parser = new Parser();
const builder = new Builder(<DataSource>connection, parser, false);
const builder = new Builder(<DataSource>dataSource, parser, false);

chai.expect(
builder.build({
Expand All @@ -144,10 +160,10 @@ describe('Builder', () => {
).to.be.rejectedWith('Processor "assets/processor/UserProcessor.ts" not found');
});

it('should be invalid __call parameter', () => {
const connection = new MockConnection();
it('should be invalid __call parameter', async () => {
await dataSource.initialize();
const parser = new Parser();
const builder = new Builder(<DataSource>connection, parser, false);
const builder = new Builder(<DataSource>dataSource, parser, false);

chai.expect(
builder.build({
Expand All @@ -164,9 +180,10 @@ describe('Builder', () => {
});

it('should be resolved entity field as promised', async () => {
const connection = new MockConnection();
await dataSource.initialize();
const parser = new Parser();
const builder = new Builder(<DataSource>connection, parser, false);
const builder = new Builder(<DataSource>dataSource, parser, false);

builder.entities = {
user1: Object.assign(new User(), {
firstName: 'foo',
Expand All @@ -188,15 +205,15 @@ describe('Builder', () => {
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',
Expand Down
20 changes: 19 additions & 1 deletion test/unit/assets/entity/Listing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { Column, Entity, PrimaryColumn, ValueTransformer } from 'typeorm';
import { Transform } from 'class-transformer';

/**
* Use to transform object in string (and reverse) to be compatible with SQLITE.
*/
const objectTransformer: ValueTransformer = {
from: (databaseValue: any): any => {
return JSON.parse(databaseValue);
},
to: (entityValue: any): any => {
return JSON.stringify(entityValue);
},
};

@Entity()
export class Listing {
@PrimaryColumn('int')
public id!: number;

@Transform(
(params) => {
return {
Expand All @@ -10,5 +27,6 @@ export class Listing {
},
{ toClassOnly: true },
)
location!: object;
@Column('varying character', { transformer: objectTransformer })
public location!: object;
}
25 changes: 18 additions & 7 deletions test/unit/assets/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import {User} from './User';
import { Column, Entity, PrimaryColumn } from 'typeorm';

import { User } from './User';

@Entity()
export class Post {
title!: string;
description!: string;
user!: Promise<User>;
@PrimaryColumn('int')
public id!: number;

@Column()
public title!: string;

@Column()
public description!: string;

@Column('int')
public user!: Promise<User>;

setTitle(value: string) {
public setTitle(value: string): void {
this.title = value;
}

setDescription(value: string) {
public setDescription(value: string): void {
this.description = value;
}

setUser(value: Promise<User>) {
public setUser(value: Promise<User>): void {
this.user = value;
}
}
27 changes: 20 additions & 7 deletions test/unit/assets/entity/User.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';

@Entity()
export class User {
firstName!: string;
lastName!: string;
email!: string;
password!: string;
@PrimaryColumn('int')
public id!: number;

@Column()
public firstName!: string;

@Column()
public lastName!: string;

@Column()
public email!: string;

@Column()
public password!: string;

setEmail(value: string) {
public setEmail(value: string): void {
this.email = value;
}

setFirstName(value: string) {
public setFirstName(value: string): void {
this.firstName = value;
}

async setPassword(value: string) {
public async setPassword(value: string): Promise<void> {
this.password = (await value) + 'md5';
}
}
18 changes: 0 additions & 18 deletions test/unit/assets/mock/Connection.ts

This file was deleted.

7 changes: 0 additions & 7 deletions test/unit/assets/mock/ListingRepository.ts

This file was deleted.

7 changes: 0 additions & 7 deletions test/unit/assets/mock/PostRepository.ts

This file was deleted.

7 changes: 0 additions & 7 deletions test/unit/assets/mock/UserRepository.ts

This file was deleted.

Loading