Skip to content

Commit

Permalink
cover api/ledger/pathfind.js with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdarkdragon committed Jul 23, 2015
1 parent 01e9622 commit 6b9710c
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 24 deletions.
65 changes: 47 additions & 18 deletions test/api-test.js
Expand Up @@ -11,6 +11,7 @@ const hashes = require('./fixtures/hashes');
const MockPRNG = require('./mock-prng');
const sjcl = require('../src').sjcl;
const address = addresses.ACCOUNT;
const NotFoundError = require('../src/api/common/errors').NotFoundError;

const orderbook = {
base: {
Expand Down Expand Up @@ -234,9 +235,8 @@ describe('RippleAPI', function() {

it('getOrderbook - direction is correct for bids and asks', function(done) {
this.api.getOrderbook(address, orderbook, {}, (error, data) => {
assert.ok(
_.every(data.bids, bid => bid.specification.direction === 'buy'));
assert.ok(
assert(_.every(data.bids, bid => bid.specification.direction === 'buy'));
assert(
_.every(data.asks, ask => ask.specification.direction === 'sell'));
done();
});
Expand All @@ -260,21 +260,50 @@ describe('RippleAPI', function() {
});

it('getPaths', function(done) {
const pathfind = {
source: {
address: address
},
destination: {
address: addresses.OTHER_ACCOUNT,
amount: {
currency: 'USD',
counterparty: addresses.ISSUER,
value: '100'
}
}
};
this.api.getPaths(pathfind,
_.partial(checkResult, responses.getPaths, done));
this.api.getPaths(requests.getPaths.normal,
_.partial(checkResult, responses.getPaths.XrpToUsd, done));
});

// @TODO
// need decide what to do with currencies/XRP:
// if add 'XRP' in currencies, then there will be exception in
// xrpToDrops function (called from toRippledAmount)
it('getPaths USD 2 USD', function(done) {
this.api.getPaths(requests.getPaths.UsdToUsd,
_.partial(checkResult, responses.getPaths.UsdToUsd, done));
});

it('getPaths XRP 2 XRP', function(done) {
this.api.getPaths(requests.getPaths.XrpToXrp,
_.partial(checkResult, responses.getPaths.XrpToXrp, done));
});

it('getPaths - XRP 2 XRP - not enough', function(done) {
this.api.getPaths(requests.getPaths.XrpToXrpNotEnough, (error) => {
assert(error instanceof NotFoundError);
done();
});
});

it('getPaths - does not accept currency', function(done) {
this.api.getPaths(requests.getPaths.NotAcceptCurrency, (error) => {
assert(error instanceof NotFoundError);
done();
});
});

it('getPaths - no paths', function(done) {
this.api.getPaths(requests.getPaths.NoPaths, (error) => {
assert(error instanceof NotFoundError);
done();
});
});

it('getPaths - no paths with source currencies', function(done) {
this.api.getPaths(requests.getPaths.NoPathsWithCurrencies, (error) => {
assert(error instanceof NotFoundError);
done();
});
});

it('getLedgerVersion', function() {
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/api/requests/getpaths/no-paths-with-currencies.json
@@ -0,0 +1,17 @@
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J",
"currencies": [
{
"currency": "USD"
}
]
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "1000002",
"currency": "USD"
}
}
}
12 changes: 12 additions & 0 deletions test/fixtures/api/requests/getpaths/no-paths.json
@@ -0,0 +1,12 @@
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J"
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "1000002",
"currency": "USD"
}
}
}
13 changes: 13 additions & 0 deletions test/fixtures/api/requests/getpaths/normal.json
@@ -0,0 +1,13 @@
{
"source": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"
},
"destination": {
"address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo",
"amount": {
"currency": "USD",
"counterparty": "rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM",
"value": "100"
}
}
}
12 changes: 12 additions & 0 deletions test/fixtures/api/requests/getpaths/not-accept-currency.json
@@ -0,0 +1,12 @@
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J"
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "0.000002",
"currency": "GBP"
}
}
}
20 changes: 20 additions & 0 deletions test/fixtures/api/requests/getpaths/usd2usd.json
@@ -0,0 +1,20 @@
{
"source": {
"address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo",
"currencies": [
{
"currency": "LTC"
},
{
"currency": "USD"
}
]
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"currency": "USD",
"value": "0.000001"
}
}
}
12 changes: 12 additions & 0 deletions test/fixtures/api/requests/getpaths/xrp2xrp-not-enough.json
@@ -0,0 +1,12 @@
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J"
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "1000002",
"currency": "XRP"
}
}
}
12 changes: 12 additions & 0 deletions test/fixtures/api/requests/getpaths/xrp2xrp.json
@@ -0,0 +1,12 @@
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J"
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "0.000002",
"currency": "XRP"
}
}
}
11 changes: 10 additions & 1 deletion test/fixtures/api/requests/index.js
Expand Up @@ -8,5 +8,14 @@ module.exports = {
preparePaymentNoCounterparty: require('./prepare-payment-no-counterparty'),
prepareSettings: require('./prepare-settings'),
prepareTrustline: require('./prepare-trustline'),
sign: require('./sign')
sign: require('./sign'),
getPaths: {
normal: require('./getpaths/normal'),
UsdToUsd: require('./getpaths/usd2usd'),
XrpToXrp: require('./getpaths/xrp2xrp'),
XrpToXrpNotEnough: require('./getpaths/xrp2xrp-not-enough'),
NotAcceptCurrency: require('./getpaths/not-accept-currency'),
NoPaths: require('./getpaths/no-paths'),
NoPathsWithCurrencies: require('./getpaths/no-paths-with-currencies')
}
};
20 changes: 20 additions & 0 deletions test/fixtures/api/responses/get-paths-send-usd.json
@@ -0,0 +1,20 @@
[
{
"source": {
"address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo",
"amount": {
"currency": "USD",
"value": "0.000001002",
"counterparty": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo"
}
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "0.000001",
"currency": "USD"
}
},
"paths": "[[{\"account\":\"rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B\"}],[{\"account\":\"rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B\"},{\"account\":\"rLMJ4db4uwHcd6NHg6jvTaYb8sH5Gy4tg5\"},{\"account\":\"rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun\"}],[{\"account\":\"rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B\"},{\"currency\":\"USD\",\"issuer\":\"rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun\"},{\"account\":\"rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun\"}],[{\"account\":\"rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B\"},{\"account\":\"rLMJ4db4uwHcd6NHg6jvTaYb8sH5Gy4tg5\"},{\"account\":\"r9vbV3EHvXWjSkeQ6CAcYVPGeq7TuiXY2X\"}]]"
}
]
19 changes: 19 additions & 0 deletions test/fixtures/api/responses/get-paths-xrp-to-xrp.json
@@ -0,0 +1,19 @@
[
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J",
"amount": {
"currency": "XRP",
"value": "0.000002"
}
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "0.000002",
"currency": "XRP"
}
},
"paths": "[]"
}
]
6 changes: 5 additions & 1 deletion test/fixtures/api/responses/index.js
Expand Up @@ -6,7 +6,11 @@ module.exports = {
getBalances: require('./get-balances.json'),
getOrderbook: require('./get-orderbook.json'),
getOrders: require('./get-orders.json'),
getPaths: require('./get-paths.json'),
getPaths: {
XrpToUsd: require('./get-paths.json'),
UsdToUsd: require('./get-paths-send-usd.json'),
XrpToXrp: require('./get-paths-xrp-to-xrp.json')
},
getServerInfo: require('./get-server-info.json'),
getSettings: require('./get-settings.json'),
getTransaction: {
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/api/rippled/index.js
Expand Up @@ -13,7 +13,11 @@ module.exports = {
account_tx: require('./account-tx'),
book_offers: require('./book-offers'),
server_info: require('./server-info'),
ripple_path_find: require('./ripple-path-find'),
ripple_path_find: {
generate: require('./ripple-path-find'),
sendUSD: require('./ripple-path-find-send-usd'),
XrpToXrp: require('./ripple-path-find-xrp-to-xrp')
},
tx: {
Payment: require('./tx/payment.json'),
AccountSet: require('./tx/account-set.json'),
Expand Down
90 changes: 90 additions & 0 deletions test/fixtures/api/rippled/ripple-path-find-send-usd.json
@@ -0,0 +1,90 @@
{
"id": 0,
"result": {
"alternatives": [
{
"paths_canonical": [],
"paths_computed": [
[
{
"account": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"type": 1,
"type_hex": "0000000000000001"
}
],
[
{
"account": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"type": 1,
"type_hex": "0000000000000001"
},
{
"account": "rLMJ4db4uwHcd6NHg6jvTaYb8sH5Gy4tg5",
"type": 1,
"type_hex": "0000000000000001"
},
{
"account": "rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun",
"type": 1,
"type_hex": "0000000000000001"
}
],
[
{
"account": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"type": 1,
"type_hex": "0000000000000001"
},
{
"currency": "USD",
"issuer": "rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun",
"type": 48,
"type_hex": "0000000000000030"
},
{
"account": "rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun",
"type": 1,
"type_hex": "0000000000000001"
}
],
[
{
"account": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"type": 1,
"type_hex": "0000000000000001"
},
{
"account": "rLMJ4db4uwHcd6NHg6jvTaYb8sH5Gy4tg5",
"type": 1,
"type_hex": "0000000000000001"
},
{
"account": "r9vbV3EHvXWjSkeQ6CAcYVPGeq7TuiXY2X",
"type": 1,
"type_hex": "0000000000000001"
}
]
],
"source_amount": {
"currency": "USD",
"issuer": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo",
"value": "0.000001002"
}
}
],
"destination_account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"destination_currencies": [
"JOE",
"BTC",
"DYM",
"CNY",
"EUR",
"015841551A748AD2C1F76FF6ECB0CCCD00000000",
"MXN",
"USD",
"XRP"
]
},
"status": "success",
"type": "response"
}
20 changes: 20 additions & 0 deletions test/fixtures/api/rippled/ripple-path-find-xrp-to-xrp.json
@@ -0,0 +1,20 @@
{
"id": 1,
"result": {
"alternatives": [],
"destination_account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"destination_currencies": [
"JOE",
"BTC",
"DYM",
"CNY",
"EUR",
"015841551A748AD2C1F76FF6ECB0CCCD00000000",
"MXN",
"USD",
"XRP"
]
},
"status": "success",
"type": "response"
}

0 comments on commit 6b9710c

Please sign in to comment.