Skip to content

Commit b8bd71d

Browse files
committed
test: hoisted logic from getTestData
1 parent d246ff6 commit b8bd71d

11 files changed

+43
-90
lines changed

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'active_stake'), name)
@@ -22,17 +22,10 @@ describe('activeStake', () => {
2222
({ client, db, logger } = await init('activeStake'))
2323
await db.connect()
2424
})
25-
2625
afterAll(async () => {
2726
await db.end()
2827
})
29-
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-
}
28+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3629

3730
it('can return active stake snapshots for an address', async () => {
3831
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;')

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'assets'), name)
@@ -23,12 +23,7 @@ describe('assets', () => {
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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3227

3328
it('can return information on assets', async () => {
3429
const result = await client.query({

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { DocumentNode } from 'graphql'
33
import gql from 'graphql-tag'
44
import util from '@cardano-graphql/util'
55
import { TestClient } from '@cardano-graphql/util-dev'
6-
import { allFieldsPopulated, init } from './util'
7-
import { Logger } from 'ts-log'
8-
import { Client, QueryResult } from 'pg'
6+
import { allFieldsPopulated, init, queryDB } from './util'
7+
import { Client } from 'pg'
8+
import Logger from 'bunyan'
99

1010
function loadQueryNode (name: string): Promise<DocumentNode> {
1111
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'blocks'), name)
@@ -20,17 +20,10 @@ describe('blocks', () => {
2020
({ client, db, logger } = await init('blocks'))
2121
await db.connect()
2222
})
23-
2423
afterAll(async () => {
2524
await db.end()
2625
})
27-
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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3427

3528
it('caps the response to 100 blocks', async () => {
3629
const result = await client.query({
@@ -91,7 +84,7 @@ describe('blocks', () => {
9184
})
9285

9386
it('Can return filtered aggregated data', async () => {
94-
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;')
87+
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;')
9588
logger.info('Block number -', dbResp.rows[0].block_no)
9689
let fee = 0
9790
let query = gql`query {

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

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

1010
function loadQueryNode (name: string): Promise<DocumentNode> {
@@ -22,12 +22,7 @@ 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-
}
25+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3126

3227
it('Returns core information about the current state of the network', async () => {
3328
const dbTip = await getTestData('SELECT max(block_no) AS block_no FROM block;')

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
@@ -23,12 +23,7 @@ describe('delegations', () => {
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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3227

3328
it('can return details for stake delegation', async () => {
3429
const result = await client.query({

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'epochs'), name)
@@ -23,12 +23,7 @@ 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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3227

3328
it('Returns epoch details by number', async () => {
3429
const dbResp = await getTestData('SELECT no, out_sum FROM epoch WHERE no = (SELECT max(no) FROM epoch);')

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'rewards'), name)
@@ -23,12 +23,7 @@ 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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3227

3328
it('can return details for rewards scoped to an address', async () => {
3429
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;')

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'stake_pools'), name)
@@ -23,12 +23,7 @@ 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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3227

3328
it('can lookup stake pools by ID', async () => {
3429
const dbResp = await getTestData('SELECT view FROM pool_hash ORDER BY RANDOM() LIMIT 1;')

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'token_mints'), name)
@@ -23,12 +23,7 @@ describe('tokenMints', () => {
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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3227

3328
it('can return information on token minting and burning', async () => {
3429
const result = await client.query({

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

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

1111
function loadQueryNode (name: string): Promise<DocumentNode> {
1212
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'transactions'), name)
@@ -23,12 +23,7 @@ describe('transactions', () => {
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-
}
26+
const getTestData = async (sql: string) => queryDB(db, logger, sql)
3227

3328
it('Returns transactions by hashes', async () => {
3429
const dbResp = await getTestData('SELECT hash FROM tx ORDER BY RANDOM() LIMIT 2;')

0 commit comments

Comments
 (0)