Skip to content

Commit

Permalink
test: hoisted logic from getTestData
Browse files Browse the repository at this point in the history
  • Loading branch information
iadmytro committed Feb 14, 2023
1 parent d246ff6 commit b8bd71d
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 90 deletions.
13 changes: 3 additions & 10 deletions packages/api-cardano-db-hasura/test/activeStake.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'active_stake'), name)
Expand All @@ -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<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
}
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;')
Expand Down
13 changes: 4 additions & 9 deletions packages/api-cardano-db-hasura/test/assets.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'assets'), name)
Expand All @@ -23,12 +23,7 @@ describe('assets', () => {
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
}
const getTestData = async (sql: string) => queryDB(db, logger, sql)

it('can return information on assets', async () => {
const result = await client.query({
Expand Down
17 changes: 5 additions & 12 deletions packages/api-cardano-db-hasura/test/blocks.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'blocks'), name)
Expand All @@ -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<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
}
const getTestData = async (sql: string) => queryDB(db, logger, sql)

it('caps the response to 100 blocks', async () => {
const result = await client.query({
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 3 additions & 8 deletions packages/api-cardano-db-hasura/test/cardano.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
Expand All @@ -22,12 +22,7 @@ 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
}
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;')
Expand Down
11 changes: 3 additions & 8 deletions packages/api-cardano-db-hasura/test/delegations.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
Expand All @@ -23,12 +23,7 @@ describe('delegations', () => {
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
}
const getTestData = async (sql: string) => queryDB(db, logger, sql)

it('can return details for stake delegation', async () => {
const result = await client.query({
Expand Down
13 changes: 4 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 @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'epochs'), name)
Expand All @@ -23,12 +23,7 @@ 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
}
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);')
Expand Down
11 changes: 3 additions & 8 deletions packages/api-cardano-db-hasura/test/rewards.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'rewards'), name)
Expand All @@ -23,12 +23,7 @@ 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
}
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;')
Expand Down
11 changes: 3 additions & 8 deletions packages/api-cardano-db-hasura/test/stakePool.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'stake_pools'), name)
Expand All @@ -23,12 +23,7 @@ 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
}
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;')
Expand Down
11 changes: 3 additions & 8 deletions packages/api-cardano-db-hasura/test/tokenMints.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'token_mints'), name)
Expand All @@ -23,12 +23,7 @@ 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
}
const getTestData = async (sql: string) => queryDB(db, logger, sql)

it('can return information on token minting and burning', async () => {
const result = await client.query({
Expand Down
11 changes: 3 additions & 8 deletions packages/api-cardano-db-hasura/test/transactions.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'transactions'), name)
Expand All @@ -23,12 +23,7 @@ describe('transactions', () => {
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
}
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;')
Expand Down
11 changes: 9 additions & 2 deletions packages/api-cardano-db-hasura/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,6 +48,13 @@ export async function init (name: string) {
}
}

export const queryDB = async (db: Client, logger: Logger, 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
}

export const allFieldsPopulated = (obj: any, except : any = []) => {
expect(obj).toBeDefined()
if (Array.isArray(obj)) {
Expand Down

0 comments on commit b8bd71d

Please sign in to comment.