Skip to content

Commit

Permalink
Modified Unit Test Of Indexer Service
Browse files Browse the repository at this point in the history
  • Loading branch information
aruokhai committed Sep 11, 2024
1 parent c2cd174 commit b65eee7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
24 changes: 24 additions & 0 deletions src/indexer/indexer.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const testData = [
{
txid: '1',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -31,6 +32,7 @@ export const testData = [
'024ac253c216532e961988e2a8ce266a447c894c781e52ef6cee902361db960004',
},
{
txid: '2',
vin: [
{
txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d',
Expand Down Expand Up @@ -62,6 +64,7 @@ export const testData = [
'024ac253c216532e961988e2a8ce266a447c894c781e52ef6cee902361db960004',
},
{
txid: '3',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -93,6 +96,7 @@ export const testData = [
'03aeea547819c08413974e2ab2b12212e007166bb2058f88b009e082b9b4914a58',
},
{
txid: '4',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -124,6 +128,7 @@ export const testData = [
'031f9a80d0938cf980b51f7cc4fad713d49037f430646dff129c0570d75a40d8f0',
},
{
txid: '5',
vin: [
{
txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d',
Expand Down Expand Up @@ -155,6 +160,7 @@ export const testData = [
'024cad5180a093d3af0f49f586bdf37f890920178e68e80561ed53351d0fa499ad',
},
{
txid: '6',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -186,6 +192,7 @@ export const testData = [
'0319949463fc6a2368d999a2a6a2bcb2dbf64a2ac6e00b3ba5659780c860a6d9e0',
},
{
txid: '7',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -219,6 +226,7 @@ export const testData = [
'02dc59cc8e8873b65c1dd5c416d4fbeb647372c329bd84a70c05b310e222e2c183',
},
{
txid: '8',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -252,6 +260,7 @@ export const testData = [
'03b990f5b1d90ea8fd4bdd5c856a9dfe17035d196958062e2c6cb4c99e413f3548',
},
{
txid: '9',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -284,6 +293,7 @@ export const testData = [
'0233c2a447b8b244e4ffcfb59fe365eaa3bb22288b31e2113b9998861f40d4d6da',
},
{
txid: '10',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -316,6 +326,7 @@ export const testData = [
'02d4e4f2c4cdb71c9c39a700a9ee1a0fc05b98362a441183f5770af7d6e2b3038c',
},
{
txid: '11',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -362,6 +373,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '12',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -393,6 +405,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '13',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -424,6 +437,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '14',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -455,6 +469,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '15',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -501,6 +516,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '16',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -537,6 +553,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '17',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -623,6 +640,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '18',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -659,6 +677,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '19',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -707,6 +726,7 @@ export const testData = [
'02213b872c9a6ee28a0d861384a1b3e3ec7257f4855ed09b4323e3899f3b028989',
},
{
txid: '20',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -747,6 +767,7 @@ export const testData = [
'028d6617f9bfe08604beb2188f4eebec923f5f8cc436fa6d14e4256e49bc32e7c8',
},
{
txid: '21',
vin: [
{
txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d',
Expand Down Expand Up @@ -788,6 +809,7 @@ export const testData = [
'02b04034f00da0678507d1345b7d56fecef825a1151f9dc7d8ca6946452a9e1f43',
},
{
txid: '22',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -829,6 +851,7 @@ export const testData = [
'02b04034f00da0678507d1345b7d56fecef825a1151f9dc7d8ca6946452a9e1f43',
},
{
txid: '23',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -866,6 +889,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '24',
scanTweak: null,
vin: [
{
Expand Down
50 changes: 28 additions & 22 deletions src/indexer/indexer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ 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<Transaction>;
beforeEach(async () => {
const 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();
Expand All @@ -30,24 +39,21 @@ describe('IndexerService', () => {
expect(service).toBeDefined();
});

it.each(testData)('should index transaction', async (testData) => {
await service.index(
'0000000000000000000000000000000000000000000000000000000000000000',
testData.vin,
testData.vout,
0,
'0000000000000000000000000000000000000000000000000000000000000000',
);

if (testData.scanTweak) {
expect(saveMock).toHaveBeenCalled();
expect(saveMock.mock.calls[0][0].scanTweak).toBe(
testData.scanTweak,
it('should index transaction', async () => {
for (const data of testData) {
await service.index(
data.txid,
data.vin,
data.vout,
0,
'0000000000000000000000000000000000000000000000000000000000000000',
);
} else {
expect(saveMock).not.toHaveBeenCalled();
}

jest.clearAllMocks();
const indexed_length = await repository.count();
expect(indexed_length).toEqual(23);
const absence_tweaked = await repository.findOne({
where: { id: '24' },
});
expect(absence_tweaked).toBeNull();
});
});

0 comments on commit b65eee7

Please # to comment.