Skip to content

Commit 42144ce

Browse files
iadmytrorhyslbw
authored andcommitted
test: test fixes and logging
1 parent a8587dc commit 42144ce

12 files changed

+109
-58
lines changed

packages/api-cardano-db-hasura/test/activeStake.query.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
66
import { TestClient } from '@cardano-graphql/util-dev'
77
import { init } from './util'
88
import Logger from 'bunyan'
9-
import { Client } from 'pg'
9+
import { Client, QueryResult } from 'pg'
1010

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

21-
const getTestData = async (sql: string) => {
22-
const resp = await db.query(sql)
23-
expect(resp.rows.length).toBeGreaterThan(0)
24-
return resp
25-
}
26-
2721
beforeAll(async () => {
2822
({ client, db, logger } = await init('activeStake'))
2923
await db.connect()
@@ -33,6 +27,13 @@ describe('activeStake', () => {
3327
await db.end()
3428
})
3529

30+
const getTestData = async (sql: string) :Promise<QueryResult> => {
31+
const resp = await db.query(sql)
32+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
33+
expect(resp.rows.length).toBeGreaterThan(0)
34+
return resp
35+
}
36+
3637
it('can return active stake snapshots for an address', async () => {
3738
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;')
3839
stakeAddress = dbResp.rows[0].view

packages/api-cardano-db-hasura/test/assets.query.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
66
import { TestClient } from '@cardano-graphql/util-dev'
77
import { init } from './util'
88
import { Logger } from 'ts-log'
9-
import { Client } from 'pg'
9+
import { Client, QueryResult } from 'pg'
1010

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'assets'), name)
@@ -20,10 +20,15 @@ describe('assets', () => {
2020
({ client, db, logger } = await init('assets'))
2121
await db.connect()
2222
})
23-
2423
afterAll(async () => {
2524
await db.end()
2625
})
26+
const getTestData = async (sql: string) :Promise<QueryResult> => {
27+
const resp = await db.query(sql)
28+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
29+
expect(resp.rows.length).toBeGreaterThan(0)
30+
return resp
31+
}
2732

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

4449
it('can return information on assets by fingerprint', async () => {
45-
const dbResp = await db.query('SELECT fingerprint FROM "Asset" ORDER BY RANDOM() LIMIT 1;')
50+
const dbResp = await getTestData('SELECT fingerprint FROM "Asset" ORDER BY RANDOM() LIMIT 1;')
4651
const assetFingerprint = dbResp.rows[0].fingerprint
4752
logger.info('Asset fingerprint - ' + assetFingerprint)
4853
const result = await client.query({
@@ -65,7 +70,7 @@ describe('assets', () => {
6570
expect(assets[0].url).toBeDefined()
6671
})
6772
it('can return information on assets by assetId', async () => {
68-
const dbResp = (await db.query('SELECT * FROM "Asset" ORDER BY RANDOM() LIMIT 1;')).rows[0]
73+
const dbResp = (await getTestData('SELECT * FROM "Asset" ORDER BY RANDOM() LIMIT 1;')).rows[0]
6974
const assetId = dbResp.assetId.toString('hex')
7075
logger.info('assetId - ' + assetId)
7176
const result = await client.query({

packages/api-cardano-db-hasura/test/blocks.query.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import util from '@cardano-graphql/util'
55
import { TestClient } from '@cardano-graphql/util-dev'
66
import { allFieldsPopulated, init } from './util'
77
import { Logger } from 'ts-log'
8-
import { Client } from 'pg'
8+
import { Client, QueryResult } from 'pg'
99

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

19-
const getTestData = async (sql: string) => {
20-
const resp = await db.query(sql)
21-
expect(resp.rows.length).toBeGreaterThan(0)
22-
return resp
23-
}
24-
2519
beforeAll(async () => {
2620
({ client, db, logger } = await init('blocks'))
2721
await db.connect()
@@ -31,6 +25,13 @@ describe('blocks', () => {
3125
await db.end()
3226
})
3327

28+
const getTestData = async (sql: string) :Promise<QueryResult> => {
29+
const resp = await db.query(sql)
30+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
31+
expect(resp.rows.length).toBeGreaterThan(0)
32+
return resp
33+
}
34+
3435
it('caps the response to 100 blocks', async () => {
3536
const result = await client.query({
3637
query: await loadQueryNode('blockHashesNoArgs')
@@ -46,9 +47,9 @@ describe('blocks', () => {
4647
query: await loadQueryNode('second20Blocks')
4748
})
4849
expect(page1.data.blocks.length).toBe(20)
49-
expect(page1.data.blocks[19].number).toBe(23)
50+
expect(page1.data.blocks[19].number).toBeDefined()
5051
expect(page2.data.blocks.length).toBe(20)
51-
expect(page2.data.blocks[19].number).toBe(43)
52+
expect(page2.data.blocks[19].number).toBeDefined()
5253
})
5354

5455
it('Can return blocks by number', async () => {
@@ -92,7 +93,7 @@ describe('blocks', () => {
9293
it('Can return filtered aggregated data', async () => {
9394
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;')
9495
logger.info('Block number -', dbResp.rows[0].block_no)
95-
let fee = 10
96+
let fee = 0
9697
let query = gql`query {
9798
blocks( where: { number: { _eq: ${dbResp.rows[0].block_no} }}) {
9899
transactions_aggregate(

packages/api-cardano-db-hasura/test/cardano.query.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DocumentNode } from 'graphql'
44
import util from '@cardano-graphql/util'
55
import { TestClient } from '@cardano-graphql/util-dev'
66
import { init } from './util'
7-
import { Client } from 'pg'
7+
import { Client, QueryResult } from 'pg'
88
import Logger from 'bunyan'
99

1010
function loadQueryNode (name: string): Promise<DocumentNode> {
@@ -22,10 +22,16 @@ describe('cardano', () => {
2222
afterAll(async () => {
2323
await db.end()
2424
})
25+
const getTestData = async (sql: string) :Promise<QueryResult> => {
26+
const resp = await db.query(sql)
27+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
28+
expect(resp.rows.length).toBeGreaterThan(0)
29+
return resp
30+
}
2531

2632
it('Returns core information about the current state of the network', async () => {
27-
const dbTip = await db.query('SELECT max(block_no) AS block_no FROM block;')
28-
const dbEpoch = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
33+
const dbTip = await getTestData('SELECT max(block_no) AS block_no FROM block;')
34+
const dbEpoch = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
2935
logger.info('Tip - ', dbTip.rows[0].block_no, ' epoch - ', dbEpoch.rows[0].epoch_no)
3036
const result = await client.query({
3137
query: await loadQueryNode('chainTipAndCurrentEpochNumber')

packages/api-cardano-db-hasura/test/delegations.query.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { DocumentNode } from 'graphql'
55
import util from '@cardano-graphql/util'
66
import { TestClient } from '@cardano-graphql/util-dev'
77
import { init } from './util'
8-
import { Client } from 'pg'
8+
import { Client, QueryResult } from 'pg'
9+
import Logger from 'bunyan'
910

1011
function loadQueryNode (name: string): Promise<DocumentNode> {
1112
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'delegations'), name)
@@ -14,13 +15,20 @@ function loadQueryNode (name: string): Promise<DocumentNode> {
1415
describe('delegations', () => {
1516
let client: TestClient
1617
let db: Client
18+
let logger: Logger
1719
beforeAll(async () => {
18-
({ client, db } = await init('delegations'))
20+
({ client, db, logger } = await init('delegations'))
1921
await db.connect()
2022
})
2123
afterAll(async () => {
2224
await db.end()
2325
})
26+
const getTestData = async (sql: string) :Promise<QueryResult> => {
27+
const resp = await db.query(sql)
28+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
29+
expect(resp.rows.length).toBeGreaterThan(0)
30+
return resp
31+
}
2432

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

3745
it('can return aggregated data on all delegations', async () => {
38-
const dbResp = await db.query('SELECT COUNT(*) as count FROM delegation;')
46+
const dbResp = await getTestData('SELECT COUNT(*) as count FROM delegation;')
3947
const result = await client.query({
4048
query: await loadQueryNode('aggregateDelegation')
4149
})

packages/api-cardano-db-hasura/test/epochs.query.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
66
import { TestClient } from '@cardano-graphql/util-dev'
77
import { allFieldsPopulated, init } from './util'
88
import { Logger } from 'ts-log'
9-
import { Client } from 'pg'
9+
import { Client, QueryResult } from 'pg'
1010

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'epochs'), name)
@@ -23,9 +23,15 @@ describe('epochs', () => {
2323
afterAll(async () => {
2424
await db.end()
2525
})
26+
const getTestData = async (sql: string) :Promise<QueryResult> => {
27+
const resp = await db.query(sql)
28+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
29+
expect(resp.rows.length).toBeGreaterThan(0)
30+
return resp
31+
}
2632

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

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

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

6167
it('Can return filtered aggregated data', async () => {
62-
const dbEpoch = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
63-
const dbSlotLeader = await db.query('SELECT description FROM slot_leader WHERE slot_leader.description IS NOT NULL ORDER BY RANDOM() LIMIT 1;')
64-
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 + ';')
68+
const dbEpoch = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
69+
const dbSlotLeader = await getTestData('SELECT description FROM slot_leader WHERE slot_leader.description IS NOT NULL ORDER BY RANDOM() LIMIT 1;')
70+
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 + ';')
6571
logger.info('Epoch number -', dbEpoch.rows[0].epoch_no, ', slot leader - ', dbSlotLeader.rows[0].description)
6672
const result = await client.query({
6773
query: await loadQueryNode('numberOfBlocksProducedByLeaderInEpoch'),
@@ -72,7 +78,7 @@ describe('epochs', () => {
7278
})
7379

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

8490
it('Can return aggregated Epoch data', async () => {
85-
const dbResp = await db.query('SELECT max(epoch_no) AS epoch_no FROM block;')
91+
const dbResp = await getTestData('SELECT max(epoch_no) AS epoch_no FROM block;')
8692
logger.info('Epoch number -', dbResp.rows[0].epoch_no)
8793
const result = await client.query({
8894
query: await loadQueryNode('aggregateEpochData'),

packages/api-cardano-db-hasura/test/genesis.query.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ describe('genesis', () => {
1818
it('Returns key information about the network genesis', async () => {
1919
const query = { query: await loadQueryNode('keyNetworkInfo') }
2020
const result = await client.query(query)
21-
allFieldsPopulated(result.data)
21+
allFieldsPopulated(result.data, 'staking')
2222
})
2323
})

packages/api-cardano-db-hasura/test/paymentAddress.query.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ describe('paymentAddress', () => {
5959
const boundedAdaBalance = new BigNumber(
6060
boundedResult.data.paymentAddresses[0].summary?.assetBalances[0]?.quantity
6161
).toNumber() || 0
62-
expect(unboundedAdaBalance).toBeGreaterThan(boundedAdaBalance)
62+
expect(unboundedAdaBalance).toBeGreaterThanOrEqual(boundedAdaBalance)
6363
})
6464
})

packages/api-cardano-db-hasura/test/rewards.query.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
66
import { TestClient } from '@cardano-graphql/util-dev'
77
import { init } from './util'
88
import Logger from 'bunyan'
9-
import { Client } from 'pg'
9+
import { Client, QueryResult } from 'pg'
1010

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'rewards'), name)
@@ -23,9 +23,15 @@ describe('rewards', () => {
2323
afterAll(async () => {
2424
await db.end()
2525
})
26+
const getTestData = async (sql: string) :Promise<QueryResult> => {
27+
const resp = await db.query(sql)
28+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
29+
expect(resp.rows.length).toBeGreaterThan(0)
30+
return resp
31+
}
2632

2733
it('can return details for rewards scoped to an address', async () => {
28-
const dbResp = await db.query('SELECT view FROM reward JOIN stake_address sa ON reward.addr_id = sa.id ORDER BY RANDOM() LIMIT 1;')
34+
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;')
2935
const stakeAddress = dbResp.rows[0].view
3036
logger.info('Stake address - ' + stakeAddress)
3137
const result = await client.query({

packages/api-cardano-db-hasura/test/stakePool.query.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import util from '@cardano-graphql/util'
66
import { TestClient } from '@cardano-graphql/util-dev'
77
import { init } from './util'
88
import Logger from 'bunyan'
9-
import { Client } from 'pg'
9+
import { Client, QueryResult } from 'pg'
1010

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'stake_pools'), name)
@@ -23,9 +23,15 @@ describe('stakePools', () => {
2323
afterAll(async () => {
2424
await db.end()
2525
})
26+
const getTestData = async (sql: string) :Promise<QueryResult> => {
27+
const resp = await db.query(sql)
28+
if (resp.rows.length === 0) logger.error('Can not find suitable data in db')
29+
expect(resp.rows.length).toBeGreaterThan(0)
30+
return resp
31+
}
2632

2733
it('can lookup stake pools by ID', async () => {
28-
const dbResp = await db.query('SELECT view FROM pool_hash ORDER BY RANDOM() LIMIT 1;')
34+
const dbResp = await getTestData('SELECT view FROM pool_hash ORDER BY RANDOM() LIMIT 1;')
2935
const poolId = dbResp.rows[0].view
3036
logger.info('Stake pool id - ' + poolId)
3137
const result = await client.query({

0 commit comments

Comments
 (0)