import 'aws-sdk-client-mock-jest'; import { mockClient } from 'aws-sdk-client-mock'; import { DynamoDBClient, QueryCommand, QueryCommandInput } from '@aws-sdk/client-dynamodb'; import { faker } from '@faker-js/faker'; import { createMockJobExecution, createMockJobExecutionDto } from '../../model/JobExecution'; import { MultiTenantJobExecutionRepository } from './MultiTenantJobExecutionRepository'; import { JOB_EXECUTION_STATUS } from '../../constants/JobExecutionStatus.constants'; import { ENTITY_TYPES } from '../../constants/Repository.constants'; describe('MultiTenantJobExecutionRepository', () => { const mockDynamoClient = mockClient(DynamoDBClient); const tableName = faker.lorem.words(); let repository: MultiTenantJobExecutionRepository; beforeEach(() => { mockDynamoClient.reset(); repository = new MultiTenantJobExecutionRepository(new DynamoDBClient(), tableName); }); describe('findByStatus', () => { it('Queries job executions by status', async () => { const tenant1 = faker.string.uuid(); const tenant2 = faker.string.uuid(); const executions = [createMockJobExecution(), createMockJobExecution(), createMockJobExecution()]; const expectedQueryInput: QueryCommandInput = { KeyConditionExpression: 'gsi1pk = :gsi1pk', ExpressionAttributeValues: { ':gsi1pk': { S: `${ENTITY_TYPES.jobExecution}~running` }, }, TableName: tableName, IndexName: 'gsi1', }; mockDynamoClient .on(QueryCommand) .resolvesOnce({ Items: [createMockJobExecutionDto({ pk: `${ENTITY_TYPES.jobExecution}~${tenant1}`, ...executions[0] })], LastEvaluatedKey: { pk: { S: 'page-2' } }, }) .resolvesOnce({ Items: [ createMockJobExecutionDto({ pk: `${ENTITY_TYPES.jobExecution}~${tenant1}`, ...executions[1] }), createMockJobExecutionDto({ pk: `${ENTITY_TYPES.jobExecution}~${tenant2}`, ...executions[2] }), ], }); const results = await repository.findByStatus(JOB_EXECUTION_STATUS.running); expect(results).toEqual({ [tenant1]: [executions[0], executions[1]], [tenant2]: [executions[2]], }); expect(mockDynamoClient).toHaveReceivedNthCommandWith(1, QueryCommand, expectedQueryInput); expect(mockDynamoClient).toHaveReceivedNthCommandWith(2, QueryCommand, { ...expectedQueryInput, ExclusiveStartKey: { pk: { S: 'page-2' } }, }); }); }); });