Skip to content

Commit

Permalink
test: add simple stats unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed May 16, 2023
1 parent cb202de commit 49703d8
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions tests/stats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { buildApiServer } from '../src/api/init';
import { cycleMigrations } from '../src/pg/migrations';
import { PgStore } from '../src/pg/pg-store';
import { DbInscriptionCountPerBlock } from '../src/pg/types';
import { TestChainhookPayloadBuilder, TestFastifyServer } from './helpers';

describe('/stats', () => {
let db: PgStore;
let fastify: TestFastifyServer;

beforeEach(async () => {
db = await PgStore.connect({ skipMigrations: true });
fastify = await buildApiServer({ db });
await cycleMigrations();
});

afterEach(async () => {
await fastify.close();
await db.close();
});

describe('/stats/inscriptions', () => {
test('returns stats', async () => {
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 778000,
hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d',
timestamp: 1676913207,
})
.transaction({
hash: '0x9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201',
})
.inscriptionRevealed({
content_bytes: '0x48656C6C6F',
content_type: 'text/plain;charset=utf-8',
content_length: 5,
inscription_number: 188,
inscription_fee: 705,
inscription_id: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201i0',
inscription_output_value: 10000,
inscriber_address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj',
ordinal_number: 257418248345364,
ordinal_block_height: 650000,
ordinal_offset: 0,
satpoint_post_inscription:
'9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0',
})
.build()
);
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 778000,
hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d',
timestamp: 1676913207,
})
.transaction({
hash: '0x9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201',
})
.inscriptionTransferred({
inscription_number: 188,
inscription_id: '9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201i0',
ordinal_number: 257418248345364,
post_transfer_output_value: 10000,
satpoint_post_transfer:
'9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0',
satpoint_pre_transfer:
'9f4a9b73b0713c5da01c0a47f97c6c001af9028d6bdd9e264dfacbc4e6790201:0:0',
updated_address: 'bc1pscktlmn99gyzlvymvrezh6vwd0l4kg06tg5rvssw0czg8873gz5sdkteqj',
})
.build()
);
await db.updateInscriptions(
new TestChainhookPayloadBuilder()
.apply()
.block({
height: 778001,
hash: '0x00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d',
timestamp: 1676913207,
})
.transaction({
hash: '0x38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc',
})
.inscriptionRevealed({
content_bytes: '0x48656C6C6F',
content_type: 'image/png',
content_length: 5,
inscription_number: 7,
inscription_fee: 2805,
inscription_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0',
inscription_output_value: 10000,
inscriber_address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td',
ordinal_number: 257418248345364,
ordinal_block_height: 51483,
ordinal_offset: 0,
satpoint_post_inscription:
'38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0:0',
})
.build()
);

const response = await fastify.inject({
method: 'GET',
url: '/ordinals/v1/stats/inscriptions',
});
expect(response.statusCode).toBe(200);

const json: { results: DbInscriptionCountPerBlock[] } = response.json();
const totals = json.results.map(i => i.inscriptions_total);
expect(isNonDecreasing(totals)).toBeTruthy();

const expected = {
results: [
{ block_height: 778_000, inscriptions: 1, inscriptions_total: 1 },
{ block_height: 778_001, inscriptions: 1, inscriptions_total: 2 },
],
};
expect(json).toStrictEqual(expected);
});
});
});

function isNonDecreasing(arr: number[]) {
return arr.every((val, i) => i === 0 || val >= arr[i - 1]);
}

0 comments on commit 49703d8

Please sign in to comment.