Added Storage#readWallet and used this from WalletFactory

This commit is contained in:
Ivan Socolsky 2014-09-23 10:16:26 -03:00
commit 0f37ea1929
4 changed files with 105 additions and 63 deletions

View file

@ -140,6 +140,23 @@ Storage.prototype._readHelper = function(walletId, k, cb) {
});
};
Storage.prototype.readWallet = function(walletId, cb) {
var self = this;
this.storage.allKeys(function(allKeys) {
var obj = {};
var keys = _.filter(allKeys, function(k) {
if (k.indexOf(walletId + '::') === 0) return true;
});
var count = keys.length;
_.each(keys, function(k) {
self._read(k, function(v) {
obj[k.split('::')[1]] = v;
if (--count === 0) return cb(obj);
})
});
});
};
Storage.prototype.getMany = function(walletId, keys, cb) {
preconditions.checkArgument(cb);

View file

@ -153,10 +153,10 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) {
err;
var obj = {};
this.storage.getMany(walletId, Wallet.PERSISTED_PROPERTIES, function(ret) {
for (var ii in ret) {
obj[ii] = ret[ii];
}
this.storage.readWallet(walletId, function(ret) {
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
if (!_.any(_.values(obj)))
return cb(new Error('Wallet not found'));

View file

@ -1,6 +1,7 @@
'use strict';
var chai = chai || require('chai');
var sinon = require('sinon');
var _ = require('underscore');
var should = chai.should();
var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined';
var copay = copay || require('../copay');
@ -230,6 +231,30 @@ describe('Storage model', function() {
});
});
describe('#readWallet', function() {
it('should read wallet', function(done) {
var data = {
'id1::a': 'x',
'id1::b': 'y',
'id2::c': 'z',
};
s.storage.allKeys = sinon.stub().yields(_.keys(data));
sinon.stub(s, '_read', function(k, cb) {
return cb(data[k]);
});
s.readWallet('id1', function(w) {
w.should.exist;
w.hasOwnProperty('a').should.be.true;
w.hasOwnProperty('b').should.be.true;
w.hasOwnProperty('c').should.be.false;
w.a.should.equal('x');
w.b.should.equal('y');
s._read.restore();
done();
});
});
});
describe('#setFromObj', function() {
it('set localstorage from an object', function(done) {
s.setFromObj('id1', {

View file

@ -246,7 +246,7 @@ describe('WalletFactory model', function() {
describe('#read', function() {
it('should fail to read unexisting wallet', function(done) {
wf.storage.getMany = sinon.stub().yields({});
wf.storage.readWallet = sinon.stub().yields({});
wf.read('id', [], function(err, w) {
should.not.exist(w);
@ -258,7 +258,7 @@ describe('WalletFactory model', function() {
});
});
it('should fail to read broken wallet', function(done) {
wf.storage.getMany = sinon.stub().yields({
wf.storage.readWallet = sinon.stub().yields({
'opts': 1
});
wf.read('id', [], function(err, w) {
@ -272,7 +272,7 @@ describe('WalletFactory model', function() {
});
it('should read existing wallet', function(done) {
var wf = new WalletFactory(config, '0.0.1');
wf.storage.getMany = sinon.stub().yields({
wf.storage.readWallet = sinon.stub().yields({
'opts': 1
});
wf.fromObj = sinon.stub().returns('ok');