-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_models.py
426 lines (368 loc) · 14.2 KB
/
test_models.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from django.forms import ValidationError
from django.test import TestCase
from django.urls import reverse
from main_app.models import (
Century,
Chant,
Feast,
Genre,
Service,
Sequence,
Source,
)
from .make_fakes import (
make_fake_century,
make_fake_chant,
make_fake_feast,
make_fake_genre,
make_fake_service,
make_fake_sequence,
make_fake_source,
)
# run with `python -Wa manage.py test main_app.tests.test_models`
# the -Wa flag tells Python to display deprecation warnings
class CenturyModelTest(TestCase):
@classmethod
def setUpTestData(cls):
# Set up non-modified objects used by all test methods
make_fake_century()
def test_name_label(self):
# Using first() is better than hardcoding the pk like Century.objects.get(pk=1)
# because the pk sequence is not reset between test cases,
# even though objects created by other tests are removed from db
century = Century.objects.first()
field_label = century._meta.get_field("name").verbose_name
self.assertEqual(field_label, "name")
def test_name_max_length(self):
century = Century.objects.first()
max_length = century._meta.get_field("name").max_length
self.assertEqual(max_length, 255)
def test_display_name(self):
century = Century.objects.first()
display_name = century.display_name
name_str = century.__str__()
self.assertEqual(display_name, name_str)
class ChantModelTest(TestCase):
@classmethod
def setUpTestData(cls):
source = make_fake_source()
make_fake_chant(source=source)
def test_get_ci_url(self):
chant = Chant.objects.first()
ci_url = chant.get_ci_url()
ci_url_correct = "https://cantusindex.org/id/{}".format(chant.cantus_id)
self.assertEqual(ci_url, ci_url_correct)
def test_index_components(self):
chant = Chant.objects.first()
weight_search_term_dict = chant.index_components()
expected_dict = {
"A": (
" ".join(
filter(
None,
[
chant.incipit,
chant.manuscript_full_text,
chant.manuscript_full_text_std_spelling,
chant.source.title,
],
)
)
),
"B": (
" ".join(
filter(
None, [chant.genre.name, chant.feast.name, chant.service.name]
)
)
),
}
self.assertEqual(weight_search_term_dict, expected_dict)
def test_get_concordances(self):
chant = Chant.objects.first()
chant_with_same_cantus_id = make_fake_chant(
cantus_id=chant.cantus_id, source=chant.source
)
concordances = chant.related_chants_by_cantus_id()
self.assertIn(chant_with_same_cantus_id, concordances)
def test_display_name(self):
chant = Chant.objects.first()
display_name = chant.display_name
name_str = chant.__str__()
self.assertEqual(display_name, name_str)
def test_absolute_url(self):
chant = Chant.objects.first()
absolute_url = reverse("chant-detail", args=[str(chant.id)])
self.assertEqual(chant.get_absolute_url(), absolute_url)
def test_chant_and_sequence_have_same_fields(self):
chant_fields = Chant.get_fields_and_properties()
seq_fields = Sequence.get_fields_and_properties()
self.assertEqual(chant_fields, seq_fields)
def test_get_next_chant__same_folio_next_sequence_number(self):
source = make_fake_source()
current_folio = "001"
chant1 = make_fake_chant(
source=source,
folio=current_folio,
c_sequence=1,
)
chant2 = make_fake_chant(
source=source,
folio=current_folio,
c_sequence=2,
)
self.assertEqual(chant1.get_next_chant(), chant2)
def test_get_next_chant__recto_to_verso(self):
source = make_fake_source()
current_folio = "001r"
next_folio = "001v"
chant1 = make_fake_chant(
source=source,
folio=current_folio,
c_sequence=1,
)
chant2 = make_fake_chant(
source=source,
folio=next_folio,
c_sequence=1,
)
self.assertEqual(chant1.get_next_chant(), chant2)
def test_get_next_chant__verso_to_recto(self):
source2 = make_fake_source()
current_folio = "555v"
next_folio = "556r"
chant1 = make_fake_chant(
source=source2,
folio=current_folio,
c_sequence=1,
)
chant2 = make_fake_chant(
source=source2,
folio=next_folio,
c_sequence=1,
)
self.assertEqual(chant1.get_next_chant(), chant2)
def test_get_next_chant__one_numbered_page_to_the_next(self):
source = make_fake_source()
current_folio = "004"
next_folio = "005"
end_of_page_chant = make_fake_chant(
source=source,
folio=current_folio,
c_sequence=2,
)
beginning_of_next_page_chant = make_fake_chant(
source=source,
folio=next_folio,
c_sequence=1,
)
self.assertEqual(
end_of_page_chant.get_next_chant(), beginning_of_next_page_chant
)
def test_get_next_chant__last_chant_in_manuscript(self):
source = make_fake_source()
last_folio_in_ms = "999r"
last_chant_in_ms = make_fake_chant(
source=source,
folio=last_folio_in_ms,
c_sequence=98,
)
self.assertIsNone(last_chant_in_ms.get_next_chant())
def test_get_next_chant__collision(self):
# if there are multiple chants with the same source, folio and c_sequence,
# someone has messed up their data entry, and we should set next_chant to None
source = make_fake_source()
current_folio = "444"
chant1 = make_fake_chant(
source=source,
folio=current_folio,
c_sequence=1,
)
chant2a = make_fake_chant(
source=source,
folio=current_folio,
c_sequence=2,
)
chant2b = make_fake_chant(
source=source,
folio=current_folio,
c_sequence=2,
)
self.assertIsNone(chant1.get_next_chant())
def test_get_next_chant__lacuna(self):
# if pages from a manuscript have been lost, the lacuna (gap) is often
# assigned to the previous folio and given a c_sequence of 99.
source = make_fake_source()
first_folio = "500v"
second_folio = "501r"
chant1 = make_fake_chant(
source=source,
folio=first_folio,
c_sequence=51,
)
lacuna = make_fake_chant(
source=source,
folio=first_folio,
c_sequence=99,
manuscript_full_text_std_spelling="LACUNA",
)
chant3 = make_fake_chant(source=source, folio=second_folio, c_sequence=1)
self.assertEqual(chant1.get_next_chant(), lacuna)
self.assertEqual(lacuna.get_next_chant(), chant3)
def test_incipit_signal(self):
"""Test whether a chant's incipit is updated to reflect its fulltext upon save"""
chant: Chant = make_fake_chant()
full_text: str = "Incipit should be five words sheep headphones bongoes"
expected_incipit: str = "Incipit should be five words"
chant.manuscript_full_text_std_spelling = full_text
chant.save()
chant.refresh_from_db()
observed_incipit: str = chant.incipit
self.assertEqual(observed_incipit, expected_incipit)
class FeastModelTest(TestCase):
@classmethod
def setUpTestData(cls):
make_fake_feast()
def test_object_name(self):
feast = Feast.objects.first()
self.assertEqual(str(feast), feast.name)
def test_date_constraints(self):
def create_fake_feast(month, day):
make_fake_feast(name="fakeFeast", month=month, day=day)
self.assertRaises(ValidationError, create_fake_feast, month=13, day=1)
self.assertRaises(ValidationError, create_fake_feast, month=0, day=1)
self.assertRaises(ValidationError, create_fake_feast, month=-1, day=1)
self.assertRaises(ValidationError, create_fake_feast, month=1, day=32)
self.assertRaises(ValidationError, create_fake_feast, month=1, day=0)
self.assertRaises(ValidationError, create_fake_feast, month=1, day=-1)
def test_display_name(self):
feast = Feast.objects.first()
display_name = feast.display_name
name_str = feast.__str__()
self.assertEqual(display_name, name_str)
def test_absolute_url(self):
feast = Feast.objects.first()
absolute_url = reverse("feast-detail", args=[str(feast.id)])
self.assertEqual(feast.get_absolute_url(), absolute_url)
def test_update_prefix_field_signal(self):
feast = make_fake_feast()
feast_code = "12345678"
expected_prefix = feast_code[:2]
feast.feast_code = feast_code
feast.save()
feast.refresh_from_db()
self.assertEqual(feast.feast_code, feast_code)
self.assertEqual(feast.prefix, expected_prefix)
feast.feast_code = None
feast.save()
feast.refresh_from_db()
self.assertIs(feast.feast_code, None)
self.assertEqual(feast.prefix, "")
feast.feast_code = ""
feast.save()
feast.refresh_from_db()
self.assertIs(feast.feast_code, "")
self.assertEqual(feast.prefix, "")
class GenreModelTest(TestCase):
@classmethod
def setUpTestData(cls):
make_fake_genre()
def test_string_representation(self):
genre = Genre.objects.first()
self.assertEqual(str(genre), f"[{genre.name}] {genre.description}")
def test_display_name(self):
genre = Genre.objects.first()
display_name = genre.display_name
name_str = genre.__str__()
self.assertEqual(display_name, name_str)
def test_absolute_url(self):
genre = Genre.objects.first()
absolute_url = reverse("genre-detail", args=[str(genre.id)])
self.assertEqual(genre.get_absolute_url(), absolute_url)
class ServiceModelTest(TestCase):
@classmethod
def setUpTestData(cls):
make_fake_service()
def test_object_string_representation(self):
service = Service.objects.first()
self.assertEqual(str(service), f"[{service.name}] {service.description}")
def test_display_name(self):
service = Service.objects.first()
display_name = service.display_name
name_str = service.__str__()
self.assertEqual(display_name, name_str)
def test_absolute_url(self):
service = Service.objects.first()
absolute_url = reverse("service-detail", args=[str(service.id)])
self.assertEqual(service.get_absolute_url(), absolute_url)
class SequenceModelTest(TestCase):
@classmethod
def setUpTestData(cls):
make_fake_sequence()
def test_display_name(self):
sequence = Sequence.objects.first()
display_name = sequence.display_name
name_str = sequence.__str__()
self.assertEqual(display_name, name_str)
def test_absolute_url(self):
sequence = Sequence.objects.first()
absolute_url = reverse("sequence-detail", args=[str(sequence.id)])
self.assertEqual(sequence.get_absolute_url(), absolute_url)
def test_chant_and_sequence_have_same_fields(self):
chant_fields = Chant.get_fields_and_properties()
seq_fields = Sequence.get_fields_and_properties()
self.assertEqual(chant_fields, seq_fields)
def test_incipit_signal(self):
"""Test whether a sequence's incipit is updated to reflect its title upon save"""
sequence: Sequence = make_fake_sequence()
title: str = "Incipit titulus esse debet"
expected_incipit: str = title
sequence.title = title
sequence.save()
sequence.refresh_from_db()
observed_incipit: str = sequence.incipit
self.assertEqual(observed_incipit, expected_incipit)
class SourceModelTest(TestCase):
@classmethod
def setUpTestData(cls):
make_fake_source()
def test_number_of_chants(self):
source = Source.objects.first()
chant = make_fake_chant(source=source)
sequence = make_fake_sequence(source=source)
self.assertIn(chant, source.chant_set.all())
self.assertIn(sequence, source.sequence_set.all())
self.assertEqual(source.number_of_chants, 2)
def test_number_of_melodies(self):
source = Source.objects.first()
make_fake_chant(source=source, volpiano="1-a-b-c")
make_fake_chant(source=source, volpiano=None)
self.assertEqual(source.number_of_melodies, 1)
def test_display_name(self):
source = Source.objects.first()
display_name = source.display_name
name_str = source.__str__()
self.assertEqual(display_name, name_str)
def test_absolute_url(self):
source = Source.objects.first()
absolute_url = reverse("source-detail", args=[str(source.id)])
self.assertEqual(source.get_absolute_url(), absolute_url)
class ChantSequenceSyncTest(TestCase):
def test_chant_sequence_sync(self):
# for each of the models:
# retrieve all fields of that model
# go through all of the fields and create a list of tuples of the following format:
# [
# ("field name", "field type"),
# ("field name", "field type"), ...
# ]
# if the two models are defined such that
# they specify the same fields (with the same name and type) in the same order,
# we assert true
chant_field_names = [
(f.name, f.get_internal_type()) for f in Chant._meta.get_fields()
]
sequence_field_names = [
(f.name, f.get_internal_type()) for f in Sequence._meta.get_fields()
]
self.assertEqual(chant_field_names, sequence_field_names)