-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
GH3440.test.ts
100 lines (79 loc) · 2.85 KB
/
GH3440.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
import { Entity, MikroORM, PrimaryKey, Property, Type, ValidationError } from '@mikro-orm/core';
import { mockLogger } from '../../helpers';
export function toBinaryUuid(uuid: string): Buffer {
const buf = Buffer.from(uuid.replace(/-/g, ''), 'hex');
return Buffer.concat([
buf.slice(0, 4),
buf.slice(4, 6),
buf.slice(6, 8),
buf.slice(8, 16),
]);
}
export function fromBinaryUuid(uuid: Buffer): string {
return [
uuid.toString('hex', 0, 2),
uuid.toString('hex', 2, 4),
uuid.toString('hex', 4, 8),
uuid.toString('hex', 8, 10),
uuid.toString('hex', 10, 16),
].join('-');
}
export class Guid extends Type<string, Buffer> {
convertToDatabaseValue(value: string): Buffer {
if (typeof value !== 'string' || value.replace(/-/g, '').length !== 32) {
throw ValidationError.invalidType(Guid, value, 'JS');
}
return toBinaryUuid(value);
}
convertToJSValue(value: Buffer): string {
return fromBinaryUuid(value);
}
getColumnType() {
return `BINARY(16)`;
}
}
@Entity()
export class Couch {
@PrimaryKey({ type: Guid })
id!: string;
@Property({ type: Guid })
userId!: string;
@Property()
name!: string;
@Property({ version: true })
version!: number;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Couch],
dbName: 'mikro_orm_test_3440',
type: 'mysql',
port: 3308,
});
await orm.schema.refreshDatabase();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 3440`, async () => {
const mock = mockLogger(orm);
const e = new Couch();
e.id = 'aaaa-aaaa-c65f42b8-408a-034a6948448f';
e.userId = 'bbbb-bbbb-c65f42b8-408a-034a6948448f';
e.name = 'n1';
await orm.em.fork().persistAndFlush(e);
const e1 = await orm.em.findOneOrFail(Couch, e);
e1.name = 'n2';
await orm.em.flush();
expect(mock.mock.calls).toHaveLength(9);
expect(mock.mock.calls[0][0]).toMatch('begin');
expect(mock.mock.calls[1][0]).toMatch("insert into `couch` (`id`, `user_id`, `name`) values (X'aaaaaaaac65f42b8408a034a6948448f', X'bbbbbbbbc65f42b8408a034a6948448f', 'n1')");
expect(mock.mock.calls[2][0]).toMatch('select `c0`.`id`, `c0`.`version` from `couch` as `c0` where `c0`.`id` in (X\'aaaaaaaac65f42b8408a034a6948448f\')');
expect(mock.mock.calls[3][0]).toMatch('commit');
expect(mock.mock.calls[4][0]).toMatch("select `c0`.* from `couch` as `c0` where `c0`.`id` = X'aaaaaaaac65f42b8408a034a6948448f' limit 1");
expect(mock.mock.calls[5][0]).toMatch('begin');
expect(mock.mock.calls[6][0]).toMatch("update `couch` set `name` = 'n2', `version` = `version` + 1 where `id` = X'aaaaaaaac65f42b8408a034a6948448f' and `version` = 1");
expect(mock.mock.calls[7][0]).toMatch('select `c0`.`id`, `c0`.`version` from `couch` as `c0` where `c0`.`id` in (X\'aaaaaaaac65f42b8408a034a6948448f\')');
expect(mock.mock.calls[8][0]).toMatch('commit');
});