
var loanService = require('../../services/loan');
var dates = require('../../utils/dates');
var async = require('simpleasync');

var loanId;

exports['clear loans'] = function (test) {
    test.async();
    
    loanService.clearLoans(function (err, data) {
        test.ok(!err);
        test.done();
    });
};

exports['new loan'] = function (test) {
    test.async();
    
    loanService.newLoan({ user: 1, amount: 1000 }, function (err, id) {
        test.ok(!err);
        test.ok(id);
        loanId = id;
        test.done();
    });
};

exports['get loan by id'] = function (test) {
    test.async();
    
    loanService.getLoanById(loanId, function (err, loan) {
        test.ok(!err);
        test.ok(loan);
        test.equal(typeof loan, 'object');
        
        test.equal(loan.user, 1);
        test.equal(loan.id, loanId);
        test.equal(loan.status, 'open');
        test.equal(loan.currency, 'ARS');
        test.ok(dates.isDateTimeString(loan.created));
        
        test.done();
    });
};

exports['get unknown loan by id'] = function (test) {
    test.async();
    
    loanService.getLoanById(0, function (err, loan) {
        test.ok(!err);
        test.strictEqual(loan, null);
        
        test.done();
    });
};

exports['get loans by user'] = function (test) {
    test.async();
    
    loanService.getLoansByUser(1, function (err, loans) {
        test.ok(!err);
        test.ok(loans);
        test.ok(Array.isArray(loans));
        test.equal(loans.length, 1);
        
        test.equal(loans[0].user, 1);
        test.equal(loans[0].id, loanId);
        
        test.done();
    });
};

exports['get loan by unknown user'] = function (test) {
    test.async();
    
    loanService.getLoansByUser(0, function (err, loans) {
        test.ok(!err);
        test.ok(loans);
        test.ok(Array.isArray(loans));
        test.equal(loans.length, 0);
        
        test.done();
    });
};

exports['get loans'] = function (test) {
    test.async();
    
    loanService.getLoans(function (err, loans) {
        test.ok(!err);
        test.ok(loans);
        test.ok(Array.isArray(loans));
        test.equal(loans.length, 1);
        
        test.equal(loans[0].user, 1);
        test.equal(loans[0].id, loanId);
        
        test.done();
    });
};

exports['update loan data'] = function (test) {
    test.async();
    
    loanService.updateLoan(loanId, { name: 'A loan' }, function (err, id) {
        test.ok(!err);
        test.ok(id);
        test.equal(id, loanId);
        
        loanService.getLoanById(loanId, function (err, loan) {
            test.ok(!err);
            test.ok(loan);
            test.equal(loan.id, loanId);
            test.equal(loan.name, 'A loan');
            test.equal(loan.user, 1);
            
            test.done();
        });
    });
};

exports['new and reject loan'] = function (test) {
    test.async();
    
    var loanId;
    
    async()
    .then(function (data, next) {
        loanService.newLoan({ user: 1, amount: 1000 }, next);
    })
    .then(function (id, next) {
        loanId = id;
        
        loanService.rejectLoan(loanId, next);
    })
    .then(function (data, next) {
        loanService.getLoanById(loanId, next);
    })
    .then(function (data, next) {
        test.ok(data);
        test.equal(data.id, loanId);
        test.equal(data.status, 'rejected');
        test.ok(dates.isDateTimeString(data.rejected));
        test.done();
    })
    .run();
};
@@ -40,24 +40,74 @@
 ces/loan');%0A
+var userService = require('../../services/user');%0A
 var dates = 
@@ -184,16 +184,39 @@
  loanId;
+%0Avar adamId;%0Avar eveId;
 %0A%0Aexport
@@ -373,32 +373,771 @@
 ();%0A    %7D);%0A%7D;%0A%0A
+exports%5B'clear users'%5D = function (test) %7B%0A    test.async();%0A    %0A    userService.clearUsers(function (err, data) %7B%0A        test.ok(!err);%0A        test.done();%0A    %7D);%0A%7D;%0A%0Aexports%5B'create loan user'%5D = function (test) %7B%0A    test.async();%0A    %0A    userService.newUser(%7B username: 'adam', firstName: 'Adam', lastName: 'Paradise' %7D, function (err, data) %7B%0A        test.ok(!err);%0A        test.ok(data);%0A        adamId = data;%0A        test.done();%0A    %7D);%0A%7D;%0A%0Aexports%5B'create investor user'%5D = function (test) %7B%0A    test.async();%0A    %0A    userService.newUser(%7B username: 'eve', firstName: 'Eve', lastName: 'Paradise' %7D, function (err, data) %7B%0A        test.ok(!err);%0A        test.ok(data);%0A        eveId = data;%0A        test.done();%0A    %7D);%0A%7D;%0A%0A
 exports%5B'new loa
@@ -1207,33 +1207,38 @@
 newLoan(%7B user: 
-1
+adamId
 , amount: 1000 %7D
@@ -1606,33 +1606,38 @@
 qual(loan.user, 
-1
+adamId
 );%0A        test.
@@ -3030,33 +3030,38 @@
 (loans%5B0%5D.user, 
-1
+adamId
 );%0A        test.
@@ -3595,25 +3595,30 @@
 (loan.user, 
-1
+adamId
 );%0A         

Refactor loan service tests, WIP