fix: tests

This commit is contained in:
Rafael Cárdenas
2022-09-07 14:31:45 -05:00
parent 4ddb8f1aa0
commit 1c1fd1619c
6 changed files with 24 additions and 28 deletions

View File

@@ -1143,8 +1143,8 @@ export interface BnsNamespaceInsertValues {
reveal_block: number;
ready_block: number;
buckets: string;
base: bigint;
coeff: bigint;
base: PgNumeric;
coeff: PgNumeric;
nonalpha_discount: number;
no_vowel_discount: number;
lifetime: number;

View File

@@ -8,7 +8,6 @@ import { getTxTypeId, getTxTypeString } from '../api/controllers/db-controller';
import {
assertNotNullish,
FoundOrNot,
hexToBuffer,
unwrapOptional,
bnsHexValueToName,
bnsNameCV,
@@ -3524,7 +3523,7 @@ export class PgStore {
}): Promise<FoundOrNot<DbBnsSubdomain & { index_block_hash: string }>> {
const queryResult = await this.sql.begin(async sql => {
const maxBlockHeight = await this.getMaxBlockHeight(sql, { includeUnanchored });
return await sql<(DbBnsSubdomain & { tx_id: Buffer; index_block_hash: Buffer })[]>`
return await sql<(DbBnsSubdomain & { tx_id: string; index_block_hash: string })[]>`
SELECT s.*, z.zonefile
FROM subdomains AS s
LEFT JOIN zonefiles AS z
@@ -3539,7 +3538,7 @@ export class PgStore {
LIMIT 1
`;
});
if (queryResult.length > 0 && !queryResult[0].zonefile_hash) {
if (queryResult.length > 0 && queryResult[0].zonefile_hash) {
return {
found: true,
result: {

View File

@@ -843,7 +843,7 @@ export class PgWriteStore extends PgStore {
}
const result = await sql`
INSERT INTO subdomains ${sql(subdomainValues)}
ON CONFLICT ON CONSTRAINT unique_fully_qualified_subdomain_tx_id_index_block_hash_microblock_hash DO
ON CONFLICT ON CONSTRAINT unique_fqs_tx_id_index_block_hash_microblock_hash DO
UPDATE SET
name = EXCLUDED.name,
namespace_id = EXCLUDED.namespace_id,
@@ -1026,8 +1026,8 @@ export class PgWriteStore extends PgStore {
microblock_sequence: I32_MAX,
microblock_canonical: true,
};
if (dbTx.rowCount > 0) {
const parsedDbTx = parseTxQueryResult(dbTx.rows[0]);
if (dbTx.count > 0) {
const parsedDbTx = parseTxQueryResult(dbTx[0]);
isCanonical = parsedDbTx.canonical;
txIndex = parsedDbTx.tx_index;
blockData.index_block_hash = parsedDbTx.index_block_hash;
@@ -1525,8 +1525,8 @@ export class PgWriteStore extends PgStore {
reveal_block: bnsNamespace.reveal_block,
ready_block: bnsNamespace.ready_block,
buckets: bnsNamespace.buckets,
base: bnsNamespace.base,
coeff: bnsNamespace.coeff,
base: bnsNamespace.base.toString(),
coeff: bnsNamespace.coeff.toString(),
nonalpha_discount: bnsNamespace.nonalpha_discount,
no_vowel_discount: bnsNamespace.no_vowel_discount,
lifetime: bnsNamespace.lifetime,

View File

@@ -93,7 +93,7 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
]);
pgm.addConstraint(
'subdomains',
'unique_fully_qualified_subdomain_tx_id_index_block_hash_microblock_hash',
'unique_fqs_tx_id_index_block_hash_microblock_hash',
'UNIQUE(fully_qualified_subdomain, tx_id, index_block_hash, microblock_hash)'
);
}

View File

@@ -1,21 +1,22 @@
import { ChainID } from '@stacks/transactions';
import { PgDataStore, cycleMigrations, runMigrations } from '../datastore/postgres-store';
import { PoolClient } from 'pg';
import { bnsNameCV, httpPostRequest } from '../helpers';
import { EventStreamServer, startEventServer } from '../event-stream/event-server';
import { TestBlockBuilder, TestMicroblockStreamBuilder } from '../test-utils/test-builders';
import { DbAssetEventTypeId, DbBnsZoneFile } from '../datastore/common';
import { PgWriteStore } from '../datastore/pg-write-store';
import { cycleMigrations, runMigrations } from '../datastore/migrations';
import { PgSqlClient } from '../datastore/connection';
describe('BNS event server tests', () => {
let db: PgDataStore;
let client: PoolClient;
let db: PgWriteStore;
let client: PgSqlClient;
let eventServer: EventStreamServer;
beforeEach(async () => {
process.env.PG_DATABASE = 'postgres';
await cycleMigrations();
db = await PgDataStore.connect({ usageName: 'tests', withNotifier: false });
client = await db.pool.connect();
db = await PgWriteStore.connect({ usageName: 'tests', withNotifier: false });
client = db.sql;
eventServer = await startEventServer({
datastore: db,
chainId: ChainID.Mainnet,
@@ -580,14 +581,13 @@ describe('BNS event server tests', () => {
});
// To validate table data we'll query it directly. There should only be one zonefile.
const result = await client.query<DbBnsZoneFile>(`SELECT * FROM zonefiles`);
expect(result.rowCount).toBe(1);
expect(result.rows[0].zonefile).toBe('$ORIGIN jnj.btc.\n$TTL 3600\n_http._tcp\tIN\tURI\t10\t1\t"https://gaia.blockstack.org/hub/1z8AzyhC42n8TvoFaUL2nscaCGHqQQWUr/profile.json"\n\n');
const result = await client<DbBnsZoneFile[]>`SELECT * FROM zonefiles`;
expect(result.count).toBe(1);
expect(result[0].zonefile).toBe('$ORIGIN jnj.btc.\n$TTL 3600\n_http._tcp\tIN\tURI\t10\t1\t"https://gaia.blockstack.org/hub/1z8AzyhC42n8TvoFaUL2nscaCGHqQQWUr/profile.json"\n\n');
});
afterEach(async () => {
await eventServer.closeAsync();
client.release();
await db?.close();
await runMigrations(undefined, 'down');
});

View File

@@ -1,5 +1,3 @@
import { PgDataStore, cycleMigrations, runMigrations } from '../datastore/postgres-store';
import { PoolClient } from 'pg';
import { ApiServer, startApiServer } from '../api/init';
import * as supertest from 'supertest';
import { startEventServer } from '../event-stream/event-server';
@@ -10,10 +8,11 @@ import * as assert from 'assert';
import { TestBlockBuilder } from '../test-utils/test-builders';
import { DataStoreBlockUpdateData } from '../datastore/common';
import { BnsGenesisBlock } from '../event-replay/helpers';
import { PgWriteStore } from '../datastore/pg-write-store';
import { cycleMigrations, runMigrations } from '../datastore/migrations';
describe('BNS V1 import', () => {
let db: PgDataStore;
let client: PoolClient;
let db: PgWriteStore;
let eventServer: Server;
let api: ApiServer;
let block: DataStoreBlockUpdateData;
@@ -21,8 +20,7 @@ describe('BNS V1 import', () => {
beforeEach(async () => {
process.env.PG_DATABASE = 'postgres';
await cycleMigrations();
db = await PgDataStore.connect({ usageName: 'tests' });
client = await db.pool.connect();
db = await PgWriteStore.connect({ usageName: 'tests' });
eventServer = await startEventServer({ datastore: db, chainId: ChainID.Testnet, httpLogLevel: 'silly' });
api = await startApiServer({ datastore: db, chainId: ChainID.Testnet, httpLogLevel: 'silly' });
@@ -167,7 +165,6 @@ describe('BNS V1 import', () => {
afterEach(async () => {
await new Promise(resolve => eventServer.close(() => resolve(true)));
await api.terminate();
client.release();
await db?.close();
await runMigrations(undefined, 'down');
});