all tests passing with tingodb memStore

This commit is contained in:
Ivan Socolsky 2015-04-21 23:16:18 -03:00
parent 7a0ec9f111
commit 95b0b72416
3 changed files with 53 additions and 63 deletions

View File

@ -17,6 +17,7 @@ var collections = {
TXS: 'txs',
ADDRESSES: 'addresses',
NOTIFICATIONS: 'notifications',
COPAYERS_LOOKUP: 'copayers_lookup',
};
var Storage = function(opts) {
@ -74,44 +75,38 @@ Storage.prototype.storeWallet = function(wallet, cb) {
};
Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
return this.storeWallet(wallet, cb);
};
var self = this;
Storage.prototype.fetchCopayerLookup2 = function(copayerId, cb) {
this.db.collection(collections.WALLETS).findOne({
'copayers.id': copayerId
}, function(err, result) {
if (err) return cb(err);
if (!result) return cb();
var copayer = _.find(result.copayers, {
id: copayerId
});
return cb(null, {
walletId: result.id,
var copayerLookups = _.map(wallet.copayers, function(copayer) {
return {
copayerId: copayer.id,
walletId: wallet.id,
requestPubKey: copayer.requestPubKey,
};
});
this.db.collection(collections.COPAYERS_LOOKUP).remove({
walletId: wallet.id
}, {
w: 1
}, function(err) {
if (err) return cb(err);
self.db.collection(collections.COPAYERS_LOOKUP).insert(copayerLookups, {
w: 1
}, function(err) {
if (err) return cb(err);
return self.storeWallet(wallet, cb);
});
});
};
Storage.prototype.fetchCopayerLookup = function(copayerId, cb) {
this.db.collection(collections.WALLETS).find({}).toArray(function(err, result) {
this.db.collection(collections.COPAYERS_LOOKUP).findOne({
copayerId: copayerId
}, function(err, result) {
if (err) return cb(err);
result = _.find(result, function(w) {
return _.any(w.copayers, {
id: copayerId
});
});
if (!result) return cb();
var copayer = _.find(result.copayers, {
id: copayerId
});
return cb(null, {
walletId: result.id,
requestPubKey: copayer.requestPubKey,
});
return cb(null, result);
});
};
@ -278,18 +273,11 @@ Storage.prototype.removeWallet = function(walletId, cb) {
}, next);
},
function(next) {
self.db.collection(collections.ADDRESSES).remove({
walletId: walletId
}, next);
},
function(next) {
self.db.collection(collections.TXS).remove({
walletId: walletId
}, next);
},
function(next) {
self.db.collection(collections.NOTIFICATIONS).remove({
walletId: walletId
var otherCollections = _.without(_.values(collections), collections.WALLETS);
async.each(otherCollections, function(col, next) {
self.db.collection(col).remove({
walletId: walletId
}, next);
}, next);
},
], cb);

View File

@ -11,7 +11,9 @@ var log = require('npmlog');
log.debug = log.verbose;
var fs = require('fs');
var tingodb = require('tingodb')();
var tingodb = require('tingodb')({
memStore: true
});
var Utils = require('../../lib/utils');
var WalletUtils = require('bitcore-wallet-utils');
@ -212,15 +214,8 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
var db, storage, blockchainExplorer;
function openDb(cb) {
var tingodb = require('tingodb')();
var dbDir = './db/test/';
fs.mkdir(dbDir, function(err) {
if (err && err.code != 'EEXIST') {
throw new Error('Could not create test db directory at ./db/test/');
}
db = new tingodb.Db(dbDir, {});
return cb();
});
db = new tingodb.Db('./db/test', {});
return cb();
};
function resetDb(cb) {

View File

@ -5,34 +5,41 @@ var async = require('async');
var chai = require('chai');
var sinon = require('sinon');
var should = chai.should();
var mongodb = require('mongodb');
var tingodb = require('tingodb')({
memStore: true
});
var Storage = require('../lib/storage');
var Model = require('../lib/model');
var db, storage;
function initDb(cb) {
var url = 'mongodb://localhost:27017';
mongodb.MongoClient.connect(url, function(err, db) {
should.not.exist(err);
db.dropDatabase(function(err) {
return cb(null, db);
});
function openDb(cb) {
db = new tingodb.Db('./db/test', {});
return cb();
};
function resetDb(cb) {
if (!db) return cb();
db.dropDatabase(function(err) {
return cb();
});
};
describe('Storage', function() {
var storage;
beforeEach(function(done) {
initDb(function(err, db) {
should.not.exist(err);
before(function(done) {
openDb(function() {
storage = new Storage({
db: db
});
done();
});
});
beforeEach(function(done) {
resetDb(done);
});
describe('Store & fetch wallet', function() {
it('should correctly store and fetch wallet', function(done) {
var wallet = Model.Wallet.create({