Statistics
| Branch: | Tag: | Revision:

root / logic / tests.py @ 5fb55fba

History | View | Annotate | Download (4 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 db.models import *
10

    
11
from logic import credits
12

    
13
from django.test import TestCase
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))
58

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

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

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

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

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

    
76
        credits.charge(vm_started)
77

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

    
81

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

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

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

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

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

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

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