Skip to content

Commit 1b0b510

Browse files
committed
test: Add test to check statement insertion with priority
1 parent d6ce0b3 commit 1b0b510

1 file changed

Lines changed: 76 additions & 4 deletions

File tree

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,96 @@
11
import assert from 'assert';
2+
import IORedis from 'ioredis';
23
import { isArray } from 'lodash';
4+
import config from '../../../../config';
35
import { StatementProcessingPriority } from '../../enums/statementProcessingPriority.enum';
6+
import { EVENT_NAME } from '../../repo/eventsRepo/utils/constants';
7+
import { getPrefixWithProcessingPriority } from '../../repo/eventsRepo/utils/getPrefixWithProcessingPriority';
8+
import connectToRedis from '../../repo/utils/connectToRedis';
9+
import { TEST_ORGANISATION_ID } from '../utils/createClientModel';
410
import createStatement from '../utils/createStatement';
511
import setup from '../utils/setup';
612
import storeStatementsInService from '../utils/storeStatementsInService';
713

14+
interface StatementPayload {
15+
readonly organisationId: string;
16+
readonly statementId: string;
17+
}
18+
19+
const checkRedisPayloadArray = async ({
20+
redisClient,
21+
priority,
22+
items,
23+
}: {
24+
readonly redisClient: IORedis.Redis;
25+
readonly priority: StatementProcessingPriority;
26+
readonly items: StatementPayload[];
27+
}) => {
28+
const eventName = `${getPrefixWithProcessingPriority(
29+
config.redis.prefix,
30+
priority,
31+
)}:${EVENT_NAME}`;
32+
const listLength = await redisClient.llen(eventName);
33+
34+
assert.equal(
35+
listLength,
36+
items.length,
37+
`Expected payload list length is incorrect for for "${priority}" priority`,
38+
);
39+
40+
const listData = await redisClient.lrange(eventName, 0, listLength);
41+
const expectedPayloadItems = items.map((statementPayload) => {
42+
return JSON.stringify(statementPayload);
43+
});
44+
45+
assert.deepEqual(
46+
listData,
47+
expectedPayloadItems,
48+
`Expected payload items are incorrect for "${priority}" priority`,
49+
);
50+
};
51+
852
describe(__filename, () => {
953
const service = setup();
1054
const storeStatements = storeStatementsInService(service);
55+
1156
const TEST_ID_1 = '1c86d8e9-f325-404f-b3d9-24c451035583';
1257
const TEST_ID_2 = '1c86d8e9-f325-404f-b3d9-24c451035584';
58+
const TEST_ID_3 = '1c86d8e9-f325-404f-b3d9-24c451035585';
1359

14-
it('should store statements with priority equals to "LOW"', async () => {
15-
const ids: string[] = await storeStatements(
60+
it('should store statements with different priority', async () => {
61+
// This is the only adapter for the events repo at the current moment
62+
const redisClient = await connectToRedis()();
63+
64+
const lowStatementIds: string[] = await storeStatements(
1665
[createStatement({ id: TEST_ID_1 }), createStatement({ id: TEST_ID_2 })],
1766
undefined,
1867
undefined,
1968
StatementProcessingPriority.LOW,
2069
);
21-
assert.equal(isArray(ids), true);
22-
assert.deepEqual(ids, [TEST_ID_1, TEST_ID_2]);
70+
assert.equal(isArray(lowStatementIds), true);
71+
assert.deepEqual(lowStatementIds, [TEST_ID_1, TEST_ID_2]);
72+
73+
const mediumStatementIds: string[] = await storeStatements(
74+
[createStatement({ id: TEST_ID_3 })],
75+
undefined,
76+
undefined,
77+
StatementProcessingPriority.MEDIUM,
78+
);
79+
assert.equal(isArray(mediumStatementIds), true);
80+
assert.deepEqual(mediumStatementIds, [TEST_ID_3]);
81+
82+
await checkRedisPayloadArray({
83+
redisClient,
84+
priority: StatementProcessingPriority.LOW,
85+
items: [
86+
{ statementId: TEST_ID_1, organisationId: TEST_ORGANISATION_ID },
87+
{ statementId: TEST_ID_2, organisationId: TEST_ORGANISATION_ID },
88+
],
89+
});
90+
await checkRedisPayloadArray({
91+
redisClient,
92+
priority: StatementProcessingPriority.MEDIUM,
93+
items: [{ statementId: TEST_ID_3, organisationId: TEST_ORGANISATION_ID }],
94+
});
2395
});
2496
});

0 commit comments

Comments
 (0)