Skip to content

Commit

Permalink
fix migration setup and attempt some inserts
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Jul 18, 2024
1 parent 29140f6 commit 77aebfe
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/src/pg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type DbPaginatedResult<T> = {

export type DbCountedQueryResult<T> = T & { total: number };

type DbRune = {
export type DbRune = {
id: string;
number: number;
name: string;
Expand Down
58 changes: 55 additions & 3 deletions api/tests/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
});
});
53 changes: 51 additions & 2 deletions api/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,11 +20,11 @@ export async function startTestApiServer(db: PgStore): Promise<TestFastifyServer
}

export async function runMigrations(db: PgStore) {
const contents = readdirSync('../../migrations');
const contents = readdirSync('../migrations');
await db.sqlWriteTransaction(async sql => {
for (const fileName of contents) {
if (!fileName.endsWith('.sql')) continue;
await db.sql.file(fileName);
await db.sql.file('../migrations/' + fileName);
}
});
}
Expand Down Expand Up @@ -51,3 +53,50 @@ export async function dropDatabase(db: PgStore) {
`;
});
}

export async function insertRune(db: PgStore, payload: DbRune): Promise<void> {
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()
)
`;
});
}
2 changes: 2 additions & 0 deletions api/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
export default (): void => {
process.env.PGDATABASE = 'postgres';
process.env.PGPASSWORD = 'postgres';
process.env.PGUSER = 'test';
process.env.PGHOST = 'localhost';
};

0 comments on commit 77aebfe

Please sign in to comment.