-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: test saving disciminators STI, cascading
This commit adds an test for checking whether discriminators are saved correctly when saving a field with cascade that uses Single-Table-Inheritance.
- Loading branch information
1 parent
cf3efec
commit a799d9f
Showing
8 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
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
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,25 @@ | ||
import { | ||
Column, | ||
Entity, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
TableInheritance, | ||
} from "../../../../src" | ||
|
||
import { PersonEntity } from "./Person" | ||
|
||
@Entity("animal") | ||
@TableInheritance({ column: { type: "varchar", name: "type" } }) | ||
export class AnimalEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column({ type: "varchar" }) | ||
name: string | ||
|
||
@ManyToOne(() => PersonEntity, ({ pets }) => pets, { | ||
onDelete: "CASCADE", | ||
onUpdate: "CASCADE", | ||
}) | ||
person: PersonEntity | ||
} |
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,10 @@ | ||
import { ChildEntity, Column } from "../../../../src" | ||
|
||
import { AnimalEntity } from "./Animal" | ||
|
||
@ChildEntity("cat") | ||
export class CatEntity extends AnimalEntity { | ||
// Cat stuff | ||
@Column() | ||
livesLeft: number | ||
} |
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,16 @@ | ||
import { | ||
Column, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
TableInheritance, | ||
} from "../../../../src" | ||
|
||
@Entity("content") | ||
@TableInheritance({ column: { type: "varchar", name: "type" } }) | ||
export class Content { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
title: string | ||
} |
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,10 @@ | ||
import { ChildEntity, Column } from "../../../../src" | ||
|
||
import { AnimalEntity } from "./Animal" | ||
|
||
@ChildEntity("dog") | ||
export class DogEntity extends AnimalEntity { | ||
// Dog stuff | ||
@Column() | ||
steaksEaten: number | ||
} |
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,30 @@ | ||
import { | ||
Entity, | ||
JoinColumn, | ||
OneToMany, | ||
OneToOne, | ||
PrimaryGeneratedColumn, | ||
} from "../../../../src" | ||
|
||
import { AnimalEntity } from "./Animal" | ||
import { Content } from "./Content" | ||
|
||
@Entity({ name: "person" }) | ||
export class PersonEntity { | ||
@PrimaryGeneratedColumn() | ||
id!: number | ||
|
||
@OneToMany(() => AnimalEntity, ({ person }) => person, { | ||
cascade: true, | ||
eager: true, | ||
}) | ||
pets!: AnimalEntity[] | ||
|
||
@OneToOne(() => Content, { | ||
cascade: true, | ||
eager: true, | ||
nullable: true, | ||
}) | ||
@JoinColumn() | ||
content?: Content | ||
} |
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,8 @@ | ||
import { ChildEntity, Column } from "../../../../src" | ||
import { Content } from "./Content" | ||
|
||
@ChildEntity("photo") | ||
export class Photo extends Content { | ||
@Column() | ||
size: number | ||
} |
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,126 @@ | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/index" | ||
import { expect } from "chai" | ||
|
||
import { PersonEntity } from "./entity/Person" | ||
import { CatEntity } from "./entity/Cat" | ||
import { DogEntity } from "./entity/Dog" | ||
import { AnimalEntity } from "./entity/Animal" | ||
import { Photo } from "./entity/Photo" | ||
|
||
describe("github issues > #7558 Child entities' wrong discriminator value when embedded in parent entity", () => { | ||
let dataSources: DataSource[] | ||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(dataSources)) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should use the correct subclass for inheritance when saving & retrieving a single STI entity (one-to-one)", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const entityManager = dataSource.createEntityManager() | ||
const personRepo = entityManager.getRepository(PersonEntity) | ||
const photoRepo = entityManager.getRepository(Photo) | ||
|
||
const photo = photoRepo.create({ | ||
title: "Cat Photo", | ||
size: 42, | ||
}) | ||
expect(photo).to.be.instanceOf(Photo) | ||
|
||
// Create a new person, cascade saving the content. | ||
const person = personRepo.create({ | ||
pets: [], | ||
content: photo, | ||
}) | ||
expect(person.content).not.to.be.undefined | ||
expect(person.content).to.be.instanceOf(Photo) | ||
await entityManager.save(person) | ||
|
||
// Retrieve it back from the DB. | ||
const persons = await personRepo.find() | ||
expect(persons.length).to.equal(1) | ||
|
||
// And check whether the content / photo is still the same. | ||
expect(persons[0].pets.length).to.equal(0) | ||
expect(persons[0].content).not.to.be.undefined | ||
expect(persons[0].content).not.to.be.null | ||
expect(persons[0].content).to.be.instanceOf(Photo) | ||
expect(persons[0].content).to.include(photo) | ||
}), | ||
)) | ||
|
||
it("should use the correct subclass for inheritance when saving & retrieving multiple STI entities (one-to-many)", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const entityManager = dataSource.createEntityManager() | ||
const personRepo = entityManager.getRepository(PersonEntity) | ||
const animalRepo = entityManager.getRepository(AnimalEntity) | ||
const dogRepo = entityManager.getRepository(DogEntity) | ||
const catRepo = entityManager.getRepository(CatEntity) | ||
|
||
const mysteryUnicorn = animalRepo.create({ | ||
name: "i-am-a-mystery", | ||
}) | ||
expect(mysteryUnicorn).to.be.instanceOf(AnimalEntity) | ||
const felix = catRepo.create({ | ||
name: "felix", | ||
livesLeft: 9, | ||
// Cat stuff | ||
}) | ||
expect(felix).to.be.instanceOf(CatEntity) | ||
const spike = dogRepo.create({ | ||
name: "spike", | ||
steaksEaten: 42, | ||
// Dog stuff | ||
}) | ||
expect(spike).to.be.instanceOf(DogEntity) | ||
const pets = [mysteryUnicorn, felix, spike] | ||
|
||
// Create a new person, cascade saving the pets. | ||
const person = personRepo.create({ | ||
pets, | ||
}) | ||
expect(person.pets[0]).to.be.instanceOf(AnimalEntity) | ||
expect(person.pets[1]).to.be.instanceOf(CatEntity) | ||
expect(person.pets[2]).to.be.instanceOf(DogEntity) | ||
await entityManager.save(person) | ||
|
||
// Retrieve it back from the DB. | ||
const persons = await personRepo.find({ relations: ["pets"] }) | ||
expect(persons.length).to.equal(1) | ||
|
||
// And check whether the pets are still the same. | ||
expect(persons[0].pets.length).to.equal(3) | ||
const animalPets = persons[0].pets.filter( | ||
(entity) => | ||
entity instanceof AnimalEntity && | ||
!(entity instanceof CatEntity) && | ||
!(entity instanceof DogEntity), | ||
) | ||
const catPets = persons[0].pets.filter( | ||
(entity) => entity instanceof CatEntity, | ||
) | ||
const dogPets = persons[0].pets.filter( | ||
(entity) => entity instanceof DogEntity, | ||
) | ||
expect(animalPets.length).to.equal(1) | ||
expect(animalPets[0]).to.include(mysteryUnicorn) | ||
expect(catPets.length).to.equal(1) | ||
expect(catPets[0]).to.include(felix) | ||
expect(dogPets.length).to.equal(1) | ||
expect(dogPets[0]).to.include(spike) | ||
}), | ||
)) | ||
}) |