From c1d52966181fdeb91f31afe002f1f1bdae9b8d5e Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 12 Jan 2014 01:16:22 -0300 Subject: [PATCH] better txitem test --- app/models/Transaction.js | 5 +++-- app/models/TransactionItem.js | 2 +- test/model/transaction.js | 20 ++++++++++++-------- test/model/txitems.json | 16 ++++++++-------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/models/Transaction.js b/app/models/Transaction.js index 87667c1..c986cf2 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -96,6 +96,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) { this.fromIdWithInfo(txid, function(err, t) { if (err || !t) return cb(err); + var index=0; async.each(t.info.vin, function(i, next_in) { /* @@ -107,7 +108,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) { txid : t.txid, value : -1 * i.value, addr : i.addr, - index : i.n, + index : index++, }, next_in); } else { @@ -130,7 +131,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) { TransactionItem.create({ txid : t.txid, value : o.value, - addr : o.scriptPubKey.addresses[0], + addr : o.scriptPubKey.addresses[0], index : o.n, }, next_out); } diff --git a/app/models/TransactionItem.js b/app/models/TransactionItem.js index 66a825f..f6ba9e9 100644 --- a/app/models/TransactionItem.js +++ b/app/models/TransactionItem.js @@ -13,7 +13,7 @@ var TransactionItemSchema = new Schema({ type: String, index: true, }, - // >0 is Input <0 is Output + // <0 is Input >0 is Output value: Number, }); diff --git a/test/model/transaction.js b/test/model/transaction.js index 6a50547..39daa20 100644 --- a/test/model/transaction.js +++ b/test/model/transaction.js @@ -10,7 +10,8 @@ var config = require('../../config/config'), Transaction = require('../../app/models/Transaction'), TransactionItem = require('../../app/models/TransactionItem'), - fs = require('fs'); + fs = require('fs'), + util = require('util'); var txItemsValid = JSON.parse(fs.readFileSync('test/model/txitems.json')); @@ -86,18 +87,21 @@ describe('Transaction', function(){ TransactionItem.find({txid: v.txid}).sort({ index:1 }).exec(function(err, readItems) { - var match=0; + var unmatch={}; + + v.items.forEach(function(validItem){ + unmatch[validItem.addr] =1; + }); v.items.forEach(function(validItem){ readItems.forEach(function(readItem){ if ( readItem.addr === validItem.addr && - parseInt(readItem.index) === parseInt(validItem.index) && - parseFloat(readItem.value) === parseFloat(validItem.value) ) { - } - match=1; + parseInt(readItem.index) == parseInt(validItem.index) && + parseFloat(readItem.value) == parseFloat(validItem.value) ) + delete unmatch[validItem.addr]; }); }); - var all = v.items.toString(); - assert(match, "Testing..." + readItems + "vs." + all); + var valid = util.inspect(v.items, { depth: null }); + assert(!Object.keys(unmatch).length, '\n\tmatched:' + Object.keys(unmatch) + "\n\n" +valid + '\nvs.\n' + readItems); done(); }); }); diff --git a/test/model/txitems.json b/test/model/txitems.json index 4ebd188..d900ded 100644 --- a/test/model/txitems.json +++ b/test/model/txitems.json @@ -3,18 +3,18 @@ "txid": "21798ddc9664ac0ef618f52b151dda82dafaf2e26d2bbef6cdaf55a6957ca237", "items": [ { - "addr": "mwcFwXv2Yquy4vJA4nnNLAbHVjrPdC8Q1Z", - "value": 1.66224, - "index": 0 + "addr": "mwcFwXv2Yquy4vJA4nnNLAbHVjrPdC8Q1Z", + "value": -1.66224, + "index": 0 }, { "addr": "mzjLe62faUqCSjkwQkwPAL5nYyR8K132fA", - "value": -1.34574, + "value": 1.34574, "index": 0 }, { "addr": "n28wb1cRGxPtfmsenYKFfsvnZ6kRapx3jF", - "value": -0.316, + "value": 0.316, "index": 1 } ] @@ -24,17 +24,17 @@ "items": [ { "addr": "mzjLe62faUqCSjkwQkwPAL5nYyR8K132fA", - "value": 0.40790667, + "value": -0.40790667, "index": 0 }, { "addr": "mhfQJUSissP6nLM5pz6DxHfctukrrLct2T", - "value": -0.193, + "value": 0.193, "index": 0 }, { "addr": "mzcDhbL877ES3MGftWnc3EuTSXs3WXDDML", - "value": -0.21440667, + "value": 0.21440667, "index": 1 } ]