Skip to content

Commit

Permalink
test: aggregator test
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Dec 8, 2023
1 parent cb1850a commit 094372d
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 26 deletions.
42 changes: 29 additions & 13 deletions cli/test/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { describe, expect, test } from '@jest/globals'
import { hashHandler, insertHandler, listHandler, removeHandler } from '../src/adapter'
import { insertHandler, listHandler, removeHandler } from '../src/adapter'

describe('CLI Adapter', function () {
const ADAPTER = {
const ADAPTER_0 = {
active: true,
name: 'X-Y',
decimals: 8,
adapterHash: '0x020e150749af3bffaec9ae337da0b9b00c3cfe0b46b854a8e2f5922f6ba2c5db',
feeds: [
{
name: 'data-X-Y',
Expand All @@ -23,17 +24,32 @@ describe('CLI Adapter', function () {
]
}

const ADAPTER_1 = {
active: true,
name: 'Z-X',
decimals: 8,
adapterHash: '0x12da2f5119ba624ed025303b424d637349c0d120d02bd66a9cfff57e98463a81',
feeds: [
{
name: 'data-Z-X',
definition: {
url: 'https://data.com',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
reducers: [
{ function: 'PARSE', args: ['PRICE'] },
{ function: 'POW10', args: '8' },
{ function: 'ROUND' }
]
}
}
]
}

let initalAdapterId
beforeAll(async () => {
// setup hash
const initAdapter = { ...ADAPTER, name: 'Z-X' }
initAdapter['adapterHash'] = (
await hashHandler()({ data: initAdapter, verify: false })
).adapterHash
ADAPTER['adapterHash'] = (await hashHandler()({ data: ADAPTER, verify: false })).adapterHash

// insert default adapter
const insertResult = await insertHandler()({ data: initAdapter })
const insertResult = await insertHandler()({ data: ADAPTER_0 })
initalAdapterId = insertResult.id
})

Expand All @@ -51,14 +67,14 @@ describe('CLI Adapter', function () {

test('Should insert new adapter', async function () {
const adapterBefore = await listHandler()()
await insertHandler()({ data: ADAPTER })
await insertHandler()({ data: ADAPTER_1 })
const adapterAfter = await listHandler()()
expect(adapterAfter.length).toEqual(adapterBefore.length + 1)
})

test('Should not allow to insert the same adapter more than once', async function () {
await insertHandler()({ data: ADAPTER })
const msg = await insertHandler()({ data: ADAPTER })
await insertHandler()({ data: ADAPTER_1 })
const msg = await insertHandler()({ data: ADAPTER_1 })
expect(msg).toEqual('Unique constraint failed on the adapter_hash')
})

Expand Down
115 changes: 102 additions & 13 deletions cli/test/aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,128 @@
import { describe, expect, test } from '@jest/globals'
import {
insertHandler as adapterInsertHandler,
listHandler as adapterListHandler,
removeHandler as adapterRemoveHandler
} from '../src/adapter'
import { insertHandler, listHandler, removeHandler } from '../src/aggregator'
import {
insertHandler as chainInsertHandler,
listHandler as chainListHandler,
removeHandler as chainRemoveHandler
} from '../src/chain'

describe('CLI Aggregator', function () {
const AGGREGATOR = {
const ADAPTER_0 = {
active: true,
name: 'X-Y',
decimals: 8,
adapterHash: '0x020e150749af3bffaec9ae337da0b9b00c3cfe0b46b854a8e2f5922f6ba2c5db',
feeds: [
{
name: 'data-X-Y',
definition: {
url: 'https://data.com',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
reducers: [
{ function: 'PARSE', args: ['PRICE'] },
{ function: 'POW10', args: '8' },
{ function: 'ROUND' }
]
}
}
]
}

const ADAPTER_1 = {
active: true,
name: 'Z-X',
decimals: 8,
adapterHash: '0x12da2f5119ba624ed025303b424d637349c0d120d02bd66a9cfff57e98463a81',
feeds: [
{
name: 'data-Z-X',
definition: {
url: 'https://data.com',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
reducers: [
{ function: 'PARSE', args: ['PRICE'] },
{ function: 'POW10', args: '8' },
{ function: 'ROUND' }
]
}
}
]
}

const AGGREGATOR_0 = {
name: 'X-Y',
aggregatorHash: '0x5bcc6c18d584dc54a666f9212229226f02f65b8dcda3ed72836b6c901f2d18e1',
address: '0x0000000000000000000000000000000000000000',
fixedHeartbeatRate: 15_000,
heartbeat: 15000,
threshold: 0.05,
absoluteThreshold: 0.1,
adapterHash: '0x020e150749af3bffaec9ae337da0b9b00c3cfe0b46b854a8e2f5922f6ba2c5db'
}

const AGGREGATOR_1 = {
name: 'Z-X',
aggregatorHash: '0x11ca65b539221125a64b38653f65dbbf961ed2ea16bcaf54408a5d2ebdc13a0b',
address: '0x0000000000000000000000000000000000000001',
heartbeat: 15000,
threshold: 0.05,
absoluteThreshold: 0.1,
adapterId: '0x00d5130063bee77302b133b5c6a0d6aede467a599d251aec842d24abeb5866a5'
adapterHash: '0x12da2f5119ba624ed025303b424d637349c0d120d02bd66a9cfff57e98463a81'
}

test.skip('Should list Aggregators', async function () {
let initialAggregatorId
beforeAll(async () => {
await chainInsertHandler()({ name: 'localhost' })
await adapterInsertHandler()({ data: ADAPTER_0 })
await adapterInsertHandler()({ data: ADAPTER_1 })

const insertResult = await insertHandler()({ data: AGGREGATOR_0, chain: 'localhost' })
initialAggregatorId = insertResult.id
})

afterAll(async () => {
const aggregators = await listHandler()({})
for (const aggregator of aggregators) {
await removeHandler()({ id: aggregator.id })
}
const adapters = await adapterListHandler()()
for (const adapter of adapters) {
await adapterRemoveHandler()({ id: adapter.id })
}
const chains = await chainListHandler()()
for (const chain of chains) {
await chainRemoveHandler()({ id: chain.id })
}
})

test('Should list Aggregators', async function () {
const aggregator = await listHandler()({})
expect(aggregator.length).toBeGreaterThan(0)
})

test.skip('Should insert new aggregator', async function () {
test('Should insert new aggregator', async function () {
const aggregatorBefore = await listHandler()({})
await insertHandler()({ data: AGGREGATOR, chain: 'localhost' })
await insertHandler()({ data: AGGREGATOR_1, chain: 'localhost' })
const aggregatorAfter = await listHandler()({})
expect(aggregatorAfter.length).toEqual(aggregatorBefore.length + 1)
})

test.skip('Should not allow to insert the same aggregator more than once', async function () {
await insertHandler()({ data: AGGREGATOR, chain: 'localhost' })
await expect(async () => {
await insertHandler()({ data: AGGREGATOR, chain: 'localhost' })
}).rejects.toThrow()
test('Should not allow to insert the same aggregator more than once', async function () {
await insertHandler()({ data: AGGREGATOR_1, chain: 'localhost' })

const msg = await insertHandler()({ data: AGGREGATOR_1, chain: 'localhost' })
expect(msg).toEqual('Unique constraint failed on the address')
})

test.skip('Should delete aggregator based on id', async function () {
test('Should delete aggregator based on id', async function () {
const aggregatorBefore = await listHandler()({})
await removeHandler()({ id: 1 })
await removeHandler()({ id: initialAggregatorId })
const aggregatorAfter = await listHandler()({})
expect(aggregatorAfter.length).toEqual(aggregatorBefore.length - 1)
})
Expand Down

0 comments on commit 094372d

Please sign in to comment.