test: write tests for sugar functions (#1631)

* test: write tests for sugar functions
This commit is contained in:
Mukul Jangid
2021-09-23 16:04:58 -04:00
committed by Mayukha Vadari
parent 98c9b9bc14
commit f2216446e5
8 changed files with 275 additions and 338 deletions

View File

@@ -41,6 +41,8 @@ async function getBalances(
options: GetBalancesOptions = {},
): Promise<Balance[]> {
// 1. Get XRP Balance
const xrpBalance: Balance[] = []
if (!options.peer) {
const xrpRequest: AccountInfoRequest = {
command: 'account_info',
account,
@@ -50,7 +52,8 @@ async function getBalances(
const balance = await this.request(xrpRequest).then(
(response) => response.result.account_data.Balance,
)
const xrpBalance = { currency: 'XRP', value: dropsToXrp(balance) }
xrpBalance.push({ currency: 'XRP', value: dropsToXrp(balance) })
}
// 2. Get Non-XRP Balance
const linesRequest: AccountLinesRequest = {
command: 'account_lines',
@@ -64,7 +67,7 @@ async function getBalances(
const accountLinesBalance = _.flatMap(responses, (response) =>
formatBalances(response.result.lines),
)
return [xrpBalance, ...accountLinesBalance]
return [...xrpBalance, ...accountLinesBalance].slice(0, options.limit)
}
export default getBalances

View File

@@ -1,3 +1,4 @@
/* eslint-disable max-lines-per-function -- Needs to process orderbooks. */
import BigNumber from 'bignumber.js'
import _ from 'lodash'
@@ -53,7 +54,7 @@ async function getOrderbook(
command: 'book_offers',
taker_pays: takerPays,
taker_gets: takerGets,
ledger_index: options.ledger_index ?? 'validated',
ledger_index: options.ledger_index,
ledger_hash: options.ledger_hash,
limit: options.limit ?? DEFAULT_LIMIT,
taker: options.taker,
@@ -73,12 +74,8 @@ async function getOrderbook(
(reverseOfferResult) => reverseOfferResult.result.offers,
)
// Sort the orders
// for both buys and sells, lowest quality is closest to mid-market
// we sort the orders so that earlier orders are closer to mid-market
const orders = [...directOffers, ...reverseOffers]
// separate out the orders amongst buy and sell
// separate out the buy and sell orders
const buy: BookOffer[] = []
const sell: BookOffer[] = []
orders.forEach((order) => {
@@ -89,7 +86,13 @@ async function getOrderbook(
sell.push(order)
}
})
return { buy: sortOffers(buy), sell: sortOffers(sell) }
// Sort the orders
// for both buys and sells, lowest quality is closest to mid-market
// we sort the orders so that earlier orders are closer to mid-market
return {
buy: sortOffers(buy).slice(0, options.limit),
sell: sortOffers(sell).slice(0, options.limit),
}
}
export default getOrderbook

View File

@@ -29,86 +29,75 @@ describe('getBalances', function () {
assertResultMatch(result, responses.getBalances, 'getBalances')
})
// it('getBalances - limit', async function () {
// const options = { limit: 3, ledgerVersion: 123456 }
// this.mockRippled.addResponse(
// 'account_info',
// rippled.account_info.normal,
// )
// this.mockRippled.addResponse(
// 'account_lines',
// rippledAccountLines.normal,
// )
// this.mockRippled.addResponse('ledger', rippled.ledger.normal)
// const expectedResponse = responses.getBalances.slice(0, 3)
// const result = await this.client.getBalances(testcase.address, options)
// assertResultMatch(result, expectedResponse, 'getBalances')
// })
it('getBalances - limit', async function () {
const request = {
account: testcase.address,
options: {
limit: 10,
},
}
this.mockRippled.addResponse(
'account_info',
rippled.account_info.normal,
)
this.mockRippled.addResponse(
'account_lines',
rippledAccountLines.normal,
)
this.mockRippled.addResponse('ledger', rippled.ledger.normal)
const expectedResponse = responses.getBalances.slice(
0,
request.options.limit,
)
const result = await this.client.getBalances(
request.account,
request.options,
)
assertResultMatch(result, expectedResponse, 'getBalances')
})
// it('getBalances - limit & currency', async function () {
// const options = { currency: 'USD', limit: 3 }
// this.mockRippled.addResponse(
// 'account_info',
// rippled.account_info.normal,
// )
// this.mockRippled.addResponse(
// 'account_lines',
// rippledAccountLines.normal,
// )
// this.mockRippled.addResponse('ledger', rippled.ledger.normal)
// const expectedResponse = responses.getBalances
// .filter((item) => item.currency === 'USD')
// .slice(0, 3)
// const result = await this.client.getBalances(testcase.address, options)
// assertResultMatch(result, expectedResponse, 'getBalances')
// })
it('getBalances - peer', async function () {
const options = {
peer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
}
this.mockRippled.addResponse(
'account_info',
rippled.account_info.normal,
)
this.mockRippled.addResponse(
'account_lines',
rippledAccountLines.normal,
)
this.mockRippled.addResponse('ledger', rippled.ledger.normal)
// it("getBalances - limit & currency", async function () {
// const options = { currency: "USD", limit: 3 };
// this.mockRippled.addResponse(
// "account_info",
// rippled.account_info.normal
// );
// this.mockRippled.addResponse(
// "account_lines",
// rippledAccountLines.normal
// );
// this.mockRippled.addResponse("ledger", rippled.ledger.normal);
// const expectedResponse = responses.getBalances
// .filter((item) => item.currency === "USD")
// .slice(0, 3);
// const result = await this.client.getBalances(test.address, options);
// console.log(expectedResponse);
// assertResultMatch(result, expectedResponse, "getBalances");
// });
const expectedResponse = responses.getBalances.filter(
(item) => item.issuer === options.peer,
)
const result = await this.client.getBalances(testcase.address, options)
assertResultMatch(result, expectedResponse, 'getBalances')
})
// it("getBalances - limit & currency & issuer", async function () {
// const options = {
// currency: "USD",
// issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
// limit: 3,
// };
// this.mockRippled.addResponse(
// "account_info",
// rippled.account_info.normal
// );
// this.mockRippled.addResponse(
// "account_lines",
// rippledAccountLines.normal
// );
// this.mockRippled.addResponse("ledger", rippled.ledger.normal);
it('getBalances - limit & peer', async function () {
const options = {
peer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
limit: 10,
}
this.mockRippled.addResponse(
'account_info',
rippled.account_info.normal,
)
this.mockRippled.addResponse(
'account_lines',
rippledAccountLines.normal,
)
this.mockRippled.addResponse('ledger', rippled.ledger.normal)
// const expectedResponse = responses.getBalances
// .filter(
// (item) =>
// item.currency === "USD" &&
// item.issuer === "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
// )
// .slice(0, 3);
// const result = await this.client.getBalances(test.address, options);
// console.log(expectedResponse);
// assertResultMatch(result, expectedResponse, "getBalances");
// });
const expectedResponse = responses.getBalances
.filter((item) => item.issuer === options.peer)
.slice(0, options.limit)
const result = await this.client.getBalances(testcase.address, options)
assertResultMatch(result, expectedResponse, 'getBalances')
})
})
})
})

View File

@@ -1,45 +1,26 @@
// import BigNumber from "bignumber.js";
// import { assert } from "chai";
import BigNumber from 'bignumber.js'
import { assert } from 'chai'
import { BookOffersRequest } from '../../src'
import { OfferLedgerFlags } from '../../src/models/ledger/offer'
import requests from '../fixtures/requests'
import responses from '../fixtures/responses'
import rippled from '../fixtures/rippled'
import { setupClient, teardownClient } from '../setupClient'
import { addressTests } from '../testUtils'
import { assertResultMatch, assertRejects } from '../testUtils'
// function checkSortingOfOrders(orders) {
// let previousRate = "0";
// for (let i = 0; i < orders.length; i++) {
// const order = orders[i];
// let rate;
// // We calculate the quality of output/input here as a test.
// // This won't hold in general because when output and input amounts get tiny,
// // the quality can differ significantly. However, the offer stays in the
// // order book where it was originally placed. It would be more consistent
// // to check the quality from the offer book, but for the test data set,
// // this calculation holds.
// if (order.specification.direction === "buy") {
// rate = new BigNumber(order.specification.quantity.value)
// .dividedBy(order.specification.totalPrice.value)
// .toString();
// } else {
// rate = new BigNumber(order.specification.totalPrice.value)
// .dividedBy(order.specification.quantity.value)
// .toString();
// }
// assert(
// new BigNumber(rate).isGreaterThanOrEqualTo(previousRate),
// `Rates must be sorted from least to greatest: ${rate} should be >= ${previousRate}`
// );
// previousRate = rate;
// }
// return true;
// }
function checkSortingOfOrders(orders): void {
let previousRate = '0'
for (const order of orders) {
assert(
new BigNumber(order.quality).isGreaterThanOrEqualTo(previousRate),
`Rates must be sorted from least to greatest: ${
order.quality as number
} should be >= ${previousRate}`,
)
previousRate = order.quality
}
}
function isUSD(currency: string): boolean {
return (
@@ -73,142 +54,141 @@ function normalRippledResponse(
throw new Error('unexpected end')
}
// function xrpRippledResponse(
// request: BookOffersRequest,
// ): Record<string, unknown> {
// if (request.taker_pays.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
// return rippled.book_offers.xrp_usd
// }
// if (request.taker_gets.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
// return rippled.book_offers.usd_xrp
// }
// throw new Error('unexpected end')
// }
function xrpRippledResponse(
request: BookOffersRequest,
): Record<string, unknown> {
if (request.taker_pays.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
return rippled.book_offers.xrp_usd
}
if (request.taker_gets.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
return rippled.book_offers.usd_xrp
}
throw new Error('unexpected end')
}
describe('client.getOrderbook', function () {
beforeEach(setupClient)
afterEach(teardownClient)
addressTests.forEach(function (testcase) {
describe(testcase.type, function () {
it('normal', async function () {
this.mockRippled.addResponse('book_offers', normalRippledResponse)
const request = {
takerPays: requests.getOrderbook.normal.takerPays,
takerGets: requests.getOrderbook.normal.takerGets,
options: {
limit: 1,
},
}
const response = await this.client.getOrderbook(
requests.getOrderbook.normal.taker_pays,
requests.getOrderbook.normal.taker_gets,
{ limit: 1 },
request.takerPays,
request.takerGets,
request.options,
)
assert.deepEqual(response, responses.getOrderbook.normal)
const expectedResponse = {
buy: responses.getOrderbook.normal.buy.slice(0, request.options.limit),
sell: responses.getOrderbook.normal.sell.slice(0, request.options.limit),
}
assertResultMatch(response, expectedResponse, 'getOrderbook')
})
// it('invalid options', async function () {
// this.mockRippled.addResponse('book_offers', normalRippledResponse)
// assertRejects(
// this.client.getOrderbook(
// testcase.address,
// requests.getOrderbook.normal,
// {
// invalid: 'options',
// },
// ),
// this.client.errors.ValidationError,
// )
// })
// it('with XRP', async function () {
// this.mockRippled.addResponse('book_offers', xrpRippledResponse)
// const response = await this.client.getOrderbook(
// testcase.address,
// requests.getOrderbook.withXRP,
// )
// assertResultMatch(
// response,
// responses.getOrderbook.withXRP,
// 'getOrderbook',
// )
// })
// 'sample XRP/JPY book has orders sorted correctly', async function () {
// const orderbookInfo = {
// base: {
// // the first currency in pair
// currency: 'XRP'
// },
// counter: {
// currency: 'JPY',
// counterparty: 'rB3gZey7VWHYRqJHLoHDEJXJ2pEPNieKiS'
// }
// }
// const myAddress = 'rE9qNjzJXpiUbVomdv7R4xhrXVeH2oVmGR'
// const response = await this.client.getOrderbook(myAddress, orderbookInfo)
// assert.deepStrictEqual([], response.bids)
// checkSortingOfOrders(response.asks)
// },
// 'sample USD/XRP book has orders sorted correctly', async function () {
// const orderbookInfo = {
// counter: {currency: 'XRP'},
// base: {
// currency: 'USD',
// counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
// }
// }
// const myAddress = 'rE9qNjzJXpiUbVomdv7R4xhrXVeH2oVmGR'
// const response = await this.client.getOrderbook(myAddress, orderbookInfo)
// checkSortingOfOrders(response.bids)
// checkSortingOfOrders(response.asks)
// },
// WARNING: This test fails to catch the sorting bug, issue #766
// it("sorted so that best deals come first [bad test]", async function () {
// this.mockRippled.addResponse("book_offers", normalRippledResponse);
// const response = await this.client.getOrderbook(
// test.address,
// requests.getOrderbook.normal
// );
// const bidRates = response.bids.map(
// (bid) => bid.properties.makerExchangeRate
// );
// const askRates = response.asks.map(
// (ask) => ask.properties.makerExchangeRate
// );
// // makerExchangeRate = quality = takerPays.value/takerGets.value
// // so the best deal for the taker is the lowest makerExchangeRate
// // bids and asks should be sorted so that the best deals come first
// assert.deepEqual(
// bidRates.sort((x) => Number(x)),
// bidRates
// );
// assert.deepEqual(
// askRates.sort((x) => Number(x)),
// askRates
// );
// });
// it("currency & counterparty are correct", async function () {
// this.mockRippled.addResponse("book_offers", normalRippledResponse);
// const response = await this.client.getOrderbook(
// test.address,
// requests.getOrderbook.normal
// );
// [...response.bids, ...response.asks].forEach((order) => {
// const quantity = order.specification.quantity;
// const totalPrice = order.specification.totalPrice;
// const { base, counter } = requests.getOrderbook.normal;
// assert.strictEqual(quantity.currency, base.currency);
// assert.strictEqual(quantity.counterparty, base.counterparty);
// assert.strictEqual(totalPrice.currency, counter.currency);
// assert.strictEqual(totalPrice.counterparty, counter.counterparty);
// });
// });
// it("direction is correct for bids and asks", async function () {
// this.mockRippled.addResponse("book_offers", normalRippledResponse);
// const response = await this.client.getOrderbook(
// test.address,
// requests.getOrderbook.normal
// );
// assert(
// response.bids.every((bid) => bid.specification.direction === "buy")
// );
// assert(
// response.asks.every((ask) => ask.specification.direction === "sell")
// );
// });
})
it('invalid options', async function () {
this.mockRippled.addResponse('book_offers', normalRippledResponse)
assertRejects(
this.client.getOrderbook(
requests.getOrderbook.normal.takerPays,
requests.getOrderbook.normal.takerGets,
{
invalid: 'options',
},
),
this.client.errors.ValidationError,
)
})
it('with XRP', async function () {
this.mockRippled.addResponse('book_offers', xrpRippledResponse)
const response = await this.client.getOrderbook(
requests.getOrderbook.withXRP.takerPays,
requests.getOrderbook.withXRP.takerGets,
)
assertResultMatch(response, responses.getOrderbook.withXRP, 'getOrderbook')
})
it('sample USD/XRP book has orders sorted correctly', async function () {
this.mockRippled.addResponse('book_offers', xrpRippledResponse)
const response = await this.client.getOrderbook(
requests.getOrderbook.withXRP.takerPays,
requests.getOrderbook.withXRP.takerGets,
)
checkSortingOfOrders(response.buy)
checkSortingOfOrders(response.sell)
})
it('sorted so that best deals come first [failure test]', async function () {
this.mockRippled.addResponse('book_offers', normalRippledResponse)
const response = await this.client.getOrderbook(
requests.getOrderbook.normal.takerPays,
requests.getOrderbook.normal.takerGets,
)
const buyRates = response.buy.map(async (item) => item.quality as number)
const sellRates = response.sell.map(async (item) => item.quality as number)
// buy and sell orders should be sorted so that the best deals come first
assert.deepEqual(
buyRates.sort((item) => Number(item)),
buyRates,
)
assert.deepEqual(
sellRates.sort((item) => Number(item)),
sellRates,
)
})
it('sorted so that best deals come first [bad test](XRP)', async function () {
this.mockRippled.addResponse('book_offers', xrpRippledResponse)
const response = await this.client.getOrderbook(
requests.getOrderbook.withXRP.takerPays,
requests.getOrderbook.withXRP.takerGets,
)
const buyRates = response.buy.map(async (item) => item.quality as number)
const sellRates = response.sell.map(async (item) => item.quality as number)
// buy and sell orders should be sorted so that the best deals come first
assert.deepEqual(
buyRates.sort((item) => Number(item)),
buyRates,
)
assert.deepEqual(
sellRates.sort((item) => Number(item)),
sellRates,
)
})
it('direction is correct for buy and sell', async function () {
this.mockRippled.addResponse('book_offers', normalRippledResponse)
const response = await this.client.getOrderbook(
requests.getOrderbook.normal.takerPays,
requests.getOrderbook.normal.takerGets,
)
assert.strictEqual(
response.buy.every((item) => item.Flags !== OfferLedgerFlags.lsfSell),
true,
)
assert.strictEqual(
response.sell.every((item) => item.Flags === OfferLedgerFlags.lsfSell),
true,
)
})
it('getOrderbook - limit', async function () {
this.mockRippled.addResponse('book_offers', normalRippledResponse)
const LIMIT = 3
const response = await this.client.getOrderbook(
requests.getOrderbook.normal.takerPays,
requests.getOrderbook.normal.takerGets,
{
limit: LIMIT,
},
)
assert(response.buy.length <= LIMIT)
assert(response.sell.length <= LIMIT)
})
})

View File

@@ -1,9 +1,9 @@
{
"taker_pays": {
"takerPays": {
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
},
"taker_gets": {
"takerGets": {
"currency": "BTC",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
}

View File

@@ -1,9 +1,9 @@
{
"base": {
"takerPays": {
"currency": "USD",
"counterparty": "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw"
"issuer": "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw"
},
"counter": {
"takerGets": {
"currency": "XRP"
}
}

View File

@@ -1,24 +1,6 @@
{
"bids": [
"buy": [
{
"specification": {
"direction": "buy",
"quantity": {
"currency": "USD",
"value": "10.1",
"counterparty": "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw"
},
"totalPrice": {
"currency": "XRP",
"value": "254391353"
}
},
"properties": {
"maker": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"sequence": 5,
"makerExchangeRate": "3.970260734451929e-8"
},
"data": {
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"BookDirectory": "A118405CF7C2C89AB0CC084417187B86870DC14325C861A0470E1AEE5CBE20D9",
"BookNode": "0000000000000000",
@@ -38,28 +20,9 @@
"owner_funds": "99999998959999928",
"quality": "3970260734451929e-29"
}
}
],
"asks": [
"sell": [
{
"specification": {
"direction": "sell",
"quantity": {
"currency": "USD",
"value": "10453252347.1",
"counterparty": "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw"
},
"totalPrice": {
"currency": "XRP",
"value": "134"
}
},
"properties": {
"maker": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"sequence": 6,
"makerExchangeRate": "0.0000780093458738806"
},
"data": {
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"BookDirectory": "A118405CF7C2C89AB0CC084417187B86870DC14325C861A0561BB6E89EFF509C",
"BookNode": "0000000000000000",
@@ -69,15 +32,14 @@
"PreviousTxnID": "CFB5786459E568DFC504E7319C515658DED657A7F4EFB5957B33E5E3BD9A1353",
"PreviousTxnLgrSeq": 13,
"Sequence": 6,
"TakerPays": "134000000",
"TakerGets": {
"currency": "USD",
"issuer": "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw",
"value": "10453252347.1"
},
"TakerPays": "134000000",
"index": "C72CDC1BA4DA529B062871F22C6D175A4D97D4F1160D0D7E646E60699278B5B5",
"quality": "78.0093458738806"
}
}
]
}

View File

@@ -66,7 +66,7 @@ export default function createMockRippled(port: number): MockedWebSocketServer {
throw new Error(`Request has no id: ${requestJSON}`)
}
if (request.command == null) {
throw new Error(`Request has no id: ${requestJSON}`)
throw new Error(`Request has no command: ${requestJSON}`)
}
if (request.command === 'ping') {
ping(conn, request)