Skip to content

Commit

Permalink
test: check STI type setting discriminator manually
Browse files Browse the repository at this point in the history
Related to: typeorm#9033
  • Loading branch information
felix-gohla committed Aug 22, 2022
1 parent bc7f85c commit 13f72d6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/github-issues/9033/entity/Animal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
Column,
Entity,
PrimaryGeneratedColumn,
TableInheritance,
} from "../../../../src"

@Entity("animal")
@TableInheritance({ column: { type: "varchar", name: "type" } })
export class AnimalEntity {
@PrimaryGeneratedColumn()
id: number

@Column({ type: "varchar" })
name: string

@Column({
name: "type",
type: "varchar",
})
type: string
}
10 changes: 10 additions & 0 deletions test/github-issues/9033/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
}
48 changes: 48 additions & 0 deletions test/github-issues/9033/issue-9033.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/index"
import { expect } from "chai"

import { CatEntity } from "./entity/Cat"
import { AnimalEntity } from "./entity/Animal"

describe("github issues > #9033 Cannot manually insert type in discriminator column of parent entity class when\
using single table inheritance when creating instance of 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("is possible to set the discriminator column manually on the base entity", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const entityManager = dataSource.createEntityManager()
const animalRepo = entityManager.getRepository(AnimalEntity)

// Create a base class entity while manually setting discriminator.
const maybeACat = animalRepo.create({
name: "i-am-maybe-a-cat",
type: "cat", // This is the discriminator for `CatEntity`.
})
await entityManager.save(maybeACat)

// Load the animal / cat from the database.
const animals = await animalRepo.find()
expect(animals.length).to.equal(1)

expect(animals[0]).to.be.instanceOf(CatEntity)
expect(animals[0].type).to.equal("cat")
}),
))
})

0 comments on commit 13f72d6

Please sign in to comment.