Statistics
| Branch: | Tag: | Revision:

root / logic / tests.py @ dd53338a

History | View | Annotate | Download (4.9 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

    
14
import hashlib
15

    
16
class CostsTestCase(TestCase):
17
    fixtures = [ 'db_test_data' ]
18

    
19
    def test_get_costs(self):
20
        """Test the Flavor cost-related methods method"""
21
        # first an easy test, a Flavor with only one FlavorCost entry
22
        flavor = Flavor.objects.get(pk=30001)
23

    
24
        start_date = datetime.datetime(year=2010, month=1, day=1)
25
        end_date = datetime.datetime(year=2010, month=1, day=2)
26

    
27
        # we now that the cost should be 5*24 (inactive) and 10*24 (active)
28
        r_active = credits.get_cost_active(flavor, start_date, end_date)
29
        r_inactive = credits.get_cost_inactive(flavor, start_date, end_date)
30

    
31
        self.assertEqual(len(r_active), 1, 'get_cost_active() should have returned 1 entry (%d)' %(len(r_active),))
32
        self.assertEqual(len(r_inactive), 1, 'get_cost_inactive() should have returned 1 entry (%d)'% (len(r_inactive),))
33

    
34
        self.assertEqual(10*24, r_active[0][1], 'get_cost_active() is not working properly (%d!=%d)' % (r_active[0][1], 10*24))
35
        self.assertEqual(5*24, r_inactive[0][1], 'get_cost_inactive() is not working properly (%d!=%d)' % (r_inactive[0][1], 5*24))
36

    
37
        # The second test, will involve a more complex cost example
38
        # The overall cost will be calculated by two FlavorCost entries
39

    
40
        flavor = Flavor.objects.get(pk=30000)
41

    
42
        start_date = datetime.datetime(year=2010, month=12, day=31)
43
        end_date = datetime.datetime(year=2011, month=01, day=2)
44

    
45
        # this is more complicated, active costs are 5*24 + 10*24 = 360
46
        # and inactive costs are 2*24 + 5*24 = 168
47

    
48
        r_active = credits.get_cost_active(flavor, start_date, end_date)
49
        r_inactive = credits.get_cost_inactive(flavor, start_date, end_date)
50

    
51
        self.assertEqual(len(r_active), 2, 'get_cost_active() should have returned 2 entries (%d)' %(len(r_active),))
52
        self.assertEqual(len(r_inactive), 2, 'get_cost_inactive() should have returned 2 entries (%d)'% (len(r_inactive),))
53

    
54
        ta_cost = sum([x[1] for x in r_active])
55
        tia_cost = sum([x[1] for x in r_inactive])
56

    
57
        self.assertEqual(360, ta_cost, 'get_cost_active() is not working properly (%d!=%d)' % (ta_cost, 360))
58
        self.assertEqual(168, tia_cost, 'get_cost_inactive() is not working properly (%d!=%d)' % (tia_cost, 168))
59

    
60
        
61
class ChargeTestCase(TestCase):
62
    fixtures = [ 'db_test_data' ]
63

    
64
    def test_charge_method(self):
65
        """Test VirtualMachine.charge() method"""
66

    
67
        # Since we have tested the costs, with this test
68
        # we must ensure the following:
69
        # 1. The vm.charged is updated
70
        # 2. Users credits are decreased
71

    
72
        vm_started = VirtualMachine.objects.get(pk=30000)
73

    
74
        initial_date = vm_started.charged
75
        initial_credits = vm_started.owner.credit
76

    
77
        credits.charge(vm_started)
78

    
79
        self.assertTrue(vm_started.charged > initial_date, 'Initial charged date should not be greater')
80
        self.assertTrue(initial_credits > vm_started.owner.credit, 'The user should have less credits now! (%d>%d)' % (initial_credits, vm_started.owner.credit))
81

    
82

    
83
class DebitAccountTestCase(TestCase):
84
    fixtures = [ 'db_test_data' ]
85

    
86
    def test_debit_account(self):
87
        """Test a SynnefoUser object"""
88
        s_user = SynnefoUser.objects.get(pk=30000)
89
        v_machine = VirtualMachine.objects.get(pk=30000)
90

    
91
        # charge the user
92
        credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!")
93

    
94
        # should have only one debit object
95
        d_list = Debit.objects.all()
96

    
97
        self.assertEqual(len(d_list), 1, 'debit_account() writes more than one or zero (%d) debit entries!' % ( len(d_list), ))
98

    
99
        # retrieve the user, now he/she should have zero credits
100
        s_user = SynnefoUser.objects.get(pk=30000)
101

    
102
        self.assertEqual(0, s_user.credit, 'SynnefoUser (pk=30000) should have zero credits (%d)' % ( s_user.credit, ))
103

    
104
class AuthTestCase(TestCase):
105
    fixtures = [ 'db_test_data' ]
106

    
107
    def _register_user(self):
108
        users.register_student ("Jimmy Page", "jpage", "jpage@zoso.com")
109
        user = SynnefoUser.objects.get(name = "jpage")
110

    
111
        return user
112

    
113
    def test_register(self):
114
        """ test user registration
115
        """
116
        user = self._register_user()
117
        self.assertNotEquals(user, None)
118

    
119
        #Check hash generation
120
        md5 = hashlib.md5()
121
        md5.update(user.uniq)
122
        md5.update(user.name)
123

    
124
        self.assertEquals(user.auth_token, md5.hexdigest())
125

    
126
    def test_del_user(self):
127
        """ test user deletion
128
        """
129
        user = self._register_user()
130
        users.delete_user(user)
131

    
132
        self.assertRaises(SynnefoUser.DoesNotExist, SynnefoUser.objects.get, name = "jpage")