Revision 8d97deff logic/tests.py
b/logic/tests.py | ||
---|---|---|
12 | 12 |
|
13 | 13 |
from django.test import TestCase |
14 | 14 |
|
15 |
class CostsTestCase(TestCase): |
|
16 |
fixtures = [ 'db_test_data' ] |
|
17 |
|
|
18 |
def test_get_costs(self): |
|
19 |
"""Test the Flavor cost-related methods method""" |
|
20 |
# first an easy test, a Flavor with only one FlavorCost entry |
|
21 |
flavor = Flavor.objects.get(pk=30001) |
|
22 |
|
|
23 |
start_date = datetime.datetime(year=2010, month=1, day=1) |
|
24 |
end_date = datetime.datetime(year=2010, month=1, day=2) |
|
25 |
|
|
26 |
# we now that the cost should be 5*24 (inactive) and 10*24 (active) |
|
27 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
|
28 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
|
29 |
|
|
30 |
self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),)) |
|
31 |
self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),)) |
|
32 |
|
|
33 |
self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24)) |
|
34 |
self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24)) |
|
35 |
|
|
36 |
# The second test, will involve a more complex cost example |
|
37 |
# The overall cost will be calculated by two FlavorCost entries |
|
38 |
|
|
39 |
flavor = Flavor.objects.get(pk=30000) |
|
40 |
|
|
41 |
start_date = datetime.datetime(year=2010, month=12, day=31) |
|
42 |
end_date = datetime.datetime(year=2011, month=01, day=2) |
|
43 |
|
|
44 |
# this is more complicated, active costs are 5*24 + 10*24 = 360 |
|
45 |
# and inactive costs are 2*24 + 5*24 = 168 |
|
46 |
|
|
47 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
|
48 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
|
49 |
|
|
50 |
self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),)) |
|
51 |
self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),)) |
|
52 |
|
|
53 |
ta_cost = sum([x[1] for x in r_active]) |
|
54 |
tia_cost = sum([x[1] for x in r_inactive]) |
|
55 |
|
|
56 |
self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360)) |
|
57 |
self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168)) |
|
15 | 58 |
|
59 |
|
|
16 | 60 |
class ChargeTestCase(TestCase): |
17 | 61 |
fixtures = [ 'db_test_data' ] |
18 | 62 |
|
... | ... | |
35 | 79 |
self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit)) |
36 | 80 |
|
37 | 81 |
|
38 |
class SynnefoUserTestCase(TestCase):
|
|
82 |
class DebitAccountTestCase(TestCase):
|
|
39 | 83 |
fixtures = [ 'db_test_data' ] |
40 | 84 |
|
41 |
def test_synnefo_user(self):
|
|
85 |
def test_debit_account(self):
|
|
42 | 86 |
"""Test a SynnefoUser object""" |
43 | 87 |
s_user = SynnefoUser.objects.get(pk=30000) |
44 | 88 |
v_machine = VirtualMachine.objects.get(pk=30000) |
... | ... | |
49 | 93 |
# should have only one debit object |
50 | 94 |
d_list = Debit.objects.all() |
51 | 95 |
|
52 |
self.assertEqual(len(d_list), 1, 'SynnefoUser.debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), ))
|
|
96 |
self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), )) |
|
53 | 97 |
|
54 | 98 |
# retrieve the user, now he/she should have zero credits |
55 | 99 |
s_user = SynnefoUser.objects.get(pk=30000) |
Also available in: Unified diff