1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-22 15:28:49 -05:00

Test device storage dao

This commit is contained in:
Tankred Hase 2014-12-10 17:39:40 +01:00
parent fb89ffd35a
commit 3944b1741e
5 changed files with 81 additions and 65 deletions

View File

@ -22,6 +22,8 @@
"inject", "inject",
"Promise", "Promise",
"qMock", "qMock",
"resolves",
"rejects",
"self", "self",
"importScripts", "importScripts",
"console", "console",

View File

@ -3,13 +3,13 @@
var ngModule = angular.module('woServices'); var ngModule = angular.module('woServices');
// expose an instance with the static dbName 'app-config' to store configuration data // expose an instance with the static dbName 'app-config' to store configuration data
ngModule.factory('appConfigStore', function(appConfigLawnchair) { ngModule.factory('appConfigStore', function(appConfigLawnchair, $q) {
return new DeviceStorage(appConfigLawnchair); return new DeviceStorage(appConfigLawnchair, $q);
}); });
// expose a singleton instance of DeviceStorage called 'accountStore' to persist user data // expose a singleton instance of DeviceStorage called 'accountStore' to persist user data
ngModule.factory('accountStore', function(accountLawnchair) { ngModule.factory('accountStore', function(accountLawnchair, $q) {
return new DeviceStorage(accountLawnchair); return new DeviceStorage(accountLawnchair, $q);
}); });
module.exports = DeviceStorage; module.exports = DeviceStorage;
@ -21,57 +21,65 @@ module.exports = DeviceStorage;
/** /**
* High level storage api that handles all persistence of a user's data on the device. * High level storage api that handles all persistence of a user's data on the device.
*/ */
function DeviceStorage(lawnchairDAO) { function DeviceStorage(lawnchairDAO, $q) {
this._lawnchairDAO = lawnchairDAO; this._lawnchairDAO = lawnchairDAO;
this._q = $q;
} }
/** /**
* Initialize the lawnchair database * Initialize the lawnchair database
* @param {String} dbName The name of the database * @param {String} dbName The name of the database
* @return {Promise}
*/ */
DeviceStorage.prototype.init = function(dbName, callback) { DeviceStorage.prototype.init = function(dbName) {
this._lawnchairDAO.init(dbName, callback); return this._lawnchairDAO.init(dbName);
}; };
/** /**
* Stores a list of encrypted items in the object store * Stores a list of encrypted items in the object store
* @param list [Array] The list of items to be persisted * @param list [Array] The list of items to be persisted
* @param type [String] The type of item to be persisted e.g. 'email' * @param type [String] The type of item to be persisted e.g. 'email'
* @return {Promise}
*/ */
DeviceStorage.prototype.storeList = function(list, type, callback) { DeviceStorage.prototype.storeList = function(list, type) {
var key, items = []; var self = this;
return self._q(function(resolve) {
var key, items = [];
list = list || [];
// nothing to store // validate type
if (!list || list.length === 0) { if (!type) {
callback(); throw new Error('Type is not set!');
return; }
}
// validate type // format items for batch storing in dao
if (!type) { list.forEach(function(i) {
callback({ key = createKey(i, type);
errMsg: 'Type is not set!'
items.push({
key: key,
object: i
});
}); });
return;
}
// format items for batch storing in dao resolve(items);
list.forEach(function(i) {
key = createKey(i, type);
items.push({ }).then(function(items) {
key: key, // nothing to store
object: i if (items.length === 0) {
}); return;
}
return self._lawnchairDAO.batch(items);
}); });
this._lawnchairDAO.batch(items, callback);
}; };
/** /**
* Deletes items of a certain type from storage * Deletes items of a certain type from storage
* @return {Promise}
*/ */
DeviceStorage.prototype.removeList = function(type, callback) { DeviceStorage.prototype.removeList = function(type) {
this._lawnchairDAO.removeList(type, callback); return this._lawnchairDAO.removeList(type);
}; };
/** /**
@ -79,17 +87,19 @@ DeviceStorage.prototype.removeList = function(type, callback) {
* @param type [String] The type of item e.g. 'email' * @param type [String] The type of item e.g. 'email'
* @param offset [Number] The offset of items to fetch (0 is the last stored item) * @param offset [Number] The offset of items to fetch (0 is the last stored item)
* @param num [Number] The number of items to fetch (null means fetch all) * @param num [Number] The number of items to fetch (null means fetch all)
* @return {Promise}
*/ */
DeviceStorage.prototype.listItems = function(type, offset, num, callback) { DeviceStorage.prototype.listItems = function(type, offset, num) {
// fetch all items of a certain type from the data-store // fetch all items of a certain type from the data-store
this._lawnchairDAO.list(type, offset, num, callback); return this._lawnchairDAO.list(type, offset, num);
}; };
/** /**
* Clear the whole device data-store * Clear the whole device data-store
* @return {Promise}
*/ */
DeviceStorage.prototype.clear = function(callback) { DeviceStorage.prototype.clear = function() {
this._lawnchairDAO.clear(callback); return this._lawnchairDAO.clear();
}; };
// //

View File

@ -52,3 +52,15 @@ require('../src/js/email');
window.qMock = function(res, rej) { window.qMock = function(res, rej) {
return new Promise(res, rej); return new Promise(res, rej);
}; };
window.resolves = function(val) {
return new Promise(function(res) {
res(val);
});
};
window.rejects = function(val) {
return new Promise(function(res, rej) {
rej(val);
});
};

View File

@ -11,27 +11,28 @@ describe('Device Storage DAO unit tests', function() {
beforeEach(function() { beforeEach(function() {
lawnchairDaoStub = sinon.createStubInstance(LawnchairDAO); lawnchairDaoStub = sinon.createStubInstance(LawnchairDAO);
storageDao = new DeviceStorageDAO(lawnchairDaoStub); storageDao = new DeviceStorageDAO(lawnchairDaoStub, window.qMock);
}); });
afterEach(function() {}); afterEach(function() {});
describe('init', function() { describe('init', function() {
it('should work', function() { it('should work', function(done) {
lawnchairDaoStub.init.yields(); lawnchairDaoStub.init.returns(resolves());
storageDao.init(testUser, function(err) { storageDao.init(testUser).then(function() {
expect(err).to.not.exist;
expect(lawnchairDaoStub.init.calledOnce).to.be.true; expect(lawnchairDaoStub.init.calledOnce).to.be.true;
done();
}); });
}); });
it('should fail', function() { it('should fail', function(done) {
lawnchairDaoStub.init.yields(new Error()); lawnchairDaoStub.init.returns(rejects(new Error()));
storageDao.init(testUser, function(err) { storageDao.init(testUser).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(lawnchairDaoStub.init.calledOnce).to.be.true; expect(lawnchairDaoStub.init.calledOnce).to.be.true;
done();
}); });
}); });
}); });
@ -40,7 +41,7 @@ describe('Device Storage DAO unit tests', function() {
it('should fail', function(done) { it('should fail', function(done) {
var list = [{}]; var list = [{}];
storageDao.storeList(list, '', function(err) { storageDao.storeList(list, '').catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
@ -49,21 +50,17 @@ describe('Device Storage DAO unit tests', function() {
it('should work with empty list', function(done) { it('should work with empty list', function(done) {
var list = []; var list = [];
storageDao.storeList(list, 'email', function(err) { storageDao.storeList(list, 'email').then(done);
expect(err).to.not.exist;
done();
});
}); });
it('should work', function(done) { it('should work', function(done) {
lawnchairDaoStub.batch.yields(); lawnchairDaoStub.batch.returns(resolves());
var list = [{ var list = [{
foo: 'bar' foo: 'bar'
}]; }];
storageDao.storeList(list, 'email', function(err) { storageDao.storeList(list, 'email').then(function() {
expect(err).to.not.exist;
expect(lawnchairDaoStub.batch.calledOnce).to.be.true; expect(lawnchairDaoStub.batch.calledOnce).to.be.true;
done(); done();
}); });
@ -72,10 +69,9 @@ describe('Device Storage DAO unit tests', function() {
describe('remove list', function() { describe('remove list', function() {
it('should work', function(done) { it('should work', function(done) {
lawnchairDaoStub.removeList.yields(); lawnchairDaoStub.removeList.returns(resolves());
storageDao.removeList('email', function(err) { storageDao.removeList('email').then(function() {
expect(err).to.not.exist;
expect(lawnchairDaoStub.removeList.calledOnce).to.be.true; expect(lawnchairDaoStub.removeList.calledOnce).to.be.true;
done(); done();
}); });
@ -84,10 +80,9 @@ describe('Device Storage DAO unit tests', function() {
describe('list items', function() { describe('list items', function() {
it('should work', function(done) { it('should work', function(done) {
lawnchairDaoStub.list.yields(); lawnchairDaoStub.list.returns(resolves());
storageDao.listItems('email', 0, null, function(err) { storageDao.listItems('email', 0, null).then(function() {
expect(err).to.not.exist;
expect(lawnchairDaoStub.list.calledOnce).to.be.true; expect(lawnchairDaoStub.list.calledOnce).to.be.true;
done(); done();
}); });
@ -96,10 +91,9 @@ describe('Device Storage DAO unit tests', function() {
describe('clear', function() { describe('clear', function() {
it('should work', function(done) { it('should work', function(done) {
lawnchairDaoStub.clear.yields(); lawnchairDaoStub.clear.returns(resolves());
storageDao.clear(function(err) { storageDao.clear().then(function() {
expect(err).to.not.exist;
expect(lawnchairDaoStub.clear.calledOnce).to.be.true; expect(lawnchairDaoStub.clear.calledOnce).to.be.true;
done(); done();
}); });

View File

@ -23,9 +23,7 @@ describe('Invitation DAO unit tests', function() {
describe('invite', function() { describe('invite', function() {
it('should invite the recipient', function(done) { it('should invite the recipient', function(done) {
restDaoStub.put.returns(new Promise(function(res) { restDaoStub.put.returns(resolves());
res();
}));
invitationDao.invite({ invitationDao.invite({
recipient: alice, recipient: alice,
@ -37,7 +35,7 @@ describe('Invitation DAO unit tests', function() {
}); });
it('should not work for http error', function(done) { it('should not work for http error', function(done) {
restDaoStub.put.throws(new Error()); restDaoStub.put.returns(rejects(new Error()));
invitationDao.invite({ invitationDao.invite({
recipient: alice, recipient: alice,