Skip to content

Commit aca3fe2

Browse files
authored
test: add test coverage for PresetDal (@fehmer) (#5615)
1 parent 5b970ec commit aca3fe2

2 files changed

Lines changed: 323 additions & 1 deletion

File tree

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
import { ObjectId } from "mongodb";
2+
import * as PresetDal from "../../src/dal/preset";
3+
import _ from "lodash";
4+
import { off } from "process";
5+
6+
describe("PresetDal", () => {
7+
describe("readPreset", () => {
8+
it("should read", async () => {
9+
//GIVEN
10+
const uid = new ObjectId().toHexString();
11+
const first = await PresetDal.addPreset(uid, "first", { ads: "sellout" });
12+
const second = await PresetDal.addPreset(uid, "second", {
13+
ads: "result",
14+
});
15+
await PresetDal.addPreset("unknown", "unknown", {});
16+
17+
//WHEN
18+
const read = await PresetDal.getPresets(uid);
19+
20+
//THEN
21+
expect(read).toHaveLength(2);
22+
expect(read).toEqual(
23+
expect.arrayContaining([
24+
expect.objectContaining({
25+
_id: new ObjectId(first.presetId),
26+
uid: uid,
27+
name: "first",
28+
config: { ads: "sellout" },
29+
}),
30+
expect.objectContaining({
31+
_id: new ObjectId(second.presetId),
32+
uid: uid,
33+
name: "second",
34+
config: { ads: "result" },
35+
}),
36+
])
37+
);
38+
});
39+
});
40+
41+
describe("addPreset", () => {
42+
it("should return error if maximum is reached", async () => {
43+
//GIVEN
44+
const uid = new ObjectId().toHexString();
45+
for (let i = 0; i < 10; i++) {
46+
await PresetDal.addPreset(uid, "test", {} as any);
47+
}
48+
49+
//WHEN / THEN
50+
expect(() =>
51+
PresetDal.addPreset(uid, "max", {} as any)
52+
).rejects.toThrowError("Too many presets");
53+
});
54+
it("should add preset", async () => {
55+
//GIVEN
56+
const uid = new ObjectId().toHexString();
57+
for (let i = 0; i < 9; i++) {
58+
await PresetDal.addPreset(uid, "test", {} as any);
59+
}
60+
61+
//WHEN
62+
const newPreset = await PresetDal.addPreset(uid, "new", {
63+
ads: "sellout",
64+
});
65+
66+
//THEN
67+
const read = await PresetDal.getPresets(uid);
68+
69+
expect(read).toHaveLength(10);
70+
expect(read).toEqual(
71+
expect.arrayContaining([
72+
expect.objectContaining({
73+
_id: new ObjectId(newPreset.presetId),
74+
uid: uid,
75+
name: "new",
76+
config: { ads: "sellout" },
77+
}),
78+
])
79+
);
80+
});
81+
});
82+
83+
describe("editPreset", () => {
84+
it("should not fail if preset is unknown", async () => {
85+
await PresetDal.editPreset(
86+
"uid",
87+
new ObjectId().toHexString(),
88+
"new",
89+
undefined
90+
);
91+
});
92+
it("should edit", async () => {
93+
//GIVEN
94+
const uid = new ObjectId().toHexString();
95+
const decoyUid = new ObjectId().toHexString();
96+
const first = (
97+
await PresetDal.addPreset(uid, "first", { ads: "sellout" })
98+
).presetId;
99+
const second = (
100+
await PresetDal.addPreset(uid, "second", {
101+
ads: "result",
102+
})
103+
).presetId;
104+
const decoy = (
105+
await PresetDal.addPreset(decoyUid, "unknown", { ads: "result" })
106+
).presetId;
107+
108+
//WHEN
109+
await PresetDal.editPreset(uid, first, "newName", { ads: "off" });
110+
111+
//THEN
112+
const read = await PresetDal.getPresets(uid);
113+
expect(read).toHaveLength(2);
114+
expect(read).toEqual(
115+
expect.arrayContaining([
116+
expect.objectContaining({
117+
_id: new ObjectId(first),
118+
uid: uid,
119+
name: "newName",
120+
config: { ads: "off" },
121+
}),
122+
expect.objectContaining({
123+
_id: new ObjectId(second),
124+
uid: uid,
125+
name: "second",
126+
config: { ads: "result" },
127+
}),
128+
])
129+
);
130+
expect(await PresetDal.getPresets(decoyUid)).toEqual(
131+
expect.arrayContaining([
132+
expect.objectContaining({
133+
_id: new ObjectId(decoy),
134+
uid: decoyUid,
135+
name: "unknown",
136+
config: { ads: "result" },
137+
}),
138+
])
139+
);
140+
});
141+
142+
it("should edit with name only", async () => {
143+
//GIVEN
144+
const uid = new ObjectId().toHexString();
145+
const first = (
146+
await PresetDal.addPreset(uid, "first", { ads: "sellout" })
147+
).presetId;
148+
149+
//WHEN undefined
150+
await PresetDal.editPreset(uid, first, "newName", undefined);
151+
expect(await PresetDal.getPresets(uid)).toEqual(
152+
expect.arrayContaining([
153+
expect.objectContaining({
154+
_id: new ObjectId(first),
155+
uid: uid,
156+
name: "newName",
157+
config: { ads: "sellout" },
158+
}),
159+
])
160+
);
161+
162+
//WHEN null
163+
await PresetDal.editPreset(uid, first, "newName", null);
164+
expect(await PresetDal.getPresets(uid)).toEqual(
165+
expect.arrayContaining([
166+
expect.objectContaining({
167+
_id: new ObjectId(first),
168+
uid: uid,
169+
name: "newName",
170+
config: { ads: "sellout" },
171+
}),
172+
])
173+
);
174+
175+
//WHEN empty
176+
await PresetDal.editPreset(uid, first, "newName", {});
177+
expect(await PresetDal.getPresets(uid)).toEqual(
178+
expect.arrayContaining([
179+
expect.objectContaining({
180+
_id: new ObjectId(first),
181+
uid: uid,
182+
name: "newName",
183+
config: { ads: "sellout" },
184+
}),
185+
])
186+
);
187+
});
188+
it("should not edit present not matching uid", async () => {
189+
//GIVEN
190+
const uid = new ObjectId().toHexString();
191+
const decoyUid = new ObjectId().toHexString();
192+
const first = (
193+
await PresetDal.addPreset(uid, "first", { ads: "sellout" })
194+
).presetId;
195+
196+
//WHEN
197+
await PresetDal.editPreset(decoyUid, first, "newName", { ads: "off" });
198+
199+
//THEN
200+
const read = await PresetDal.getPresets(uid);
201+
202+
expect(read).toEqual(
203+
expect.arrayContaining([
204+
expect.objectContaining({
205+
_id: new ObjectId(first),
206+
uid: uid,
207+
name: "first",
208+
config: { ads: "sellout" },
209+
}),
210+
])
211+
);
212+
});
213+
});
214+
215+
describe("removePreset", () => {
216+
it("should fail if preset is unknown", async () => {
217+
expect(() =>
218+
PresetDal.removePreset("uid", new ObjectId().toHexString())
219+
).rejects.toThrowError("Preset not found");
220+
});
221+
it("should remove", async () => {
222+
//GIVEN
223+
const uid = new ObjectId().toHexString();
224+
const decoyUid = new ObjectId().toHexString();
225+
const first = (await PresetDal.addPreset(uid, "first", {})).presetId;
226+
const second = (
227+
await PresetDal.addPreset(uid, "second", { ads: "result" })
228+
).presetId;
229+
const decoy = (
230+
await PresetDal.addPreset(decoyUid, "unknown", { ads: "result" })
231+
).presetId;
232+
233+
//WHEN
234+
PresetDal.removePreset(uid, first);
235+
236+
//THEN
237+
const read = await PresetDal.getPresets(uid);
238+
expect(read).toHaveLength(1);
239+
expect(read).toEqual(
240+
expect.arrayContaining([
241+
expect.objectContaining({
242+
_id: new ObjectId(second),
243+
uid: uid,
244+
name: "second",
245+
config: { ads: "result" },
246+
}),
247+
])
248+
);
249+
expect(await PresetDal.getPresets(decoyUid)).toEqual(
250+
expect.arrayContaining([
251+
expect.objectContaining({
252+
_id: new ObjectId(decoy),
253+
uid: decoyUid,
254+
name: "unknown",
255+
config: { ads: "result" },
256+
}),
257+
])
258+
);
259+
});
260+
it("should not remove present not matching uid", async () => {
261+
//GIVEN
262+
const uid = new ObjectId().toHexString();
263+
const decoyUid = new ObjectId().toHexString();
264+
const first = (
265+
await PresetDal.addPreset(uid, "first", { ads: "sellout" })
266+
).presetId;
267+
268+
//WHEN
269+
expect(() =>
270+
PresetDal.removePreset(decoyUid, first)
271+
).rejects.toThrowError("Preset not found");
272+
273+
//THEN
274+
const read = await PresetDal.getPresets(uid);
275+
276+
expect(read).toEqual(
277+
expect.arrayContaining([
278+
expect.objectContaining({
279+
_id: new ObjectId(first),
280+
uid: uid,
281+
name: "first",
282+
config: { ads: "sellout" },
283+
}),
284+
])
285+
);
286+
});
287+
});
288+
289+
describe("deleteAllPresets", () => {
290+
it("should not fail if preset is unknown", async () => {
291+
await PresetDal.deleteAllPresets("uid");
292+
});
293+
it("should delete all", async () => {
294+
//GIVEN
295+
const uid = new ObjectId().toHexString();
296+
const decoyUid = new ObjectId().toHexString();
297+
await PresetDal.addPreset(uid, "first", {});
298+
await PresetDal.addPreset(uid, "second", { ads: "result" });
299+
const decoy = (
300+
await PresetDal.addPreset(decoyUid, "unknown", { ads: "result" })
301+
).presetId;
302+
303+
//WHEN
304+
await PresetDal.deleteAllPresets(uid);
305+
306+
//THEN
307+
const read = await PresetDal.getPresets(uid);
308+
expect(read).toHaveLength(0);
309+
310+
expect(await PresetDal.getPresets(decoyUid)).toEqual(
311+
expect.arrayContaining([
312+
expect.objectContaining({
313+
_id: new ObjectId(decoy),
314+
uid: decoyUid,
315+
name: "unknown",
316+
config: { ads: "result" },
317+
}),
318+
])
319+
);
320+
});
321+
});
322+
});

backend/src/dal/preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function editPreset(
5656
uid: string,
5757
presetId: string,
5858
name: string,
59-
config: SharedTypes.ConfigPreset
59+
config: SharedTypes.ConfigPreset | null | undefined
6060
): Promise<void> {
6161
const presetUpdates =
6262
config !== undefined && config !== null && Object.keys(config).length > 0

0 commit comments

Comments
 (0)