From 1da79bdc15a5eb0dfe15c6c0fc2772bfcd4c177e Mon Sep 17 00:00:00 2001 From: Joshua Aruokhai Date: Wed, 11 Sep 2024 14:50:03 +0100 Subject: [PATCH] Chore: Improve Unit Test Of Indexer Service --- src/indexer/indexer.fixture.ts | 24 +++++++++++++++ src/indexer/indexer.service.spec.ts | 46 +++++++++++++++++++---------- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/indexer/indexer.fixture.ts b/src/indexer/indexer.fixture.ts index bc326f5..7785595 100644 --- a/src/indexer/indexer.fixture.ts +++ b/src/indexer/indexer.fixture.ts @@ -1,5 +1,6 @@ export const testData = [ { + txid: '1', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -31,6 +32,7 @@ export const testData = [ '024ac253c216532e961988e2a8ce266a447c894c781e52ef6cee902361db960004', }, { + txid: '2', vin: [ { txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d', @@ -62,6 +64,7 @@ export const testData = [ '024ac253c216532e961988e2a8ce266a447c894c781e52ef6cee902361db960004', }, { + txid: '3', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -93,6 +96,7 @@ export const testData = [ '03aeea547819c08413974e2ab2b12212e007166bb2058f88b009e082b9b4914a58', }, { + txid: '4', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -124,6 +128,7 @@ export const testData = [ '031f9a80d0938cf980b51f7cc4fad713d49037f430646dff129c0570d75a40d8f0', }, { + txid: '5', vin: [ { txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d', @@ -155,6 +160,7 @@ export const testData = [ '024cad5180a093d3af0f49f586bdf37f890920178e68e80561ed53351d0fa499ad', }, { + txid: '6', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -186,6 +192,7 @@ export const testData = [ '0319949463fc6a2368d999a2a6a2bcb2dbf64a2ac6e00b3ba5659780c860a6d9e0', }, { + txid: '7', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -219,6 +226,7 @@ export const testData = [ '02dc59cc8e8873b65c1dd5c416d4fbeb647372c329bd84a70c05b310e222e2c183', }, { + txid: '8', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -252,6 +260,7 @@ export const testData = [ '03b990f5b1d90ea8fd4bdd5c856a9dfe17035d196958062e2c6cb4c99e413f3548', }, { + txid: '9', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -284,6 +293,7 @@ export const testData = [ '0233c2a447b8b244e4ffcfb59fe365eaa3bb22288b31e2113b9998861f40d4d6da', }, { + txid: '10', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -316,6 +326,7 @@ export const testData = [ '02d4e4f2c4cdb71c9c39a700a9ee1a0fc05b98362a441183f5770af7d6e2b3038c', }, { + txid: '11', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -362,6 +373,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '12', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -393,6 +405,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '13', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -424,6 +437,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '14', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -455,6 +469,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '15', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -501,6 +516,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '16', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -537,6 +553,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '17', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -623,6 +640,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '18', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -659,6 +677,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '19', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -707,6 +726,7 @@ export const testData = [ '02213b872c9a6ee28a0d861384a1b3e3ec7257f4855ed09b4323e3899f3b028989', }, { + txid: '20', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -747,6 +767,7 @@ export const testData = [ '028d6617f9bfe08604beb2188f4eebec923f5f8cc436fa6d14e4256e49bc32e7c8', }, { + txid: '21', vin: [ { txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d', @@ -788,6 +809,7 @@ export const testData = [ '02b04034f00da0678507d1345b7d56fecef825a1151f9dc7d8ca6946452a9e1f43', }, { + txid: '22', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -829,6 +851,7 @@ export const testData = [ '02b04034f00da0678507d1345b7d56fecef825a1151f9dc7d8ca6946452a9e1f43', }, { + txid: '23', vin: [ { txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16', @@ -866,6 +889,7 @@ export const testData = [ '0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8', }, { + txid: '24', scanTweak: null, vin: [ { diff --git a/src/indexer/indexer.service.spec.ts b/src/indexer/indexer.service.spec.ts index 5187b05..281387e 100644 --- a/src/indexer/indexer.service.spec.ts +++ b/src/indexer/indexer.service.spec.ts @@ -4,21 +4,32 @@ import { getRepositoryToken } from '@nestjs/typeorm'; import { Transaction } from '@/transactions/transaction.entity'; import { IndexerService } from '@/indexer/indexer.service'; import { testData } from '@/indexer/indexer.fixture'; +import { DataSource, Repository } from 'typeorm'; describe('IndexerService', () => { let service: IndexerService; - const saveMock = jest.fn(); + let repository: Repository; + let dataSource: DataSource; + + beforeEach(async () => { + dataSource = new DataSource({ + type: 'sqlite', + database: ':memory:', + dropSchema: true, + entities: [Transaction], + synchronize: true, + logging: false, + }); + await dataSource.initialize(); + repository = dataSource.getRepository(Transaction); - beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ IndexerService, TransactionsService, { provide: getRepositoryToken(Transaction), - useValue: { - save: saveMock, - }, + useValue: repository, }, ], }).compile(); @@ -30,24 +41,27 @@ describe('IndexerService', () => { expect(service).toBeDefined(); }); - it.each(testData)('should index transaction', async (testData) => { + it.each(testData)('should index transaction', async (transaction) => { await service.index( - '0000000000000000000000000000000000000000000000000000000000000000', - testData.vin, - testData.vout, + transaction.txid, + transaction.vin, + transaction.vout, 0, '0000000000000000000000000000000000000000000000000000000000000000', ); - if (testData.scanTweak) { - expect(saveMock).toHaveBeenCalled(); - expect(saveMock.mock.calls[0][0].scanTweak).toBe( - testData.scanTweak, - ); + const transaction_index = await repository.findOne({ + where: { id: transaction.txid }, + }); + + if (transaction.scanTweak) { + expect(transaction_index.scanTweak).toBe(transaction.scanTweak); } else { - expect(saveMock).not.toHaveBeenCalled(); + expect(transaction_index).toBeNull(); } + }); - jest.clearAllMocks(); + afterEach(async () => { + await dataSource.destroy(); }); });