phone_repository.test.ts (4133B)
1 import { 2 mapFromPhoneEKYC, 3 mapToPhoneEKYC, 4 PhoneEKYCDto, 5 } from "../infrastructure/memory/mapper/phone.ts"; 6 import { 7 EntityLocked, 8 EntityNotFound, 9 Repository, 10 } from "../core/application/repository_error.ts"; 11 import { PhoneEKYC } from "#core/domain/phone_ekyc.ts"; 12 import { MemoryPhoneRepositoryAdapter } from "#infrastructure/memory/phone.ts"; 13 import "$dotenv"; 14 import { Pool } from "$postgres"; 15 import { assertEquals } from "$std/assert/assert_equals.ts"; 16 import { assertRejects } from "$std/assert/assert_rejects.ts"; 17 import { FakeTime } from "$std/testing/time.ts"; 18 import { PostgresPhoneRepositoryAdapter } from "../infrastructure/postgres/phone.ts"; 19 20 const uuid = "9272d511-a47f-4c91-8e41-d056ca797b42"; 21 const phoneNumber = "+41 77 777 77 77"; 22 const code = "769262"; 23 24 const implementations = [ 25 function memoryPhoneRepository() { 26 return new MemoryPhoneRepositoryAdapter(new Map()); 27 }, 28 async function postgresAuthRepository() { 29 const pool = new Pool(undefined, 4); 30 const connection = await pool.connect(); 31 await connection.queryArray`delete from phone;`; 32 connection.release(); 33 return new PostgresPhoneRepositoryAdapter(pool); 34 }, 35 ] as ReadonlyArray<() => Promise<Repository<PhoneEKYC>>>; 36 37 for (const factory of implementations) { 38 Deno.test({ 39 name: `integration test of ${factory.name}`, 40 async fn(t) { 41 const clock = new FakeTime(new Date("2024-07-10T08:00:00Z")); 42 try { 43 const phoneRepo = await factory(); 44 45 await t.step("should be not found invalid uuid", async () => { 46 const act = async () => await phoneRepo.find("invalid uuid"); 47 await assertRejects(act, EntityNotFound); 48 }); 49 50 await t.step("should be not found in empty", async () => { 51 const act = async () => await phoneRepo.find(uuid); 52 await assertRejects(act, EntityNotFound); 53 }); 54 55 const sample1: PhoneEKYCDto = { 56 uuid, 57 phoneNumber, 58 phoneNumberVerified: true, 59 phoneNumberCode: null, 60 phoneNumberCodeExpire: new Date(0), 61 phoneNumberChallengeRequest: 0, 62 phoneNumberChallengeRequestExpire: new Date(0), 63 phoneNumberChallengeAttempt: 0, 64 phoneNumberChallengeAttemptExpire: new Date(0), 65 version: 0, 66 }; 67 68 await t.step("should be store sample 1", async () => { 69 const act = await phoneRepo.store(mapToPhoneEKYC(sample1)); 70 assertEquals(act, sample1.version + 1); 71 }); 72 73 await t.step("should be find stored sample 1", async () => { 74 const act = mapFromPhoneEKYC(await phoneRepo.find(uuid)); 75 assertEquals( 76 act, 77 Object.assign({}, sample1, { version: sample1.version + 1 }), 78 ); 79 }); 80 81 const sample2: PhoneEKYCDto = { 82 uuid, 83 phoneNumber, 84 phoneNumberVerified: true, 85 phoneNumberCode: code, 86 phoneNumberCodeExpire: new Date("2024-07-10T10:00:00Z"), 87 phoneNumberChallengeRequest: 1, 88 phoneNumberChallengeRequestExpire: new Date("2024-07-10T10:00:00Z"), 89 phoneNumberChallengeAttempt: 2, 90 phoneNumberChallengeAttemptExpire: new Date("2024-07-10T10:00:00Z"), 91 version: 1, 92 }; 93 94 await t.step("should be store sample 2", async () => { 95 const act = await phoneRepo.store(mapToPhoneEKYC(sample2)); 96 assertEquals(act, sample2.version + 1); 97 }); 98 99 await t.step("should be find stored sample 2", async () => { 100 const act = mapFromPhoneEKYC(await phoneRepo.find(uuid)); 101 assertEquals( 102 act, 103 Object.assign({}, sample2, { version: sample2.version + 1 }), 104 ); 105 }); 106 107 await t.step( 108 "should be optimistic lock on store invalid version", 109 async () => { 110 const act = async () => 111 await phoneRepo.store(mapToPhoneEKYC(sample2)); 112 await assertRejects(act, EntityLocked); 113 }, 114 ); 115 } finally { 116 clock.restore(); 117 } 118 }, 119 sanitizeResources: false, 120 }); 121 }