root / db / tests.py @ 9cedbaf2
History | View | Annotate | Download (4.7 kB)
1 |
#
|
---|---|
2 |
# Unit Tests for db
|
3 |
#
|
4 |
# Provides automated tests for db module
|
5 |
#
|
6 |
# Copyright 2010 Greek Research and Technology Network
|
7 |
#
|
8 |
|
9 |
from synnefo.db.models import * |
10 |
|
11 |
from django.test import TestCase |
12 |
|
13 |
|
14 |
class FlavorTestCase(TestCase): |
15 |
fixtures = [ 'db_test_data' ]
|
16 |
|
17 |
def test_flavor(self): |
18 |
"""Test a flavor object, its internal cost calculation and naming methods"""
|
19 |
flavor = Flavor.objects.get(pk=30000)
|
20 |
|
21 |
# test current active/inactive costs
|
22 |
c_active = flavor.current_cost_active |
23 |
c_inactive = flavor.current_cost_inactive |
24 |
|
25 |
self.assertEqual(c_active, 10, 'flavor.cost_active should be 10! (%d)' % (c_active,)) |
26 |
self.assertEqual(c_inactive, 5, 'flavor.cost_inactive should be 5! (%d)' % (c_inactive,)) |
27 |
|
28 |
# test name property, should be C1R1024D10
|
29 |
f_name = flavor.name |
30 |
|
31 |
self.assertEqual(f_name, 'C1R1024D10', 'flavor.name is not generated correctly, C1R1024D10! (%s)' % (f_name,)) |
32 |
|
33 |
def test_flavor_get_costs(self): |
34 |
"""Test the Flavor _get_costs() method"""
|
35 |
# first an easy test, a Flavor with only one FlavorCost entry
|
36 |
flavor = Flavor.objects.get(pk=30001)
|
37 |
|
38 |
start_date = datetime.datetime(year=2010, month=1, day=1) |
39 |
end_date = datetime.datetime(year=2010, month=1, day=2) |
40 |
|
41 |
# we now that the cost should be 5*24 (inactive) and 10*24 (active)
|
42 |
r_active = flavor.get_cost_active(start_date, end_date) |
43 |
r_inactive = flavor.get_cost_inactive(start_date, end_date) |
44 |
|
45 |
self.assertEqual(len(r_active), 1, 'flavor.get_cost_active() should have returned 1 entry (%d)' %(len(r_active),)) |
46 |
self.assertEqual(len(r_inactive), 1, 'flavor.get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),)) |
47 |
|
48 |
self.assertEqual(10*24, r_active[0][1], 'flavor.get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24)) |
49 |
self.assertEqual(5*24, r_inactive[0][1], 'flavor.get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24)) |
50 |
|
51 |
# The second test, will involve a more complex cost example
|
52 |
# The overall cost will be calculated by two FlavorCost entries
|
53 |
|
54 |
flavor = Flavor.objects.get(pk=30000)
|
55 |
|
56 |
start_date = datetime.datetime(year=2010, month=12, day=31) |
57 |
end_date = datetime.datetime(year=2011, month=01, day=2) |
58 |
|
59 |
# this is more complicated, active costs are 5*24 + 10*24 = 360
|
60 |
# and inactive costs are 2*24 + 5*24 = 168
|
61 |
|
62 |
r_active = flavor.get_cost_active(start_date, end_date) |
63 |
r_inactive = flavor.get_cost_inactive(start_date, end_date) |
64 |
|
65 |
self.assertEqual(len(r_active), 2, 'flavor.get_cost_active() should have returned 2 entries (%d)' %(len(r_active),)) |
66 |
self.assertEqual(len(r_inactive), 2, 'flavor.get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),)) |
67 |
|
68 |
ta_cost = sum([x[1] for x in r_active]) |
69 |
tia_cost = sum([x[1] for x in r_inactive]) |
70 |
|
71 |
self.assertEqual(360, ta_cost, 'flavor.get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360)) |
72 |
self.assertEqual(168, tia_cost, 'flavor.get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168)) |
73 |
|
74 |
|
75 |
class VirtualMachineTestCase(TestCase): |
76 |
fixtures = [ 'db_test_data' ]
|
77 |
|
78 |
def test_charge_method(self): |
79 |
"""Test VirtualMachine.charge() method"""
|
80 |
|
81 |
# Since we have tested the costs, with this test
|
82 |
# we must ensure the following:
|
83 |
# 1. The vm.charged is updated
|
84 |
# 2. Users credits are decreased
|
85 |
|
86 |
vm_started = VirtualMachine.objects.get(pk=30000)
|
87 |
|
88 |
initial_date = vm_started.charged |
89 |
initial_credits = vm_started.owner.credit |
90 |
|
91 |
vm_started.charge() |
92 |
|
93 |
self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater') |
94 |
self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit)) |
95 |
|
96 |
|
97 |
class SynnefoUserTestCase(TestCase): |
98 |
fixtures = [ 'db_test_data' ]
|
99 |
|
100 |
def test_synnefo_user(self): |
101 |
"""Test a SynnefoUser object"""
|
102 |
s_user = SynnefoUser.objects.get(pk=30000)
|
103 |
v_machine = VirtualMachine.objects.get(pk=30000)
|
104 |
|
105 |
# charge the user
|
106 |
s_user.debit_account(10, v_machine, "This should be a structured debit message!") |
107 |
|
108 |
# should have only one debit object
|
109 |
d_list = Debit.objects.all() |
110 |
|
111 |
self.assertEqual(len(d_list), 1, 'SynnefoUser.debit_account() writes more than one debit entries!') |
112 |
|
113 |
# retrieve the user, now he/she should have zero credits
|
114 |
s_user = SynnefoUser.objects.get(pk=30000)
|
115 |
|
116 |
self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, )) |