From f957a57de848e4c3916e2ca9487b38312c656b5e Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 13 Jan 2014 19:57:13 -0300 Subject: [PATCH 1/3] addr tests work from simple txs --- app/models/Address.js | 22 +++++++++++++++------- test/model/addr.js | 9 +++++---- test/model/addr.json | 30 ++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/app/models/Address.js b/app/models/Address.js index 0541488..ce6cccb 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -28,13 +28,21 @@ function spec() { Address.prototype.__defineGetter__('balance', function(){ - -console.log('#################### '+this.balanceSat); - - - return this.balanceSat / BitcoreUtil.COIN; + return parseFloat(this.balanceSat) / parseFloat(BitcoreUtil.COIN); }); + + Address.prototype.__defineGetter__('totalReceived', function(){ + return parseFloat(this.totalReceivedSat) / parseFloat(BitcoreUtil.COIN); + }); + + + Address.prototype.__defineGetter__('totalSent', function(){ + return parseFloat(this.totalSentSat) / parseFloat(BitcoreUtil.COIN); + }); + + + Address.prototype.update = function(next) { if (! this.addrStr) { @@ -58,9 +66,9 @@ console.log('#################### '+this.balanceSat); that.transactions.push(txItem.txid); if (txItem.value_sat > 0) - that.totalSentSat += txItem.value_sat; + that.totalReceivedSat += txItem.value_sat; else - that.totalReceivedSat += Math.abs(txItem.value_sat); + that.totalSentSat += Math.abs(txItem.value_sat); }); return cb(); }); diff --git a/test/model/addr.js b/test/model/addr.js index 2a59f61..c19988e 100644 --- a/test/model/addr.js +++ b/test/model/addr.js @@ -10,7 +10,7 @@ var mongoose= require('mongoose'), addrValid = JSON.parse(fs.readFileSync('test/model/addr.json')); -describe('Address update', function(){ +describe('Address balances', function(){ before(function(done) { mongoose.connect(config.db); @@ -27,21 +27,22 @@ describe('Address update', function(){ console.log(v.addr + " => disabled in JSON"); } else { - it('should retrieve the correct info for:' + v.addr, function(done) { + it('Info for:' + v.addr, function(done) { this.timeout(5000); var a = new Address(v.addr); a.update(function(err) { if (err) done(err); - assert.equal(v.addr, a.addrStr); + console.log("TX count:" + a.transactions.length); + if (v.balance) assert.equal(v.balance, a.balance); if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived); if (v.totalSent) assert.equal(v.totalSent, a.totalSent); if (v.transactions) { v.transactions.forEach( function(tx) { - assert(tx in a.inTransactions); + assert(tx in a.transactions); }); } done(); diff --git a/test/model/addr.json b/test/model/addr.json index 22f4255..e669a1d 100644 --- a/test/model/addr.json +++ b/test/model/addr.json @@ -1,17 +1,31 @@ [ { - "addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H" + "addr": "mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ", + "balance": 43.1 }, { "addr": "mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS", "balance": 0, "totalReceived": 50, "totalSent": 50.0 - } - , + }, { - "addr": "mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ", - "balance": 43.1 + "addr": "muyg1K5WsHkfMVCkUXU2y7Xp5ZD6RGzCeH", + "balance": 0.38571339, + "totalReceived": 0.38571339, + "totalSent": 0 + }, + { + "addr": "mhPEfAmeKVwT7arwMYbhwnL2TfwuWbP4r4", + "balance": 1065, + "totalReceived": 1069, + "totalSent": 4 + }, + { + "addr": "n47CfqnKWdNwqY1UWxTmNJAqYutFxdH3zY", + "balance": 0, + "totalReceived":26.4245, + "totalSent": 26.4245 }, { "disabled":1, @@ -19,11 +33,7 @@ "balance": 910.39522682, "totalReceived": 910.39522682, "totalSent": 0 - } - , - - - + }, { "disabled":1, "addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H", From 5290314bd05ad8253d31b50dbe777a6f3c4b4f40 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 13 Jan 2014 19:59:31 -0300 Subject: [PATCH 2/3] addr tests work from simple txs --- test/model/addr.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/model/addr.json b/test/model/addr.json index e669a1d..b6c8a15 100644 --- a/test/model/addr.json +++ b/test/model/addr.json @@ -27,6 +27,12 @@ "totalReceived":26.4245, "totalSent": 26.4245 }, + { + "addr": "mzSyyXgofoBxpr6gYcU3cV345G8hJpixRd", + "balance": 0, + "totalReceived":3.9775, + "totalSent": 3.9775 + }, { "disabled":1, "addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29", From 801651d494f23f6f3211d977b067c010288c2148 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 13 Jan 2014 20:38:51 -0300 Subject: [PATCH 3/3] all addr test OK --- app/models/Address.js | 4 ++-- test/model/addr.js | 3 ++- test/model/addr.json | 8 ++------ 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/models/Address.js b/app/models/Address.js index ce6cccb..b01cc2d 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -54,12 +54,12 @@ function spec() { // TODO TXout! //T function (cb) { - TransactionItem.find({addr:that.addrStr}, function(err,txItems){ + TransactionItem.find({addr:that.addrStr}).sort({ts:1}).exec(function(err,txItems){ if (err) return cb(err); txItems.forEach(function(txItem){ - // console.log(txItem.txid + ' : ' + txItem.value_sat); +// console.log(txItem.txid + ':' + txItem.ts+ ' : ' + (txItem.value_sat/parseFloat(BitcoreUtil.COIN) ) ); that.txApperances +=1; that.balanceSat += txItem.value_sat; diff --git a/test/model/addr.js b/test/model/addr.js index c19988e..1a300f1 100644 --- a/test/model/addr.js +++ b/test/model/addr.js @@ -41,8 +41,9 @@ describe('Address balances', function(){ if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived); if (v.totalSent) assert.equal(v.totalSent, a.totalSent); if (v.transactions) { + v.transactions.forEach( function(tx) { - assert(tx in a.transactions); + assert(a.transactions.indexOf(tx)>-1); }); } done(); diff --git a/test/model/addr.json b/test/model/addr.json index b6c8a15..d5408ba 100644 --- a/test/model/addr.json +++ b/test/model/addr.json @@ -34,21 +34,18 @@ "totalSent": 3.9775 }, { -"disabled":1, "addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29", - "balance": 910.39522682, - "totalReceived": 910.39522682, + "balance": 910.3952268, + "totalReceived": 910.3952268, "totalSent": 0 }, { -"disabled":1, "addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H", "balance": 46413.0, "totalReceived": 357130.17644359, "totalSent": 310717.17644359 }, { -"disabled":1, "addr": "mgKY35SXqxFpcKK3Dq9mW9919N7wYXvcFM", "balance": 0.01979459, "totalReceived": 0.01979459, @@ -56,7 +53,6 @@ "transactions": [ "91800d80bb4c69b238c9bfd94eb5155ab821e6b25cae5c79903d12853bbb4ed5" ] }, { -"disabled":1, "addr": "mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5", "balance": 10580.50027254, "totalReceived": 12157.65075053,