Skip to content

Commit

Permalink
Add data parity tests for epoch details (#177)
Browse files Browse the repository at this point in the history
* Add data parity tests for epoch details

* Refactor transactions data parity tests

Reduce number of API calls, change array to set as the different order of
elements in array for rest vs graphql response caused one test to fail.

Co-authored-by: Rhys Bartels-Waller <rhys.bartelswaller@iohk.io>
  • Loading branch information
ArturWieczorek and rhyslbw authored May 25, 2020
1 parent a2713ce commit 1ec280c
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 274 deletions.
17 changes: 6 additions & 11 deletions src/__test__/dataparitytests/blocks.parity.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import gql from 'graphql-tag'
import { TestClient } from '../TestClient'
import fetch from 'node-fetch'

async function getDataFromAPI (path: string) {
const response = await fetch(`https://explorer.cardano.org/api/${path}`)
return response.json()
}
import { getDataFromAPI } from '../util'

export function blocksTests (createClient: () => Promise<TestClient>) {
describe('blocks', () => {
describe('blocks ', () => {
let client: TestClient

beforeEach(async () => {
client = await createClient()
}, 60000)

it('returns the same height', async () => {
it('return the same height', async () => {
const restResult = await getDataFromAPI('blocks/pages')
const graphQLResult = await client.query({
query: gql`query Blockheight {
cardano {
blockHeight
}
}
}`
})

const restResultBlockHeight = restResult['Right'][1][0]['cbeBlkHeight']
const graphQLBlockHeight = graphQLResult['data']['cardano']['blockHeight']
const restResultBlockHeight = restResult.Right[1][0].cbeBlkHeight
const graphQLBlockHeight = graphQLResult.data.cardano.blockHeight

// As we're calling an external API to check equality on something that changes every 20 seconds
// there is a small delta in the test condition to allow for this where the second API value can be
Expand Down
122 changes: 122 additions & 0 deletions src/__test__/dataparitytests/epochs.parity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import gql from 'graphql-tag'
import { TestClient } from '../TestClient'
import { getDataFromAPI, timestampToIsoStringWithoutTimezone } from '../util'

export function epochsTests (createClient: () => Promise<TestClient>) {
describe('epochs ', () => {
let client: TestClient
let restData: any
let graphQLData: any
const epoch = 111
const slot = 3313

beforeAll(async () => {
client = await createClient()
const restResult = await getDataFromAPI(`epochs/${epoch}/${slot}`)
restData = restResult.Right[0]
const graphQLResult = await client.query({
query: gql`query EpochDetails{
epochs(where: {number:{_eq:${epoch}}}){
number
startedAt
lastBlockTime
blocks (where: {slotWithinEpoch:{_eq:${slot}}}){
slotNo
epochNo
slotWithinEpoch
number
hash
createdAt
transactionsCount
transactions{
totalOutput
}
size
createdBy
fees
}
output
}
}`
})
graphQLData = graphQLResult.data.epochs[0].blocks[0]
}, 30000)

it('return the same epoch number', async () => {
const restResultEpochNumber = restData.cbeEpoch
const graphQLEpochNumber = graphQLData.epochNo

expect(restResultEpochNumber).toEqual(graphQLEpochNumber)
})

it('return the same slot number', async () => {
const restResultSlotNumber = restData.cbeSlot
const graphQLSlotNumber = graphQLData.slotWithinEpoch

expect(restResultSlotNumber).toEqual(graphQLSlotNumber)
})

it('return the same block height', async () => {
const restResultBlockHeight = restData.cbeBlkHeight
const graphQLBlockHeight = graphQLData.number

expect(restResultBlockHeight).toEqual(graphQLBlockHeight)
})

it('return the same block hash', async () => {
const restResultBlockHash = restData.cbeBlkHash
const graphQLBlockHash = graphQLData.id

expect(restResultBlockHash).toEqual(graphQLBlockHash)
})

it('return the same block creation time', async () => {
const restResultBlockCreationUnixEpochTime = restData.cbeTimeIssued
const graphQLBlockCreationDateTime = graphQLData.createdAt
const restResultBlockCreationDateTime = timestampToIsoStringWithoutTimezone(restResultBlockCreationUnixEpochTime)

expect(restResultBlockCreationDateTime).toEqual(graphQLBlockCreationDateTime)
})

it('return the same transactions count', async () => {
const restResultTxCount = restData.cbeTxNum
const graphQLTxCount = parseInt(graphQLData.transactionsCount)

expect(restResultTxCount).toEqual(graphQLTxCount)
})

it('return the same total output', async () => {
const restResultTotalSent = parseInt(restData.cbeTotalSent.getCoin)
let graphQLTotalSent = 0

graphQLData.transactions.forEach(
(tx: any) => {
graphQLTotalSent += parseInt(tx.totalOutput)
}
)

expect(restResultTotalSent).toEqual(graphQLTotalSent)
})

it('return the same block size', async () => {
const restResultBlockSize = restData.cbeSize
const graphQLBlockSize = graphQLData.size

expect(restResultBlockSize).toEqual(graphQLBlockSize)
})

it('return the same block leader', async () => {
const restResultBlockLeader = restData.cbeBlockLead
const graphQLBlockLeader = graphQLData.createdBy.split('-')[1]

expect(restResultBlockLeader).toMatch(graphQLBlockLeader)
})

it('return the same fee', async () => {
const restResultFee = parseInt(restData.cbeFees.getCoin)
const graphQLFee = graphQLData.fees

expect(restResultFee).toEqual(graphQLFee)
})
})
}
1 change: 1 addition & 0 deletions src/__test__/dataparitytests/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './blocks.parity.test'
export * from './transactions.parity.test'
export * from './epochs.parity.test'
Loading

0 comments on commit 1ec280c

Please sign in to comment.