Skip to content

Commit

Permalink
Implement more e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Nov 9, 2021
1 parent 62ff641 commit 25d3e8e
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions packages/airnode-adapter/e2e/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from 'hardhat';
import { extractAndEncodeResponse } from '../src';
import type { Contract } from 'ethers';

// Chai is able to assert that "expect(BigNumber).to.equal(string)"" but fails to assert
// Chai is able to assert that "expect(BigNumber).to.equal(string)" but fails to assert
// the values if wrapped in array "expect(BigNumber[]).to.equal(string[])"
function assertArrayEquals<T = unknown>(actual: T, expected: T) {
if (!Array.isArray(actual)) {
Expand Down Expand Up @@ -126,6 +126,14 @@ describe('Extraction, encoding and simple on chain decoding', () => {
});
});

it('floors the number after multiplying it', async () => {
expect(
await testDecoder.decodeSignedInt256(
extractAndEncodeResponse(apiResponse, { _type: 'int256', _times: '100', _path: 'float' }).encodedValue
)
).to.equal(1234567);
});

it('decodes bool encoded by the adapter package', async () => {
expect(
await testDecoder.decodeBool(
Expand Down Expand Up @@ -191,6 +199,16 @@ describe('Extraction, encoding and simple on chain decoding', () => {
);
});

it('1 dimension fixed length with _times parameter', async () => {
assertArrayEquals(
await testDecoder.decode1DFixedArray(
extractAndEncodeResponse(apiResponse, { _type: 'int256[2]', _path: 'array.int256', _times: '1000' })
.encodedValue
),
[123000, 456000]
);
});

it('mixed fixes/unlimited sized arrays', async () => {
// Solidity arrays are specified "backwards". See https://ethereum.stackexchange.com/a/129
const encodedBytes = extractAndEncodeResponse(apiResponse, {
Expand All @@ -206,7 +224,7 @@ describe('Extraction, encoding and simple on chain decoding', () => {
describe('Failures', () => {
it('throws on invalid type', () => {
// 'true' is not a valid _type, 'bool' should be used
expect(() => extractAndEncodeResponse(apiResponse.boolTrue, { _type: 'true' }).encodedValue).to.Throw(
expect(() => extractAndEncodeResponse(apiResponse, { _type: 'true', _path: 'boolTrue' }).encodedValue).to.Throw(
'Invalid type: true'
);
});
Expand All @@ -223,7 +241,14 @@ describe('Extraction, encoding and simple on chain decoding', () => {
}
});

// TODO: _times not big enough
// TODO: strange.key test
it('throws on invalid path', () => {
const dynamicKey = 'strange.key';

expect(
() =>
extractAndEncodeResponse(apiResponse, { _type: 'int256', _times: '100', _path: `json.${dynamicKey}` })
.encodedValue
).to.Throw("Unable to find value from path: 'json.strange.key'");
});
});
});

0 comments on commit 25d3e8e

Please sign in to comment.