test: simplified test for issue #8018 (#8308)

* test: create simplified test for https://github.com/typeorm/typeorm/issues/8018

* test: improve test title for issue 8018 to be consistent with project

Co-authored-by: cduff <chris@praxhub.com>
This commit is contained in:
Chris Duff 2021-10-28 16:16:19 +11:00 committed by GitHub
parent 0adad8810e
commit bc55836fff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,24 @@
import {
Column,
Entity,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from "../../../../src";
import { Grandchild } from "./Grandchild";
import { Parent } from "./Parent";
@Entity()
export class Child {
@PrimaryGeneratedColumn()
id: number;
@Column()
name?: string;
@ManyToOne(() => Parent, (parent) => parent.children)
parent?: Parent;
@OneToMany(() => Grandchild, (grandchild) => grandchild.parent)
children?: Grandchild[];
}

View File

@ -0,0 +1,11 @@
import { Entity, ManyToOne, PrimaryGeneratedColumn } from "../../../../src";
import { Child } from "./Child";
@Entity()
export class Grandchild {
@PrimaryGeneratedColumn()
id: number;
@ManyToOne(() => Child, (parent) => parent.children)
parent?: Child;
}

View File

@ -0,0 +1,19 @@
import {
Column,
Entity,
OneToMany,
PrimaryGeneratedColumn,
} from "../../../../src";
import { Child } from "./Child";
@Entity()
export class Parent {
@PrimaryGeneratedColumn()
id: number;
@Column()
name?: string;
@OneToMany(() => Child, (child) => child.parent)
children?: Child[];
}

View File

@ -0,0 +1,52 @@
import { expect } from "chai";
import "reflect-metadata";
import { Connection } from "../../../src";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils";
import { Child } from "./entity/Child";
import { Grandchild } from "./entity/Grandchild";
import { Parent } from "./entity/Parent";
describe("github issues > #8018 Non-unique relation property names causes entity mixup in query results", () => {
let connections: Connection[];
before(
async () =>
(connections = await createTestingConnections({
entities: [Parent, Child, Grandchild],
schemaCreate: true,
dropSchema: true,
}))
);
beforeEach(async () => await reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should create child entities of the correct type", async () =>
await Promise.all(
connections.map(async (connection) => {
const parent = new Parent();
parent.name = "parent";
const child1 = new Child();
child1.name = "child1";
child1.parent = parent;
const child2 = new Child();
child2.name = "child2";
child2.parent = parent;
await connection.manager.save([parent, child1, child2]);
const result = await connection.manager.find(Parent, {
relations: ["children"],
});
expect(result).to.have.lengthOf(1);
expect(result[0].children).to.have.lengthOf(2);
expect(result[0].children![0]).to.be.instanceOf(Child);
expect(result[0].children![1]).to.be.instanceOf(Child);
})
));
});