From 5509e941ba561f49bf3d8abe86c3b649dfa4f6b0 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 8 Jan 2014 17:04:45 -0300 Subject: [PATCH] mocha test for transaction --- app/models/Transaction.js | 13 ++++++----- test/mocha.opts | 1 + test/model/block.js | 15 +++++++++---- test/model/transaction.js | 46 +++++++++++++++++++++++++++++++++++++++ test/test.js | 9 -------- 5 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 test/model/transaction.js delete mode 100644 test/test.js diff --git a/app/models/Transaction.js b/app/models/Transaction.js index 5d2a6ee7..fb8cd869 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -5,7 +5,10 @@ */ var mongoose = require('mongoose'), Schema = mongoose.Schema, - async = require('async'); + async = require('async'), + RpcClient = require('bitcore/RpcClient').class(), + config = require('../../config/config'); + /** */ @@ -31,14 +34,14 @@ TransactionSchema.statics.load = function(id, cb) { }; -TransactionSchema.statics.fromID = function(txid, cb) { +TransactionSchema.statics.fromId = function(txid, cb) { this.findOne({ txid: txid, }).exec(cb); }; -TransactionSchema.statics.fromIDWithInfo = function(txid, cb) { - this.fromHash(hash, function(err, tx) { +TransactionSchema.statics.fromIdWithInfo = function(txid, cb) { + this.fromId(txid, function(err, tx) { if (err) return cb(err); tx.getInfo(function(err) { return cb(err,tx); } ); @@ -82,7 +85,7 @@ TransactionSchema.methods.getInfo = function (next) { var that = this; var rpc = new RpcClient(config.bitcoind); - rpc.getRawTransaction(this.txid, function(err, txInfo) { + rpc.getRawTransaction(this.txid, 1, function(err, txInfo) { if (err) return next(err); that.info = txInfo.result; diff --git a/test/mocha.opts b/test/mocha.opts index 74590dc4..a9caeb4a 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,4 +1,5 @@ --require should -R spec --ui bdd +--recursive diff --git a/test/model/block.js b/test/model/block.js index f164be20..6a2b5f08 100644 --- a/test/model/block.js +++ b/test/model/block.js @@ -14,7 +14,7 @@ var mongoose.connection.on('error', function(err) { console.log(err); }); -describe('getInfo', function(){ +describe('Block getInfo', function(){ before(function(done) { mongoose.connect(config.db); @@ -26,15 +26,22 @@ describe('getInfo', function(){ done(); }); - it('should pool block\'s info from bitcoind', function(done) { + it('should poll block\'s info from mongoose', function(done) { var block2 = Block.fromHashWithInfo(TESTING_BLOCK, function(err, b2) { if (err) done(err); assert.equal(b2.hash, TESTING_BLOCK); - assert.equal(b2.info.hash, TESTING_BLOCK); - assert.equal(b2.info.chainwork, '00000000000000000000000000000000000000000000000000446af21d50acd3'); done(); }); }); + + it('should poll block\'s info from bitcoind', function(done) { + var block2 = Block.fromHashWithInfo(TESTING_BLOCK, function(err, b2) { + if (err) done(err); + assert.equal(b2.info.hash, TESTING_BLOCK); + assert.equal(b2.info.chainwork, '00000000000000000000000000000000000000000000000000446af21d50acd3'); + done(); + }); + }); }); diff --git a/test/model/transaction.js b/test/model/transaction.js new file mode 100644 index 00000000..bef066f1 --- /dev/null +++ b/test/model/transaction.js @@ -0,0 +1,46 @@ +#!/usr/bin/env node + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + + +var TESTING_TX = '9f4648538a8fd773029139f7e67cee51586bced78d7ff0388d10cb71096f2289'; + +var + mongoose= require('mongoose'), + assert = require('assert'), + config = require('../../config/config'), + Transaction = require('../../app/models/Transaction'); + + +mongoose.connection.on('error', function(err) { console.log(err); }); + +describe('Transaction getInfo', function(){ + + before(function(done) { + mongoose.connect(config.db); + done(); + }); + + after(function(done) { + mongoose.connection.close(); + done(); + }); + + it('should pool tx\'s object from mongoose', function(done) { + Transaction.fromIdWithInfo(TESTING_TX, function(err, tx) { + if (err) done(err); + assert.equal(tx.txid, TESTING_TX); + done(); + }); + }); + + it('should pool tx\'s info from bitcoind', function(done) { + Transaction.fromIdWithInfo(TESTING_TX, function(err, tx) { + if (err) done(err); + assert.equal(tx.info.txid, TESTING_TX); + assert.equal(tx.info.blockhash, '000000007af2a08af7ce4934167dc2afd7a2e6bfd31472332db02a6f38cb7b4d'); + done(); + }); + }); +}); + diff --git a/test/test.js b/test/test.js deleted file mode 100644 index bab25b64..00000000 --- a/test/test.js +++ /dev/null @@ -1,9 +0,0 @@ -var assert = require("assert") -describe('Array', function(){ - describe('#indexOf()', function(){ - it('should return -1 when the value is not present', function(){ - assert.equal(-1, [1,2,3].indexOf(5)); - assert.equal(-1, [1,2,3].indexOf(0)); - }) - }) -})