Skip to content

Commit

Permalink
Add tests for github issue typeorm#2729
Browse files Browse the repository at this point in the history
  • Loading branch information
yazshel committed Oct 8, 2018
1 parent bcd5d82 commit 3b8b228
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test/github-issues/2729/entity/Bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "../../../../src";
import { Foo } from "./Foo";

@Entity()
export class Bar {

@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@OneToMany(() => Foo, foo => foo.bar, { lazy: true })
foos: Promise<Foo[]>;
}
14 changes: 14 additions & 0 deletions test/github-issues/2729/entity/Baz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "../../../../src";
import { Foo } from "./Foo";

@Entity()
export class Baz {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@OneToMany(() => Foo, foo => foo.baz, { lazy: true })
foos: Promise<Foo[]>;
}
18 changes: 18 additions & 0 deletions test/github-issues/2729/entity/Foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "../../../../src";
import { Bar } from "./Bar";
import { Baz } from "./Baz";

@Entity()
export class Foo {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@ManyToOne(() => Bar, bar => bar.foos, { lazy: true })
bar: Promise<Bar>;

@ManyToOne(() => Baz, baz => baz.foos, { lazy: true })
baz: Promise<Baz>;
}
76 changes: 76 additions & 0 deletions test/github-issues/2729/issue-2729.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import "reflect-metadata";

// import { expect } from "chai";

import { Connection } from "../../../src/connection/Connection";
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Foo } from "./entity/Foo";
import { Bar } from "./entity/Bar";
import { Baz } from "./entity/Baz";

// import { expect } from "chai";
describe("github issues > #2729 creating entities with lazy relationships fails to handle Promises in input", () => {

let connections: Connection[];
before(async () => {
connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
});
});

beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("saves instance with unresolved Promise on \"one\" end lazy relation property", () => Promise.all(connections.map(async connection => {

const fooRepo = connection.manager.getRepository(Foo);
const barRepo = connection.manager.getRepository(Bar);

const barPromise = barRepo.save(barRepo.create({ name: "Bar" }));

const foo = fooRepo.create({ name: "Foo", bar: barPromise });

await fooRepo.save(foo);

foo.id.should.not.be.null;

})));

it("saves instance with unresolved Promise on \"many\" end of lazy relation property", () => Promise.all(connections.map(async connection => {

const fooRepo = connection.manager.getRepository(Foo);
const bazRepo = connection.manager.getRepository(Baz);

const fooValues = [
{ name: "Foo1" },
{ name: "Foo2" },
{ name: "Foo3" },
];

// Create baz with Promise.resolve of foo values
const baz1 = bazRepo.create({
name: "Baz1",
foos: Promise.resolve(fooValues),
});
await bazRepo.save(baz1);

// Create baz with resolved promise of actual entities
const savedFoosPromise = fooRepo.save(fooValues.map(values => fooRepo.create(values)));

const baz2 = await bazRepo.save(bazRepo.create({
name: "Baz2",
foos: savedFoosPromise,
}));

baz1.id.should.not.be.null;
const baz1Foos = await baz1.foos;
baz1Foos.length.should.equal(3);

baz2.id.should.not.be.null;
const baz2Foos = await baz2.foos;
baz2Foos.length.should.equal(3);
})));

});

0 comments on commit 3b8b228

Please sign in to comment.