forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: check STI type setting discriminator manually
Related to: typeorm#9033
- Loading branch information
1 parent
bc7f85c
commit 13f72d6
Showing
3 changed files
with
80 additions
and
0 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
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 | ||
} |
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,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") | ||
}), | ||
)) | ||
}) |