Revision 92c53da1 logic/tests.py

b/logic/tests.py
5 5
#
6 6
# Copyright 2010 Greek Research and Technology Network
7 7
#
8

  
9
from db.models import *
10

  
11
from logic import credits
12

  
13
from django.test import TestCase
14

  
15

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

  
19
    def test_charge_method(self):
20
        """Test VirtualMachine.charge() method"""
21

  
22
        # Since we have tested the costs, with this test
23
        # we must ensure the following:
24
        # 1. The vm.charged is updated
25
        # 2. Users credits are decreased
26

  
27
        vm_started = VirtualMachine.objects.get(pk=30000)
28

  
29
        initial_date = vm_started.charged
30
        initial_credits = vm_started.owner.credit
31

  
32
        credits.charge(vm_started)
33

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

  
37

  
38
class SynnefoUserTestCase(TestCase):
39
    fixtures = [ 'db_test_data' ]
40

  
41
    def test_synnefo_user(self):
42
        """Test a SynnefoUser object"""
43
        s_user = SynnefoUser.objects.get(pk=30000)
44
        v_machine = VirtualMachine.objects.get(pk=30000)
45

  
46
        # charge the user
47
        credits.debit_account(s_user, 10, v_machine, "This should be a structured debit message!")
48

  
49
        # should have only one debit object
50
        d_list = Debit.objects.all()
51

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

  
54
        # retrieve the user, now he/she should have zero credits
55
        s_user = SynnefoUser.objects.get(pk=30000)
56

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

Also available in: Unified diff