Skip to content

Commit

Permalink
cleaner tests without need to do cb = this
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfl committed Mar 6, 2014
1 parent a63c330 commit e095f45
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 57 deletions.
26 changes: 15 additions & 11 deletions test/simple_tests.js
Expand Up @@ -36,22 +36,12 @@ module.exports = function test (name, fun) {
tests[name].errors = [];
};

function run(name) {
function run(name, self) {
var self = tests[name];
if(self.running == true) return;
var can_run = true;
var args = [];
//console.log('>> running: ', name);
for(var a=0; a < self.deps.length; a++) {
if(tests[self.deps[a]].finish) {
args.push(tests[self.deps[a]].result);
}else{
can_run = false;
tests[self.deps[a]].required.push(name);
run(self.deps[a]);
}
}
if(!can_run) return; // can not run this test yet
function finish(result){
if(self.finish)
throw new Error("Called finish on a test "+name+" twice");
Expand All @@ -71,6 +61,20 @@ function run(name) {
if(do_throw === true) throw e;
}
};
for(var a=0; a < self.deps.length; a++) {
if(self.deps[a] == 'assert') {
args.push(finish.assert);
}else if(self.deps[a] == 'cb' || self.deps[a] == 'callback') {
args.push(finish);
}else if(tests[self.deps[a]].finish) {
args.push(tests[self.deps[a]].result);
}else{
can_run = false;
tests[self.deps[a]].required.push(name);
run(self.deps[a]);
}
}
if(!can_run) return; // can not run this test yet
running_test++;
self.running = true;
console.log('Running test:', name);
Expand Down
96 changes: 50 additions & 46 deletions test/test.js
Expand Up @@ -64,40 +64,34 @@ test('bank_account_create', function (marketplace) {
});


test('update_customer', function (customer_create) {
var cb = this;
test('update_customer', function (assert, customer_create) {
customer_create.name = "testing name";
return customer_create.save().then(function (c) {
customer_create;
cb.assert(c.name == 'testing name');
assert(c.name == 'testing name');
});
});

test('add_card_to_customer', function(customer_create, card_create) {
var cb = this;
test('add_card_to_customer', function(assert, customer_create, card_create) {
return card_create.associate_to_customer(customer_create).then(function () {
cb.assert(card_create.links.customer == customer_create.id);
assert(card_create.links.customer == customer_create.id);
return card_create;
});
});


test('add_bank_account_to_customer', function(bank_account_create, customer_create) {
var cb = this;
test('add_bank_account_to_customer', function(assert, bank_account_create, customer_create) {
return bank_account_create.associate_to_customer(customer_create)
.then(function () {
cb.assert(bank_account_create.links.customer == customer_create.id);
assert(bank_account_create.links.customer == customer_create.id);
});
});

test('debit_card', function (add_card_to_customer){
var cb = this;
return add_card_to_customer.debit({amount: 500});
return add_card_to_customer.debit({amount: 5000});
});


test('hold_card', function (add_card_to_customer) {
var cb = this;
return add_card_to_customer.hold({amount: 400});
});

Expand Down Expand Up @@ -136,8 +130,7 @@ test('filter_customer_debits', function (marketplace) {
});
});

test('test_order_restrictions', function (marketplace) {
var cb = this;
test('test_order_restrictions', function (assert, marketplace) {
var merchant = marketplace.customers.create();
var merchant_other = marketplace.customers.create();
var buyer = marketplace.customers.create();
Expand All @@ -157,10 +150,10 @@ test('test_order_restrictions', function (marketplace) {
other_ba.credit({amount: 2000, order: order.href})
.then(
function () {
cb.assert(false);
assert(false);
},
function (err) {
cb.assert(err.toString().indexOf(
assert(err.toString().indexOf(
'is not associated with order customer'
) != -1);
}
Expand All @@ -171,28 +164,26 @@ test('test_order_restrictions', function (marketplace) {
});


test('delete_card', function (marketplace) {
test('delete_card', function (cb, assert, marketplace) {
// behavior for deleting is getting tweaked slightly soon
var cb = this;
marketplace.cards.create(fixtures.card).then(function(card) {
var href = card.href;
return card.delete().then(function () {
return balanced.get(href)
.catch(function (err) {
cb.assert(err);
assert(err);
cb();
});
});
});
});

test('verify_bank_account', function (marketplace) {
var cb = this;
test('verify_bank_account', function (assert, marketplace) {
return marketplace.bank_accounts.create(fixtures.bank_account).then(function (bank_account) {
cb.assert(bank_account.can_debit == false);
assert(bank_account.can_debit == false);
return bank_account.verify().then(function (verification) {
return bank_account.confirm(1,1).then(function (bank_account) {
cb.assert(bank_account.can_debit == true);
assert(bank_account.can_debit == true);
});
});
});
Expand All @@ -202,67 +193,80 @@ test('capture_hold', function(hold_card) {
return hold_card.capture({});
});

test('paging_get_first', function (marketplace) {
var cb = this;
test('paging_get_first', function (cb, assert, marketplace) {
var customer = marketplace.customers.create();
return marketplace.cards.create(fixtures.card)
.associate_to_customer(customer)
.then(function (cc) {
customer.cards.get(0).then(function (c) {
cb.assert(cc.href == c.href);
assert(cc.href == c.href);
cb();
});
});
});

test('paging_all', function (marketplace, add_card_to_customer, customer_create) {
var cb = this;
test('paging_all', function (cb, assert, marketplace, add_card_to_customer, customer_create) {
customer_create.cards.then(function (card_page) {
card_page.all().then(function (arr) {
cb.assert(arr instanceof Array);
assert(arr instanceof Array);
for(var i=0; i < arr.length; i++) {
cb.assert(arr[i]._type == 'card')
cb.assert(arr[i].links.customer == customer_create.id);
assert(arr[i]._type == 'card')
assert(arr[i].links.customer == customer_create.id);
}
cb();
});
})
});

test('paging_none', function (marketplace) {
var cb = this;
test('paging_none', function (cb, marketplace) {
marketplace.customers.create().cards.get(0).catch(function () {
cb();
});
});

test('paging_first', function (marketplace) {
var cb = this;
test('paging_first', function (cb, assert, marketplace) {
marketplace.customers.create().cards.first().then(function (a) {
cb.assert(a === null);
assert(a === null);
cb();
});
});


test('api_key_page', function (marketplace) {
var cb = this;
test('api_key_page', function (cb, assert, marketplace) {
balanced.api_key.query.then(function (page) {
page.all().then(function (arr) {
cb.assert(arr.length == 1)
cb.assert(arr[0]._type == 'api_key');
assert(arr.length == 1)
assert(arr[0]._type == 'api_key');
cb();
});
});
});

test('page_range', function (add_card_to_customer) {
var cb = this;
test('page_range', function (cb, assert, add_card_to_customer) {
balanced.marketplace.cards.range(0, 20).then(function (arr) {
cb.assert(arr.length == 20);
assert(arr.length == 20);
// we should have at least one card created already, but not 20
cb.assert(arr[0] != null);
cb.assert(arr[19] == null);
assert(arr[0] != null);
assert(arr[19] == null);
cb();
});
});

test('credit_create_bank_account', function (cb, assert, marketplace, debit_card) {
balanced.marketplace.credits.create({
amount: 500,
destination: fixtures.bank_account
}).then(function (credit) {
assert(credit);
credit.destination.then(function (dest) {
// TODO: bug in the api
// we should be able to get back the bank account
// even though we can't credit/debit it any more
// but we are getting an error
assert(dest);
cb();
}, function(err) {
cb(); // TODO: remove
});
});
});

0 comments on commit e095f45

Please sign in to comment.