cover api/ledger/transactions.js with unit tests

This commit is contained in:
Ivan Tivonenko
2015-07-23 06:24:05 +03:00
parent ab694381d5
commit 068bda0c95
3 changed files with 109 additions and 6 deletions

View File

@@ -11,6 +11,8 @@ const hashes = require('./fixtures/hashes');
const MockPRNG = require('./mock-prng');
const sjcl = require('../src').sjcl;
const address = addresses.ACCOUNT;
const RippleError = require('../src/core/rippleerror').RippleError;
const utils = require('../src/api/ledger/utils');
const schemaValidate = require('../src/api/common/schema-validator');
const orderbook = {
@@ -209,6 +211,83 @@ describe('RippleAPI', function() {
'getTransactions', done));
});
it('getTransactions - earliest first', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
earliestFirst: true
};
const expected = _.cloneDeep(responses.getTransactions)
.sort(utils.compareTransactions);
this.api.getTransactions(address, options,
_.partial(checkResult, expected, done));
});
it('getTransactions - earliest first with start option', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
start: hashes.VALID_TRANSACTION_HASH,
earliestFirst: true
};
this.api.getTransactions(address, options, (error, data) => {
assert.strictEqual(data.length, 0);
done(error);
});
});
it('getTransactions - gap', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
maxLedgerVersion: 348858000
};
this.api.getTransactions(address, options, (error) => {
assert.ok(error instanceof this.errors.MissingLedgerHistoryError);
done();
});
});
it('getTransactions - tx not found', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 2,
start: hashes.NOTFOUND_TRANSACTION_HASH,
counterparty: address
};
this.api.getTransactions(address, options, (error) => {
assert.ok(error instanceof RippleError);
assert.strictEqual(error.remote.error, 'txnNotFound');
done();
});
});
it('getTransactions - filters', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 10,
excludeFailures: true,
counterparty: addresses.ISSUER
};
this.api.getTransactions(address, options, (error, data) => {
assert.strictEqual(data.length, 10);
assert.ok(_.every(data, t => t.type === 'payment' || t.type === 'order'));
assert.ok(_.every(data, t => t.outcome.result === 'tesSUCCESS'));
done();
});
});
it('getTransactions - filters for incoming', function(done) {
const options = {types: ['payment', 'order'], initiated: false, limit: 10,
excludeFailures: true,
counterparty: addresses.ISSUER
};
this.api.getTransactions(address, options, (error, data) => {
assert.strictEqual(data.length, 10);
assert.ok(_.every(data, t => t.type === 'payment' || t.type === 'order'));
assert.ok(_.every(data, t => t.outcome.result === 'tesSUCCESS'));
done();
});
});
it('getTransactions - error', function(done) {
const options = {types: ['payment', 'order'], initiated: true, limit: 13};
this.api.getTransactions(address, options, (error) => {
assert.ok(error instanceof RippleError);
done();
});
});
// TODO: this doesn't test much, just that it doesn't crash
it('getTransactions with start option', function(done) {
const options = {

View File

@@ -86,7 +86,7 @@
}
]
},
"ledgerVersion": 348860,
"ledgerVersion": 348859,
"indexInLedger": 0
}
},
@@ -177,7 +177,7 @@
}
]
},
"ledgerVersion": 348860,
"ledgerVersion": 348858,
"indexInLedger": 0
}
}

View File

@@ -4,6 +4,8 @@ const _ = require('lodash');
const hashes = require('../../hashes');
const addresses = require('../../addresses');
const SerializedObject = require('../../../../src/core').SerializedObject;
const AccountSet = require('./tx/account-set.json');
const NotFound = require('./tx/not-found.json');
module.exports = function(request, options={}) {
_.defaults(options, {
@@ -17,7 +19,7 @@ module.exports = function(request, options={}) {
validated: true
});
const tx = {
let tx = {
Account: addresses.ACCOUNT,
Amount: {
currency: 'USD',
@@ -52,7 +54,7 @@ module.exports = function(request, options={}) {
TxnSignature: '304502204EE3E9D1B01D8959B08450FCA9E22025AF503DEF310E34A93863A85CAB3C0BC5022100B61F5B567F77026E8DEED89EED0B7CAF0E6C96C228A2A65216F0DC2D04D52083'
};
const meta = {
let meta = {
AffectedNodes: [
{
ModifiedNode: {
@@ -196,15 +198,37 @@ module.exports = function(request, options={}) {
TransactionResult: 'tesSUCCESS'
};
let marker = +request.marker || 0;
marker += 1;
if (marker === 5) {
meta.TransactionResult = 'tecINSUFFICIENT_RESERVE';
} else if (marker === 6) {
tx = _.cloneDeep(AccountSet.result);
meta = tx.meta;
delete tx.meta;
} else if (marker === 7) {
tx.Account = addresses.OTHER_ACCOUNT;
} else if (marker === 8) {
tx.Destination = addresses.THIRD_ACCOUNT;
} else if (marker > 25) {
marker = undefined;
} else if (marker > 15) {
tx.Account = addresses.ISSUER;
tx.Destination = addresses.ACCOUNT;
}
if (request.limit === 13) {
const res = _.assign({}, NotFound, {id: request.id});
return JSON.stringify(res);
}
return JSON.stringify({
id: request.id,
status: 'success',
type: 'response',
result: {
marker: request.marker === undefined ? 'ABC' : undefined,
marker: marker === undefined ? undefined : String(marker),
transactions: [
{
ledger_index: 348860,
ledger_index: 348860 - +marker,
tx_blob: SerializedObject.from_json(tx).to_hex(),
meta: SerializedObject.from_json(meta).to_hex(),
validated: options.validated