Skip to content

Commit c0f0597

Browse files
committed
chore: use static uuids to siplify testing
It's easier to debug failing tests with constant UUIDs. Signed-off-by: Marek Libra <[email protected]>
1 parent 8740058 commit c0f0597

File tree

1 file changed

+36
-54
lines changed

1 file changed

+36
-54
lines changed

plugins/notifications-backend/src/database/DatabaseNotificationsStore.test.ts

+36-54
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
1717
import { DatabaseNotificationsStore } from './DatabaseNotificationsStore';
1818
import { Knex } from 'knex';
19-
import { v4 as uuid } from 'uuid';
2019
import { Notification } from '@backstage/plugin-notifications-common';
2120

2221
jest.setTimeout(60_000);
@@ -54,6 +53,15 @@ const otherUserNotification: Partial<Notification> = {
5453
user: 'user:default/jane.doe',
5554
};
5655

56+
const id1 = '01e0871e-e60a-4f68-8110-5ae3513f992e';
57+
const id2 = '02e0871e-e60a-4f68-8110-5ae3513f992e';
58+
const id3 = '03e0871e-e60a-4f68-8110-5ae3513f992e';
59+
const id4 = '04e0871e-e60a-4f68-8110-5ae3513f992e';
60+
const id5 = '05e0871e-e60a-4f68-8110-5ae3513f992e';
61+
const id6 = '06e0871e-e60a-4f68-8110-5ae3513f992e';
62+
const id7 = '07e0871e-e60a-4f68-8110-5ae3513f992e';
63+
const id8 = '08e0871e-e60a-4f68-8110-5ae3513f992e';
64+
5765
describe.each(databases.eachSupportedId())(
5866
'DatabaseNotificationsStore (%s)',
5967
databaseId => {
@@ -94,11 +102,9 @@ describe.each(databases.eachSupportedId())(
94102

95103
describe('getNotifications', () => {
96104
it('should return all notifications for user', async () => {
97-
const id1 = uuid();
98-
const id2 = uuid();
99105
await insertNotification({ id: id1, ...testNotification });
100106
await insertNotification({ id: id2, ...testNotification });
101-
await insertNotification({ id: uuid(), ...otherUserNotification });
107+
await insertNotification({ id: id3, ...otherUserNotification });
102108

103109
const notifications = await storage.getNotifications({ user });
104110
expect(notifications.length).toBe(2);
@@ -107,13 +113,10 @@ describe.each(databases.eachSupportedId())(
107113
});
108114

109115
it('should return read notifications for user', async () => {
110-
const id1 = uuid();
111-
const id2 = uuid();
112-
const id3 = uuid();
113116
await insertNotification({ id: id1, ...testNotification });
114117
await insertNotification({ id: id2, ...testNotification });
115118
await insertNotification({ id: id3, ...testNotification });
116-
await insertNotification({ id: uuid(), ...otherUserNotification });
119+
await insertNotification({ id: id4, ...otherUserNotification });
117120

118121
await storage.markRead({ ids: [id1, id3], user });
119122

@@ -127,13 +130,10 @@ describe.each(databases.eachSupportedId())(
127130
});
128131

129132
it('should return unread notifications for user', async () => {
130-
const id1 = uuid();
131-
const id2 = uuid();
132-
const id3 = uuid();
133133
await insertNotification({ id: id1, ...testNotification });
134134
await insertNotification({ id: id2, ...testNotification });
135135
await insertNotification({ id: id3, ...testNotification });
136-
await insertNotification({ id: uuid(), ...otherUserNotification });
136+
await insertNotification({ id: id4, ...otherUserNotification });
137137

138138
await storage.markRead({ ids: [id1, id3], user });
139139

@@ -146,13 +146,10 @@ describe.each(databases.eachSupportedId())(
146146
});
147147

148148
it('should return both read and unread notifications for user', async () => {
149-
const id1 = uuid();
150-
const id2 = uuid();
151-
const id3 = uuid();
152149
await insertNotification({ id: id1, ...testNotification });
153150
await insertNotification({ id: id2, ...testNotification });
154151
await insertNotification({ id: id3, ...testNotification });
155-
await insertNotification({ id: uuid(), ...otherUserNotification });
152+
await insertNotification({ id: id4, ...otherUserNotification });
156153

157154
await storage.markRead({ ids: [id1, id3], user });
158155

@@ -167,8 +164,6 @@ describe.each(databases.eachSupportedId())(
167164
});
168165

169166
it('should allow searching for notifications', async () => {
170-
const id1 = uuid();
171-
const id2 = uuid();
172167
await insertNotification({
173168
id: id1,
174169
...testNotification,
@@ -179,7 +174,7 @@ describe.each(databases.eachSupportedId())(
179174
},
180175
});
181176
await insertNotification({ id: id2, ...testNotification });
182-
await insertNotification({ id: uuid(), ...otherUserNotification });
177+
await insertNotification({ id: id3, ...otherUserNotification });
183178

184179
const notifications = await storage.getNotifications({
185180
user,
@@ -190,8 +185,6 @@ describe.each(databases.eachSupportedId())(
190185
});
191186

192187
it('should filter notifications based on created date', async () => {
193-
const id1 = uuid();
194-
const id2 = uuid();
195188
await insertNotification({
196189
id: id1,
197190
...testNotification,
@@ -206,7 +199,7 @@ describe.each(databases.eachSupportedId())(
206199
},
207200
created: new Date() /* now */,
208201
});
209-
await insertNotification({ id: uuid(), ...otherUserNotification });
202+
await insertNotification({ id: id3, ...otherUserNotification });
210203

211204
const notifications = await storage.getNotifications({
212205
user,
@@ -218,13 +211,8 @@ describe.each(databases.eachSupportedId())(
218211

219212
it('should apply pagination', async () => {
220213
const now = Date.now();
221-
const id1 = uuid();
222-
const id2 = uuid();
223-
const id3 = uuid();
224-
const id4 = uuid();
225-
const id5 = uuid();
226-
const id6 = uuid();
227-
const id7 = uuid();
214+
const timeDelay = 5 * 1000; /* 5 secs */
215+
228216
await insertNotification({
229217
id: id1,
230218
...testNotification,
@@ -238,30 +226,30 @@ describe.each(databases.eachSupportedId())(
238226
await insertNotification({
239227
id: id3,
240228
...testNotification,
241-
created: new Date(now + 1),
229+
created: new Date(now - 5 * timeDelay),
242230
});
243231
await insertNotification({
244232
id: id4,
245233
...testNotification,
246-
created: new Date(now + 2),
234+
created: new Date(now - 4 * timeDelay),
247235
});
248236
await insertNotification({
249237
id: id5,
250238
...testNotification,
251-
created: new Date(now + 3),
239+
created: new Date(now - 3 * timeDelay),
252240
});
253241
await insertNotification({
254242
id: id6,
255243
...testNotification,
256-
created: new Date(now + 4),
244+
created: new Date(now - 2 * timeDelay),
257245
});
258246
await insertNotification({
259247
id: id7,
260248
...testNotification,
261-
created: new Date(now + 5),
249+
created: new Date(now - 1 * timeDelay),
262250
});
263251

264-
await insertNotification({ id: uuid(), ...otherUserNotification });
252+
await insertNotification({ id: id8, ...otherUserNotification });
265253

266254
const allUserNotifications = await storage.getNotifications({
267255
user,
@@ -271,44 +259,46 @@ describe.each(databases.eachSupportedId())(
271259
const notifications = await storage.getNotifications({
272260
user,
273261
createdAfter: new Date(now - 5 * 60 * 1000 /* 5 mins */),
262+
// so far no pagination
274263
});
275264
expect(notifications.length).toBe(6);
276-
expect(notifications.at(0)?.id).toEqual(id7);
277-
expect(notifications.at(1)?.id).toEqual(id6);
265+
expect(notifications.at(0)?.id).toEqual(id2);
266+
expect(notifications.at(1)?.id).toEqual(id7);
267+
expect(notifications.at(2)?.id).toEqual(id6);
268+
expect(notifications.at(3)?.id).toEqual(id5);
269+
expect(notifications.at(4)?.id).toEqual(id4);
278270

279271
const allUserNotificationsPageOne = await storage.getNotifications({
280272
user,
281273
limit: 3,
282274
offset: 0,
283275
});
284276
expect(allUserNotificationsPageOne.length).toBe(3);
285-
expect(allUserNotificationsPageOne.at(0)?.id).toEqual(id7);
286-
expect(allUserNotificationsPageOne.at(1)?.id).toEqual(id6);
287-
expect(allUserNotificationsPageOne.at(2)?.id).toEqual(id5);
277+
expect(allUserNotificationsPageOne.at(0)?.id).toEqual(id2);
278+
expect(allUserNotificationsPageOne.at(1)?.id).toEqual(id7);
279+
expect(allUserNotificationsPageOne.at(2)?.id).toEqual(id6);
288280

289281
const allUserNotificationsPageTwo = await storage.getNotifications({
290282
user,
291283
limit: 3,
292284
offset: 3,
293285
});
294286
expect(allUserNotificationsPageTwo.length).toBe(3);
295-
expect(allUserNotificationsPageTwo.at(0)?.id).toEqual(id4);
296-
expect(allUserNotificationsPageTwo.at(1)?.id).toEqual(id3);
297-
expect(allUserNotificationsPageTwo.at(2)?.id).toEqual(id2);
287+
expect(allUserNotificationsPageTwo.at(0)?.id).toEqual(id5);
288+
expect(allUserNotificationsPageTwo.at(1)?.id).toEqual(id4);
289+
expect(allUserNotificationsPageTwo.at(2)?.id).toEqual(id3);
298290
});
299291
});
300292

301293
describe('getStatus', () => {
302294
it('should return status for user', async () => {
303-
const id1 = uuid();
304-
const id2 = uuid();
305295
await insertNotification({
306296
id: id1,
307297
...testNotification,
308298
read: new Date(),
309299
});
310300
await insertNotification({ id: id2, ...testNotification });
311-
await insertNotification({ id: uuid(), ...otherUserNotification });
301+
await insertNotification({ id: id3, ...otherUserNotification });
312302

313303
const status = await storage.getStatus({ user });
314304
expect(status.read).toEqual(1);
@@ -318,7 +308,6 @@ describe.each(databases.eachSupportedId())(
318308

319309
describe('getExistingScopeNotification', () => {
320310
it('should return existing scope notification', async () => {
321-
const id1 = uuid();
322311
const notification: any = {
323312
...testNotification,
324313
id: id1,
@@ -343,7 +332,6 @@ describe.each(databases.eachSupportedId())(
343332

344333
describe('restoreExistingNotification', () => {
345334
it('should return restore existing scope notification', async () => {
346-
const id1 = uuid();
347335
const notification: any = {
348336
...testNotification,
349337
id: id1,
@@ -377,7 +365,6 @@ describe.each(databases.eachSupportedId())(
377365

378366
describe('getNotification', () => {
379367
it('should return notification by id', async () => {
380-
const id1 = uuid();
381368
await insertNotification({ id: id1, ...testNotification });
382369

383370
const notification = await storage.getNotification({ id: id1 });
@@ -387,7 +374,6 @@ describe.each(databases.eachSupportedId())(
387374

388375
describe('markRead', () => {
389376
it('should mark notification read', async () => {
390-
const id1 = uuid();
391377
await insertNotification({ id: id1, ...testNotification });
392378

393379
await storage.markRead({ ids: [id1], user });
@@ -398,7 +384,6 @@ describe.each(databases.eachSupportedId())(
398384

399385
describe('markUnread', () => {
400386
it('should mark notification unread', async () => {
401-
const id1 = uuid();
402387
await insertNotification({
403388
id: id1,
404389
...testNotification,
@@ -413,7 +398,6 @@ describe.each(databases.eachSupportedId())(
413398

414399
describe('markSaved', () => {
415400
it('should mark notification saved', async () => {
416-
const id1 = uuid();
417401
await insertNotification({ id: id1, ...testNotification });
418402

419403
await storage.markSaved({ ids: [id1], user });
@@ -424,7 +408,6 @@ describe.each(databases.eachSupportedId())(
424408

425409
describe('markUnsaved', () => {
426410
it('should mark notification not saved', async () => {
427-
const id1 = uuid();
428411
await insertNotification({
429412
id: id1,
430413
...testNotification,
@@ -439,7 +422,6 @@ describe.each(databases.eachSupportedId())(
439422

440423
describe('saveNotification', () => {
441424
it('should store a notification', async () => {
442-
const id1 = uuid();
443425
await storage.saveNotification({
444426
id: id1,
445427
user,

0 commit comments

Comments
 (0)