Skip to content

Commit

Permalink
test: test fixes and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
iadmytro committed Feb 14, 2023
1 parent 94ace9b commit d246ff6
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 58 deletions.
15 changes: 8 additions & 7 deletions packages/api-cardano-db-hasura/test/activeStake.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { init } from './util'
import Logger from 'bunyan'
import { Client } from 'pg'
import { Client, QueryResult } from 'pg'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'active_stake'), name)
Expand All @@ -18,12 +18,6 @@ describe('activeStake', () => {
let db: Client
let stakeAddress: string

const getTestData = async (sql: string) => {
const resp = await db.query(sql)
expect(resp.rows.length).toBeGreaterThan(0)
return resp
}

beforeAll(async () => {
({ client, db, logger } = await init('activeStake'))
await db.connect()
Expand All @@ -33,6 +27,13 @@ describe('activeStake', () => {
await db.end()
})

const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

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;')
stakeAddress = dbResp.rows[0].view
Expand Down
13 changes: 9 additions & 4 deletions packages/api-cardano-db-hasura/test/assets.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { init } from './util'
import { Logger } from 'ts-log'
import { Client } from 'pg'
import { Client, QueryResult } from 'pg'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'assets'), name)
Expand All @@ -20,10 +20,15 @@ describe('assets', () => {
({ client, db, logger } = await init('assets'))
await db.connect()
})

afterAll(async () => {
await db.end()
})
const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('can return information on assets', async () => {
const result = await client.query({
Expand All @@ -42,7 +47,7 @@ describe('assets', () => {
})

it('can return information on assets by fingerprint', async () => {
const dbResp = await db.query('SELECT fingerprint FROM "Asset" ORDER BY RANDOM() LIMIT 1;')
const dbResp = await getTestData('SELECT fingerprint FROM "Asset" ORDER BY RANDOM() LIMIT 1;')
const assetFingerprint = dbResp.rows[0].fingerprint
logger.info('Asset fingerprint - ' + assetFingerprint)
const result = await client.query({
Expand All @@ -65,7 +70,7 @@ describe('assets', () => {
expect(assets[0].url).toBeDefined()
})
it('can return information on assets by assetId', async () => {
const dbResp = (await db.query('SELECT * FROM "Asset" ORDER BY RANDOM() LIMIT 1;')).rows[0]
const dbResp = (await getTestData('SELECT * FROM "Asset" ORDER BY RANDOM() LIMIT 1;')).rows[0]
const assetId = dbResp.assetId.toString('hex')
logger.info('assetId - ' + assetId)
const result = await client.query({
Expand Down
21 changes: 11 additions & 10 deletions packages/api-cardano-db-hasura/test/blocks.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 } from 'pg'
import { Client, QueryResult } from 'pg'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'blocks'), name)
Expand All @@ -16,12 +16,6 @@ describe('blocks', () => {
let client: TestClient
let db: Client

const getTestData = async (sql: string) => {
const resp = await db.query(sql)
expect(resp.rows.length).toBeGreaterThan(0)
return resp
}

beforeAll(async () => {
({ client, db, logger } = await init('blocks'))
await db.connect()
Expand All @@ -31,6 +25,13 @@ describe('blocks', () => {
await db.end()
})

const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('caps the response to 100 blocks', async () => {
const result = await client.query({
query: await loadQueryNode('blockHashesNoArgs')
Expand All @@ -46,9 +47,9 @@ describe('blocks', () => {
query: await loadQueryNode('second20Blocks')
})
expect(page1.data.blocks.length).toBe(20)
expect(page1.data.blocks[19].number).toBe(23)
expect(page1.data.blocks[19].number).toBeDefined()
expect(page2.data.blocks.length).toBe(20)
expect(page2.data.blocks[19].number).toBe(43)
expect(page2.data.blocks[19].number).toBeDefined()
})

it('Can return blocks by number', async () => {
Expand Down Expand Up @@ -92,7 +93,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;')
logger.info('Block number -', dbResp.rows[0].block_no)
let fee = 10
let fee = 0
let query = gql`query {
blocks( where: { number: { _eq: ${dbResp.rows[0].block_no} }}) {
transactions_aggregate(
Expand Down
12 changes: 9 additions & 3 deletions packages/api-cardano-db-hasura/test/cardano.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DocumentNode } from 'graphql'
import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { init } from './util'
import { Client } from 'pg'
import { Client, QueryResult } from 'pg'
import Logger from 'bunyan'

function loadQueryNode (name: string): Promise<DocumentNode> {
Expand All @@ -22,10 +22,16 @@ describe('cardano', () => {
afterAll(async () => {
await db.end()
})
const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('Returns core information about the current state of the network', async () => {
const dbTip = await db.query('SELECT max(block_no) AS block_no FROM block;')
const dbEpoch = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
const dbTip = await getTestData('SELECT max(block_no) AS block_no FROM block;')
const dbEpoch = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
logger.info('Tip - ', dbTip.rows[0].block_no, ' epoch - ', dbEpoch.rows[0].epoch_no)
const result = await client.query({
query: await loadQueryNode('chainTipAndCurrentEpochNumber')
Expand Down
14 changes: 11 additions & 3 deletions packages/api-cardano-db-hasura/test/delegations.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { DocumentNode } from 'graphql'
import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { init } from './util'
import { Client } from 'pg'
import { Client, QueryResult } from 'pg'
import Logger from 'bunyan'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'delegations'), name)
Expand All @@ -14,13 +15,20 @@ function loadQueryNode (name: string): Promise<DocumentNode> {
describe('delegations', () => {
let client: TestClient
let db: Client
let logger: Logger
beforeAll(async () => {
({ client, db } = await init('delegations'))
({ client, db, logger } = await init('delegations'))
await db.connect()
})
afterAll(async () => {
await db.end()
})
const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('can return details for stake delegation', async () => {
const result = await client.query({
Expand All @@ -35,7 +43,7 @@ describe('delegations', () => {
})

it('can return aggregated data on all delegations', async () => {
const dbResp = await db.query('SELECT COUNT(*) as count FROM delegation;')
const dbResp = await getTestData('SELECT COUNT(*) as count FROM delegation;')
const result = await client.query({
query: await loadQueryNode('aggregateDelegation')
})
Expand Down
24 changes: 15 additions & 9 deletions packages/api-cardano-db-hasura/test/epochs.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 } from 'pg'
import { Client, QueryResult } from 'pg'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'epochs'), name)
Expand All @@ -23,9 +23,15 @@ describe('epochs', () => {
afterAll(async () => {
await db.end()
})
const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('Returns epoch details by number', async () => {
const dbResp = await db.query('SELECT no, out_sum FROM epoch WHERE no = (SELECT max(no) FROM epoch);')
const dbResp = await getTestData('SELECT no, out_sum FROM epoch WHERE no = (SELECT max(no) FROM epoch);')
logger.info('Epoch number -', dbResp.rows[0].no)
const result = await client.query({
query: await loadQueryNode('epochDetailsByNumber'),
Expand All @@ -36,7 +42,7 @@ describe('epochs', () => {
})

it('Includes protocol params in effect for the epoch', async () => {
const dbResp = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
const dbResp = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
logger.info('Epoch number -', dbResp.rows[0].no)
const result = await client.query({
query: await loadQueryNode('epochProtocolParams'),
Expand All @@ -46,7 +52,7 @@ describe('epochs', () => {
})

it('Can return aggregated data', async () => {
const dbResp = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
const dbResp = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
logger.info('Epoch number -', dbResp.rows[0].no)
const result = await client.query({
query: await loadQueryNode('aggregateDataWithinEpoch'),
Expand All @@ -59,9 +65,9 @@ describe('epochs', () => {
})

it('Can return filtered aggregated data', async () => {
const dbEpoch = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
const dbSlotLeader = await db.query('SELECT description FROM slot_leader WHERE slot_leader.description IS NOT NULL ORDER BY RANDOM() LIMIT 1;')
const dbCount = await db.query('SELECT COUNT(*) as count FROM block join slot_leader sl on block.slot_leader_id = sl.id where description = ' + '\'' + dbSlotLeader.rows[0].description + '\' and epoch_no = ' + dbEpoch.rows[0].epoch_no + ';')
const dbEpoch = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
const dbSlotLeader = await getTestData('SELECT description FROM slot_leader WHERE slot_leader.description IS NOT NULL ORDER BY RANDOM() LIMIT 1;')
const dbCount = await getTestData('SELECT COUNT(*) as count FROM block join slot_leader sl on block.slot_leader_id = sl.id where description = ' + '\'' + dbSlotLeader.rows[0].description + '\' and epoch_no = ' + dbEpoch.rows[0].epoch_no + ';')
logger.info('Epoch number -', dbEpoch.rows[0].epoch_no, ', slot leader - ', dbSlotLeader.rows[0].description)
const result = await client.query({
query: await loadQueryNode('numberOfBlocksProducedByLeaderInEpoch'),
Expand All @@ -72,7 +78,7 @@ describe('epochs', () => {
})

it('Returns epoch details by number range', async () => {
const dbResp = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
const dbResp = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
const result = await client.query({
query: await loadQueryNode('epochDetailsInRange'),
variables: { from: dbResp.rows[0].epoch_no - 2, to: dbResp.rows[0].epoch_no }
Expand All @@ -82,7 +88,7 @@ describe('epochs', () => {
})

it('Can return aggregated Epoch data', async () => {
const dbResp = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
const dbResp = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
logger.info('Epoch number -', dbResp.rows[0].epoch_no)
const result = await client.query({
query: await loadQueryNode('aggregateEpochData'),
Expand Down
2 changes: 1 addition & 1 deletion packages/api-cardano-db-hasura/test/genesis.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ describe('genesis', () => {
it('Returns key information about the network genesis', async () => {
const query = { query: await loadQueryNode('keyNetworkInfo') }
const result = await client.query(query)
allFieldsPopulated(result.data)
allFieldsPopulated(result.data, 'staking')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ describe('paymentAddress', () => {
const boundedAdaBalance = new BigNumber(
boundedResult.data.paymentAddresses[0].summary?.assetBalances[0]?.quantity
).toNumber() || 0
expect(unboundedAdaBalance).toBeGreaterThan(boundedAdaBalance)
expect(unboundedAdaBalance).toBeGreaterThanOrEqual(boundedAdaBalance)
})
})
10 changes: 8 additions & 2 deletions packages/api-cardano-db-hasura/test/rewards.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { init } from './util'
import Logger from 'bunyan'
import { Client } from 'pg'
import { Client, QueryResult } from 'pg'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'rewards'), name)
Expand All @@ -23,9 +23,15 @@ describe('rewards', () => {
afterAll(async () => {
await db.end()
})
const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('can return details for rewards scoped to an address', async () => {
const dbResp = await db.query('SELECT view FROM reward JOIN stake_address sa ON reward.addr_id = sa.id ORDER BY RANDOM() LIMIT 1;')
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;')
const stakeAddress = dbResp.rows[0].view
logger.info('Stake address - ' + stakeAddress)
const result = await client.query({
Expand Down
10 changes: 8 additions & 2 deletions packages/api-cardano-db-hasura/test/stakePool.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { init } from './util'
import Logger from 'bunyan'
import { Client } from 'pg'
import { Client, QueryResult } from 'pg'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'stake_pools'), name)
Expand All @@ -23,9 +23,15 @@ describe('stakePools', () => {
afterAll(async () => {
await db.end()
})
const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('can lookup stake pools by ID', async () => {
const dbResp = await db.query('SELECT view FROM pool_hash ORDER BY RANDOM() LIMIT 1;')
const dbResp = await getTestData('SELECT view FROM pool_hash ORDER BY RANDOM() LIMIT 1;')
const poolId = dbResp.rows[0].view
logger.info('Stake pool id - ' + poolId)
const result = await client.query({
Expand Down
10 changes: 8 additions & 2 deletions packages/api-cardano-db-hasura/test/tokenMints.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { init } from './util'
import Logger from 'bunyan'
import { Client } from 'pg'
import { Client, QueryResult } from 'pg'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'token_mints'), name)
Expand All @@ -23,6 +23,12 @@ describe('tokenMints', () => {
afterAll(async () => {
await db.end()
})
const getTestData = async (sql: string) :Promise<QueryResult> => {
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
}

it('can return information on token minting and burning', async () => {
const result = await client.query({
Expand All @@ -40,7 +46,7 @@ describe('tokenMints', () => {
})

it('can return information on assets by fingerprint', async () => {
const dbResp = await db.query('SELECT fingerprint FROM "Asset" ORDER BY RANDOM() LIMIT 1;')
const dbResp = await getTestData('SELECT fingerprint FROM "Asset" ORDER BY RANDOM() LIMIT 1;')
const fingerprint = dbResp.rows[0].fingerprint
logger.info('Fingerprint - ' + fingerprint)
const result = await client.query({
Expand Down
Loading

0 comments on commit d246ff6

Please sign in to comment.