Skip to content

Commit

Permalink
test: test saving disciminators STI, cascading
Browse files Browse the repository at this point in the history
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
felix-gohla committed May 24, 2022
1 parent cf3efec commit a799d9f
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/github-issues/2927/issue-2927.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("github issues > #2927 When using base class' custom repository, the di
// Retrieve it back from the DB.
const contents = await repository.find()
expect(contents.length).to.equal(1)
expect(contents[0] instanceof Photo).to.equal(true)
expect(contents[0]).to.be.an.instanceOf(Photo)
const fetchedPhoto = contents[0] as Photo
expect(fetchedPhoto).to.eql(photo)
}),
Expand All @@ -64,7 +64,7 @@ describe("github issues > #2927 When using base class' custom repository, the di
// Retrieve it back from the DB.
const contents = await repository.find()
expect(contents.length).to.equal(1)
expect(contents[0] instanceof SpecialPhoto).to.equal(true)
expect(contents[0]).to.be.an.instanceOf(SpecialPhoto)
const fetchedSpecialPhoto = contents[0] as SpecialPhoto
expect(fetchedSpecialPhoto).to.eql(specialPhoto)
}),
Expand Down
25 changes: 25 additions & 0 deletions test/github-issues/7558/entity/Animal.ts
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
}
10 changes: 10 additions & 0 deletions test/github-issues/7558/entity/Cat.ts
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
}
16 changes: 16 additions & 0 deletions test/github-issues/7558/entity/Content.ts
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
}
10 changes: 10 additions & 0 deletions test/github-issues/7558/entity/Dog.ts
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
}
30 changes: 30 additions & 0 deletions test/github-issues/7558/entity/Person.ts
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
}
8 changes: 8 additions & 0 deletions test/github-issues/7558/entity/Photo.ts
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
}
126 changes: 126 additions & 0 deletions test/github-issues/7558/issue-7758.ts
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)
}),
))
})

0 comments on commit a799d9f

Please sign in to comment.