Skip to content

Commit

Permalink
fix the spaced name regex and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Jul 23, 2024
1 parent b94e387 commit d6174ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
5 changes: 4 additions & 1 deletion api/src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ const RuneNumberSchema = Type.RegEx(/^[0-9]+$/, { title: 'Rune number' });
export const RuneNumberSchemaCType = TypeCompiler.Compile(RuneNumberSchema);
const RuneNameSchema = Type.RegEx(/^[A-Z]+$/, { title: 'Rune name' });
export const RuneNameSchemaCType = TypeCompiler.Compile(RuneNameSchema);
const RuneSpacedNameSchema = Type.RegEx(/^[A-Z](•[A-Z]+)+$/, { title: 'Rune name with spacers' });
const RuneSpacedNameSchema = Type.RegEx(/^[A-Za-z]+(•[A-Za-z]+)+$/, {
title: 'Rune name with spacers',
});
// const RuneSpacedNameSchema = Type.RegEx(/^[A-Z](•\u2022\u2981[A-Z]+)*$/, {
export const RuneSpacedNameSchemaCType = TypeCompiler.Compile(RuneSpacedNameSchema);

export const RuneSchema = Type.Union([
Expand Down
33 changes: 26 additions & 7 deletions api/tests/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ describe('Etchings', () => {
let db: PgStore;
let fastify: TestFastifyServer;

const rune = sampleRune('1:1', 'Sample Rune');
const ledgerEntry = sampleLedgerEntry(rune.id);

beforeEach(async () => {
ENV.PGDATABASE = 'postgres';
db = await PgStore.connect();
fastify = await startTestApiServer(db);
await runMigrations(db);
await insertRune(db, rune);
const event_index = 0;
await insertDbEntry(db, ledgerEntry, event_index);
await insertSupply(db, rune.id, 1);
});

afterEach(async () => {
Expand All @@ -33,24 +40,36 @@ describe('Etchings', () => {
await db.close();
});

test('displays etched rune', async () => {
const rune = sampleRune('1:1', 'Sample Rune');
const ledgerEntry = sampleLedgerEntry(rune.id);
await insertRune(db, rune);
const event_index = 0;
await insertDbEntry(db, ledgerEntry, event_index);
await insertSupply(db, rune.id, 1);
test('lists runes', async () => {
const runesResponse = await fastify.inject({
method: 'GET',
url: '/runes/v1/etchings',
});
expect(runesResponse.statusCode).toBe(200);
expect(runesResponse.json().results).not.toHaveLength(0);

const response = await fastify.inject({
method: 'GET',
url: '/runes/v1/etchings/' + ledgerEntry.rune_id,
});
expect(response.statusCode).toBe(200);
expect(response.json().name).toEqual(rune.name);
});
test('can fetch by spaced name', async () => {
const url = '/runes/v1/etchings/' + rune.spaced_name;
const response = await fastify.inject({
method: 'GET',
url: url,
});
expect(response.statusCode).toBe(200);
expect(response.json().spaced_name).toEqual(rune.spaced_name);
});
test('can not fetch by spaced name if lacking bullets', async () => {
const url = '/runes/v1/etchings/' + rune.spaced_name.replaceAll('•', '-');
const response = await fastify.inject({
method: 'GET',
url: url,
});
expect(response.statusCode).toBe(400);
});
});
4 changes: 2 additions & 2 deletions api/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function toSpacedName(name: string | null): string | null {
export function sampleRune(id: string, name?: string): DbRune {
return {
id: '1:1',
name: name || 'Sample Rune Name',
spaced_name: (name && toSpacedName(name)) || 'Sample•Rune•Name',
name: name || 'SAMPLE RUNE NAME',
spaced_name: (name && toSpacedName(name)) || 'SAMPLE•RUNE•NAME',
number: 1,
block_hash: '0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5',
block_height: '840000',
Expand Down

0 comments on commit d6174ad

Please sign in to comment.