|
1 | 1 | import assert from 'assert'; |
| 2 | +import IORedis from 'ioredis'; |
2 | 3 | import { isArray } from 'lodash'; |
| 4 | +import config from '../../../../config'; |
3 | 5 | 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'; |
4 | 10 | import createStatement from '../utils/createStatement'; |
5 | 11 | import setup from '../utils/setup'; |
6 | 12 | import storeStatementsInService from '../utils/storeStatementsInService'; |
7 | 13 |
|
| 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 | + |
8 | 52 | describe(__filename, () => { |
9 | 53 | const service = setup(); |
10 | 54 | const storeStatements = storeStatementsInService(service); |
| 55 | + |
11 | 56 | const TEST_ID_1 = '1c86d8e9-f325-404f-b3d9-24c451035583'; |
12 | 57 | const TEST_ID_2 = '1c86d8e9-f325-404f-b3d9-24c451035584'; |
| 58 | + const TEST_ID_3 = '1c86d8e9-f325-404f-b3d9-24c451035585'; |
13 | 59 |
|
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( |
16 | 65 | [createStatement({ id: TEST_ID_1 }), createStatement({ id: TEST_ID_2 })], |
17 | 66 | undefined, |
18 | 67 | undefined, |
19 | 68 | StatementProcessingPriority.LOW, |
20 | 69 | ); |
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 | + }); |
23 | 95 | }); |
24 | 96 | }); |
0 commit comments