Skip to content
Browse files

operation with customers in the 'tools' module reorganized with promi…

…ses. This helps to avoid pollution of unnecessary callbacks, like'done' form the other mmodules
  • Loading branch information...
1 parent b877edb commit 75c0c31e00bf5c3338fc51a676b5404ddfe58878 @andriykuba committed
Showing with 147 additions and 91 deletions.
  1. +50 −26 test/functional.js
  2. +97 −65 test/tools.js
View
76 test/functional.js
@@ -9,7 +9,7 @@
process.env.NODE_ENV = 'test';
//Accept self signed sertificate. Trivial sertificate for testing
-process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
+process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var request = require('supertest');
var url = require('url');
@@ -214,7 +214,14 @@ describe('Debitoor', function(){
describe('import', function(){
before(function(done){
- tools.cleanCustomers(done);
+ tools
+ .cleanCustomers()
+ .then(function(){
+ done();
+ })
+ .fail(function(err){
+ done(err);
+ });
});
it('all contacts as new customers', function(done){
@@ -231,11 +238,11 @@ describe('Debitoor', function(){
.end(tools.testResponse(done, cb));
}
- function maskCustomer(cusromer){
+ function maskCustomer(customer){
var masked = {};
tools.customersImportedMask.forEach(function(field){
- if(typeof cusromer[field] !== 'undefined'){
- masked[field] = cusromer[field];
+ if(typeof customer[field] !== 'undefined'){
+ masked[field] = customer[field];
}
});
return masked;
@@ -245,17 +252,24 @@ describe('Debitoor', function(){
res.body.should.have.property('complete');
res.body.complete.should.be.true;
- tools.readCustomers(done, function(customers){
- var customersLength = tools.customersImported.length;
- customers.should.be.instanceof(Array).and.have.lengthOf(customersLength);
-
- //Debitoor add some data to our import
- //We need to rid of that data to correct check
- var customersMasked = customers.map(maskCustomer);
- cb(customersMasked);
-
- done();
- });
+ tools
+ .readCustomers()
+ .then(function(debitoor){
+ var customers = debitoor.customers;
+
+ var customersLength = tools.customersImported.length;
+ customers.should.be.instanceof(Array).and.have.lengthOf(customersLength);
+
+ //Debitoor add some data to our import
+ //We need to rid of that data to correct check
+ var customersMasked = customers.map(maskCustomer);
+ cb(customersMasked);
+
+ done();
+ })
+ .fail(function(err){
+ done(err);
+ });
}
function checkImportedContactsEquals(res, done){
@@ -266,25 +280,35 @@ describe('Debitoor', function(){
});
}
- function checkImportedContactsDifferent(delited, updated, res, done){
+ function checkImportedContactsDifferent(modifyed, res, done){
checkImportedContacts(res, done, function(customers){
- customers.should.includeEql(maskCustomer(updated));
- customers.should.includeEql(maskCustomer(delited));
+ customers.should.includeEql(maskCustomer(modifyed.updated));
+ customers.should.includeEql(maskCustomer(modifyed.deleted));
});
}
it('new customers or update present customers', function(done){
- tools.modifyCustomers(done, function(){
- importContacts(done, 'update', checkImportedContactsEquals);
- });
+ tools
+ .modifyCustomers()
+ .then(function(){
+ importContacts(done, 'update', checkImportedContactsEquals);
+ })
+ .fail(function(err){
+ done(err);
+ });
});
it('only contacts that is not present as customers', function(done){
- tools.modifyCustomers(done, function(delited, updated){
- importContacts(done, 'ignore', function (res){
- checkImportedContactsDifferent(delited,updated, res, done);
+ tools
+ .modifyCustomers()
+ .then(function(modifyed){
+ importContacts(done, 'ignore', function (res){
+ checkImportedContactsDifferent(modifyed, res, done);
+ });
+ })
+ .fail(function(err){
+ done(err);
});
- });
});
});
View
162 test/tools.js
@@ -14,6 +14,24 @@ var customersImportedMask = require('./data/customers-imported-mask.json');
var currentUserName = 'user1';
var currentUserPassword = 'password1';
+//promises
+function promiseErrRes(deferred, processor){
+ return function(err,res){
+ if(err){
+ deferred.reject(err);
+ }else if(processor){
+ try{
+ var processed = processor(res);
+ deferred.resolve(processed);
+ }catch(error){
+ deferred.reject(error);
+ }
+ }else{
+ deferred.resolve(res);
+ }
+ };
+}
+
//Auth
function getAuthHeader(userData){
var userDataHash = new Buffer(userData).toString('base64');
@@ -33,29 +51,19 @@ function dropDatabase(cb){
function getUsers(){
var deferred = Q.defer();
- db.collection('users',function(err,collection){
- if(err){
- deferred.reject(err);
- }else{
- deferred.resolve(collection);
- }
- });
+
+ db.collection('users', promiseErrRes(deferred));
+
return deferred.promise;
}
function getCurrentUser(collection){
var deferred = Q.defer();
+
collection.findOne({
'username': currentUserName
- },function (err, doc) {
- if(err) {
- deferred.reject(err);
- }else if(doc === null){
- deferred.reject(new Error('Token was not found'));
- }else{
- deferred.resolve(doc);
- }
- });
+ },promiseErrRes(deferred));
+
return deferred.promise;
}
@@ -66,21 +74,24 @@ function getDebitoorToken(){
}
//Debitoor
-function cleanCustomers(done){
- readCustomers(done, function(customers, token){
- var tasks = customers.map(function(item){
- return function(cb){
- deleteCustomer(token, item, cb);
- };
- });
+function cleanCustomersPromise(debitoor){
+ var deferred = Q.defer();
- async.parallel(
- tasks,
- function(err){
- if(err) return done(err);
- done();
- });
- });
+ function deleteCustomerTask(customer){
+ return function(cb){
+ deleteCustomer(debitoor.token, customer, cb);
+ };
+ }
+
+ var tasks = debitoor.customers.map(deleteCustomerTask);
+
+ async.parallel(tasks, promiseErrRes(deferred));
+
+ return deferred.promise;
+}
+
+function cleanCustomers(){
+ return readCustomers().then(cleanCustomersPromise);
}
function deleteCustomer(token, customer, cb){
@@ -99,46 +110,67 @@ function updateCustomer(token, customer, cb){
});
}
-function readCustomers(done, cb){
- getDebitoorToken().then(function (token){
- request(config.debitoor.api.customersURL)
- .get('')
- .set('x-token', token)
- .end(testResponse(done, function(res){
- var customers = res.body.filter(function(customer){
- return !customer.isArchived;
- });
- cb(customers, token);
- }));
- }).fail(function(err){
- return done(err);
- });
+function readCustomersPromise(token){
+ var deferred = Q.defer();
+
+ request(config.debitoor.api.customersURL)
+ .get('')
+ .set('x-token', token)
+ .end(promiseErrRes(deferred, filterCustomers));
+
+ function filterCustomers(res){
+ var customers = res.body.filter(ridOffDeleted);
+ return {
+ 'customers':customers,
+ 'token':token
+ };
+ }
+
+ function ridOffDeleted(customer){
+ return !customer.isArchived;
+ }
+
+ return deferred.promise;
}
-function modifyCustomers(done, cb){
- readCustomers(done, function(customers, token){
- var tasks = [];
-
- var deletedCustomer = customers[0];
- var updatedCustomer = customers[1];
- updatedCustomer.name = 'UPDATED NAME';
+function readCustomers(){
+ return getDebitoorToken().then(readCustomersPromise);
+}
- tasks.push(function(callback){
- deleteCustomer(token, deletedCustomer, callback);
- });
-
- tasks.push(function(callback){
- updateCustomer(token, updatedCustomer, callback);
- });
-
- async.parallel(
- tasks,
- function(err){
- if(err) return done(err);
+function modifyCustomersPromise(debitoor){
+ var deferred = Q.defer();
- cb(deletedCustomer, updatedCustomer);
- });
+ var tasks = [];
+
+ var deletedCustomer = debitoor.customers[0];
+ var updatedCustomer = debitoor.customers[1];
+ updatedCustomer.name = 'UPDATED NAME';
+
+ tasks.push(function(callback){
+ deleteCustomer(debitoor.token, deletedCustomer, callback);
});
+
+ tasks.push(function(callback){
+ updateCustomer(debitoor.token, updatedCustomer, callback);
+ });
+
+ async.parallel(
+ tasks,
+ promiseErrRes(deferred, returnModifiedCustomers)
+ );
+
+ function returnModifiedCustomers(){
+ return {
+ 'deleted': deletedCustomer,
+ 'updated': updatedCustomer
+ };
+ }
+
+ return deferred.promise;
+}
+
+function modifyCustomers(){
+ return readCustomers().then(modifyCustomersPromise);
}
//Mocha will not show error in the console, and test will shut down

0 comments on commit 75c0c31

Please sign in to comment.
Something went wrong with that request. Please try again.