Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Testing that the discriminatorValue gets saved for ChildEntity …
…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
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...onal/table-inheritance/single-table/relations/one-to-many-casecade-save/entity/Faculty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[]; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...al/table-inheritance/single-table/relations/one-to-many-casecade-save/entity/Professor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
15 changes: 15 additions & 0 deletions
15
...l/table-inheritance/single-table/relations/one-to-many-casecade-save/entity/Researcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
17 changes: 17 additions & 0 deletions
17
...tional/table-inheritance/single-table/relations/one-to-many-casecade-save/entity/Staff.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...-inheritance/single-table/relations/one-to-many-casecade-save/one-to-many-cascade-save.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}))); | ||
|
||
}); |