From 7c229963c77c61195e0bf1f37e8143ac09e8c7be Mon Sep 17 00:00:00 2001 From: "brady.ouren" Date: Mon, 22 Jul 2024 12:03:41 -0700 Subject: [PATCH] fix some types and setup functions --- api/tests/api/api.test.ts | 37 ++++++++++++++++++++++--------- api/tests/helpers.ts | 46 ++++++++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/api/tests/api/api.test.ts b/api/tests/api/api.test.ts index 24e15b8..1129944 100644 --- a/api/tests/api/api.test.ts +++ b/api/tests/api/api.test.ts @@ -1,8 +1,9 @@ import { ENV } from '../../src/env'; import { PgStore } from '../../src/pg/pg-store'; -import { DbRune } from '../../src/pg/types'; +import { DbLedgerEntry, DbRune } from '../../src/pg/types'; import { dropDatabase, + insertDbEntry, insertRune, runMigrations, startTestApiServer, @@ -30,17 +31,29 @@ describe('Etchings', () => { }); test('displays etched rune', async () => { - // '1:0', 0, 'UNCOMMONGOODS', 'UNCOMMON•GOODS', // '0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5', 840000, 0, '', '⧉', 1, // '340282366920938463463374607431768211455', 840000, 1050000, 0 + const ledgerEntry: DbLedgerEntry = { + rune_id: '1:1', + block_hash: 'sample_block_hash', + block_height: '1', + tx_index: 0, + tx_id: '0', + output: 0, + address: '0', + receiver_address: '0', + amount: '0', + operation: 'etching', + timestamp: 0 + }; const rune: DbRune = { id: '1:1', name: 'Sample Rune Name', spaced_name: 'Sample•Rune•Name', number: 1, block_hash: 'sample_block_hash', - block_height: '0x1a', + block_height: '10', tx_index: 0, tx_id: 'sample_tx_id', divisibility: 8, @@ -61,16 +74,20 @@ describe('Etchings', () => { total_operations: '2000', timestamp: Date.now(), }; - // await insertRune(db, rune); - // const runes = await fastify.inject({ - // method: 'GET', - // url: '/runes/v1/etchings/', - // }); + await insertRune(db, rune); + const runes = await fastify.inject({ + method: 'GET', + url: '/runes/v1/etchings', + }); + expect(JSON.parse(runes.body).results.not.toHaveLength(0)); + expect(runes.statusCode).toBe(200); + // TODO: ???? + const event_index = 0; + await insertDbEntry(db, ledgerEntry, event_index); // console.log(runes); - const etching = 'UNCOMMON GOODS' const response = await fastify.inject({ method: 'GET', - url: '/runes/v1/etchings/' + etching, + url: '/runes/v1/etchings/' + ledgerEntry.rune_id, }); expect(response.statusCode).toBe(200); }); diff --git a/api/tests/helpers.ts b/api/tests/helpers.ts index 63d781c..3eec929 100644 --- a/api/tests/helpers.ts +++ b/api/tests/helpers.ts @@ -5,7 +5,7 @@ import { IncomingMessage, Server, ServerResponse } from 'http'; import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; import { buildApiServer } from '../src/api/init'; import { Rune } from '../src/api/schemas'; -import { DbRune } from '../src/pg/types'; +import { DbLedgerEntry, DbRune } from '../src/pg/types'; export type TestFastifyServer = FastifyInstance< Server, @@ -54,9 +54,40 @@ export async function dropDatabase(db: PgStore) { }); } -export async function insertRune(db: PgStore, payload: DbRune): Promise { +export async function insertDbEntry( + db: PgStore, + payload: DbLedgerEntry, + event_index: number +): Promise { await db.sqlWriteTransaction(async sql => { + const { + rune_id, + block_hash, + block_height, + tx_index, + tx_id, + output, + address, + receiver_address, + amount, + operation, + } = payload; + await sql` + INSERT INTO ledger ( + rune_id, block_hash, block_height, tx_index, tx_id, output, + address, receiver_address, amount, operation, timestamp, event_index + ) + VALUES ( + + ${rune_id}, ${block_hash}, ${block_height}, ${tx_index}, ${tx_id}, ${output}, ${address}, ${receiver_address}, ${amount}, ${operation}, 0, ${event_index} + ) + `; + }); +} + +export async function insertRune(db: PgStore, payload: DbRune): Promise { + await db.sqlWriteTransaction(async sql => { const { id, name, @@ -74,9 +105,6 @@ export async function insertRune(db: PgStore, payload: DbRune): Promise { terms_height_end, } = payload; - // Insert a new rune into the 'runes' table - // Ensure the column names and types match your database schema - // INSERT INTO runes ( // id, number, name, spaced_name, block_hash, block_height, tx_index, tx_id, symbol, terms_amount, // terms_cap, terms_height_start, terms_height_end, timestamp @@ -91,11 +119,9 @@ export async function insertRune(db: PgStore, payload: DbRune): Promise { ) VALUES ( - ${id}, ${number}, ${sql(name)}, ${sql(spaced_name)}, ${sql(block_hash)}, ${sql( - block_height - )}, ${tx_index}, ${sql(tx_id)}, ${sql(symbol)}, ${cenotaph}, ${sql(terms_amount || '')}, ${sql( - terms_cap || '' - )}, ${terms_height_start}, ${terms_height_end}, NOW() + ${id}, ${number}, ${name}, ${spaced_name}, ${block_hash}, ${block_height}, ${tx_index}, ${tx_id}, ${symbol}, ${cenotaph}, ${ + terms_amount || '' + }, ${terms_cap || ''}, ${terms_height_start}, ${terms_height_end}, 0 ) `; });