From ebe670475eefce4cf7394beb4f16c65c9aa05ec8 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 18 Jun 2014 17:27:36 +1000 Subject: [PATCH] network: moves tests to fixtures --- test/fixtures/network.json | 84 ++++++++++++++++++++++++++++++++++++++ test/network.js | 83 ++++++------------------------------- 2 files changed, 96 insertions(+), 71 deletions(-) create mode 100644 test/fixtures/network.json diff --git a/test/fixtures/network.json b/test/fixtures/network.json new file mode 100644 index 0000000..e2ed767 --- /dev/null +++ b/test/fixtures/network.json @@ -0,0 +1,84 @@ +{ + "valid": [ + { + "description": "when txSize < 1kb", + "network": "bitcoin", + "txSize": 1, + "fee": 10000 + }, + { + "description": "when txSize >= 1kb", + "network": "bitcoin", + "txSize": 1000, + "fee": 10000 + }, + { + "description": "rounding", + "network": "bitcoin", + "txSize": 2800, + "fee": 30000 + }, + { + "description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used", + "network": "dogecoin", + "txSize": 1000, + "outputs": [ + { + "value": 100000000 + } + ], + "fee": 100000000 + }, + { + "description": "when not every outputs.value > DUST_SOFT_LIMIT", + "network": "dogecoin", + "txSize": 1000, + "outputs": [ + { + "value": 99999999 + }, + { + "value": 99999999 + } + ], + "fee": 300000000 + }, + { + "description": "rounding", + "network": "dogecoin", + "txSize": 2800, + "fee": 300000000 + }, + { + "description": "when outputs.value > DUST_SOFT_LIMIT, feePerKb is used", + "network": "litecoin", + "txSize": 1000, + "outputs": [ + { + "value": 100000 + } + ], + "fee": 100000 + }, + { + "description": "when not every outputs.value > DUST_SOFT_LIMIT", + "network": "litecoin", + "txSize": 1000, + "outputs": [ + { + "value": 99999 + }, + { + "value": 99999 + } + ], + "fee": 300000 + }, + { + "description": "rounding", + "network": "litecoin", + "txSize": 2800, + "fee": 300000 + } + ] +} diff --git a/test/network.js b/test/network.js index 8662b1c..ad674b6 100644 --- a/test/network.js +++ b/test/network.js @@ -3,6 +3,8 @@ var networks = require('../src/networks') var sinon = require('sinon') var Transaction = require('../src/transaction') +var fixtures = require('./fixtures/network') + describe('networks', function() { var txToBuffer before(function(){ @@ -13,80 +15,19 @@ describe('networks', function() { Transaction.prototype.toBuffer.restore() }) - describe('bitcoin', function() { - describe('estimateFee', function() { - var estimateFee = networks.bitcoin.estimateFee + fixtures.valid.forEach(function(f) { + describe(f.network + ' estimateFee', function() { + var network = networks[f.network] - it('works at boundry', function() { - txToBuffer.returns(new Buffer(1000)) + it('calculates the fee correctly for ' + f.description, function() { + var buffer = new Buffer(f.txSize) + txToBuffer.returns(buffer) + + var estimateFee = network.estimateFee var tx = new Transaction() - assert.equal(estimateFee(tx), 10000) - }) + tx.outs = f.outputs || [] - it('rounds up to the closest kb for estimation', function() { - txToBuffer.returns(new Buffer(2800)) - var tx = new Transaction() - assert.equal(estimateFee(tx), 30000) - }) - }) - }) - - describe('dogecoin', function() { - describe('estimateFee', function() { - var estimateFee = networks.dogecoin.estimateFee - - it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() { - txToBuffer.returns(new Buffer(1000)) - var tx = new Transaction() - tx.outs[0] = { value: 100000000 } - - assert.equal(estimateFee(tx), 100000000) - }) - - it('applies additional fee on every output with value below DUST_SOFT_LIMIT', function() { - txToBuffer.returns(new Buffer(1000)) - var tx = new Transaction() - tx.outs[0] = { value: 99999999 } - tx.outs[1] = { value: 99999999 } - - assert.equal(estimateFee(tx), 3 * 100000000) - }) - - it('rounds up to the closest kb for estimation', function() { - txToBuffer.returns(new Buffer(2800)) - var tx = new Transaction() - - assert.equal(estimateFee(tx), 300000000) - }) - }) - }) - - describe('litecoin', function() { - describe('estimateFee', function() { - var estimateFee = networks.litecoin.estimateFee - - it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() { - txToBuffer.returns(new Buffer(1000)) - var tx = new Transaction() - tx.outs[0] = { value: 100000 } - - assert.equal(estimateFee(tx), 100000) - }) - - it('applies additional fee on every output with value below DUST_SOFT_LIMIT', function() { - txToBuffer.returns(new Buffer(1000)) - var tx = new Transaction() - tx.outs[0] = { value: 99999 } - tx.outs[1] = { value: 99999 } - - assert.equal(estimateFee(tx), 3 * 100000) - }) - - it('rounds up to the closest kb for estimation', function() { - txToBuffer.returns(new Buffer(2800)) - var tx = new Transaction() - - assert.equal(estimateFee(tx), 300000) + assert.equal(estimateFee(tx), f.fee) }) }) })