-
Notifications
You must be signed in to change notification settings - Fork 32
/
tests.py
2606 lines (2291 loc) · 110 KB
/
tests.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from django.core.exceptions import ValidationError
from mock import patch, Mock, PropertyMock
from core.collections.documents import CollectionDocument
from core.collections.models import CollectionReference, Collection, Expansion
from core.collections.models import ExpansionParameters
from core.collections.parsers import CollectionReferenceExpressionStringParser, \
CollectionReferenceSourceAllExpressionParser, CollectionReferenceOldStyleToExpandedStructureParser, \
CollectionReferenceParser
from core.collections.tests.factories import OrganizationCollectionFactory, ExpansionFactory
from core.collections.utils import is_mapping, is_concept, is_version_specified, \
get_concept_by_expression
from core.common.constants import CUSTOM_VALIDATION_SCHEMA_OPENMRS
from core.common.tasks import add_references, seed_children_to_new_version
from core.common.tasks import update_collection_active_concepts_count
from core.common.tasks import update_collection_active_mappings_count
from core.common.tests import OCLTestCase
from core.concepts.documents import ConceptDocument
from core.concepts.models import Concept
from core.concepts.tests.factories import ConceptFactory, LocalizedTextFactory
from core.mappings.documents import MappingDocument
from core.mappings.tests.factories import MappingFactory
from core.orgs.tests.factories import OrganizationFactory
from core.sources.tests.factories import OrganizationSourceFactory
class CollectionTest(OCLTestCase):
def test_resource_version_type(self):
self.assertEqual(Collection().resource_version_type, 'Collection Version')
def test_resource_type(self):
self.assertEqual(Collection().resource_type, 'Collection')
def test_get_search_document(self):
self.assertEqual(Collection.get_search_document(), CollectionDocument)
def test_collection(self):
self.assertEqual(Collection(mnemonic='coll').collection, 'coll')
self.assertEqual(Collection().collection, '')
def test_is_versioned(self):
self.assertTrue(Collection().is_versioned)
def test_add_expressions(self):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
self.assertEqual(collection.expansion.concepts.count(), 0)
self.assertEqual(collection.references.count(), 0)
self.assertEqual(collection.active_concepts, None)
source = OrganizationSourceFactory()
concept = ConceptFactory(parent=source, sources=[source])
concept_expression = concept.uri
collection.add_expressions(dict(expressions=[concept_expression]), collection.created_by)
collection.refresh_from_db()
self.assertEqual(collection.expansion.concepts.count(), 1)
self.assertEqual(collection.references.count(), 1)
self.assertEqual(collection.references.first().expression, concept.uri)
self.assertEqual(collection.expansion.concepts.first().id, concept.id)
self.assertEqual(collection.active_concepts, 1)
_, errors = collection.add_expressions(dict(concepts=[concept_expression]), collection.created_by)
self.assertEqual(
errors, {concept_expression: ['Concept or Mapping reference name must be unique in a collection.']}
)
collection.refresh_from_db()
self.assertEqual(collection.expansion.concepts.count(), 1)
self.assertEqual(collection.references.count(), 1)
self.assertEqual(collection.active_concepts, 1)
def test_add_expressions_openmrs_schema(self):
collection = OrganizationCollectionFactory(custom_validation_schema=CUSTOM_VALIDATION_SCHEMA_OPENMRS)
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
self.assertEqual(collection.expansion.concepts.count(), 0)
self.assertEqual(collection.references.count(), 0)
source = OrganizationSourceFactory()
concept = ConceptFactory(parent=source, sources=[source])
concept_expression = concept.uri
collection.add_expressions(dict(expressions=[concept_expression]), collection.created_by)
self.assertEqual(collection.references.count(), 1)
self.assertEqual(collection.references.first().expression, concept.uri)
self.assertEqual(collection.expansion.concepts.count(), 1)
self.assertEqual(collection.expansion.concepts.first(), concept)
concept2 = ConceptFactory(parent=source, sources=[source])
collection.add_expressions(dict(expressions=[concept2.uri]), collection.created_by)
self.assertEqual(collection.expansion.concepts.count(), 2)
self.assertEqual(collection.references.count(), 2)
def test_delete_references(self):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
source = OrganizationSourceFactory()
concept1 = ConceptFactory(parent=source)
concept2 = ConceptFactory(parent=source)
mapping = MappingFactory(from_concept=concept1, to_concept=concept2, parent=source)
collection.add_expressions(dict(expressions=[concept1.uri, concept2.uri, mapping.uri]), collection.created_by)
self.assertEqual(collection.expansion.concepts.count(), 2)
self.assertEqual(collection.expansion.mappings.count(), 1)
self.assertEqual(collection.references.count(), 3)
collection.delete_references(
[concept2.get_latest_version().uri, concept2.uri, mapping.get_latest_version().uri, mapping.uri])
self.assertEqual(collection.expansion.concepts.count(), 1)
self.assertEqual(collection.expansion.mappings.count(), 0)
self.assertEqual(collection.references.count(), 1)
self.assertEqual(collection.expansion.concepts.first().uri, concept1.uri)
self.assertEqual(collection.references.first().expression, concept1.uri)
def test_seed_references(self):
collection1 = OrganizationCollectionFactory()
expansion1 = ExpansionFactory(collection_version=collection1)
collection1.expansion_uri = expansion1.uri
collection1.save()
collection2 = OrganizationCollectionFactory(
version='v1', mnemonic=collection1.mnemonic, organization=collection1.organization
)
expansion2 = ExpansionFactory(collection_version=collection2)
collection2.expansion_uri = expansion2.uri
collection2.save()
self.assertTrue(collection1.is_head)
self.assertFalse(collection2.is_head)
source = OrganizationSourceFactory()
concept = ConceptFactory(parent=source, sources=[source])
concept_expression = concept.uri
collection1.add_expressions(dict(expressions=[concept_expression]), collection1.created_by)
self.assertEqual(collection1.references.count(), 1)
self.assertEqual(collection2.references.count(), 0)
collection2.seed_references()
self.assertEqual(collection1.references.count(), 1)
self.assertEqual(collection2.references.count(), 1)
self.assertEqual(collection1.references.first().expression, collection2.references.first().expression)
self.assertNotEqual(collection1.references.first().id, collection2.references.first().id)
def test_validate_reference_already_exists(self):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
ch_locale = LocalizedTextFactory(locale_preferred=True, locale='ch')
en_locale = LocalizedTextFactory(locale_preferred=True, locale='en')
concept = ConceptFactory(names=[ch_locale, en_locale])
reference = CollectionReference(expression=concept.uri, collection=collection)
reference.save()
self.assertEqual(collection.references.count(), 1)
with self.assertRaises(ValidationError) as ex:
collection.validate(reference)
self.assertEqual(
ex.exception.message_dict,
{
concept.uri: [
'Concept or Mapping reference name must be unique in a collection.'
]
}
)
def test_validate_openmrs_schema_duplicate_locale_type(self):
ch_locale = LocalizedTextFactory(locale_preferred=True, locale='ch')
en_locale = LocalizedTextFactory(locale_preferred=True, locale='en')
concept1 = ConceptFactory(names=[ch_locale, en_locale])
collection = OrganizationCollectionFactory(custom_validation_schema=CUSTOM_VALIDATION_SCHEMA_OPENMRS)
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
expansion.concepts.add(concept1)
concept1_reference = CollectionReference(expression=concept1.uri, collection=collection)
concept1_reference.save()
concept2 = ConceptFactory(names=[ch_locale, en_locale])
concept2_reference = CollectionReference(expression=concept2.uri, collection=collection)
with self.assertRaises(ValidationError) as ex:
collection.validate(concept2_reference)
self.assertEqual(
ex.exception.message_dict,
{'names': ['Concept fully specified name must be unique for same collection and locale.']}
)
def test_validate_openmrs_schema_matching_name_locale(self):
ch_locale = LocalizedTextFactory(locale_preferred=False, locale='ch')
concept1 = ConceptFactory(names=[ch_locale])
collection = OrganizationCollectionFactory(custom_validation_schema=CUSTOM_VALIDATION_SCHEMA_OPENMRS)
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
collection.expansion.concepts.add(concept1)
concept1_reference = CollectionReference(expression=concept1.uri, collection=collection)
concept1_reference.save()
en_locale1 = LocalizedTextFactory(locale='en', locale_preferred=False, name='name')
en_locale2 = LocalizedTextFactory(locale='en', locale_preferred=True, name='name')
concept2 = ConceptFactory(names=[en_locale1, en_locale2])
concept2_reference = CollectionReference(expression=concept2.uri, collection=collection)
with self.assertRaises(ValidationError) as ex:
collection.validate(concept2_reference)
self.assertEqual(
ex.exception.message_dict,
{'names': ['Concept fully specified name must be unique for same collection and locale.']}
)
def test_parent_id(self):
self.assertIsNone(Collection().parent_id)
self.assertEqual(Collection(user_id=1).parent_id, 1)
self.assertEqual(Collection(organization_id=1).parent_id, 1)
def test_last_concept_update(self):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
self.assertIsNone(collection.last_concept_update)
concept = ConceptFactory()
collection.expansion.concepts.add(concept)
self.assertEqual(collection.last_concept_update, concept.updated_at)
def test_last_mapping_update(self):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
self.assertIsNone(collection.last_mapping_update)
mapping = MappingFactory()
collection.expansion.mappings.add(mapping)
self.assertEqual(collection.last_mapping_update, mapping.updated_at)
def test_last_child_update(self):
collection = OrganizationCollectionFactory()
self.assertEqual(collection.last_child_update, collection.updated_at)
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
self.assertEqual(collection.last_child_update, collection.updated_at)
mapping = MappingFactory()
collection.expansion.mappings.add(mapping)
self.assertEqual(collection.last_child_update, mapping.updated_at)
concept = ConceptFactory()
collection.expansion.concepts.add(concept)
self.assertEqual(collection.last_child_update, concept.updated_at)
@patch('core.collections.models.Collection.expansion', new_callable=PropertyMock)
@patch('core.collections.models.Collection.batch_index')
def test_index_children(self, batch_index_mock, expansion_mock):
expansion_mock.return_value = Mock(concepts='concepts-qs', mappings='mappings-qs')
collection = Collection(expansion_uri='foobar')
collection.index_children()
self.assertEqual(batch_index_mock.call_count, 2)
self.assertEqual(batch_index_mock.mock_calls[0].args, ('concepts-qs', ConceptDocument))
self.assertEqual(batch_index_mock.mock_calls[1].args, ('mappings-qs', MappingDocument))
def test_get_cascaded_mapping_uris_from_concept_expressions(self):
concept1 = ConceptFactory()
concept2 = ConceptFactory(parent=concept1.parent)
mapping1 = MappingFactory(
from_concept=concept1, to_concept=concept2, parent=concept1.parent)
mapping2 = MappingFactory(from_concept=concept1)
mapping3 = MappingFactory(to_concept=concept2, parent=concept1.parent)
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
expansion.mappings.set([mapping1, mapping2, mapping3])
expressions = [concept1.get_latest_version().url, concept2.get_latest_version().url]
self.assertEqual(collection.get_cascaded_mapping_uris_from_concept_expressions(expressions), [])
collection.expansion_uri = expansion.url
collection.save()
self.assertEqual(
sorted(collection.get_cascaded_mapping_uris_from_concept_expressions(expressions)),
sorted([mapping1.url, mapping2.url])
)
class CollectionReferenceTest(OCLTestCase):
def test_uri(self):
org = OrganizationFactory(mnemonic='MyOrg')
collection = OrganizationCollectionFactory(organization=org, mnemonic='MyCollection', version='HEAD')
reference = CollectionReference(expression='/foo/bar', collection=collection)
reference.save()
self.assertEqual(reference.uri, f'/orgs/MyOrg/collections/MyCollection/references/{reference.id}/')
collection_v1 = OrganizationCollectionFactory(organization=org, mnemonic='MyCollection', version='v1')
reference = CollectionReference(expression='/foo/bar', collection=collection_v1)
reference.save()
self.assertEqual(reference.uri, f'/orgs/MyOrg/collections/MyCollection/v1/references/{reference.id}/')
def test_invalid_expression(self):
reference = CollectionReference(expression='', collection=OrganizationCollectionFactory())
with self.assertRaises(ValidationError) as ex:
reference.full_clean()
self.assertEqual(
ex.exception.message_dict,
{'expression': ['This field cannot be blank.']}
)
def test_reference_type(self):
reference = CollectionReference(expression='')
self.assertEqual(reference.reference_type, 'concepts')
reference = CollectionReference(expression=None)
self.assertEqual(reference.reference_type, 'concepts')
reference = CollectionReference(
expression='/parent/parent-mnemonic/sources/source-mnemonic/concepts/concept-mnemonic/',
reference_type=None
)
reference.clean()
self.assertEqual(reference.reference_type, 'concepts')
reference = CollectionReference(
expression='/parent/parent-mnemonic/sources/source-mnemonic/concepts/concept-mnemonic/',
reference_type='mappings' # if you set wrong it stays as is
)
reference.clean()
self.assertEqual(reference.reference_type, 'mappings')
reference = CollectionReference(
expression='/parent/parent-mnemonic/sources/source-mnemonic/mappings/mapping-mnemonic/',
reference_type=None
)
reference.clean()
self.assertEqual(reference.reference_type, 'mappings')
reference = CollectionReference(
expression='/parent/parent-mnemonic/sources/source-mnemonic/mappings/mapping-mnemonic/',
reference_type='mappings'
)
reference.clean()
self.assertEqual(reference.reference_type, 'mappings')
def test_reference_as_concept_version(self):
collection = OrganizationCollectionFactory()
concept = ConceptFactory()
expression = concept.uri
reference = CollectionReference(expression=expression, collection=collection)
reference.full_clean()
self.assertEqual(len(reference._concepts), 1) # pylint: disable=protected-access
self.assertTrue(isinstance(reference._concepts[0], Concept)) # pylint: disable=protected-access
self.assertEqual(reference._concepts[0].id, concept.get_latest_version().id) # pylint: disable=protected-access
def test_concept_filter_schema(self):
ref = CollectionReference(expression='/concepts/', filter=None)
ref.clean()
ref = CollectionReference(expression='/concepts/', filter='')
ref.clean()
ref = CollectionReference(expression='/concepts/', filter=[])
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(expression='/concepts/', filter=[{}, {}])
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(expression='/concepts/', filter=[{'foo': 'bar'}])
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(expression='/concepts/', filter=[{'property': 'bar'}])
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(
expression='/concepts/', filter=[{'property': 'map_type', 'value': 'foobar', 'op': '='}])
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(
expression='/concepts/', filter=[{'property': 'concept_class', 'value': 'foobar', 'op': 'foobar'}])
ref.clean()
with self.assertRaises(ValidationError) as ex:
ref = CollectionReference(
expression='/concepts/', filter=[{'property': 'concept_class', 'value': 'foobar', 'op': 'not in'}])
ref.clean()
self.assertEqual(ex.exception.message_dict, dict(filter=['Invalid filter schema.']))
ref = CollectionReference(
expression='/concepts/', filter=[{'property': 'concept_class', 'value': 'foobar', 'op': '='}])
ref.clean()
ref = CollectionReference(
expression='/concepts/', filter=[{'property': 'external_id', 'value': 'foobar', 'op': 'in'}])
ref.clean()
def test_mapping_filter_schema(self):
mapping = MappingFactory()
MappingDocument().update([mapping]) # to create mappings index in ES
ref = CollectionReference(expression='/mappings/', filter=None, reference_type='mappings')
ref.clean()
ref = CollectionReference(expression='/mappings/', filter='', reference_type='mappings')
ref.clean()
ref = CollectionReference(expression='/mappings/', filter=[], reference_type='mappings')
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(expression='/mappings/', filter=[{}, {}], reference_type='mappings')
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(expression='/mappings/', filter=[{'foo': 'bar'}], reference_type='mappings')
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(expression='/mappings/', filter=[{'property': 'bar'}], reference_type='mappings')
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(
expression='/mappings/',
reference_type='mappings',
filter=[{'property': 'concept_class', 'value': 'foobar', 'op': '='}])
ref.clean()
with self.assertRaises(ValidationError):
ref = CollectionReference(
expression='/mappings/',
reference_type='mappings',
filter=[{'property': 'map_type', 'value': 'foobar', 'op': 'foobar'}])
ref.clean()
with self.assertRaises(ValidationError) as ex:
ref = CollectionReference(
expression='/mappings/',
reference_type='mappings',
filter=[{'property': 'map_type', 'value': 'foobar', 'op': 'not in'}])
ref.clean()
self.assertEqual(ex.exception.message_dict, dict(filter=['Invalid filter schema.']))
ref = CollectionReference(
expression='/mappings/',
reference_type='mappings',
filter=[{'property': 'map_type', 'value': 'foobar', 'op': '='}])
ref.clean()
ref = CollectionReference(
expression='/mappings/',
reference_type='mappings',
filter=[{'property': 'external_id', 'value': 'foobar', 'op': 'in'}])
ref.clean()
def test_get_concepts(self): # pylint: disable=too-many-locals,too-many-statements
source = OrganizationSourceFactory()
source_v1 = OrganizationSourceFactory(organization=source.organization, mnemonic=source.mnemonic, version='v1')
coll1 = OrganizationCollectionFactory()
coll1_v1 = OrganizationCollectionFactory(organization=coll1.organization, mnemonic=coll1.mnemonic, version='v1')
coll2 = OrganizationCollectionFactory()
coll2_v1 = OrganizationCollectionFactory(organization=coll2.organization, mnemonic=coll2.mnemonic, version='v1')
concept1 = ConceptFactory(parent=source)
prev_latest_version = concept1.get_latest_version()
prev_latest_version.is_latest_version = True
prev_latest_version.save()
Concept.create_new_version_for(concept1.clone(), {}, concept1.created_by)
concept1_latest = concept1.get_latest_version()
concept1.sources.add(source)
concept1_latest.sources.add(source)
prev_latest_version.sources.add(source_v1)
expansion_coll1 = ExpansionFactory(collection_version=coll1)
coll1.expansion_uri = expansion_coll1.uri
coll1.save()
expansion_coll1_v1 = ExpansionFactory(collection_version=coll1_v1)
coll1_v1.expansion_uri = expansion_coll1_v1.uri
coll1_v1.save()
concept2 = ConceptFactory(parent=source)
expansion_coll1_v1.concepts.add(concept1_latest)
mapping1 = MappingFactory(from_concept=concept1, to_concept=concept2, parent=source)
reference = CollectionReference(system=source.uri, created_by=source.created_by)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 2)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([concept1.id, concept2.id])
)
self.assertEqual(mappings.count(), 0)
reference = CollectionReference(
system=source.uri, created_by=source.created_by, cascade=dict(method='sourcemappings')
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 2)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([concept1.id, concept2.id])
)
self.assertEqual(mappings.count(), 1)
self.assertEqual(mappings.first().id, mapping1.id)
reference = CollectionReference(
system=source.uri,
code=concept1.mnemonic,
created_by=source.created_by,
cascade=dict(method='sourcemappings')
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 1)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([concept1.id])
)
self.assertEqual(mappings.count(), 1)
self.assertEqual(mappings.first().id, mapping1.id)
reference = CollectionReference(
system=source.uri,
code=concept1.mnemonic,
created_by=source.created_by,
cascade=dict(method='sourcetoconcepts')
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 2)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([concept1.id, concept2.id])
)
self.assertEqual(mappings.count(), 1)
self.assertEqual(mappings.first().id, mapping1.id)
reference = CollectionReference(
system=source.uri,
code=concept1.mnemonic,
created_by=source.created_by,
cascade=dict(method='sourcetoconcepts'),
transform='resourceVersions'
)
concepts, mappings = reference.get_concepts()
concepts = concepts.distinct('id')
self.assertEqual(concepts.count(), 2)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))),
sorted([concept1_latest.id, concept2.get_latest_version().id])
)
self.assertEqual(mappings.count(), 1)
self.assertEqual(mappings.first().id, mapping1.get_latest_version().id)
reference = CollectionReference(
system=source.uri,
valueset=[coll1_v1.uri, coll2_v1.uri],
created_by=source.created_by
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 0)
self.assertEqual(mappings.count(), 0)
reference = CollectionReference(
system=source.uri,
valueset=[coll1_v1.uri],
created_by=source.created_by,
transform='resourceversions'
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 1)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([concept1_latest.id])
)
self.assertEqual(mappings.count(), 0)
reference = CollectionReference(
system=source.uri,
created_by=source.created_by,
code=concept1.mnemonic,
transform='resourceversions'
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 1)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([concept1_latest.id])
)
self.assertEqual(mappings.count(), 0)
reference = CollectionReference(
system=source.uri,
created_by=source.created_by,
code=concept1.mnemonic,
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 1)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([concept1.id])
)
self.assertEqual(mappings.count(), 0)
reference = CollectionReference(
system=f"{source.uri}|v1",
created_by=source.created_by,
code=concept1.mnemonic,
resource_version=prev_latest_version.version
)
concepts, mappings = reference.get_concepts()
self.assertEqual(concepts.count(), 1)
self.assertEqual(
sorted(list(concepts.values_list('id', flat=True))), sorted([prev_latest_version.id])
)
self.assertEqual(mappings.count(), 0)
def test_build_expression(self):
self.assertEqual(
CollectionReference(expression='/foobar/').build_expression(), '/foobar/')
self.assertEqual(
CollectionReference(expression='/foobar/', system='http://foo.com').build_expression(), '/foobar/')
reference = CollectionReference(
system='http://source.com',
version='v1',
valueset=['http//coll.com', 'https://coll-global.com'],
namespace='/orgs/MyOrg/',
code='c1',
reference_type='concepts',
resource_version='123'
)
self.assertEqual(reference.build_expression(), 'http://source.com|v1/concepts/c1/123/')
reference = CollectionReference(
system='http://source.com',
version='v1',
namespace='/orgs/MyOrg/',
code='c1',
reference_type='concepts',
resource_version='123'
)
self.assertEqual(reference.build_expression(), 'http://source.com|v1/concepts/c1/123/')
reference = CollectionReference(
system='http://source.com',
namespace='/orgs/MyOrg/',
version='v1',
)
self.assertEqual(reference.build_expression(), 'http://source.com|v1')
reference = CollectionReference(
system='http://source.com',
namespace='/orgs/MyOrg/',
)
self.assertEqual(reference.build_expression(), 'http://source.com')
reference = CollectionReference(
system='/orgs/MyOrg/sources/MySource/',
version='v1',
valueset=['http//coll.com', 'https://coll-global.com'],
namespace='/orgs/MyOrg/',
code='c1',
reference_type='concepts',
resource_version='123'
)
self.assertEqual(reference.build_expression(), '/orgs/MyOrg/sources/MySource/v1/concepts/c1/123/')
reference = CollectionReference(
system='/orgs/MyOrg/sources/MySource/',
reference_type='concepts',
)
self.assertEqual(reference.build_expression(), '/orgs/MyOrg/sources/MySource/concepts/')
reference = CollectionReference(
reference_type='concepts',
)
self.assertEqual(reference.build_expression(), '/concepts/')
reference = CollectionReference(
reference_type='mappings',
)
self.assertEqual(reference.build_expression(), '/mappings/')
reference = CollectionReference(
system='/orgs/MyOrg/sources/MySource/',
reference_type='concepts',
filter=[dict(property='q', value='foo', operator='='), dict(property='name', value='foobar', operator='=')]
)
self.assertEqual(
reference.build_expression(), '/orgs/MyOrg/sources/MySource/concepts/?q=foo&name=foobar'
)
reference = CollectionReference(
valueset=['/orgs/MyOrg/collections/Coll/'],
reference_type='concepts',
filter=[dict(property='q', value='foo', operator='='), dict(property='name', value='foobar', operator='=')]
)
self.assertEqual(
reference.build_expression(), '/orgs/MyOrg/collections/Coll/concepts/?q=foo&name=foobar'
)
reference = CollectionReference(
valueset=['/orgs/MyOrg/collections/Coll/', '/orgs/MyOrg/collections/Coll1/'],
reference_type='concepts',
filter=[dict(property='q', value='foo', operator='='), dict(property='name', value='foobar', operator='=')]
)
self.assertEqual(
reference.build_expression(), '/orgs/MyOrg/collections/Coll/concepts/?q=foo&name=foobar'
)
class CollectionUtilsTest(OCLTestCase):
def test_is_mapping(self):
self.assertFalse(is_mapping(None))
self.assertFalse(is_mapping(''))
self.assertFalse(is_mapping('orgs/org-1/sources/source-1/mapping/'))
self.assertTrue(is_mapping('orgs/org-1/sources/source-1/mappings/'))
self.assertTrue(is_mapping('users/user-1/sources/source-1/mappings/'))
self.assertTrue(is_mapping('users/user-1/collections/coll-1/mappings/'))
self.assertTrue(is_mapping('/mappings/'))
def test_is_concept(self):
self.assertFalse(is_concept(None))
self.assertFalse(is_concept(''))
self.assertFalse(is_concept('orgs/org-1/sources/source-1/concept/'))
self.assertTrue(is_concept('orgs/org-1/sources/source-1/concepts/'))
self.assertTrue(is_concept('users/user-1/sources/source-1/concepts/'))
self.assertTrue(is_concept('users/user-1/collections/coll-1/concepts/'))
self.assertTrue(is_concept('/concepts/'))
def test_is_version_specified(self):
concept_head = ConceptFactory()
concept_v1 = ConceptFactory(
parent=concept_head.parent, version='v1', mnemonic=concept_head.mnemonic, versioned_object=concept_head
)
self.assertTrue(is_version_specified(concept_v1.uri))
self.assertFalse(is_version_specified(concept_head.uri))
def test_get_concept_by_expression(self):
concept_head = ConceptFactory()
concept_v1 = ConceptFactory(
parent=concept_head.parent, version='v1', mnemonic=concept_head.mnemonic, versioned_object=concept_head
)
self.assertEqual(get_concept_by_expression(concept_head.uri), concept_head)
self.assertEqual(get_concept_by_expression(concept_v1.uri), concept_v1)
self.assertIsNone(get_concept_by_expression('/foobar/'))
class TasksTest(OCLTestCase):
def test_add_references_task(self):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
concept1 = ConceptFactory()
concept2 = ConceptFactory()
mapping1 = MappingFactory(
parent=concept2.parent,
from_concept=concept2.get_latest_version(),
to_concept=concept1.get_latest_version()
)
mapping2 = MappingFactory()
errors = add_references(
collection.created_by.id,
dict(expressions=[
obj.get_latest_version().url for obj in [concept1, concept2, mapping2]
]),
collection.id,
'sourcemappings'
)
self.assertEqual(errors, {})
self.assertListEqual(
sorted(list(
collection.references.values_list('expression', flat=True)
)),
sorted([
concept1.get_latest_version().url, concept2.get_latest_version().url,
mapping1.url, mapping2.get_latest_version().url,
])
)
self.assertEqual(
sorted(list(expansion.concepts.values_list('uri', flat=True))),
sorted([concept1.get_latest_version().url, concept2.get_latest_version().url])
)
self.assertEqual(
sorted(list(expansion.mappings.values_list('uri', flat=True))),
sorted([mapping1.url, mapping2.get_latest_version().url])
)
@patch('core.collections.models.Collection.index_children')
@patch('core.common.tasks.export_collection')
def test_seed_children_task(self, export_collection_task, index_children_mock):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
concept = ConceptFactory()
mapping = MappingFactory()
concept_latest_version = concept.get_latest_version()
mapping_latest_version = mapping.get_latest_version()
collection.add_expressions(
dict(expressions=[concept_latest_version.version_url, mapping_latest_version.version_url]),
collection.created_by
)
self.assertEqual(collection.references.count(), 2)
self.assertEqual(collection.expansion.concepts.count(), 1)
self.assertEqual(collection.expansion.mappings.count(), 1)
collection_v1 = OrganizationCollectionFactory(
organization=collection.organization, version='v1', mnemonic=collection.mnemonic
)
self.assertEqual(collection_v1.expansions.count(), 0)
self.assertEqual(collection_v1.references.count(), 0)
seed_children_to_new_version('collection', collection_v1.id, False) # pylint: disable=no-value-for-parameter
collection_v1.refresh_from_db()
self.assertEqual(collection_v1.expansions.count(), 1)
self.assertEqual(collection_v1.references.count(), 2)
expansion = collection_v1.expansion
self.assertEqual(expansion.concepts.count(), 1)
self.assertEqual(expansion.mappings.count(), 1)
export_collection_task.delay.assert_not_called()
index_children_mock.assert_not_called()
@patch('core.collections.models.Collection.index_children')
@patch('core.common.tasks.export_collection')
def test_seed_children_task_with_export(self, export_collection_task, index_children_mock):
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.uri
collection.save()
concept = ConceptFactory()
mapping = MappingFactory()
concept_latest_version = concept.get_latest_version()
mapping_latest_version = mapping.get_latest_version()
collection.add_expressions(
dict(expressions=[concept_latest_version.version_url, mapping_latest_version.version_url]),
collection.created_by
)
self.assertEqual(collection.references.count(), 2)
self.assertEqual(collection.expansion.concepts.count(), 1)
self.assertEqual(collection.expansion.mappings.count(), 1)
collection_v1 = OrganizationCollectionFactory(
organization=collection.organization, version='v1', mnemonic=collection.mnemonic
)
self.assertEqual(collection_v1.expansions.count(), 0)
self.assertEqual(collection_v1.references.count(), 0)
seed_children_to_new_version('collection', collection_v1.id) # pylint: disable=no-value-for-parameter
collection_v1.refresh_from_db()
self.assertEqual(collection_v1.expansions.count(), 1)
self.assertEqual(collection_v1.references.count(), 2)
expansion = collection_v1.expansions.first()
self.assertEqual(expansion.concepts.count(), 1)
self.assertEqual(expansion.mappings.count(), 1)
export_collection_task.delay.assert_called_once_with(collection_v1.id)
index_children_mock.assert_called_once()
def test_update_collection_active_mappings_count(self):
mapping1 = MappingFactory()
mapping2 = MappingFactory(retired=True)
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
collection.expansion_uri = expansion.url
expansion.mappings.add(mapping1)
expansion.mappings.add(mapping2)
collection.save()
self.assertEqual(collection.active_mappings, None)
update_collection_active_mappings_count(collection.id)
collection.refresh_from_db()
self.assertEqual(collection.active_mappings, 1)
def test_update_collection_active_concepts_count(self):
concept1 = ConceptFactory()
concept2 = ConceptFactory(retired=True)
collection = OrganizationCollectionFactory()
expansion = ExpansionFactory(collection_version=collection)
expansion.concepts.add(concept1)
expansion.concepts.add(concept2)
collection.expansion_uri = expansion.url
collection.save()
self.assertEqual(collection.active_concepts, None)
update_collection_active_concepts_count(collection.id)
collection.refresh_from_db()
self.assertEqual(collection.active_concepts, 1)
class ExpansionTest(OCLTestCase):
@patch('core.collections.models.seed_children_to_expansion')
def test_persist_without_mnemonic(self, seed_children_to_expansion_mock):
collection = OrganizationCollectionFactory()
expansion = Expansion.persist(index=False, collection_version=collection)
self.assertIsNotNone(expansion.id)
self.assertEqual(expansion.id, expansion.mnemonic)
self.assertEqual(expansion.collection_version, collection)
seed_children_to_expansion_mock.assert_called_once_with(expansion.id, False)
@patch('core.collections.models.seed_children_to_expansion')
def test_persist_with_mnemonic(self, seed_children_to_expansion_mock):
collection = OrganizationCollectionFactory()
expansion = Expansion.persist(index=True, mnemonic='e1', collection_version=collection)
self.assertIsNotNone(expansion.id)
self.assertEqual(expansion.mnemonic, 'e1')
self.assertNotEqual(expansion.id, expansion.mnemonic)
self.assertEqual(expansion.collection_version, collection)
seed_children_to_expansion_mock.assert_called_once_with(expansion.id, True)
def test_owner_url(self):
self.assertEqual(
Expansion(uri='/orgs/org/collections/coll/HEAD/expansions/e1/').owner_url, '/orgs/org/')
self.assertEqual(
Expansion(uri='/users/user/collections/coll/HEAD/expansions/e1/').owner_url, '/users/user/')
self.assertEqual(
Expansion(uri='/orgs/org/collections/coll/v1/expansions/e1/').owner_url, '/orgs/org/')
self.assertEqual(
Expansion(uri='/orgs/org/collections/coll/expansions/e1/').owner_url, '/orgs/org/')
def test_expansion(self):
self.assertEqual(Expansion(mnemonic='e1').expansion, 'e1')
def test_get_url_kwarg(self):
self.assertEqual(Expansion().get_url_kwarg(), 'expansion')
def test_get_resource_url_kwarg(self):
self.assertEqual(Expansion().get_resource_url_kwarg(), 'expansion')
def test_is_default(self):
self.assertTrue(Expansion(uri='foobar', collection_version=Collection(expansion_uri='foobar')).is_default)
self.assertFalse(Expansion(uri='foobar', collection_version=Collection(expansion_uri='foo')).is_default)
self.assertFalse(Expansion(uri='foobar', collection_version=Collection(expansion_uri=None)).is_default)
def test_clean(self):
expansion = Expansion(parameters=None)
expansion.clean()
self.assertIsNotNone(expansion.parameters)
def test_delete_expressions_all(self):
collection = OrganizationCollectionFactory()
concept1 = ConceptFactory()
concept2 = ConceptFactory(parent=concept1.parent)
mapping = MappingFactory(
from_concept=concept1, to_concept=concept2, parent=concept1.parent)
expansion = ExpansionFactory(collection_version=collection)
expansion.concepts.set([concept1, concept2])
expansion.mappings.set([mapping])
expansion.delete_expressions('*')
self.assertEqual(expansion.concepts.count(), 0)
self.assertEqual(expansion.mappings.count(), 0)
def test_delete_expressions_specific(self):
collection = OrganizationCollectionFactory()
concept1 = ConceptFactory()
concept2 = ConceptFactory(parent=concept1.parent)
mapping1 = MappingFactory(
from_concept=concept1, to_concept=concept2, parent=concept1.parent)
mapping2 = MappingFactory(
to_concept=concept1, from_concept=concept2, parent=concept1.parent)