-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
GH997.test.ts
122 lines (93 loc) · 3.45 KB
/
GH997.test.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Entity, MikroORM, PrimaryKey, Property, OneToMany, ManyToOne, Collection, QueryOrder } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
abstract class Base {
@PrimaryKey()
id!: number;
}
@Entity({
discriminatorColumn: 'type',
abstract: true
})
class Parent extends Base {
@Property()
type!: string;
@OneToMany(() => Relation1, e => e.parent)
qaInfo = new Collection<Relation1>(this);
}
@Entity()
class Relation1 extends Base {
@ManyToOne()
parent!: Parent;
}
@Entity({discriminatorValue: 'Child1'})
class Child1 extends Parent {
@OneToMany(() => Child1Specific, e => e.child1)
rel = new Collection<Child1Specific>(this);
}
@Entity()
class Child1Specific extends Base {
@ManyToOne()
child1!: Child1;
}
@Entity({discriminatorValue: 'Child2'})
class Child2 extends Parent {}
describe('GH issue 997', () => {
let orm: MikroORM<SqliteDriver>;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Base, Relation1, Child1Specific, Parent, Child1, Child2],
dbName: ':memory:',
type: 'sqlite',
});
await orm.getSchemaGenerator().ensureDatabase();
await orm.getSchemaGenerator().dropSchema();
await orm.getSchemaGenerator().createSchema();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 997`, async () => {
// for unknown reason, for me setup from GH845.test.ts doesn't work,
// complaining about default values for table columns,
// so i tweaked it until it works
const c1 = new Child1();
const c2 = new Child2();
await orm.em.persistAndFlush([c1, c2]);
orm.em.clear();
const [ci1, ci2]: [Child1, Child2] = await orm.em.find(Parent, {}, ['qaInfo.parent', 'rel'], { type: QueryOrder.ASC }) as any;
ci1.rel.add(new Child1Specific());
ci1.rel.add(new Child1Specific());
ci1.rel.add(new Child1Specific());
ci2.qaInfo.add(new Relation1());
ci2.qaInfo.add(new Relation1());
ci2.qaInfo.add(new Relation1());
await orm.em.persistAndFlush([ci1, ci2]);
orm.em.clear();
const results = await orm.em.createQueryBuilder(Parent)
.offset(0)
.limit(10)
.orderBy({type: QueryOrder.ASC})
.getResult();
const parents = await orm.em.populate(results, ['qaInfo.parent', 'rel']);
expect(parents[0]).toBeInstanceOf(Child1);
expect(parents[0].type).toBe('Child1');
expect(parents[0].qaInfo.length).toBe(0);
expect((parents[0] as Child1).rel.length).toBe(3);
expect((parents[0] as Child1).rel[0]).toBeInstanceOf(Child1Specific);
expect((parents[0] as Child1).rel[0].child1).toBeInstanceOf(Child1);
expect((parents[0] as Child1).rel[1]).toBeInstanceOf(Child1Specific);
expect((parents[0] as Child1).rel[1].child1).toBeInstanceOf(Child1);
expect((parents[0] as Child1).rel[2]).toBeInstanceOf(Child1Specific);
expect((parents[0] as Child1).rel[2].child1).toBeInstanceOf(Child1);
expect(parents[1]).toBeInstanceOf(Child2);
expect(parents[1].type).toBe('Child2');
expect(parents[1].qaInfo.length).toBe(3);
expect(parents[1].qaInfo[0]).toBeInstanceOf(Relation1);
expect(parents[1].qaInfo[0].parent).toBeInstanceOf(Child2);
expect(parents[1].qaInfo[1]).toBeInstanceOf(Relation1);
expect(parents[1].qaInfo[1].parent).toBeInstanceOf(Child2);
expect(parents[1].qaInfo[2]).toBeInstanceOf(Relation1);
expect(parents[1].qaInfo[2].parent).toBeInstanceOf(Child2);
expect((parents[1] as Child1).rel).toBeUndefined();
});
});