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; reveal_block: number;
ready_block: number; ready_block: number;
buckets: string; buckets: string;
base: bigint; base: PgNumeric;
coeff: bigint; coeff: PgNumeric;
nonalpha_discount: number; nonalpha_discount: number;
no_vowel_discount: number; no_vowel_discount: number;
lifetime: number; lifetime: number;

View File

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

View File

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

View File

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

View File

@@ -1,21 +1,22 @@
import { ChainID } from '@stacks/transactions'; import { ChainID } from '@stacks/transactions';
import { PgDataStore, cycleMigrations, runMigrations } from '../datastore/postgres-store';
import { PoolClient } from 'pg';
import { bnsNameCV, httpPostRequest } from '../helpers'; import { bnsNameCV, httpPostRequest } from '../helpers';
import { EventStreamServer, startEventServer } from '../event-stream/event-server'; import { EventStreamServer, startEventServer } from '../event-stream/event-server';
import { TestBlockBuilder, TestMicroblockStreamBuilder } from '../test-utils/test-builders'; import { TestBlockBuilder, TestMicroblockStreamBuilder } from '../test-utils/test-builders';
import { DbAssetEventTypeId, DbBnsZoneFile } from '../datastore/common'; 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', () => { describe('BNS event server tests', () => {
let db: PgDataStore; let db: PgWriteStore;
let client: PoolClient; let client: PgSqlClient;
let eventServer: EventStreamServer; let eventServer: EventStreamServer;
beforeEach(async () => { beforeEach(async () => {
process.env.PG_DATABASE = 'postgres'; process.env.PG_DATABASE = 'postgres';
await cycleMigrations(); await cycleMigrations();
db = await PgDataStore.connect({ usageName: 'tests', withNotifier: false }); db = await PgWriteStore.connect({ usageName: 'tests', withNotifier: false });
client = await db.pool.connect(); client = db.sql;
eventServer = await startEventServer({ eventServer = await startEventServer({
datastore: db, datastore: db,
chainId: ChainID.Mainnet, 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. // To validate table data we'll query it directly. There should only be one zonefile.
const result = await client.query<DbBnsZoneFile>(`SELECT * FROM zonefiles`); const result = await client<DbBnsZoneFile[]>`SELECT * FROM zonefiles`;
expect(result.rowCount).toBe(1); expect(result.count).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'); 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 () => { afterEach(async () => {
await eventServer.closeAsync(); await eventServer.closeAsync();
client.release();
await db?.close(); await db?.close();
await runMigrations(undefined, 'down'); 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 { ApiServer, startApiServer } from '../api/init';
import * as supertest from 'supertest'; import * as supertest from 'supertest';
import { startEventServer } from '../event-stream/event-server'; import { startEventServer } from '../event-stream/event-server';
@@ -10,10 +8,11 @@ import * as assert from 'assert';
import { TestBlockBuilder } from '../test-utils/test-builders'; import { TestBlockBuilder } from '../test-utils/test-builders';
import { DataStoreBlockUpdateData } from '../datastore/common'; import { DataStoreBlockUpdateData } from '../datastore/common';
import { BnsGenesisBlock } from '../event-replay/helpers'; import { BnsGenesisBlock } from '../event-replay/helpers';
import { PgWriteStore } from '../datastore/pg-write-store';
import { cycleMigrations, runMigrations } from '../datastore/migrations';
describe('BNS V1 import', () => { describe('BNS V1 import', () => {
let db: PgDataStore; let db: PgWriteStore;
let client: PoolClient;
let eventServer: Server; let eventServer: Server;
let api: ApiServer; let api: ApiServer;
let block: DataStoreBlockUpdateData; let block: DataStoreBlockUpdateData;
@@ -21,8 +20,7 @@ describe('BNS V1 import', () => {
beforeEach(async () => { beforeEach(async () => {
process.env.PG_DATABASE = 'postgres'; process.env.PG_DATABASE = 'postgres';
await cycleMigrations(); await cycleMigrations();
db = await PgDataStore.connect({ usageName: 'tests' }); db = await PgWriteStore.connect({ usageName: 'tests' });
client = await db.pool.connect();
eventServer = await startEventServer({ datastore: db, chainId: ChainID.Testnet, httpLogLevel: 'silly' }); eventServer = await startEventServer({ datastore: db, chainId: ChainID.Testnet, httpLogLevel: 'silly' });
api = await startApiServer({ 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 () => { afterEach(async () => {
await new Promise(resolve => eventServer.close(() => resolve(true))); await new Promise(resolve => eventServer.close(() => resolve(true)));
await api.terminate(); await api.terminate();
client.release();
await db?.close(); await db?.close();
await runMigrations(undefined, 'down'); await runMigrations(undefined, 'down');
}); });