diff --git a/api/src/pg/types.ts b/api/src/pg/types.ts index ce058c4..99e5c28 100644 --- a/api/src/pg/types.ts +++ b/api/src/pg/types.ts @@ -5,7 +5,7 @@ export type DbPaginatedResult = { export type DbCountedQueryResult = T & { total: number }; -type DbRune = { +export type DbRune = { id: string; number: number; name: string; diff --git a/api/tests/api/api.test.ts b/api/tests/api/api.test.ts index 251165b..24e15b8 100644 --- a/api/tests/api/api.test.ts +++ b/api/tests/api/api.test.ts @@ -1,6 +1,13 @@ import { ENV } from '../../src/env'; import { PgStore } from '../../src/pg/pg-store'; -import { dropDatabase, runMigrations, startTestApiServer, TestFastifyServer } from '../helpers'; +import { DbRune } from '../../src/pg/types'; +import { + dropDatabase, + insertRune, + runMigrations, + startTestApiServer, + TestFastifyServer, +} from '../helpers'; describe('Etchings', () => { let db: PgStore; @@ -14,12 +21,57 @@ describe('Etchings', () => { }); afterEach(async () => { - await fastify.close(); + if (fastify) { + await fastify.close(); + } + await dropDatabase(db); await db.close(); }); test('displays etched rune', async () => { - // + + // '1:0', 0, 'UNCOMMONGOODS', 'UNCOMMON•GOODS', + // '0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5', 840000, 0, '', '⧉', 1, + // '340282366920938463463374607431768211455', 840000, 1050000, 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', + tx_index: 0, + tx_id: 'sample_tx_id', + divisibility: 8, + premine: '1000', + symbol: 'SRN', + cenotaph: true, + terms_amount: '1000000', + terms_cap: '5000000', + terms_height_start: null, + terms_height_end: null, + terms_offset_start: null, + terms_offset_end: null, + turbo: false, + minted: '1000', + total_mints: '1500', + burned: '500', + total_burns: '750', + total_operations: '2000', + timestamp: Date.now(), + }; + // await insertRune(db, rune); + // const runes = await fastify.inject({ + // method: 'GET', + // url: '/runes/v1/etchings/', + // }); + // console.log(runes); + const etching = 'UNCOMMON GOODS' + const response = await fastify.inject({ + method: 'GET', + url: '/runes/v1/etchings/' + etching, + }); + expect(response.statusCode).toBe(200); }); }); diff --git a/api/tests/helpers.ts b/api/tests/helpers.ts index 9e689d5..63d781c 100644 --- a/api/tests/helpers.ts +++ b/api/tests/helpers.ts @@ -4,6 +4,8 @@ import { FastifyBaseLogger, FastifyInstance } from 'fastify'; 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'; export type TestFastifyServer = FastifyInstance< Server, @@ -18,11 +20,11 @@ export async function startTestApiServer(db: PgStore): Promise { for (const fileName of contents) { if (!fileName.endsWith('.sql')) continue; - await db.sql.file(fileName); + await db.sql.file('../migrations/' + fileName); } }); } @@ -51,3 +53,50 @@ export async function dropDatabase(db: PgStore) { `; }); } + +export async function insertRune(db: PgStore, payload: DbRune): Promise { + await db.sqlWriteTransaction(async sql => { + + const { + id, + name, + spaced_name, + number, + block_hash, + block_height, + tx_index, + tx_id, + symbol, + cenotaph, + terms_amount, + terms_cap, + terms_height_start, + 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 + // ) + // '1:0', 0, 'UNCOMMONGOODS', 'UNCOMMON•GOODS', + // '0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5', 840000, 0, '', '⧉', 1, + // '340282366920938463463374607431768211455', 840000, 1050000, 0 + await sql` + INSERT INTO runes ( + 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, timestamp + ) + 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() + ) + `; + }); +} diff --git a/api/tests/setup.ts b/api/tests/setup.ts index 2caf110..3b7ab9f 100644 --- a/api/tests/setup.ts +++ b/api/tests/setup.ts @@ -2,4 +2,6 @@ export default (): void => { process.env.PGDATABASE = 'postgres'; process.env.PGPASSWORD = 'postgres'; + process.env.PGUSER = 'test'; + process.env.PGHOST = 'localhost'; };