Statistics
| Branch: | Tag: | Revision:

root / db / tests.py @ 92c53da1

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