-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
issue-2927.ts
103 lines (91 loc) · 4.15 KB
/
issue-2927.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/index"
import { expect } from "chai"
import { Content } from "./entity/Content"
import { Photo } from "./entity/Photo"
import { SpecialPhoto } from "./entity/SpecialPhoto"
import { Post } from "./entity/Post"
describe("github issues > #2927 When using base class' custom repository, the discriminator is ignored", () => {
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 and retrieving concrete instance", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const entityManager = dataSource.createEntityManager()
const repository = entityManager.getRepository(Content)
// Create and save a new Photo.
const photo = new Photo()
photo.title = "some title"
photo.description = "some description"
photo.size = 42
await repository.save(photo)
// 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)
const fetchedPhoto = contents[0] as Photo
expect(fetchedPhoto).to.eql(photo)
}),
))
it("should work for deeply nested classes", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const entityManager = dataSource.createEntityManager()
const repository = entityManager.getRepository(Content)
// Create and save a new SpecialPhoto.
const specialPhoto = new SpecialPhoto()
specialPhoto.title = "some title"
specialPhoto.description = "some description"
specialPhoto.size = 42
specialPhoto.specialProperty = 420
await repository.save(specialPhoto)
// 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)
const fetchedSpecialPhoto = contents[0] as SpecialPhoto
expect(fetchedSpecialPhoto).to.eql(specialPhoto)
}),
))
it("should work for saving and fetching different subclasses", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const entityManager = dataSource.createEntityManager()
const repository = entityManager.getRepository(Content)
// Create and save a new Post.
const post = new Post()
post.title = "some title"
post.description = "some description"
post.viewCount = 69
// Create and save a new SpecialPhoto.
const specialPhoto = new SpecialPhoto()
specialPhoto.title = "some title"
specialPhoto.description = "some description"
specialPhoto.size = 42
specialPhoto.specialProperty = 420
await repository.save([post, specialPhoto])
// Retrieve them back from the DB.
const contents = await repository.find()
expect(contents.length).to.equal(2)
expect(contents.find((content) => content instanceof Post)).not
.to.be.undefined
expect(
contents.find((content) => content instanceof SpecialPhoto),
).not.to.be.undefined
}),
))
})