Project

General

Profile

Download (5.78 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / agenda / tests.py @ 76974b6f

1
"""
2
This file demonstrates writing tests using the unittest module. These will pass
3
when you run "manage.py test".
4

    
5
Replace this with more appropriate tests for your application.
6
"""
7
from datetime import datetime, date, timedelta
8

    
9
from django.test import TestCase
10
from django.contrib.auth.models import User
11

    
12
from calebasse.agenda.models import Event, EventType, EventWithAct
13
from calebasse.dossiers.models import create_patient
14
from calebasse.ressources.models import ActType, Service, WorkerType
15
from calebasse.personnes.models import Worker
16
from calebasse.actes.models import Act
17

    
18
class EventTest(TestCase):
19
    fixtures = ['services', 'filestates']
20
    def setUp(self):
21
        self.creator = User.objects.create(username='John')
22

    
23
    def test_recurring_events(self):
24
        event = Event.objects.create(start_datetime=datetime(2012, 10, 20, 13),
25
                end_datetime=datetime(2012, 10, 20, 13, 30), event_type=EventType(id=1),
26
                recurrence_week_period=3, recurrence_end_date=date(2012, 12, 1))
27
        event.clean()
28
        event.save()
29
        # Test model
30
        self.assertEqual(event.timedelta(), timedelta(minutes=30))
31
        occurences = list(event.all_occurences())
32
        self.assertEqual(occurences[0].start_datetime,
33
                datetime(2012, 10, 20, 13))
34
        self.assertEqual(occurences[0].end_datetime,
35
                datetime(2012, 10, 20, 13, 30))
36
        self.assertEqual(occurences[1].start_datetime,
37
                datetime(2012, 11, 10, 13))
38
        self.assertEqual(occurences[1].end_datetime,
39
                datetime(2012, 11, 10, 13, 30))
40
        self.assertEqual(occurences[2].start_datetime,
41
                datetime(2012, 12,  1, 13))
42
        self.assertEqual(occurences[2].end_datetime,
43
                datetime(2012, 12,  1, 13, 30))
44
        self.assertEqual(len(occurences), 3)
45
        self.assertTrue(event.is_recurring())
46
        self.assertEqual(event.next_occurence(today=date(2012, 10, 21)).start_datetime,
47
            datetime(2012, 11, 10, 13))
48
        self.assertEqual(event.next_occurence(today=date(2012, 11,  30)).start_datetime,
49
            datetime(2012, 12,  1, 13))
50
        self.assertEqual(event.next_occurence(today=date(2012, 12,  2)), None)
51
        # Test manager
52
        i = datetime(2012, 10, 1)
53
        while i < datetime(2013, 2, 1):
54
            d = i.date()
55
            if d in (date(2012, 10, 20), date(2012, 11, 10), date(2012, 12, 1)):
56
                self.assertEqual(list(Event.objects.for_today(d)), [event], d)
57
            else:
58
                self.assertEqual(list(Event.objects.for_today(d)), [], d)
59
            i += timedelta(days=1)
60

    
61

    
62
    def test_create_appointments(self):
63
        service_camsp = Service.objects.get(name='CAMSP')
64

    
65
        patient = create_patient('Jean', 'LEPOULPE', service_camsp, self.creator, date_selected=datetime(2020, 10, 5))
66
        wtype = WorkerType.objects.create(name='ElDoctor', intervene=True)
67
        therapist1 = Worker.objects.create(first_name='Bob', last_name='Leponge', type=wtype)
68
        therapist2 = Worker.objects.create(first_name='Jean', last_name='Valjean', type=wtype)
69
        act_type = ActType.objects.create(name='trepanation')
70
        service = Service.objects.create(name='CMPP')
71
        appointment1 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV avec M X', patient,
72
                [therapist1, therapist2], act_type, service,
73
                start_datetime=datetime(2020, 10, 2, 7, 15),
74
                end_datetime=datetime(2020, 10, 2, 9, 20),
75
                periodicity=None, until=None)
76
        self.assertEqual(unicode(appointment1), u'Rdv le 2020-10-02 07:15:00 de Jean LEPOULPE avec Bob LEPONGE, Jean VALJEAN pour trepanation (1)')
77

    
78
    def test_create_recurring_appointment(self):
79
        service_camsp = Service.objects.get(name='CAMSP')
80
        patient = create_patient('Jean', 'LEPOULPE', service_camsp, self.creator, date_selected=datetime(2020, 10, 5))
81
        wtype = WorkerType.objects.create(name='ElDoctor', intervene=True)
82
        therapist3 = Worker.objects.create(first_name='Pierre', last_name='PaulJacques', type=wtype)
83
        act_type = ActType.objects.create(name='trepanation')
84
        service = Service.objects.create(name='CMPP')
85
        appointment2 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV avec M Y', patient, [therapist3],
86
                act_type, service, start_datetime=datetime(2020, 10, 2, 10, 15),
87
                end_datetime=datetime(2020, 10, 2, 12, 20),
88
                periodicity=2, until=date(2020, 10, 16))
89
        occurences = list(appointment2.all_occurences())
90
        self.assertEqual(len(occurences), 2)
91
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(),
92
                2)
93
        self.assertEqual(occurences[0].act.date, occurences[0].start_datetime.date())
94
        self.assertEqual(occurences[1].act.date, occurences[1].start_datetime.date())
95
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(),
96
                2)
97
        appointment2.recurrence_week_period = None
98
        appointment2.save()
99
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(),
100
                1)
101
        appointment2.recurrence_week_period = 2
102
        appointment2.recurrence_end_date = date(2020, 10, 16)
103
        appointment2.save()
104
        occurences = list(appointment2.all_occurences())
105
        self.assertEqual(len(occurences), 2)
106
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(), 2)
107
        occurences[1].act.set_state('ANNUL_NOUS', self.creator)
108
        appointment2.recurrence_week_period = None
109
        appointment2.save()
110
        occurences = list(appointment2.all_occurences())
111
        self.assertEqual(len(occurences), 1)
112
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(), 1)
113
        self.assertEqual(Act.objects.count(), 2)
114

    
(8-8/10)