Project

General

Profile

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

calebasse / calebasse / agenda / tests.py @ 784abc51

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_triweekly_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=2),
26
                recurrence_periodicity=3, recurrence_end_date=date(2012, 12, 1))
27
        # Test model
28
        self.assertEqual(event.timedelta(), timedelta(minutes=30))
29
        occurences = list(event.all_occurences())
30
        self.assertEqual(occurences[0].start_datetime,
31
                datetime(2012, 10, 20, 13))
32
        self.assertEqual(occurences[0].end_datetime,
33
                datetime(2012, 10, 20, 13, 30))
34
        self.assertEqual(occurences[1].start_datetime,
35
                datetime(2012, 11, 10, 13))
36
        self.assertEqual(occurences[1].end_datetime,
37
                datetime(2012, 11, 10, 13, 30))
38
        self.assertEqual(occurences[2].start_datetime,
39
                datetime(2012, 12,  1, 13))
40
        self.assertEqual(occurences[2].end_datetime,
41
                datetime(2012, 12,  1, 13, 30))
42
        self.assertEqual(len(occurences), 3)
43
        self.assertTrue(event.is_recurring())
44
        self.assertEqual(event.next_occurence(today=date(2012, 10, 21)).start_datetime,
45
            datetime(2012, 11, 10, 13))
46
        self.assertEqual(event.next_occurence(today=date(2012, 11,  30)).start_datetime,
47
            datetime(2012, 12,  1, 13))
48
        self.assertEqual(event.next_occurence(today=date(2012, 12,  2)), None)
49
        # Test manager
50
        i = datetime(2012, 10, 1)
51
        while i < datetime(2013, 2, 1):
52
            d = i.date()
53
            if d in (date(2012, 10, 20), date(2012, 11, 10), date(2012, 12, 1)):
54
                self.assertEqual(list(Event.objects.for_today(d)), [event], d)
55
            else:
56
                self.assertEqual(list(Event.objects.for_today(d)), [], d)
57
            i += timedelta(days=1)
58

    
59
    def test_odd_week_events(self):
60
        event = Event.objects.create(start_datetime=datetime(2012, 10, 27, 13),
61
                end_datetime=datetime(2012, 10, 27, 13, 30), event_type=EventType(id=2),
62
                recurrence_periodicity=12, recurrence_end_date=date(2012, 11, 10))
63
        occurences = list(event.all_occurences())
64
        self.assertEqual(len(occurences), 2)
65
        self.assertEqual(occurences[0].start_datetime,
66
                datetime(2012, 10, 27, 13))
67
        self.assertEqual(occurences[0].end_datetime,
68
                datetime(2012, 10, 27, 13, 30))
69
        self.assertEqual(occurences[1].start_datetime,
70
                datetime(2012, 11, 10, 13))
71
        self.assertEqual(occurences[1].end_datetime,
72
                datetime(2012, 11, 10, 13, 30))
73

    
74
    def test_first_week_of_month(self):
75
        event = Event.objects.create(start_datetime=datetime(2012, 9, 3, 13),
76
                end_datetime=datetime(2012, 9, 3, 13, 30), event_type=EventType(id=2),
77
                recurrence_periodicity=6, recurrence_end_date=date(2012, 11, 10))
78
        occurences = list(event.all_occurences())
79
        self.assertEqual(len(occurences), 3)
80
        self.assertEqual(occurences[0].start_datetime,
81
                datetime(2012, 9, 3, 13))
82
        self.assertEqual(occurences[0].end_datetime,
83
                datetime(2012, 9, 3, 13, 30))
84
        self.assertEqual(occurences[1].start_datetime,
85
                datetime(2012, 10, 1, 13))
86
        self.assertEqual(occurences[1].end_datetime,
87
                datetime(2012, 10, 1, 13, 30))
88
        self.assertEqual(occurences[2].start_datetime,
89
                datetime(2012, 11, 5, 13))
90
        self.assertEqual(occurences[2].end_datetime,
91
                datetime(2012, 11, 5, 13, 30))
92

    
93
    def test_create_appointments(self):
94
        service_camsp = Service.objects.get(name='CAMSP')
95

    
96
        patient = create_patient('Jean', 'LEPOULPE', service_camsp, self.creator, date_selected=datetime(2020, 10, 5))
97
        wtype = WorkerType.objects.create(name='ElDoctor', intervene=True)
98
        therapist1 = Worker.objects.create(first_name='Bob', last_name='Leponge', type=wtype)
99
        therapist2 = Worker.objects.create(first_name='Jean', last_name='Valjean', type=wtype)
100
        act_type = ActType.objects.create(name='trepanation')
101
        service = Service.objects.create(name='CMPP')
102
        appointment1 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV avec M X', patient,
103
                [therapist1, therapist2], act_type, service,
104
                start_datetime=datetime(2020, 10, 2, 7, 15),
105
                end_datetime=datetime(2020, 10, 2, 9, 20),
106
                periodicity=None, until=None)
107
        self.assertEqual(unicode(appointment1), u'Rdv le 2020-10-02 07:15:00 de Jean LEPOULPE avec Bob LEPONGE, Jean VALJEAN pour trepanation (1)')
108

    
109
    def test_create_recurring_appointment(self):
110
        service_camsp = Service.objects.get(name='CAMSP')
111
        patient = create_patient('Jean', 'LEPOULPE', service_camsp, self.creator, date_selected=datetime(2020, 10, 5))
112
        wtype = WorkerType.objects.create(name='ElDoctor', intervene=True)
113
        therapist3 = Worker.objects.create(first_name='Pierre', last_name='PaulJacques', type=wtype)
114
        act_type = ActType.objects.create(name='trepanation')
115
        service = Service.objects.create(name='CMPP')
116
        appointment2 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV avec M Y', patient, [therapist3],
117
                act_type, service, start_datetime=datetime(2020, 10, 2, 10, 15),
118
                end_datetime=datetime(2020, 10, 2, 12, 20),
119
                periodicity=2, until=date(2020, 10, 16))
120
        occurences = list(appointment2.all_occurences())
121
        self.assertEqual(len(occurences), 2)
122
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(), 0)
123
        self.assertEqual(occurences[0].act.date, occurences[0].start_datetime.date())
124
        self.assertEqual(occurences[1].act.date, occurences[1].start_datetime.date())
125
        appointment2.recurrence_periodicity = None
126
        appointment2.save()
127
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(), 0)
128
        appointment2.recurrence_periodicity = 2
129
        appointment2.recurrence_end_date = date(2020, 10, 16)
130
        appointment2.save()
131
        occurences = list(appointment2.all_occurences())
132
        self.assertEqual(len(occurences), 2)
133
        [o.act for o in occurences]
134
        occurences[1].act.set_state('ANNUL_NOUS', self.creator)
135
        occurences[0].delete()
136
        occurences = list(appointment2.all_occurences())
137
        self.assertEqual(len(occurences), 1)
138
        self.assertEqual(Act.objects.filter(parent_event=appointment2).count(), 1)
139

    
140
    def test_weekly_event_with_exception(self):
141
        '''
142
           We create a single weekly event for 3 weeks between 2012-10-01
143
           and 2012-10-15, then we add two exception:
144
            - a change on the date for the second occurrence,
145
            - a cancellation for the third occurrence.
146
        '''
147
        event = Event.objects.create(start_datetime=datetime(2012, 10, 1, 13),
148
                end_datetime=datetime(2012, 10, 1, 13, 30), event_type=EventType(id=2),
149
                recurrence_periodicity=1, recurrence_end_date=date(2012, 10, 15))
150
        exception1 = Event.objects.create(start_datetime=datetime(2012, 10, 9, 13, 30),
151
                end_datetime=datetime(2012, 10, 9, 14), event_type=EventType(id=2),
152
                exception_to=event, exception_date=date(2012, 10, 8))
153
        exception2 = Event.objects.create(start_datetime=datetime(2012, 10, 15, 13, 30),
154
                end_datetime=datetime(2012, 10, 15, 14), event_type=EventType(id=2),
155
                exception_to=event, exception_date=date(2012, 10, 15), canceled=True)
156
        a = Event.objects.for_today(date(2012, 10, 1))
157
        self.assertEqual(list(a), [event])
158
        b = Event.objects.for_today(date(2012, 10, 8))
159
        self.assertEqual(list(b), [])
160
        b1 = Event.objects.for_today(date(2012, 10, 9))
161
        self.assertEqual(list(b1), [exception1])
162
        c = Event.objects.for_today(date(2012, 10, 15))
163
        self.assertEqual(list(c), [])
164

    
165
    def test_weekly_event_exception_creation(self):
166
        '''
167
           We create a single weekly event for 3 weeks between 2012-10-01
168
           and 2012-10-15, then we list its occurrences, modify them and save them:
169
            - a change on the date for the second occurrence,
170
            - a cancellation for the third occurrence.
171
        '''
172
        wtype = WorkerType.objects.create(name='ElDoctor', intervene=True)
173
        therapist1 = Worker.objects.create(first_name='Pierre', last_name='PaulJacques', type=wtype)
174
        therapist2 = Worker.objects.create(first_name='Bob', last_name='Leponge', type=wtype)
175
        event = Event.objects.create(start_datetime=datetime(2012, 10, 1, 13),
176
                end_datetime=datetime(2012, 10, 1, 13, 30), event_type=EventType(id=2),
177
                recurrence_periodicity=1, recurrence_end_date=date(2012, 10, 15))
178
        event.participants = [ therapist1 ]
179
        occurrences = list(event.all_occurences())
180
        self.assertEqual(len(occurrences), 3)
181
        self.assertEqual(occurrences[1].start_datetime, datetime(2012, 10, 8, 13))
182
        occurrences[1].start_datetime = datetime(2012, 10, 9, 13)
183
        occurrences[1].end_datetime = datetime(2012, 10, 9, 13, 30)
184
        occurrences[1].save()
185
        occurrences[1].participants = [ therapist1, therapist2 ]
186
        occurrences[2].canceled = True
187
        occurrences[2].save()
188
        a = Event.objects.today_occurrences(date(2012, 10, 1))
189
        self.assertEqual(list(a), [event])
190
        self.assertEqual(set(a[0].participants.select_subclasses()), set([therapist1]))
191
        a1 = list(a[0].all_occurences())
192
        self.assertEqual(len(a1), 1)
193
        b = Event.objects.for_today(date(2012, 10, 8))
194
        self.assertEqual(list(b), [])
195
        b1 = Event.objects.for_today(date(2012, 10, 9))
196
        self.assertEqual(list(b1), [occurrences[1]])
197
        self.assertEqual(set(b1[0].participants.select_subclasses()), set([therapist1, therapist2]))
198
        c = Event.objects.for_today(date(2012, 10, 15))
199
        self.assertEqual(list(c), [])
(8-8/10)