Browse Source

all tests passing with tingodb memStore

activeAddress
Ivan Socolsky 10 years ago
parent
commit
95b0b72416
  1. 66
      lib/storage.js
  2. 13
      test/integration/server.js
  3. 29
      test/storage.js

66
lib/storage.js

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

13
test/integration/server.js

@ -11,7 +11,9 @@ var log = require('npmlog');
log.debug = log.verbose; log.debug = log.verbose;
var fs = require('fs'); var fs = require('fs');
var tingodb = require('tingodb')(); var tingodb = require('tingodb')({
memStore: true
});
var Utils = require('../../lib/utils'); var Utils = require('../../lib/utils');
var WalletUtils = require('bitcore-wallet-utils'); var WalletUtils = require('bitcore-wallet-utils');
@ -212,15 +214,8 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
var db, storage, blockchainExplorer; var db, storage, blockchainExplorer;
function openDb(cb) { function openDb(cb) {
var tingodb = require('tingodb')(); db = new tingodb.Db('./db/test', {});
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(); return cb();
});
}; };
function resetDb(cb) { function resetDb(cb) {

29
test/storage.js

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

Loading…
Cancel
Save