From 5dcefc53291086a839cac8ca7c58eedf2152e085 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Tue, 17 Jun 2014 23:35:40 +0800 Subject: [PATCH] Network estimateFee tests no longer relies on fixtures --- test/network.js | 76 ++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/test/network.js b/test/network.js index 9e178b2..8662b1c 100644 --- a/test/network.js +++ b/test/network.js @@ -1,23 +1,31 @@ var assert = require('assert') var networks = require('../src/networks') +var sinon = require('sinon') var Transaction = require('../src/transaction') -var fixtureTxes = require('./fixtures/mainnet_tx') -var fixtureTx1Hex = fixtureTxes.prevTx -var fixtureTxBigHex = fixtureTxes.bigTx - describe('networks', function() { + var txToBuffer + before(function(){ + txToBuffer = sinon.stub(Transaction.prototype, "toBuffer") + }) + + after(function(){ + Transaction.prototype.toBuffer.restore() + }) + describe('bitcoin', function() { describe('estimateFee', function() { var estimateFee = networks.bitcoin.estimateFee - it('works for fixture tx 1', function() { - var tx = Transaction.fromHex(fixtureTx1Hex) + it('works at boundry', function() { + txToBuffer.returns(new Buffer(1000)) + var tx = new Transaction() assert.equal(estimateFee(tx), 10000) }) - it('works for fixture big tx', function() { - var tx = Transaction.fromHex(fixtureTxBigHex) + it('rounds up to the closest kb for estimation', function() { + txToBuffer.returns(new Buffer(2800)) + var tx = new Transaction() assert.equal(estimateFee(tx), 30000) }) }) @@ -28,28 +36,26 @@ describe('networks', function() { var estimateFee = networks.dogecoin.estimateFee it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() { - var tx = Transaction.fromHex(fixtureTx1Hex) - tx.outs.forEach(function(e){ - e.value = 100000000 - }) + 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() { - var tx = Transaction.fromHex(fixtureTx1Hex) - tx.outs.forEach(function(e){ - e.value = 99999999 - }) + txToBuffer.returns(new Buffer(1000)) + var tx = new Transaction() + tx.outs[0] = { value: 99999999 } + tx.outs[1] = { value: 99999999 } - assert.equal(estimateFee(tx), 4 * 100000000) // 3 outs in total + assert.equal(estimateFee(tx), 3 * 100000000) }) - it('works for fixture big tx', function() { - var tx = Transaction.fromHex(fixtureTxBigHex) - tx.outs.forEach(function(e){ - e.value = 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) }) }) @@ -60,28 +66,26 @@ describe('networks', function() { var estimateFee = networks.litecoin.estimateFee it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() { - var tx = Transaction.fromHex(fixtureTx1Hex) - tx.outs.forEach(function(e){ - e.value = 100000 - }) + 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() { - var tx = Transaction.fromHex(fixtureTx1Hex) - tx.outs.forEach(function(e){ - e.value = 99999 - }) + txToBuffer.returns(new Buffer(1000)) + var tx = new Transaction() + tx.outs[0] = { value: 99999 } + tx.outs[1] = { value: 99999 } - assert.equal(estimateFee(tx), 4 * 100000) // 3 outs in total + assert.equal(estimateFee(tx), 3 * 100000) }) - it('works for fixture big tx', function() { - var tx = Transaction.fromHex(fixtureTxBigHex) - tx.outs.forEach(function(e){ - e.value = 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) }) })