Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Modified Unit Test Of Indexer Service #38

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: '0000000000000000000000000000000000000000000000000000000000000001',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -31,6 +32,7 @@ export const testData = [
'024ac253c216532e961988e2a8ce266a447c894c781e52ef6cee902361db960004',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000002',
vin: [
{
txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d',
Expand Down Expand Up @@ -62,6 +64,7 @@ export const testData = [
'024ac253c216532e961988e2a8ce266a447c894c781e52ef6cee902361db960004',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000003',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -93,6 +96,7 @@ export const testData = [
'03aeea547819c08413974e2ab2b12212e007166bb2058f88b009e082b9b4914a58',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000004',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -124,6 +128,7 @@ export const testData = [
'031f9a80d0938cf980b51f7cc4fad713d49037f430646dff129c0570d75a40d8f0',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000005',
vin: [
{
txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d',
Expand Down Expand Up @@ -155,6 +160,7 @@ export const testData = [
'024cad5180a093d3af0f49f586bdf37f890920178e68e80561ed53351d0fa499ad',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000006',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -186,6 +192,7 @@ export const testData = [
'0319949463fc6a2368d999a2a6a2bcb2dbf64a2ac6e00b3ba5659780c860a6d9e0',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000007',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -219,6 +226,7 @@ export const testData = [
'02dc59cc8e8873b65c1dd5c416d4fbeb647372c329bd84a70c05b310e222e2c183',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000008',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -252,6 +260,7 @@ export const testData = [
'03b990f5b1d90ea8fd4bdd5c856a9dfe17035d196958062e2c6cb4c99e413f3548',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000009',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -284,6 +293,7 @@ export const testData = [
'0233c2a447b8b244e4ffcfb59fe365eaa3bb22288b31e2113b9998861f40d4d6da',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000010',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -316,6 +326,7 @@ export const testData = [
'02d4e4f2c4cdb71c9c39a700a9ee1a0fc05b98362a441183f5770af7d6e2b3038c',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000011',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -362,6 +373,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000012',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -393,6 +405,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000013',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -424,6 +437,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000014',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -455,6 +469,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000015',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -501,6 +516,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000016',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -537,6 +553,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000017',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -623,6 +640,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000018',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -659,6 +677,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000019',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -707,6 +726,7 @@ export const testData = [
'02213b872c9a6ee28a0d861384a1b3e3ec7257f4855ed09b4323e3899f3b028989',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000020',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -747,6 +767,7 @@ export const testData = [
'028d6617f9bfe08604beb2188f4eebec923f5f8cc436fa6d14e4256e49bc32e7c8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000021',
vin: [
{
txid: 'a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d',
Expand Down Expand Up @@ -788,6 +809,7 @@ export const testData = [
'02b04034f00da0678507d1345b7d56fecef825a1151f9dc7d8ca6946452a9e1f43',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000022',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -829,6 +851,7 @@ export const testData = [
'02b04034f00da0678507d1345b7d56fecef825a1151f9dc7d8ca6946452a9e1f43',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000023',
vin: [
{
txid: 'f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16',
Expand Down Expand Up @@ -866,6 +889,7 @@ export const testData = [
'0314bec14463d6c0181083d607fecfba67bb83f95915f6f247975ec566d5642ee8',
},
{
txid: '0000000000000000000000000000000000000000000000000000000000000024',
scanTweak: null,
vin: [
{
Expand Down
61 changes: 39 additions & 22 deletions src/indexer/indexer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Transaction>;
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();
Expand All @@ -30,24 +41,30 @@ 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.each(testData)(
'should validate that scanTweaks are created for only valid transactions',
async (transaction) => {
await service.index(
transaction.txid,
transaction.vin,
transaction.vout,
0,
'0000000000000000000000000000000000000000000000000000000000000000',
);
} else {
expect(saveMock).not.toHaveBeenCalled();
}

jest.clearAllMocks();
const transactionEntity = await repository.findOne({
where: { id: transaction.txid },
});

if (transaction.scanTweak) {
expect(transactionEntity.scanTweak).toBe(transaction.scanTweak);
} else {
expect(transactionEntity).toBeNull();
}
},
);

afterEach(async () => {
await dataSource.destroy();
});
});