/
test_models_mixins.py
260 lines (212 loc) · 8.76 KB
/
test_models_mixins.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""
Unittests for opal.models.mixins
"""
import datetime
import pytz
from django.db import models
from mock import patch
from opal.core import exceptions
from opal.core.fields import ForeignKeyOrFreeText
from opal.core.test import OpalTestCase
from opal.tests import models as test_models
from opal.models import (
UpdatesFromDictMixin, Serialisable, ToDictMixin
)
class DatingModel(UpdatesFromDictMixin, models.Model):
datetime = models.DateTimeField()
consistency_token = None
class UpdatableModelInstance(UpdatesFromDictMixin, models.Model):
foo = models.CharField(max_length=200, blank=True, null=True)
bar = models.CharField(max_length=200, blank=True, null=True)
pid = models.CharField(max_length=200, blank=True, null=True)
hatty = ForeignKeyOrFreeText(test_models.Hat)
pid_fields = 'pid', 'hatty'
class GetterModel(ToDictMixin, models.Model):
foo = models.CharField(max_length=200, blank=True, null=True)
def get_foo(self, user):
return "gotten"
class SerialisableModel(Serialisable, models.Model):
_description = "so this is nice"
pid = models.CharField(max_length=200, blank=True, null=True)
hatty = ForeignKeyOrFreeText(test_models.Hat)
class SerialisableTestCase(OpalTestCase):
def test_get_fieldnames(self):
names = SerialisableModel._get_fieldnames_to_serialize()
expected = set([
'id',
'pid',
'hatty',
])
self.assertEqual(expected, set(names))
def test_get_field_type(self):
self.assertEqual(
models.ForeignKey, SerialisableModel._get_field_type('patient_id')
)
def test_get_display_name_from_property(self):
self.assertEqual(
'Wearer of Hats', test_models.HatWearer.get_display_name()
)
def test_get_display_name_from_meta_verbose_name(self):
self.assertEqual(
'Invisible Wearer of Hats',
test_models.InvisibleHatWearer.get_display_name()
)
def test_get_display_name_from_verbose_name_but_capwords(self):
self.assertEqual(
'Dog Owner',
test_models.DogOwner.get_display_name()
)
def test_get_description(self):
self.assertEqual(
SerialisableModel.get_description(), "so this is nice"
)
def test_get_human_readable_type_boolean(self):
with patch.object(SerialisableModel, "_get_field") as get_field:
get_field.return_value = models.BooleanField()
self.assertEqual(
SerialisableModel.get_human_readable_type("tree"),
"Either True or False",
)
def test_get_human_readable_type_null_boolean(self):
with patch.object(SerialisableModel, "_get_field") as get_field:
get_field.return_value = models.NullBooleanField()
self.assertEqual(
SerialisableModel.get_human_readable_type("tree"),
"Either True, False or None",
)
def test_get_human_readable_type_date_field(self):
with patch.object(SerialisableModel, "_get_field") as get_field:
get_field.return_value = models.DateField()
self.assertEqual(
SerialisableModel.get_human_readable_type("tree"),
"Date",
)
def test_get_human_readable_type_datetime_field(self):
with patch.object(SerialisableModel, "_get_field") as get_field:
get_field.return_value = models.DateTimeField()
self.assertEqual(
SerialisableModel.get_human_readable_type("tree"),
"Date & Time",
)
def test_get_human_readable_type_numeric_field(self):
numeric_fields = [
models.AutoField,
models.BigIntegerField,
models.IntegerField,
models.FloatField,
models.DecimalField
]
for numeric_field in numeric_fields:
with patch.object(SerialisableModel, "_get_field") as get_field:
get_field.return_value = numeric_field()
self.assertEqual(
SerialisableModel.get_human_readable_type("tree"),
"Number"
)
def test_get_human_readable_type_reverse_foreign_key_field(self):
self.assertEqual(
test_models.HatWearer.get_human_readable_type("created_by"),
"Relationship"
)
def test_get_human_readable_type_many_to_many_field(self):
self.assertEqual(
test_models.HatWearer.get_human_readable_type("hats"),
"Relationship"
)
def test_description(self):
description = test_models.FamousLastWords.get_field_description(
'words'
)
self.assertEqual(description, "Ultimate popular words")
def test_description_enum(self):
description = test_models.FavouriteColour.get_field_description('name')
self.assertEqual(description, "One of purple, yellow, blue")
def test_build_field_schema(self):
schema = SerialisableModel.build_field_schema()
expected = [
{
'model': 'SerialisableModel',
'description': None,
'enum': None,
'lookup_list': None,
'type': 'string',
'name': 'pid',
'default': None,
'title': u'Pid'
},
{
'model': 'SerialisableModel',
'description': None,
'enum': None,
'lookup_list': 'hat',
'type': 'string',
'name': 'hatty',
'default': None,
'title': 'Hatty'
}
]
self.assertEqual(schema, expected)
def test_build_schema_for_field_name(self):
expected_fields = [
"name",
"title",
"type",
"lookup_list",
"default",
"model",
"description",
"enum"
]
result = test_models.Colour.build_schema_for_field_name("name")
self.assertEqual(set(result), set(expected_fields))
def test_get_lookup_list_api_name(self):
result = test_models.HoundOwner.get_lookup_list_api_name("dog")
self.assertEqual(result, "dog")
def test_get_lookup_list_many_to_many_api_name(self):
result = test_models.HatWearer.get_lookup_list_api_name("hats")
self.assertEqual(result, "hat")
def test_get_lookup_list_uses_api_name(self):
with patch.object(test_models.Dog, "get_api_name") as get_api_name:
get_api_name.return_value = "hound"
result = test_models.HoundOwner.get_lookup_list_api_name("dog")
self.assertEqual(result, "hound")
get_api_name.assert_called_once_with()
def test_get_lookup_list_api_name_when_none(self):
# when we're not in a M2M field or a FK or FT field then
# then we should just return None
result = test_models.HoundOwner.get_lookup_list_api_name("name")
self.assertIsNone(result)
class ToDictMixinTestCase(OpalTestCase):
def setUp(self):
self.model_instance = GetterModel(foo="blah")
def test_getter_is_used(self):
self.assertEqual(
self.model_instance.to_dict(self.user),
dict(foo="gotten", id=None)
)
class UpdatesFromDictMixinTestCase(OpalTestCase):
def setUp(self):
self.model = UpdatableModelInstance
def test_get_fieldnames_to_serialize(self):
expected = ['id', 'foo', 'bar', 'pid', 'hatty']
self.assertEqual(expected, self.model._get_fieldnames_to_serialize())
def test_get_fieldnames_to_extract(self):
expected = ['id', 'foo', 'bar']
self.assertEqual(expected, self.model._get_fieldnames_to_extract())
def test_get_fieldnames_to_extract_fkorft_438(self):
# Regression test for https://github.com/opal/issues/438
fnames = self.model._get_fieldnames_to_extract()
self.assertFalse('hatty_fk_id' in fnames)
self.assertFalse('hatty_ft' in fnames)
def test_get_field_type_unknown(self):
with self.assertRaises(exceptions.UnexpectedFieldNameError):
self.model._get_field_type('not_a_real_field')
def test_update_from_dict_datetime(self):
data = {'datetime': '04/11/1953 12:20:00'}
expected = datetime.datetime(1953, 11, 4, 12, 20, 0, 0, pytz.UTC)
instance = DatingModel()
with patch.object(instance, '_get_field_type') as mock_type:
with patch.object(instance, 'save'):
mock_type.return_value = models.DateTimeField
result = instance.update_from_dict(data, None)
self.assertEqual(expected, instance.datetime)