|
| 1 | +import { Logger } from "pino"; |
| 2 | +import { createTables, DBContext, deleteTables, setupDynamoDBContainer } from "./db"; |
| 3 | +import { createTestLogger, LogStream } from "./logs"; |
| 4 | +import { SupplierRepository } from "../supplier-repository"; |
| 5 | +import { Supplier } from "../types"; |
| 6 | +import { v4 as uuidv4 } from 'uuid'; |
| 7 | + |
| 8 | +function createSupplier(status: 'ENABLED' | 'DISABLED', apimId = uuidv4()): Omit<Supplier, 'updatedAt'> { |
| 9 | + return { |
| 10 | + id: uuidv4(), |
| 11 | + name: 'Supplier One', |
| 12 | + apimId, |
| 13 | + status |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +// Database tests can take longer, especially with setup and teardown |
| 18 | +jest.setTimeout(30000); |
| 19 | + |
| 20 | +describe('SupplierRepository', () => { |
| 21 | + let db: DBContext; |
| 22 | + let supplierRepository: SupplierRepository; |
| 23 | + let logStream: LogStream; |
| 24 | + let logger: Logger; |
| 25 | + |
| 26 | + beforeAll(async () => { |
| 27 | + db = await setupDynamoDBContainer(); |
| 28 | + }); |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + await createTables(db); |
| 32 | + ( |
| 33 | + { logStream, logger } = createTestLogger() |
| 34 | + ); |
| 35 | + |
| 36 | + supplierRepository = new SupplierRepository(db.docClient, logger, db.config); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(async () => { |
| 40 | + await deleteTables(db); |
| 41 | + jest.useRealTimers(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterAll(async () => { |
| 45 | + await db.container.stop(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('creates an enabled supplier with provided values and timestamps', async () => { |
| 49 | + jest.useFakeTimers(); |
| 50 | + // Month is zero-indexed in JS Date |
| 51 | + jest.setSystemTime(new Date(2020, 1, 1)); |
| 52 | + |
| 53 | + const supplier = createSupplier('ENABLED'); |
| 54 | + |
| 55 | + const persistedSupplier = await supplierRepository.putSupplier(supplier); |
| 56 | + |
| 57 | + expect(persistedSupplier).toEqual(expect.objectContaining({ |
| 58 | + ...supplier, |
| 59 | + updatedAt: '2020-02-01T00:00:00.000Z', |
| 60 | + })); |
| 61 | + }); |
| 62 | + |
| 63 | + test('fetches a supplier by its ID', async () => { |
| 64 | + const supplier = createSupplier('DISABLED') |
| 65 | + await supplierRepository.putSupplier(supplier); |
| 66 | + |
| 67 | + const fetched = await supplierRepository.getSupplierById(supplier.id); |
| 68 | + |
| 69 | + expect(fetched).toEqual(expect.objectContaining({ |
| 70 | + ...supplier |
| 71 | + })); |
| 72 | + }); |
| 73 | + |
| 74 | + test('throws an error fetching a supplier that does not exist', async () => { |
| 75 | + await expect(supplierRepository.getSupplierById('non-existent-id')) |
| 76 | + .rejects.toThrow('Supplier with id non-existent-id not found'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('overwrites an existing supplier entry', async () => { |
| 80 | + const supplier = createSupplier('DISABLED'); |
| 81 | + |
| 82 | + const original = await supplierRepository.putSupplier(supplier); |
| 83 | + expect(original.status).toBe('DISABLED'); |
| 84 | + |
| 85 | + supplier.status = 'ENABLED'; |
| 86 | + const updated = await supplierRepository.putSupplier(supplier); |
| 87 | + expect(updated.status).toBe('ENABLED'); |
| 88 | + }); |
| 89 | + |
| 90 | + test('rethrows errors from DynamoDB when creating a letter', async () => { |
| 91 | + const misconfiguredRepository = new SupplierRepository(db.docClient, logger, { |
| 92 | + ...db.config, |
| 93 | + suppliersTableName: 'nonexistent-table' |
| 94 | + }); |
| 95 | + await expect(misconfiguredRepository.putSupplier(createSupplier('ENABLED'))) |
| 96 | + .rejects.toThrow('Cannot do operations on a non-existent table'); |
| 97 | + }); |
| 98 | + |
| 99 | + test('fetches a supplier by apimId', async () => { |
| 100 | + const supplier = createSupplier('ENABLED'); |
| 101 | + |
| 102 | + await supplierRepository.putSupplier(supplier); |
| 103 | + |
| 104 | + const fetched = await supplierRepository.getSupplierByApimId(supplier.apimId); |
| 105 | + expect(fetched).toEqual(expect.objectContaining({ |
| 106 | + ...supplier |
| 107 | + })); |
| 108 | + }); |
| 109 | + |
| 110 | + test('throws an error fetching a supplier by apimId that does not exist', async () => { |
| 111 | + await expect(supplierRepository.getSupplierByApimId('non-existent-apim-id')) |
| 112 | + .rejects.toThrow('Supplier with apimId non-existent-apim-id not found'); |
| 113 | + }); |
| 114 | + |
| 115 | + test('throws an error fetching a supplier by apimId when multiple exist', async () => { |
| 116 | + const apimId = 'duplicate-apim-id'; |
| 117 | + const supplier1 = createSupplier('ENABLED', apimId); |
| 118 | + const supplier2 = createSupplier('DISABLED', apimId); |
| 119 | + |
| 120 | + await supplierRepository.putSupplier(supplier1); |
| 121 | + await supplierRepository.putSupplier(supplier2); |
| 122 | + |
| 123 | + await expect(supplierRepository.getSupplierByApimId(apimId)) |
| 124 | + .rejects.toThrow(`Multiple suppliers found with apimId ${apimId}`); |
| 125 | + }); |
| 126 | + |
| 127 | +}); |
0 commit comments