root / logic / tests.py @ 44193110
History | View | Annotate | Download (5 kB)
1 |
#
|
---|---|
2 |
# Unit Tests for logic
|
3 |
#
|
4 |
# Provides automated tests for logic module
|
5 |
#
|
6 |
# Copyright 2010 Greek Research and Technology Network
|
7 |
#
|
8 |
|
9 |
from synnefo.db.models import * |
10 |
from synnefo.logic import credits |
11 |
from synnefo.logic import users |
12 |
from django.test import TestCase |
13 |
from django.core.exceptions import ObjectDoesNotExist |
14 |
|
15 |
import hashlib |
16 |
|
17 |
class CostsTestCase(TestCase): |
18 |
fixtures = [ 'db_test_data' ]
|
19 |
|
20 |
def test_get_costs(self): |
21 |
"""Test the Flavor cost-related methods method"""
|
22 |
# first an easy test, a Flavor with only one FlavorCost entry
|
23 |
flavor = Flavor.objects.get(pk=30001)
|
24 |
|
25 |
start_date = datetime.datetime(year=2010, month=1, day=1) |
26 |
end_date = datetime.datetime(year=2010, month=1, day=2) |
27 |
|
28 |
# we now that the cost should be 5*24 (inactive) and 10*24 (active)
|
29 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
30 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
31 |
|
32 |
self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),)) |
33 |
self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),)) |
34 |
|
35 |
self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24)) |
36 |
self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24)) |
37 |
|
38 |
# The second test, will involve a more complex cost example
|
39 |
# The overall cost will be calculated by two FlavorCost entries
|
40 |
|
41 |
flavor = Flavor.objects.get(pk=30000)
|
42 |
|
43 |
start_date = datetime.datetime(year=2010, month=12, day=31) |
44 |
end_date = datetime.datetime(year=2011, month=01, day=2) |
45 |
|
46 |
# this is more complicated, active costs are 5*24 + 10*24 = 360
|
47 |
# and inactive costs are 2*24 + 5*24 = 168
|
48 |
|
49 |
r_active = credits.get_cost_active(flavor, start_date, end_date) |
50 |
r_inactive = credits.get_cost_inactive(flavor, start_date, end_date) |
51 |
|
52 |
self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),)) |
53 |
self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),)) |
54 |
|
55 |
ta_cost = sum([x[1] for x in r_active]) |
56 |
tia_cost = sum([x[1] for x in r_inactive]) |
57 |
|
58 |
self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360)) |
59 |
self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168)) |
60 |
|
61 |
|
62 |
class ChargeTestCase(TestCase): |
63 |
fixtures = [ 'db_test_data' ]
|
64 |
|
65 |
def test_charge_method(self): |
66 |
"""Test VirtualMachine.charge() method"""
|
67 |
|
68 |
# Since we have tested the costs, with this test
|
69 |
# we must ensure the following:
|
70 |
# 1. The vm.charged is updated
|
71 |
# 2. Users credits are decreased
|
72 |
|
73 |
vm_started = VirtualMachine.objects.get(pk=30000)
|
74 |
|
75 |
initial_date = vm_started.charged |
76 |
initial_credits = vm_started.owner.credit |
77 |
|
78 |
credits.charge(vm_started) |
79 |
|
80 |
self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater') |
81 |
self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit)) |
82 |
|
83 |
|
84 |
class DebitAccountTestCase(TestCase): |
85 |
fixtures = [ 'db_test_data' ]
|
86 |
|
87 |
def test_debit_account(self): |
88 |
"""Test a SynnefoUser object"""
|
89 |
s_user = SynnefoUser.objects.get(pk=30000)
|
90 |
v_machine = VirtualMachine.objects.get(pk=30000)
|
91 |
|
92 |
# charge the user
|
93 |
credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!") |
94 |
|
95 |
# should have only one debit object
|
96 |
d_list = Debit.objects.all() |
97 |
|
98 |
self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), )) |
99 |
|
100 |
# retrieve the user, now he/she should have zero credits
|
101 |
s_user = SynnefoUser.objects.get(pk=30000)
|
102 |
|
103 |
self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, )) |
104 |
|
105 |
class AuthTestCase(TestCase): |
106 |
fixtures = [ 'db_test_data' ]
|
107 |
|
108 |
def _register_user(self): |
109 |
users.register_student ("Jimmy Page", "jpage", "jpage@zoso.com") |
110 |
user = SynnefoUser.objects.get(name = "jpage")
|
111 |
|
112 |
return user
|
113 |
|
114 |
def test_register(self): |
115 |
""" test user registration
|
116 |
"""
|
117 |
user = self._register_user()
|
118 |
self.assertNotEquals(user, None) |
119 |
|
120 |
#Check hash generation
|
121 |
md5 = hashlib.md5() |
122 |
md5.update(user.uniq) |
123 |
md5.update(user.name) |
124 |
|
125 |
self.assertEquals(user.auth_token, md5.hexdigest())
|
126 |
|
127 |
def test_del_user(self): |
128 |
""" test user deletion
|
129 |
"""
|
130 |
user = self._register_user()
|
131 |
users.delete_user(user) |
132 |
user1 = None
|
133 |
|
134 |
try:
|
135 |
user1 = SynnefoUser.objects.get(name = "jpage")
|
136 |
except ObjectDoesNotExist:
|
137 |
self.assertEquals(user1, None) |