-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtypeorm.ts
More file actions
275 lines (255 loc) · 6.65 KB
/
Copy pathtypeorm.ts
File metadata and controls
275 lines (255 loc) · 6.65 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { getConnection, getRepository, ObjectLiteral } from "typeorm";
import { request, gql } from "graphql-request";
import { connect, listen } from "../examples/typeorm";
import typeormResolvers from "../examples/typeorm/resolvers";
import { Company } from "../examples/typeorm/entities/Company";
import { Desk } from "../examples/typeorm/entities/Desk";
import { Chair } from "../examples/typeorm/entities/Chair";
import { Cert } from "../examples/typeorm/entities/Cert";
import { Employee } from "../examples/typeorm/entities/Employee";
let close: () => Promise<void>;
let endpoint: string;
const seed = async () => {
const [company, company2] = await Promise.all(
[{ name: "company1" }, { name: "company2" }].map((v) =>
getRepository(Company, "test").save(new Company(v))
)
);
const [desk1, desk2, desk3] = await Promise.all(
[
{ name: "desk1", company },
{ name: "desk2", company },
{ name: "desk3", company },
].map((v) => getRepository(Desk, "test").save(new Desk(v)))
);
const [chair1] = await Promise.all(
[
{ name: "chair1", company, desk: desk1 },
{ name: "chair2", company: company2 },
].map((v) => getRepository(Chair, "test").save(new Chair(v)))
);
const [cert1, cert2, cert3] = await Promise.all(
[{ name: "cert1" }, { name: "cert2" }, { name: "cert3" }].map((v) =>
getRepository(Cert, "test").save(new Cert(v))
)
);
const [employee1, employee2, employee3] = await Promise.all(
[
{ name: "employee1", company, desk: desk1, certs: [cert1, cert2] },
{ name: "employee2", company, desk: desk2, certs: [cert1] },
{ name: "employee3", company, certs: [] },
].map((v) => getRepository(Employee, "test").save(new Employee(v)))
);
};
beforeAll(async () => {
await connect();
await seed();
const { port, close: _close } = await listen(0, typeormResolvers);
close = _close;
endpoint = `http://localhost:${port}/graphql`;
});
afterAll(async () => {
await close();
await getConnection("test").close();
});
const objectTypes = {
Company,
Employee,
Desk,
Chair,
Cert,
};
type typename = "Company" | "Employee" | "Desk" | "Chair" | "Cert";
const coalesceTypenames = (objects: ObjectLiteral[]): typename => {
const typename = objects
.map((a) => a.__typename)
.reduce((a, b) => (a === b ? a : null));
if (typename == null) {
throw Error("typename mismatch");
}
return typename;
};
const verify = async <Entity extends ObjectLiteral>(
objectOrObjects: ObjectLiteral | ObjectLiteral[],
entityOrEntities: Entity | Entity[]
) => {
if (Array.isArray(objectOrObjects)) {
if (!Array.isArray(entityOrEntities)) {
throw Error("entityOrEntities type mismatch");
}
const entities = entityOrEntities;
const objects = objectOrObjects;
expect(objects.length).toEqual(entities.length);
if (objects.length === 0) {
return;
}
coalesceTypenames(objects);
await Promise.all(
objects.map((object) => {
const entity = entities.find((entity) => entity.name === object.name);
if (entity == null) {
throw Error("Corresponding entity was not found");
}
return verify(object, entity);
})
);
} else {
if (Array.isArray(entityOrEntities)) {
throw Error("entityOrEntities type mismatch");
}
const obj = objectOrObjects;
const entity = entityOrEntities;
expect(obj.name).toEqual(entity.name);
await Promise.all(
Object.keys(obj).map(async (k) => {
const nextObj = obj[k];
const getSelfEntity = async () =>
(await getRepository(
objectTypes[obj.__typename as typename]
, "test").findOneOrFail({
where: { name: obj.name },
relations: [k],
})) as any;
if (Array.isArray(nextObj)) {
// ToMany field
const nextEntities = await (await getSelfEntity())[k];
return verify(nextObj, nextEntities);
} else {
// ToOne field (null)
if (nextObj == null) {
expect(await (await getSelfEntity())[k]).toBeNull();
return;
}
// Column field
const typename = nextObj.__typename as typename;
if (typename == null) {
return;
}
// ToOne field
const nextEntity = await (await getSelfEntity())[k];
return verify(nextObj, nextEntity);
}
})
);
}
};
test("verify query companies", async () => {
const query = gql`
query {
companies {
__typename
name
employees {
__typename
name
company {
__typename
name
employees {
__typename
name
}
}
}
desks {
__typename
name
company {
__typename
name
}
}
chairs {
__typename
name
company {
__typename
name
chairs {
__typename
name
}
}
}
}
}
`;
const data = await request(endpoint, query);
await verify(data.companies, await getRepository(Company, "test").find());
});
test("verify query employees", async () => {
const query = gql`
query {
employees {
__typename
name
company {
__typename
name
}
desk {
__typename
name
employee {
__typename
name
}
}
certs {
__typename
name
}
}
}
`;
const data = await request(endpoint, query);
await verify(data.employees, await getRepository(Employee, "test").find());
});
test("verify query certs", async () => {
const query = gql`
query {
certs {
__typename
name
employees {
__typename
name
}
}
}
`;
const data = await request(endpoint, query);
await verify(data.certs, await getRepository(Cert, "test").find());
});
test("verify query desks", async () => {
const query = gql`
query {
desks {
__typename
name
company {
__typename
name
}
employee {
__typename
name
}
chair {
__typename
name
company {
__typename
name
}
desk {
__typename
name
}
}
}
}
`;
const data = await request(endpoint, query);
await verify(data.desks, await getRepository(Desk, "test").find());
});