Skip to content

Commit

Permalink
test: Testing that the discriminatorValue gets saved for ChildEntity …
Browse files Browse the repository at this point in the history
…when saved by cascade (#6671)

* test: Testing that the discriminatorValue gets saved for ChildEntity when saved by cascade

* test: Update assertions to not be specific to retrieval order

Co-authored-by: Gareth Parker <gareth.parker@ros.gov.uk>
  • Loading branch information
Garethp and Gareth Parker committed Sep 8, 2020
1 parent 620a243 commit 2d4a8d7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
@@ -0,0 +1,19 @@
import {Column} from "../../../../../../../src/decorator/columns/Column";
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Staff} from "./Staff";
import {OneToMany} from "../../../../../../../src";

@Entity()
export class Faculty {

@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@OneToMany(type => Staff, staff => staff.faculty, { cascade: true, eager: true })
staff: Staff[];

}
@@ -0,0 +1,15 @@
import {ChildEntity} from "../../../../../../../src/decorator/entity/ChildEntity";
import {Staff} from "./Staff";
import {Column} from "../../../../../../../src";

@ChildEntity("PROFESSOR")
export class Professor extends Staff {

constructor(className: string) {
super();
this.className = className;
}

@Column()
className: string;
}
@@ -0,0 +1,15 @@
import {ChildEntity} from "../../../../../../../src/decorator/entity/ChildEntity";
import {Staff} from "./Staff";
import {Column} from "../../../../../../../src";

@ChildEntity("RESEARCHER")
export class Researcher extends Staff {

constructor(areaOfStudy: string) {
super();
this.areaOfStudy = areaOfStudy;
}

@Column()
areaOfStudy: string;
}
@@ -0,0 +1,17 @@
import {Column, Entity, ManyToOne, PrimaryGeneratedColumn, TableInheritance} from "../../../../../../../src";
import {Faculty} from "./Faculty";

@Entity()
@TableInheritance({ column: { name: "type", type: "varchar" } })
export class Staff {

@PrimaryGeneratedColumn()
id: number;

@ManyToOne(type => Faculty, faculty => faculty.staff)
faculty: Faculty;

@Column()
type: string;

}
@@ -0,0 +1,42 @@
import "reflect-metadata";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases
} from "../../../../../utils/test-utils";
import {expect} from "chai";
import {Connection} from "../../../../../../src/connection/Connection";
import {Faculty} from "./entity/Faculty";
import {Professor} from "./entity/Professor";
import {Researcher} from "./entity/Researcher";

describe("table-inheritance > single-table > relations > one-to-many-cascade-save", () => {

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

it("should work correctly with OneToMany relations", () => Promise.all(connections.map(async connection => {

// -------------------------------------------------------------------------
// Create
// -------------------------------------------------------------------------

const researcher = new Researcher("Economics");
await connection.getRepository(Researcher).save(researcher);

const faculty1 = new Faculty();
faculty1.name = "Economics";
faculty1.staff = [ new Professor("Economics 101"), researcher ];
await connection.getRepository(Faculty).save(faculty1);

const loadedFaculty = await connection.getRepository(Faculty).findOne() as Faculty;

expect(loadedFaculty.staff.find(staff => staff.type === "PROFESSOR")).to.not.be.undefined;
expect(loadedFaculty.staff.find(staff => staff.type === "RESEARCHER")).to.not.be.undefined;
})));

});

0 comments on commit 2d4a8d7

Please sign in to comment.