-
Notifications
You must be signed in to change notification settings - Fork 1
/
votes.spec.ts
434 lines (383 loc) · 14.1 KB
/
votes.spec.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import { eq } from 'drizzle-orm';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import assert from 'node:assert/strict';
import { after, before, describe, test } from 'node:test';
import { createTestDatabase, seed } from '../db';
import * as schema from '../db/schema';
import { environmentVariables } from '../types';
import {
calculatePluralScore,
calculateQuadraticScore,
groupsDictionary,
numOfVotesDictionary,
queryGroupCategories,
queryVoteData,
saveVote,
updateOptionScore,
updateVoteScoreInDatabase,
updateVoteScorePlural,
updateVoteScoreQuadratic,
userCanVote,
validateVote,
} from './votes';
describe('service: votes', () => {
let dbPool: NodePgDatabase<typeof schema>;
let deleteTestDatabase: () => Promise<void>;
let testData: { optionId: string; numOfVotes: number };
let cycle: schema.Cycle | undefined;
let questionOption: schema.Option | undefined;
let otherQuestionOption: schema.Option | undefined;
let forumQuestion: schema.Question | undefined;
let otherForumQuestion: schema.Question | undefined;
let groupCategory: schema.GroupCategory | undefined;
let otherGroupCategory: schema.GroupCategory | undefined;
let user: schema.User | undefined;
let secondUser: schema.User | undefined;
let thirdUser: schema.User | undefined;
before(async () => {
const envVariables = environmentVariables.parse(process.env);
const { dbClient, teardown } = await createTestDatabase(envVariables);
dbPool = dbClient.db;
deleteTestDatabase = teardown;
// seed
const { users, questionOptions, forumQuestions, cycles, groupCategories } = await seed(dbPool);
// Insert registration fields for the user
questionOption = questionOptions[0];
otherQuestionOption = questionOptions[1];
forumQuestion = forumQuestions[0];
otherForumQuestion = forumQuestions[1];
groupCategory = groupCategories[0];
otherGroupCategory = groupCategories[1];
user = users[0];
secondUser = users[1];
thirdUser = users[2];
cycle = cycles[0];
testData = {
numOfVotes: 1,
optionId: questionOption?.id ?? '',
};
});
test('validation should return false if no option id is specified', async () => {
const response = await validateVote(dbPool, { numOfVotes: 1, optionId: '' }, user!.id ?? '');
assert.equal(response.isValid, false);
assert(response.error);
});
test('validation should return false if a non-existing optionid is specified', async () => {
const response = await validateVote(
dbPool,
{ numOfVotes: 1, optionId: '00000000-0000-0000-0000-000000000000' },
user!.id ?? '',
);
assert.equal(response.isValid, false);
assert(response.error);
});
test('validation should return false if the cycle is not open', async () => {
await dbPool
.update(schema.cycles)
.set({ status: 'CLOSED' })
.where(eq(schema.cycles.id, cycle!.id));
const response = await validateVote(
dbPool,
{ numOfVotes: 1, optionId: questionOption?.id ?? '' },
user!.id ?? '',
);
assert.equal(response.isValid, false);
assert(response.error);
});
test('validation should return false if a user is not approved', async () => {
await dbPool
.update(schema.cycles)
.set({ status: 'OPEN' })
.where(eq(schema.cycles.id, cycle!.id));
const response = await validateVote(
dbPool,
{ numOfVotes: 1, optionId: questionOption?.id ?? '' },
user!.id ?? '',
);
assert.equal(response.isValid, false);
assert(response.error);
});
test('validation should return true all validation checks pass', async () => {
await dbPool.insert(schema.registrations).values({
status: 'APPROVED',
userId: user!.id ?? '',
eventId: cycle!.eventId ?? '',
});
const response = await validateVote(
dbPool,
{ numOfVotes: 1, optionId: questionOption?.id ?? '' },
user!.id ?? '',
);
assert.equal(response.isValid, true);
assert.equal(response.error, null);
});
test('userCanVote returns false if user does not have an approved registration', async () => {
const response = await userCanVote(dbPool, secondUser!.id ?? '', questionOption?.id ?? '');
assert.equal(response, false);
});
test('userCanVote returns true if user has an approved registration', async () => {
await dbPool.insert(schema.registrations).values({
status: 'APPROVED',
userId: secondUser!.id ?? '',
eventId: cycle!.eventId ?? '',
});
const response = await userCanVote(dbPool, secondUser!.id ?? '', questionOption?.id ?? '');
assert.equal(response, true);
});
test('userCanVote returns false if no option id gets provided', async () => {
const response = await userCanVote(dbPool, secondUser!.id ?? '', '');
assert.equal(response, false);
});
test('should save vote', async () => {
const { data: response } = await saveVote(
dbPool,
testData,
user?.id ?? '',
forumQuestion?.id ?? '',
);
assert(response);
assert(response?.id);
assert(response?.userId);
assert(response?.optionId);
assert(response?.numOfVotes);
assert(response?.questionId);
assert(response?.createdAt);
assert(response?.updatedAt);
});
test('should not save vote with invalid test data', async () => {
const invalidTestData = {
optionId: '',
numOfVotes: 2,
};
const response = await saveVote(
dbPool,
invalidTestData,
user?.id ?? '',
forumQuestion?.id ?? '',
);
assert.equal(response.data, null);
assert(response.error);
});
test('UpdateOptionScore returns an error if not all question ids are the same', async () => {
const mockData = [
{ optionId: 'option1', numOfVotes: 10 },
{ optionId: 'option2', numOfVotes: 5 },
{ optionId: 'option3', numOfVotes: 2 },
];
const questionIds = [forumQuestion?.id ?? '', otherForumQuestion?.id ?? ''];
const response = await updateOptionScore(dbPool, mockData, questionIds);
assert.equal(response.data, null);
assert(response.errors);
assert(response.errors[0]);
});
test('UpdateOptionScore returns an error if no question id is found', async () => {
const mockData = [
{ optionId: 'option1', numOfVotes: 10 },
{ optionId: 'option2', numOfVotes: 5 },
{ optionId: 'option3', numOfVotes: 2 },
];
const questionIds = [
'00000000-0000-0000-0000-000000000000',
'00000000-0000-0000-0000-000000000000',
];
const response = await updateOptionScore(dbPool, mockData, questionIds);
assert.equal(response.data, null);
assert(response.errors);
assert(response.errors[0]);
});
test('UpdateOptionScore returns an error if a valid but non existing uuid gets provided', async () => {
const mockData = [
{ optionId: 'option1', numOfVotes: 10 },
{ optionId: 'option2', numOfVotes: 5 },
{ optionId: 'option3', numOfVotes: 2 },
];
const questionIds = ['', ''];
const response = await updateOptionScore(dbPool, mockData, questionIds);
assert.equal(response.data, null);
assert(response.errors);
assert(response.errors[0]);
});
test('should fetch vote data correctly', async () => {
// register second user
await dbPool.insert(schema.registrations).values({
status: 'APPROVED',
userId: secondUser!.id ?? '',
eventId: cycle!.eventId ?? '',
});
await saveVote(dbPool, testData, secondUser!.id, forumQuestion?.id ?? '');
const voteArray = await queryVoteData(dbPool, questionOption?.id ?? '');
assert(voteArray);
assert.equal(voteArray.length, 2);
voteArray?.forEach((vote) => {
assert(vote);
assert(vote.userId);
assert(vote.numOfVotes);
assert(Number.isInteger(vote.numOfVotes));
});
assert.equal(voteArray[0]?.numOfVotes, 1);
});
test('should transform voteArray correctly', () => {
// Mock voteMultiplierArray
const voteArray = [
{ userId: 'user1', numOfVotes: 10 },
{ userId: 'user2', numOfVotes: 0 },
{ userId: 'user3', numOfVotes: 5 },
{ userId: 'user4', numOfVotes: 0 },
];
const result = numOfVotesDictionary(voteArray);
assert(result);
assert.equal(Object.keys(result).length, 2);
assert.equal(result.user1, 10);
assert.equal(result.user3, 5);
});
test('should include users with zero votes if there are no non-zero votes', () => {
// Mock voteMultiplierArray with all zero votes
const voteArray = [
{ userId: 'user1', numOfVotes: 0 },
{ userId: 'user2', numOfVotes: 0 },
];
const result = numOfVotesDictionary(voteArray);
assert(result);
assert.equal(Object.keys(result).length, 2);
assert.equal(result.user1, 0);
assert.equal(result.user2, 0);
});
test('vote dictionary should not contain users voting for another option', async () => {
// create vote for another question option
await dbPool.insert(schema.votes).values({
numOfVotes: 5,
optionId: otherQuestionOption!.id,
questionId: forumQuestion!.id,
userId: thirdUser!.id,
});
const voteArray = await queryVoteData(dbPool, questionOption?.id ?? '');
const result = await numOfVotesDictionary(voteArray);
assert(user);
assert(secondUser);
assert(thirdUser);
assert.equal(user.id in result, true);
assert.equal(secondUser.id in result, true);
assert.equal(thirdUser.id in result, false);
});
test('that query group categories returns the correct amount of group category ids', async () => {
// Get vote data required for groups
const groupCategoriesIdArray = await queryGroupCategories(dbPool, forumQuestion!.id);
assert(groupCategoriesIdArray);
assert(groupCategoriesIdArray.data);
assert.equal(groupCategoriesIdArray.data.length, 1);
assert.equal(typeof groupCategoriesIdArray.data[0], 'string');
});
test('that query group categories returns an empty array if their are no group categories specified for a specific question', async () => {
const groupCategoriesIdArray = await queryGroupCategories(dbPool, otherForumQuestion!.id);
assert(groupCategoriesIdArray);
assert.equal(groupCategoriesIdArray.data, null);
});
test('only return groups for users who voted for the option', async () => {
const voteArray = await queryVoteData(dbPool, questionOption?.id ?? '');
const votesDictionary = await numOfVotesDictionary(voteArray);
const groups = await groupsDictionary(dbPool, votesDictionary, [groupCategory!.id]);
assert(groups);
assert(groups['unexpectedKey'] === undefined);
assert(typeof groups === 'object');
// check that the groups dictionary only has user ids from the votes dictionary
for (const key in groups) {
for (const userId of groups[key]!) {
assert(userId in votesDictionary, `User ${userId} not in votes dictionary`);
}
}
});
test('only return groups for users who voted for the option with two elidgible group categories', async () => {
const voteArray = await queryVoteData(dbPool, questionOption?.id ?? '');
const votesDictionary = await numOfVotesDictionary(voteArray);
const groups = await groupsDictionary(dbPool, votesDictionary, [
groupCategory!.id,
otherGroupCategory!.id,
]);
assert(groups);
assert(groups['unexpectedKey'] === undefined);
assert(typeof groups === 'object');
assert.equal(Object.keys(groups).length, 2);
assert.equal(groups[Object.keys(groups)[0]!]!.length, 2);
});
test('should calculate the plural score correctly', () => {
// Mock groups dictionary
const groupsDictionary = {
group0: ['user0', 'user1'],
group1: ['user1', 'user2', 'user3'],
group2: ['user0', 'user2'],
};
// Mock number of votes dictionary
const numOfVotesDictionary = {
user0: 1,
user1: 2,
user2: 3,
user3: 4,
};
const result = calculatePluralScore(groupsDictionary, numOfVotesDictionary);
assert(result);
assert.equal(typeof result, 'number');
assert.equal(result, 4.597873224984399);
});
test('plural score should be 0 when every user vote is zero', () => {
// Mock groups dictionary
const groupsDictionary = {
group0: ['user0', 'user1'],
group1: ['user1', 'user2', 'user3'],
group2: ['user0', 'user2'],
};
// Mock number of votes dictionary
const numOfVotesDictionary = {
user0: 0,
user1: 0,
user2: 0,
user3: 0,
};
const result = calculatePluralScore(groupsDictionary, numOfVotesDictionary);
assert.equal(typeof result, 'number');
assert.equal(result, 0);
});
test('test quadratic score calculation', () => {
// Mock number of votes dictionary
const numOfVotesDictionary = {
user0: 4,
user1: 4,
user2: 9,
user3: 9,
};
const result = calculateQuadraticScore(numOfVotesDictionary);
assert(result);
assert.equal(typeof result, 'number');
assert.equal(result, 10);
});
test('update vote score in database', async () => {
// update db with dummy score
const score = 100;
await updateVoteScoreInDatabase(dbPool, questionOption?.id ?? '', score);
// query updated db in schema
const updatedDbScore = await dbPool.query.options.findFirst({
where: eq(schema.options.id, questionOption?.id ?? ''),
});
assert(updatedDbScore);
assert(updatedDbScore.voteScore);
assert.equal(updatedDbScore.voteScore, '100');
});
test('that the plurality score is correct if both users are in the same group', async () => {
const score = await updateVoteScorePlural(
dbPool,
questionOption?.id ?? '',
forumQuestion?.id ?? '',
);
// sqrt of 2 because the two users are in the same group
// voting for the same option with 1 vote each
assert.equal(score, Math.sqrt(2));
});
test('that the quadratic score is correctly calculated as the sum of square roots', async () => {
const score = await updateVoteScoreQuadratic(dbPool, questionOption?.id ?? '');
// two users voting for the same option with 1 vote each
// sqrt of 1 + sqrt of 1 = 2
assert.equal(score, 2);
});
after(async () => {
await deleteTestDatabase();
});
});