diff --git a/packages/api-cardano-db-hasura/test/activeStake.query.test.ts b/packages/api-cardano-db-hasura/test/activeStake.query.test.ts index f0b91126..4372e1aa 100644 --- a/packages/api-cardano-db-hasura/test/activeStake.query.test.ts +++ b/packages/api-cardano-db-hasura/test/activeStake.query.test.ts @@ -4,9 +4,9 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { init } from './util' +import { init, queryDB } from './util' import Logger from 'bunyan' -import { Client, QueryResult } from 'pg' +import { Client } from 'pg' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'active_stake'), name) @@ -22,17 +22,10 @@ describe('activeStake', () => { ({ client, db, logger } = await init('activeStake')) await db.connect() }) - afterAll(async () => { await db.end() }) - - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('can return active stake snapshots for an address', async () => { const dbResp = await getTestData('WITH current_epoch AS (SELECT max(epoch_no) AS epoch_no FROM block) select view from epoch_stake join stake_address on epoch_stake.addr_id = stake_address.id where epoch_no=(SELECT epoch_no FROM current_epoch) ORDER BY RANDOM() LIMIT 1;') diff --git a/packages/api-cardano-db-hasura/test/assets.query.test.ts b/packages/api-cardano-db-hasura/test/assets.query.test.ts index bc671e35..e00d83c1 100644 --- a/packages/api-cardano-db-hasura/test/assets.query.test.ts +++ b/packages/api-cardano-db-hasura/test/assets.query.test.ts @@ -4,9 +4,9 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { init } from './util' -import { Logger } from 'ts-log' -import { Client, QueryResult } from 'pg' +import { init, queryDB } from './util' +import { Client } from 'pg' +import Logger from 'bunyan' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'assets'), name) @@ -23,12 +23,7 @@ describe('assets', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('can return information on assets', async () => { const result = await client.query({ diff --git a/packages/api-cardano-db-hasura/test/blocks.query.test.ts b/packages/api-cardano-db-hasura/test/blocks.query.test.ts index c49cd3e9..2f15f150 100644 --- a/packages/api-cardano-db-hasura/test/blocks.query.test.ts +++ b/packages/api-cardano-db-hasura/test/blocks.query.test.ts @@ -3,9 +3,9 @@ import { DocumentNode } from 'graphql' import gql from 'graphql-tag' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { allFieldsPopulated, init } from './util' -import { Logger } from 'ts-log' -import { Client, QueryResult } from 'pg' +import { allFieldsPopulated, init, queryDB } from './util' +import { Client } from 'pg' +import Logger from 'bunyan' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'blocks'), name) @@ -20,17 +20,10 @@ describe('blocks', () => { ({ client, db, logger } = await init('blocks')) await db.connect() }) - afterAll(async () => { await db.end() }) - - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('caps the response to 100 blocks', async () => { const result = await client.query({ @@ -91,7 +84,7 @@ describe('blocks', () => { }) it('Can return filtered aggregated data', async () => { - const dbResp = await getTestData('SELECT block_no, tx_count FROM block WHERE block_no IS NOT NULL AND tx_count > 10 ORDER BY RANDOM() LIMIT 1;') + const dbResp = await getTestData('SELECT block_id, block_no, tx_count, fee FROM block JOIN tx t ON block.id = t.block_id WHERE block_no IS NOT NULL AND tx_count>0 GROUP BY block_id, block_no, tx_count, fee HAVING sum(fee)>0 ORDER BY RANDOM() LIMIT 1;') logger.info('Block number -', dbResp.rows[0].block_no) let fee = 0 let query = gql`query { diff --git a/packages/api-cardano-db-hasura/test/cardano.query.test.ts b/packages/api-cardano-db-hasura/test/cardano.query.test.ts index 845d3289..919fce56 100644 --- a/packages/api-cardano-db-hasura/test/cardano.query.test.ts +++ b/packages/api-cardano-db-hasura/test/cardano.query.test.ts @@ -3,8 +3,8 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { init } from './util' -import { Client, QueryResult } from 'pg' +import { init, queryDB } from './util' +import { Client } from 'pg' import Logger from 'bunyan' function loadQueryNode (name: string): Promise { @@ -22,12 +22,7 @@ describe('cardano', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('Returns core information about the current state of the network', async () => { const dbTip = await getTestData('SELECT max(block_no) AS block_no FROM block;') diff --git a/packages/api-cardano-db-hasura/test/delegations.query.test.ts b/packages/api-cardano-db-hasura/test/delegations.query.test.ts index 5327975c..d1ab03c8 100644 --- a/packages/api-cardano-db-hasura/test/delegations.query.test.ts +++ b/packages/api-cardano-db-hasura/test/delegations.query.test.ts @@ -4,8 +4,8 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { init } from './util' -import { Client, QueryResult } from 'pg' +import { init, queryDB } from './util' +import { Client } from 'pg' import Logger from 'bunyan' function loadQueryNode (name: string): Promise { @@ -23,12 +23,7 @@ describe('delegations', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('can return details for stake delegation', async () => { const result = await client.query({ diff --git a/packages/api-cardano-db-hasura/test/epochs.query.test.ts b/packages/api-cardano-db-hasura/test/epochs.query.test.ts index 41506737..7c9a457d 100644 --- a/packages/api-cardano-db-hasura/test/epochs.query.test.ts +++ b/packages/api-cardano-db-hasura/test/epochs.query.test.ts @@ -4,9 +4,9 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { allFieldsPopulated, init } from './util' -import { Logger } from 'ts-log' -import { Client, QueryResult } from 'pg' +import { allFieldsPopulated, init, queryDB } from './util' +import Logger from 'bunyan' +import { Client } from 'pg' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'epochs'), name) @@ -23,12 +23,7 @@ describe('epochs', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('Returns epoch details by number', async () => { const dbResp = await getTestData('SELECT no, out_sum FROM epoch WHERE no = (SELECT max(no) FROM epoch);') diff --git a/packages/api-cardano-db-hasura/test/rewards.query.test.ts b/packages/api-cardano-db-hasura/test/rewards.query.test.ts index 8e37f526..916c0893 100644 --- a/packages/api-cardano-db-hasura/test/rewards.query.test.ts +++ b/packages/api-cardano-db-hasura/test/rewards.query.test.ts @@ -4,9 +4,9 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { init } from './util' +import {init, queryDB} from './util' import Logger from 'bunyan' -import { Client, QueryResult } from 'pg' +import { Client } from 'pg' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'rewards'), name) @@ -23,12 +23,7 @@ describe('rewards', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('can return details for rewards scoped to an address', async () => { const dbResp = await getTestData('SELECT * FROM reward JOIN stake_address sa ON reward.addr_id = sa.id WHERE amount>0 ORDER BY RANDOM() LIMIT 1;') diff --git a/packages/api-cardano-db-hasura/test/stakePool.query.test.ts b/packages/api-cardano-db-hasura/test/stakePool.query.test.ts index 3df54d5d..52389626 100644 --- a/packages/api-cardano-db-hasura/test/stakePool.query.test.ts +++ b/packages/api-cardano-db-hasura/test/stakePool.query.test.ts @@ -4,9 +4,9 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { init } from './util' +import { init, queryDB } from './util' import Logger from 'bunyan' -import { Client, QueryResult } from 'pg' +import { Client } from 'pg' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'stake_pools'), name) @@ -23,12 +23,7 @@ describe('stakePools', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('can lookup stake pools by ID', async () => { const dbResp = await getTestData('SELECT view FROM pool_hash ORDER BY RANDOM() LIMIT 1;') diff --git a/packages/api-cardano-db-hasura/test/tokenMints.query.test.ts b/packages/api-cardano-db-hasura/test/tokenMints.query.test.ts index da502bef..7e7c917d 100644 --- a/packages/api-cardano-db-hasura/test/tokenMints.query.test.ts +++ b/packages/api-cardano-db-hasura/test/tokenMints.query.test.ts @@ -4,9 +4,9 @@ import path from 'path' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { init } from './util' +import { init, queryDB } from './util' import Logger from 'bunyan' -import { Client, QueryResult } from 'pg' +import { Client } from 'pg' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'token_mints'), name) @@ -23,12 +23,7 @@ describe('tokenMints', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('can return information on token minting and burning', async () => { const result = await client.query({ diff --git a/packages/api-cardano-db-hasura/test/transactions.query.test.ts b/packages/api-cardano-db-hasura/test/transactions.query.test.ts index 6bdff841..fd968f57 100644 --- a/packages/api-cardano-db-hasura/test/transactions.query.test.ts +++ b/packages/api-cardano-db-hasura/test/transactions.query.test.ts @@ -4,9 +4,9 @@ import { gql } from 'apollo-boost' import { DocumentNode } from 'graphql' import util from '@cardano-graphql/util' import { TestClient } from '@cardano-graphql/util-dev' -import { allFieldsPopulated, init } from './util' +import { allFieldsPopulated, init, queryDB } from './util' import Logger from 'bunyan' -import { Client, QueryResult } from 'pg' +import { Client } from 'pg' function loadQueryNode (name: string): Promise { return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'transactions'), name) @@ -23,12 +23,7 @@ describe('transactions', () => { afterAll(async () => { await db.end() }) - const getTestData = async (sql: string) :Promise => { - const resp = await db.query(sql) - if (resp.rows.length === 0) logger.error('Can not find suitable data in db') - expect(resp.rows.length).toBeGreaterThan(0) - return resp - } + const getTestData = async (sql: string) => queryDB(db, logger, sql) it('Returns transactions by hashes', async () => { const dbResp = await getTestData('SELECT hash FROM tx ORDER BY RANDOM() LIMIT 2;') diff --git a/packages/api-cardano-db-hasura/test/util.ts b/packages/api-cardano-db-hasura/test/util.ts index 59bb0335..b596f5fb 100644 --- a/packages/api-cardano-db-hasura/test/util.ts +++ b/packages/api-cardano-db-hasura/test/util.ts @@ -2,9 +2,9 @@ import utilDev from '@cardano-graphql/util-dev' import pRetry from 'p-retry' import { gql } from 'apollo-boost' import util from '@cardano-graphql/util' -import { Client } from 'pg' +import { Client, QueryResult} from 'pg' import { getTestConfig } from './env.config' -import { createLogger } from 'bunyan' +import Logger, { createLogger } from 'bunyan' export async function buildClient ( apiUri: string @@ -48,6 +48,13 @@ export async function init (name: string) { } } +export const queryDB = async (db: Client, logger: Logger, sql: string) :Promise => { + const resp = await db.query(sql) + if (resp.rows.length === 0) logger.error('Can not find suitable data in db') + expect(resp.rows.length).toBeGreaterThan(0) + return resp +} + export const allFieldsPopulated = (obj: any, except : any = []) => { expect(obj).toBeDefined() if (Array.isArray(obj)) {