-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
GH4129.test.ts
109 lines (79 loc) · 2.91 KB
/
GH4129.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
import { Collection, Entity, LoadStrategy, ManyToOne, OneToMany, PrimaryKey, Property, SimpleLogger } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';
import { mockLogger } from '../../helpers';
@Entity()
class Channel {
@PrimaryKey()
id!: number;
@Property()
name!: string;
@OneToMany(() => DeviceChannel, item => item.channel, { orphanRemoval: true })
links = new Collection<DeviceChannel>(this);
}
@Entity()
class Device {
@PrimaryKey()
id!: number;
@Property()
serial!: string;
@OneToMany(() => DeviceChannel, item => item.device, { orphanRemoval: true })
links = new Collection<DeviceChannel>(this);
}
@Entity()
class DeviceChannel {
@ManyToOne({ entity: () => Device, primary: true })
device!: Device;
@ManyToOne({ entity: () => Channel, primary: true })
channel!: Channel;
@Property()
lastTime!: Date;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Device, Channel, DeviceChannel],
dbName: ':memory:',
loggerFactory: options => new SimpleLogger(options),
loadStrategy: LoadStrategy.JOINED,
});
await orm.schema.refreshDatabase();
});
afterAll(async () => {
await orm.close(true);
});
beforeEach(async () => {
await orm.schema.clearDatabase();
const chn = orm.em.create(Channel, { id: 1, name: 'ChannelOne', links: [] });
const dev1 = orm.em.create(Device, { id: 2, serial: 'DeviceOne', links: [] });
const link1 = orm.em.create(DeviceChannel, { device: dev1, channel: chn, lastTime: new Date() });
chn.links.add(link1);
await orm.em.flush();
orm.em.clear();
});
test('GH 4129 (1/3)', async () => {
const channel = await orm.em.findOneOrFail(Channel, { name: 'ChannelOne' }, { populate: ['links'] });
for (const link of channel.links) {
link.lastTime = new Date(1678803173316);
}
const mock = mockLogger(orm);
await orm.em.flush();
expect(mock.mock.calls[1][0]).toBe('[query] update `device_channel` set `last_time` = 1678803173316 where (`device_id`, `channel_id`) in ((2, 1))');
});
test('GH 4129 (2/3)', async () => {
const channel = await orm.em.findOneOrFail(Channel, { name: 'ChannelOne' }, { populate: ['links.device', 'links.channel'] });
for (const link of channel.links) {
link.lastTime = new Date(1678803173316);
}
const mock = mockLogger(orm);
await orm.em.flush();
expect(mock.mock.calls[1][0]).toBe('[query] update `device_channel` set `last_time` = 1678803173316 where (`device_id`, `channel_id`) in ((2, 1))');
});
test('GH 4129 (3/3)', async () => {
const channel = await orm.em.findOneOrFail(Channel, { name: 'ChannelOne' }, { populate: ['links.device'] });
for (const link of channel.links) {
link.lastTime = new Date(1678803173316);
}
const mock = mockLogger(orm);
await orm.em.flush();
expect(mock.mock.calls[1][0]).toBe('[query] update `device_channel` set `last_time` = 1678803173316 where (`device_id`, `channel_id`) in ((2, 1))');
});