-
Notifications
You must be signed in to change notification settings - Fork 87
/
test_episode.py
212 lines (175 loc) · 7.53 KB
/
test_episode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
Unittests for opal.models.Episode
"""
import datetime
from mock import patch, MagicMock
from django.contrib.auth.models import User
from six import text_type
from opal.core.episodes import InpatientEpisode
from opal.core.test import OpalTestCase
from opal.models import Patient, Episode, Tagging, UserProfile
from opal.tests import test_patient_lists # NOQA ensure the lists are loaded
from opal.tests.models import (
Hat, HatWearer, InvisibleHatWearer
)
class EpisodeTest(OpalTestCase):
def setUp(self):
self.patient, self.episode = self.new_patient_and_episode_please()
self.episode.stage = "Active TB"
self.episode.save()
def test_unicode(self):
self.episode.start = datetime.date(2017, 1, 1)
self.episode.save()
self.patient.demographics_set.update(
first_name="Wilma", surname="Flintstone", hospital_number="123"
)
self.assertEqual(
text_type(self.episode),
"Episode: 1 - 123 - Wilma Flintstone - 2017-01-01"
)
def test_unicode_without_start(self):
self.patient.demographics_set.update(
first_name="Wilma", surname="Flintstone", hospital_number="123"
)
self.assertEqual(
text_type(self.episode),
"Episode: 1 - 123 - Wilma Flintstone - None"
)
def test_unicode_without_demographics(self):
self.patient.demographics_set.all().delete()
self.episode.start = datetime.date(2017, 1, 1)
self.episode.save()
self.assertEqual(
text_type(self.episode),
"Episode: 1 - 2017-01-01"
)
def test_unicode_without_demographics_without_start(self):
self.patient.demographics_set.all().delete()
self.assertEqual(
text_type(self.episode),
"Episode: 1 - None"
)
def test_singleton_subrecord_created(self):
self.assertEqual(1, self.episode.episodename_set.count())
def test_category(self):
self.episode.category_name = 'Inpatient'
self.assertEqual(self.episode.category.__class__, InpatientEpisode)
self.assertEqual(self.episode.category.episode, self.episode)
def test_visible_to(self):
self.assertTrue(self.episode.visible_to(self.user))
def test_can_set_tag_names(self):
test_cases = [
['microbiology', 'mine'],
['microbiology', 'hiv'],
['hiv', 'mine'],
]
for tag_names in test_cases:
self.episode.set_tag_names(tag_names, self.user)
self.assertEqual(set(tag_names),
set(self.episode.get_tag_names(self.user)))
def test_set_tagging_parent(self):
self.episode.set_tag_names(["mine", "herbivore"], self.user)
self.assertTrue(Tagging.objects.filter(
value='eater', archived=False).exists()
)
self.assertTrue(Tagging.objects.filter(
value='herbivore', archived=False).exists()
)
self.assertTrue(Tagging.objects.filter(
value='mine', user=self.user, archived=False).exists()
)
def test_user_cannot_see_other_users_mine_tag(self):
other_user = User.objects.create(username='seconduser')
self.episode.set_tag_names(['carnivore', 'mine'], self.user)
self.assertEqual(['carnivore'], list(self.episode.get_tag_names(other_user)))
def test_active_if_tagged_by_non_mine_tag(self):
self.episode.set_tag_names(['carnivore'], self.user)
self.assertTrue(self.episode.active)
def test_active_if_only_tagged_by_mine_tag(self):
self.episode.set_tag_names(['mine'], self.user)
self.assertTrue(self.episode.active)
def test_to_dict_fields(self):
as_dict = self.episode.to_dict(self.user)
expected = [
'id', 'category_name', 'active',
'consistency_token', 'start', 'end', 'stage'
]
for field in expected:
self.assertIn(field, as_dict)
self.assertEqual(as_dict["stage"], "Active TB")
def test_get_field_names_to_extract(self):
# field names to extract should be the same
# as the field names to serialise
self.assertEqual(
Episode._get_fieldnames_to_serialize(),
Episode._get_fieldnames_to_extract()
)
@patch('opal.models.episode_subrecords')
def test_not_bulk_serialisable_episode_subrecords(self, episode_subrecords):
episode_subrecords.return_value = [InvisibleHatWearer]
_, episode = self.new_patient_and_episode_please()
to_dict = episode.to_dict(self.user)
self.assertNotIn(InvisibleHatWearer.get_api_name(), to_dict)
def test_to_dict_with_multiple_episodes(self):
self.episode.start = datetime.date(2015, 7, 25)
self.episode.save()
prev = self.patient.create_episode()
prev.start = datetime.date(2012, 7, 25)
prev.end = datetime.date(2012, 8, 12)
prev.active=False
prev.save()
serialised = self.episode.to_dict(self.user)
self.assertEqual(2, len(serialised['episode_history']))
self.assertEqual(datetime.date(2012, 7, 25),
serialised['episode_history'][0]['start'])
def test_to_dict_episode_ordering(self):
patient = Patient.objects.create()
prev = patient.create_episode()
prev.start = datetime.date(2012, 7, 25)
prev.end = datetime.date(2012, 8, 12)
prev.active = False
prev.save()
previouser = patient.create_episode()
previouser.start = datetime.date(2011, 7, 25)
previouser.active = False
previouser.save()
episode = patient.create_episode()
episode.start = datetime.date(2014, 6, 23)
episode.save()
serialised = episode.to_dict(self.user)
self.assertEqual(3, len(serialised['episode_history']))
self.assertEqual(datetime.date(2011, 7, 25),
serialised['episode_history'][0]['start'])
self.assertEqual(datetime.date(2012, 7, 25),
serialised['episode_history'][1]['start'])
def test_to_dict_episode_history_includes_no_dates(self):
prev = self.patient.create_episode()
serialised = self.episode.to_dict(self.user)
self.assertEqual(2, len(serialised['episode_history']))
@patch('opal.models.episode_subrecords')
def test_to_dict_episode_with_many_to_many(self, episode_subrecords):
episode_subrecords.return_value = [HatWearer]
prev = self.patient.create_episode()
bowler = Hat.objects.create(name="bowler")
top = Hat.objects.create(name="top")
hw = HatWearer.objects.create(episode=prev)
hw.hats.add(bowler, top)
serialised = prev.to_dict(self.user)
self.assertEqual(serialised["hat_wearer"][0]["hats"], [u'bowler', u'top'])
class EpisodeCategoryTestCase(OpalTestCase):
def setUp(self):
_, self.episode = self.new_patient_and_episode_please()
self.today = datetime.date.today()
self.yesterday = self.today - datetime.timedelta(1)
def test_episode_visible_false(self):
user = User.objects.create()
UserProfile.objects.create(user=user, restricted_only=True)
self.assertFalse(
self.episode.category.episode_visible_to(self.episode, user)
)
def test_episode_visible_true(self):
user = User.objects.create()
UserProfile.objects.create(user=user, restricted_only=False)
self.assertTrue(
self.episode.category.episode_visible_to(self.episode, user)
)