testing thin models
This commit is contained in:
parent
5aa508ae8a
commit
08f106aa0c
@ -4,10 +4,11 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
var mongoose = require('mongoose'),
|
||||
Schema = mongoose.Schema;
|
||||
|
||||
var async = require('async');
|
||||
var Transaction = require('./Transaction');
|
||||
Schema = mongoose.Schema,
|
||||
async = require('async'),
|
||||
RpcClient = require('bitcore/RpcClient').class(),
|
||||
config = require('../../config/config')
|
||||
;
|
||||
|
||||
/**
|
||||
* Block Schema
|
||||
@ -52,4 +53,38 @@ BlockSchema.statics.fromHash = function(hash, cb) {
|
||||
}).exec(cb);
|
||||
};
|
||||
|
||||
|
||||
BlockSchema.statics.fromHashWithInfo = function(hash, cb) {
|
||||
this.fromHash(hash, function(err, block) {
|
||||
if (err) return cb(err);
|
||||
|
||||
block.getInfo(function(err) { return cb(err,block); } );
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
// TODO: Can we store the rpc instance in the Block object?
|
||||
BlockSchema.methods.getInfo = function (next) {
|
||||
|
||||
var that = this;
|
||||
var rpc = new RpcClient(config.bitcoind);
|
||||
|
||||
rpc.getBlock(this.hash, function(err, blockInfo) {
|
||||
if (err) return next(err);
|
||||
|
||||
/*
|
||||
* Not sure this is the right way to do it.
|
||||
* Any other way to lazy load a property in a mongoose object?
|
||||
*/
|
||||
|
||||
that.info = blockInfo.result;
|
||||
|
||||
//console.log("THAT", that);
|
||||
return next(null, that.info);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
module.exports = mongoose.model('Block', BlockSchema);
|
||||
|
||||
@ -37,6 +37,14 @@ TransactionSchema.statics.fromID = function(txid, cb) {
|
||||
}).exec(cb);
|
||||
};
|
||||
|
||||
TransactionSchema.statics.fromIDWithInfo = function(txid, cb) {
|
||||
this.fromHash(hash, function(err, tx) {
|
||||
if (err) return cb(err);
|
||||
|
||||
tx.getInfo(function(err) { return cb(err,tx); } );
|
||||
});
|
||||
};
|
||||
|
||||
TransactionSchema.statics.createFromArray = function(txs, next) {
|
||||
|
||||
var that = this;
|
||||
@ -68,13 +76,22 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* virtual
|
||||
*/
|
||||
|
||||
// ugly? new object every call?
|
||||
TransactionSchema.virtual('info').get(function () {
|
||||
|
||||
});
|
||||
TransactionSchema.methods.getInfo = function (next) {
|
||||
|
||||
var that = this;
|
||||
var rpc = new RpcClient(config.bitcoind);
|
||||
|
||||
rpc.getRawTransaction(this.txid, function(err, txInfo) {
|
||||
if (err) return next(err);
|
||||
that.info = txInfo.result;
|
||||
|
||||
//console.log("THAT", that);
|
||||
return next(null, that.info);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = mongoose.model('Transaction', TransactionSchema);
|
||||
|
||||
@ -152,7 +152,7 @@ function spec(b) {
|
||||
|
||||
mongoose.connect(config.db);
|
||||
var db = mongoose.connection;
|
||||
this.rpc = new RpcClient(config.bitcoind);
|
||||
this.rpc = new RpcClient(config.bitcoind);
|
||||
var that = this;
|
||||
|
||||
|
||||
|
||||
41
test/model/block.js
Normal file
41
test/model/block.js
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||
|
||||
|
||||
var TESTING_BLOCK = '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4';
|
||||
|
||||
var
|
||||
mongoose= require('mongoose'),
|
||||
assert = require('assert'),
|
||||
config = require('../../config/config'),
|
||||
Block = require('../../app/models/Block');
|
||||
|
||||
mongoose.connect(config.db);
|
||||
|
||||
var db = mongoose.connection;
|
||||
|
||||
describe('getInfo', function(){
|
||||
|
||||
var block_hash = TESTING_BLOCK;
|
||||
|
||||
|
||||
db.on('error', console.error.bind(console, 'connection error:'));
|
||||
|
||||
db.once('open', function (){
|
||||
|
||||
|
||||
var block2 = Block.fromHashWithInfo(block_hash, function(err, b2) {
|
||||
if (err) done(err);
|
||||
|
||||
console.log("Block obj:");
|
||||
console.log(b2);
|
||||
console.log("Block.info:");
|
||||
console.log(b2.info);
|
||||
db.close();
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user