-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindexing_api_test.py
997 lines (900 loc) · 41.7 KB
/
indexing_api_test.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
"""
Tests for search API functions.
"""
import itertools
from unittest.mock import patch
from ddt import (
data,
ddt,
unpack,
)
from django.conf import settings
from django.db.models.signals import post_save
from django.test import override_settings
from opensearchpy.exceptions import NotFoundError
from factory.django import mute_signals
from dashboard.factories import (
CachedCertificateFactory,
CachedEnrollmentFactory,
CachedCurrentGradeFactory,
ProgramEnrollmentFactory
)
from dashboard.models import ProgramEnrollment
from dashboard.serializers import UserProgramSearchSerializer
from courses.factories import (
ProgramFactory,
CourseFactory,
CourseRunFactory,
)
from profiles.factories import (
EducationFactory,
EmploymentFactory,
ProfileFactory,
)
from profiles.serializers import (
ProfileSerializer
)
from roles.models import Role
from roles.roles import (
Instructor,
Staff
)
from search.base import ESTestCase, reindex_test_es_data
from search.connection import (
ALL_INDEX_TYPES,
get_aliases,
get_default_alias,
make_alias_name,
make_backing_index_name,
GLOBAL_DOC_TYPE,
PERCOLATE_INDEX_TYPE,
PUBLIC_ENROLLMENT_INDEX_TYPE,
PRIVATE_ENROLLMENT_INDEX_TYPE,
)
from search.exceptions import ReindexException
from search.factories import PercolateQueryFactory
from search.indexing_api import (
clear_and_create_index,
delete_indices,
get_conn,
refresh_index,
index_program_enrolled_users,
remove_program_enrolled_user,
serialize_program_enrolled_user,
serialize_public_enrolled_user,
filter_current_work,
index_percolate_queries,
delete_percolate_query, create_backing_indices,
)
from search.models import PercolateQuery
from search.util import traverse_mapping
DOC_TYPES_PER_ENROLLMENT = 1
# pylint: disable=too-many-lines
class ESTestActions:
"""
Provides helper functions for tests to communicate with ES
"""
def __init__(self):
self.conn = get_conn(verify=False)
def search(self, index_type):
"""Gets full index data from the _search endpoint"""
alias = get_default_alias(index_type)
refresh_index(alias)
return self.conn.search(index=alias)['hits']
def get_percolate_query(self, _id):
"""Get percolate query"""
index = get_default_alias(PERCOLATE_INDEX_TYPE)
return self.conn.get(id=_id, doc_type=GLOBAL_DOC_TYPE, index=index)
def get_mappings(self, index_type):
"""Gets mapping data"""
alias = get_default_alias(index_type)
refresh_index(alias)
mapping = self.conn.indices.get_mapping(index=alias)
return list(mapping.values())[0]['mappings']
def get_default_backing_index(self, index_type):
"""Get the default backing index"""
alias = get_default_alias(index_type)
return list(self.conn.indices.get_alias(name=alias).keys())[0]
es = ESTestActions()
def get_sources(results):
"""
Get sources from hits, sorted by source id
Args:
results (dict): Opensearch results
Returns:
list of dict: The list of source dicts
"""
sorted_hits = sorted(results['hits'], key=lambda hit: hit['_source']['id'])
return [hit['_source'] for hit in sorted_hits]
def remove_es_keys(hit):
"""
Removes ES keys from a hit object in-place
Args:
hit (dict): Opensearch hit object
Returns:
dict: modified Opensearch hit object
"""
del hit['_id']
if '_type' in hit:
del hit['_type']
return hit
def assert_search(results, program_enrollments, *, index_type):
"""
Assert that search results match program-enrolled users
"""
assert results['total']['value'] == len(program_enrollments) * DOC_TYPES_PER_ENROLLMENT
sources_advanced = get_sources(results)
sorted_program_enrollments = sorted(program_enrollments, key=lambda program_enrollment: program_enrollment.id)
if index_type == PRIVATE_ENROLLMENT_INDEX_TYPE:
serialized = [
remove_es_keys(serialize_program_enrolled_user(program_enrollment))
for program_enrollment in sorted_program_enrollments
]
elif index_type == PUBLIC_ENROLLMENT_INDEX_TYPE:
serialized = [
remove_es_keys(serialize_public_enrolled_user(
serialize_program_enrolled_user(program_enrollment)
))
for program_enrollment in sorted_program_enrollments
]
else:
raise Exception("Unexpected index type")
assert serialized == sources_advanced
# pylint: disable=unused-argument
@ddt
@patch('search.signals.transaction.on_commit', side_effect=lambda callback: callback())
class IndexTests(ESTestCase):
"""
Tests for indexing
"""
# pylint: disable=too-many-public-methods
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_program_enrollment_add(self, index_type, mock_on_commit):
"""
Test that a newly created ProgramEnrollment is indexed properly
"""
assert es.search(index_type)['total']['value'] == 0
program_enrollment = ProgramEnrollmentFactory.create()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_program_enrollment_delete(self, index_type, mock_on_commit):
"""
Test that ProgramEnrollment is removed from index after the user is removed
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
program_enrollment.user.delete()
assert es.search(index_type)['total']['value'] == 0
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_profile_update(self, index_type, mock_on_commit):
"""
Test that ProgramEnrollment is reindexed after the User's Profile has been updated
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
profile = program_enrollment.user.profile
profile.first_name = 'updated'
profile.save()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_education_add(self, index_type, mock_on_commit):
"""
Test that Education is indexed after being added
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
EducationFactory.create(profile=program_enrollment.user.profile)
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_education_update(self, index_type, mock_on_commit):
"""
Test that Education is reindexed after being updated
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
education = EducationFactory.create(profile=program_enrollment.user.profile)
education.school_city = 'city'
education.save()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_education_delete(self, index_type, mock_on_commit):
"""
Test that Education is removed from index after being deleted
"""
program_enrollment = ProgramEnrollmentFactory.create()
education = EducationFactory.create(profile=program_enrollment.user.profile)
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
education.delete()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_employment_add(self, index_type, mock_on_commit):
"""
Test that Employment is indexed after being added
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
EmploymentFactory.create(profile=program_enrollment.user.profile, end_date=None)
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_employment_update(self, index_type, mock_on_commit):
"""
Test that Employment is reindexed after being updated
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
employment = EmploymentFactory.create(profile=program_enrollment.user.profile, end_date=None)
employment.city = 'city'
employment.save()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_employment_delete(self, index_type, mock_on_commit):
"""
Test that Employment is removed from index after being deleted
"""
program_enrollment = ProgramEnrollmentFactory.create()
employment = EmploymentFactory.create(profile=program_enrollment.user.profile, end_date=None)
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
employment.delete()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_past_employment_add(self, index_type, mock_on_commit):
"""
Test that past work history is not indexed
"""
program_enrollment = ProgramEnrollmentFactory.create()
EmploymentFactory.create(profile=program_enrollment.user.profile, end_date=None)
EmploymentFactory.create(profile=program_enrollment.user.profile)
search_result = es.search(index_type)['hits'][0]['_source']['profile']['work_history']
assert len(search_result) == 1
self.assertFalse(search_result[0]['end_date'])
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_remove_program_enrolled_user(self, index_type, mock_on_commit):
"""
Test that remove_program_enrolled_user removes the user from the index for that program
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
remove_program_enrolled_user(program_enrollment.id)
assert_search(es.search(index_type), [], index_type=index_type)
# pylint: disable=too-many-locals
def test_index_program_enrolled_users(self, mock_on_commit):
"""
Test that index_program_enrolled_users indexes an iterable of program-enrolled users
"""
num_enrollments = 10
chunk_size = 4
with mute_signals(post_save):
program_enrollments = [
ProgramEnrollmentFactory.create() for _ in range(num_enrollments)
]
for enrollment in program_enrollments:
ProfileFactory.create(user=enrollment.user)
private = [serialize_program_enrolled_user(enrollment) for enrollment in program_enrollments]
private_dicts = {serialized['id']: serialized for serialized in private}
public = [serialize_public_enrolled_user(serialized) for serialized in private]
public_dicts = {serialized['id']: serialized for serialized in public}
with patch(
'search.indexing_api._index_chunk', autospec=True, return_value=0
) as index_chunk, patch(
'search.indexing_api.serialize_program_enrolled_user', autospec=True,
side_effect=lambda x: private_dicts[x.id]
) as serialize_mock, patch(
'search.indexing_api.serialize_public_enrolled_user', autospec=True,
side_effect=lambda x: public_dicts[x['id']]
) as serialize_public_mock:
index_program_enrolled_users(program_enrollments, chunk_size=chunk_size)
assert index_chunk.call_count == 6 # 10 enrollments divided in chunks of 4, times the number of types (2)
public_index = make_alias_name(PUBLIC_ENROLLMENT_INDEX_TYPE, is_reindexing=False)
private_index = make_alias_name(PRIVATE_ENROLLMENT_INDEX_TYPE, is_reindexing=False)
for offset in range(0, num_enrollments, chunk_size):
# each enrollment should get yielded twice to account for each doctype
index_chunk.assert_any_call(
public[offset:offset+4], # ordered dicts FTW
index=public_index
)
index_chunk.assert_any_call(
private[offset:offset+4],
index=private_index
)
assert serialize_mock.call_count == len(program_enrollments)
assert serialize_public_mock.call_count == len(program_enrollments)
for enrollment in program_enrollments:
serialize_mock.assert_any_call(enrollment)
serialize_public_mock.assert_any_call(private_dicts[enrollment.id])
def test_index_program_enrolled_users_missing_profiles(self, mock_on_commit):
"""
Test that index_program_enrolled_users doesn't index users missing profiles
"""
with mute_signals(post_save):
program_enrollments = [ProgramEnrollmentFactory.build() for _ in range(10)]
with patch(
'search.indexing_api._index_chunk', autospec=True, return_value=0
) as index_chunk, patch(
'search.indexing_api.serialize_program_enrolled_user',
autospec=True,
side_effect=lambda x: None # simulate a missing profile
) as serialize_mock, patch(
'search.indexing_api.serialize_public_enrolled_user', autospec=True, side_effect=lambda x: x
) as serialize_public_mock:
index_program_enrolled_users(program_enrollments)
assert index_chunk.call_count == 0
assert serialize_public_mock.call_count == 0
assert serialize_mock.call_count == len(program_enrollments)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_add_edx_record(self, index_type, mock_on_commit):
"""
Test that cached edX records are indexed after being added
"""
program_enrollment = ProgramEnrollmentFactory.create()
for edx_cached_model_factory in [CachedCertificateFactory, CachedEnrollmentFactory, CachedCurrentGradeFactory]:
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
course = CourseFactory.create(program=program_enrollment.program)
course_run = CourseRunFactory.create(course=course)
edx_cached_model_factory.create(user=program_enrollment.user, course_run=course_run)
index_program_enrolled_users([program_enrollment])
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_update_edx_record(self, index_type, mock_on_commit):
"""
Test that a cached edX record is reindexed after being updated
"""
program_enrollment = ProgramEnrollmentFactory.create()
for edx_cached_model_factory in [CachedCertificateFactory, CachedEnrollmentFactory, CachedCurrentGradeFactory]:
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
course = CourseFactory.create(program=program_enrollment.program)
course_run = CourseRunFactory.create(course=course)
edx_record = edx_cached_model_factory.create(user=program_enrollment.user, course_run=course_run)
index_program_enrolled_users([program_enrollment])
edx_record.data.update({'new': 'data'})
edx_record.save()
index_program_enrolled_users([program_enrollment])
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
@data(PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE)
def test_delete_edx_record(self, index_type, mock_on_commit):
"""
Test that a cached edX record is removed from index after being deleted
"""
program_enrollment = ProgramEnrollmentFactory.create()
for edx_cached_model_factory in [CachedCertificateFactory, CachedEnrollmentFactory, CachedCurrentGradeFactory]:
course = CourseFactory.create(program=program_enrollment.program)
course_run = CourseRunFactory.create(course=course)
edx_record = edx_cached_model_factory.create(user=program_enrollment.user, course_run=course_run)
index_program_enrolled_users([program_enrollment])
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
edx_record.delete()
index_program_enrolled_users([program_enrollment])
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
def test_analyzed(self, mock_on_commit):
"""
Most string fields in the mapping should be 'analyzed' since we don't want to
tokenize strings arbitrarily when filtering on fields.
"""
program_enrollment = ProgramEnrollmentFactory.create()
EducationFactory.create(profile=program_enrollment.user.profile)
EmploymentFactory.create(profile=program_enrollment.user.profile)
for index_type in ALL_INDEX_TYPES:
mapping = es.get_mappings(index_type)
nodes = list(traverse_mapping(mapping, ""))
for key, node in nodes:
if key == "folded":
assert node['analyzer'] == "folding"
elif node.get('type') == 'string':
assert node['index'] == 'not_analyzed'
def test_folded(self, mock_on_commit):
"""
Check that we have a folded type for first_name, last_name, and preferred_name
"""
program_enrollment = ProgramEnrollmentFactory.create()
EducationFactory.create(profile=program_enrollment.user.profile)
EmploymentFactory.create(profile=program_enrollment.user.profile)
for index_type in ALL_INDEX_TYPES:
mapping = es.get_mappings(index_type)
properties = mapping['properties']
if index_type == PUBLIC_ENROLLMENT_INDEX_TYPE:
# Make sure we aren't exposing people's email addresses
assert 'email' not in properties
else:
assert properties['email']['fields']['folded']['analyzer'] == 'folding'
profile_properties = properties['profile']['properties']
for key in 'first_name', 'last_name', 'preferred_name', 'full_name', 'username':
assert profile_properties[key]['fields']['folded']['analyzer'] == 'folding'
@data(
*itertools.product(
[Staff.ROLE_ID, Instructor.ROLE_ID],
[PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE],
)
)
@unpack
def test_role_add(self, role, index_type, mock_on_commit):
"""
Test that `is_learner` status is change when role is save
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
sources = get_sources(es.search(index_type))
# user is learner
assert sources[0]['program']['is_learner'] is True
Role.objects.create(
user=program_enrollment.user,
program=program_enrollment.program,
role=role
)
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
# user is not learner
sources = get_sources(es.search(index_type))
assert sources[0]['program']['is_learner'] is False
@data(
*itertools.product(
[Staff.ROLE_ID, Instructor.ROLE_ID],
[PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE],
)
)
@unpack
def test_role_delete(self, role, index_type, mock_on_commit):
"""
Test that `is_learner` status is restore once role is removed for a user.
"""
program_enrollment = ProgramEnrollmentFactory.create()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
sources = get_sources(es.search(index_type))
# user is learner
assert sources[0]['program']['is_learner'] is True
Role.objects.create(
user=program_enrollment.user,
program=program_enrollment.program,
role=role
)
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
# user is not learner
sources = get_sources(es.search(index_type))
assert sources[0]['program']['is_learner'] is False
# when staff role is deleted
Role.objects.filter(
user=program_enrollment.user,
program=program_enrollment.program,
role=role
).delete()
assert es.search(index_type)['total']['value'] == DOC_TYPES_PER_ENROLLMENT
sources = get_sources(es.search(index_type))
# user is learner
assert sources[0]['program']['is_learner'] is True
class SerializerTests(ESTestCase):
"""
Tests for document serializers
"""
@classmethod
def setUpTestData(cls):
with mute_signals(post_save):
cls.profile = ProfileFactory.create()
EducationFactory.create(profile=cls.profile)
EmploymentFactory.create(profile=cls.profile)
EmploymentFactory.create(profile=cls.profile, end_date=None)
program = ProgramFactory.create()
course = CourseFactory.create(program=program)
course_runs = [CourseRunFactory.create(course=course) for _ in range(2)]
for course_run in course_runs:
CachedCertificateFactory.create(user=cls.profile.user, course_run=course_run)
CachedEnrollmentFactory.create(user=cls.profile.user, course_run=course_run)
cls.program_enrollment = ProgramEnrollment.objects.create(user=cls.profile.user, program=program)
def test_program_enrolled_user_serializer(self):
"""
Asserts the output of the serializer for program-enrolled users (ProgramEnrollments)
"""
profile = self.profile
program_enrollment = self.program_enrollment
assert serialize_program_enrolled_user(program_enrollment) == {
'_id': program_enrollment.id,
'id': program_enrollment.id,
'user_id': profile.user.id,
'email': profile.user.email,
'profile': filter_current_work(ProfileSerializer(profile).data),
'program': UserProgramSearchSerializer.serialize(program_enrollment)
}
def test_public_enrolled_user_serializer(self):
"""
Asserts the output of the public serializer for program-enrolled users (ProgramEnrollments)
"""
profile = self.profile
program_enrollment = self.program_enrollment
serialized = serialize_program_enrolled_user(program_enrollment)
assert serialize_public_enrolled_user(serialized) == {
'_id': program_enrollment.id,
'id': program_enrollment.id,
'user_id': profile.user.id,
'profile': {
'first_name': profile.first_name,
'last_name': profile.last_name,
'full_name': profile.full_name,
'preferred_name': profile.preferred_name,
'romanized_first_name': profile.romanized_first_name,
'romanized_last_name': profile.romanized_last_name,
'image': '/media/{}'.format(profile.image),
'image_small': '/media/{}'.format(profile.image_small),
'image_medium': '/media/{}'.format(profile.image_medium),
'username': profile.user.username,
'filled_out': profile.filled_out,
'account_privacy': profile.account_privacy,
'country': profile.country,
'state_or_territory': profile.state_or_territory,
'city': profile.city,
'birth_country': profile.birth_country,
'work_history': serialized['profile']['work_history'],
},
'program': {
'id': program_enrollment.program.id,
'enrollments': [{
'course_title': enrollment['course_title'],
'semester': enrollment['semester']
} for enrollment in serialized['program']['enrollments']],
'courses': [{
'course_title': enrollment['course_title']
} for enrollment in serialized['program']['courses']],
'course_runs': [{
'semester': semester_enrolled['semester']
} for semester_enrolled in serialized['program']['course_runs']],
'is_learner': True,
'total_courses': 1,
}
}
@ddt
class GetConnTests(ESTestCase):
"""
Tests for get_conn
"""
def setUp(self):
"""
Start without any index
"""
super().setUp()
conn = get_conn(verify=False)
for index in conn.indices.get_alias().keys():
if index.startswith(settings.OPENSEARCH_INDEX):
conn.indices.delete(index)
# Clear globals
from search import indexing_api
indexing_api._CONN = None # pylint: disable=protected-access
indexing_api._CONN_VERIFIED = False # pylint: disable=protected-access
def test_no_index(self):
"""
Test that an error is raised if we don't have an index
"""
with self.assertRaises(ReindexException) as ex:
get_conn()
assert "Unable to find index" in str(ex.exception)
def test_no_index_not_default(self):
"""
Test that an error is raised if we don't have an index
"""
# Reset default index so it does not cause an error
reindex_test_es_data()
other_index = "other"
delete_indices()
with self.assertRaises(ReindexException) as ex:
get_conn(verify_indices=[other_index])
assert str(ex.exception) == "Unable to find index {}".format(other_index)
@data(
[False, PRIVATE_ENROLLMENT_INDEX_TYPE, ('testindex_private_enrollment_default',)],
[False, PUBLIC_ENROLLMENT_INDEX_TYPE, ('testindex_public_enrollment_default',)],
[False, PERCOLATE_INDEX_TYPE, ('testindex_percolate_default',)],
[True, PRIVATE_ENROLLMENT_INDEX_TYPE,
('testindex_private_enrollment_default', 'testindex_private_enrollment_reindexing')],
[True, PUBLIC_ENROLLMENT_INDEX_TYPE,
('testindex_public_enrollment_default', 'testindex_public_enrollment_reindexing')],
[True, PERCOLATE_INDEX_TYPE,
('testindex_percolate_default', 'testindex_percolate_reindexing')],
)
@override_settings(OPENSEARCH_INDEX='testindex')
@unpack
# pylint: disable=too-many-arguments
def test_get_aliases(self, is_reindex, index_type, expected_indices):
"""
We should choose the correct alias and doc type given the circumstances
"""
conn = get_conn(verify=False)
alias = make_alias_name(index_type, is_reindexing=False)
backing_index = make_backing_index_name()
# Skip the mapping because it's invalid for 2.x schema, and we don't need it here
clear_and_create_index(backing_index, index_type=index_type, skip_mapping=True)
conn.indices.put_alias(index=backing_index, name=alias)
if is_reindex:
conn.indices.put_alias(index=backing_index, name=make_alias_name(index_type, is_reindexing=True))
aliases = get_aliases(index_type)
assert aliases == list(expected_indices)
assert get_default_alias(index_type) == aliases[0]
@ddt
class RecreateIndexTests(ESTestCase):
"""
Tests for management commands
"""
def tearDown(self):
super().tearDown()
conn = get_conn(verify=False)
for index in conn.indices.get_mapping().keys():
conn.indices.delete(index=index)
@data(PUBLIC_ENROLLMENT_INDEX_TYPE, PRIVATE_ENROLLMENT_INDEX_TYPE)
def test_create_index(self, index_type):
"""
Test that recreate_index will create an index and let search successfully
"""
assert es.search(index_type)['total']['value'] == 0
@data(*itertools.product(
[True, False],
[PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE],
))
@unpack
def test_keep_alias(self, existing_temp_alias, index_type):
"""
Test that recreate_index will point an existing alias at a new backing index
"""
conn = get_conn(verify=False)
default_alias = make_alias_name(index_type, is_reindexing=False)
temp_alias = make_alias_name(index_type, is_reindexing=True)
assert conn.indices.exists_alias(name=temp_alias) is False
if existing_temp_alias:
# Create a temp alias to assert that it doesn't change anything
backing_index = "{}_backing".format(temp_alias)
conn.indices.create(backing_index)
conn.indices.put_alias(name=temp_alias, index=backing_index)
old_backing_indexes = list(conn.indices.get_alias(name=default_alias).keys())
assert len(old_backing_indexes) == 1
reindex_test_es_data()
new_backing_indexes = list(conn.indices.get_alias(name=default_alias).keys())
assert len(new_backing_indexes) == 1
# Backing index should have changed
assert old_backing_indexes != new_backing_indexes
# Temp index should have been deleted
assert conn.indices.exists_alias(name=temp_alias) is False
@data(PUBLIC_ENROLLMENT_INDEX_TYPE, PRIVATE_ENROLLMENT_INDEX_TYPE)
def test_update_index(self, index_type):
"""
Test that recreate_index will clear old data and index all profiles
"""
with patch('search.signals.transaction.on_commit', side_effect=lambda callback: callback()):
program_enrollment = ProgramEnrollmentFactory.create()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
remove_program_enrolled_user(program_enrollment.id)
assert_search(es.search(index_type), [], index_type=index_type)
# recreate_index should index the program-enrolled user
reindex_test_es_data()
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
def test_update_during_recreate_index(self):
"""
If an indexing action happens during a recreate_index it should update all active indices
"""
conn = get_conn(verify=False)
reindex_test_es_data()
temp_aliases = {}
index_types = [PRIVATE_ENROLLMENT_INDEX_TYPE, PUBLIC_ENROLLMENT_INDEX_TYPE]
for index_type in index_types:
# create temporary index
temp_index = make_backing_index_name()
temp_alias = make_alias_name(index_type=index_type, is_reindexing=True)
clear_and_create_index(temp_index, index_type=index_type)
conn.indices.put_alias(index=temp_index, name=temp_alias)
temp_aliases[index_type] = temp_alias
with patch('search.signals.transaction.on_commit', side_effect=lambda callback: callback()):
program_enrollment = ProgramEnrollmentFactory.create()
for index_type in index_types:
assert_search(es.search(index_type), [program_enrollment], index_type=index_type)
# Temp alias should get updated
temp_alias = temp_aliases[index_type]
refresh_index(temp_alias)
temp_hits = conn.search(index=temp_alias)['hits']
assert_search(temp_hits, [program_enrollment], index_type=index_type)
class PercolateQueryTests(ESTestCase):
"""
Tests for indexing of percolate queries
"""
def test_index_percolate_query(self):
"""Test that we index the percolate query"""
query = {"query": {"match": {"profile.first_name": "here"}}}
percolate_query = PercolateQueryFactory.create(query=query, original_query="original")
percolate_query_id = 123
percolate_query.id = percolate_query_id
# Don't save since that will trigger a signal which will update the index
with self.assertRaises(NotFoundError):
es.get_percolate_query(percolate_query_id)
index_percolate_queries([percolate_query])
assert es.get_percolate_query(percolate_query_id) == {
'_id': str(percolate_query_id),
'_index': es.get_default_backing_index(PERCOLATE_INDEX_TYPE),
'_source': query,
'_seq_no': 0,
'_primary_term': 1,
'_type': GLOBAL_DOC_TYPE,
'_version': 1,
'found': True,
}
def test_delete_percolate_queries(self):
"""Test that we delete the percolate query from the index"""
query = {"query": {"match": {"profile.first_name": "here"}}}
with patch('search.signals.transaction', on_commit=lambda callback: callback()):
percolate_query = PercolateQueryFactory.create(query=query, original_query="original")
assert es.get_percolate_query(percolate_query.id) == {
'_id': str(percolate_query.id),
'_index': es.get_default_backing_index(PERCOLATE_INDEX_TYPE),
'_source': query,
'_seq_no': 0,
'_primary_term': 1,
'_type': GLOBAL_DOC_TYPE,
'_version': 1,
'found': True,
}
delete_percolate_query(percolate_query.id)
with self.assertRaises(NotFoundError):
es.get_percolate_query(percolate_query.id)
# If we delete it again there should be no exception
delete_percolate_query(percolate_query.id)
with self.assertRaises(NotFoundError):
es.get_percolate_query(percolate_query.id)
def test_fix_percolate_query(self):
"""
Make sure all nested -> filter are replaced with nested -> query
"""
input_query = {
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"term": {
"program.is_learner": True
}
}
],
"should": [
{
"term": {
"program.id": 34
}
}
],
"minimum_should_match": 1
}
},
{
"term": {
"profile.filled_out": True
}
},
{
"bool": {
"must": [
{
"nested": {
"path": "program.course_runs",
"filter": {
"term": {
"program.course_runs.semester": "2015 - Summer"
}
}
}
},
{
"term": {
"program.id": 34
}
}
]
}
}
]
}
}
}
query = PercolateQueryFactory.create(query=input_query)
assert index_percolate_queries([query]) == 1
def test_fix_field_error(self):
"""recreate_index should not cause any error with this percolate query"""
query = {
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"term": {
"program.is_learner": True
}
},
{
"term": {
"profile.email_optin": True
}
}
],
"should": [
{
"term": {
"program.id": 1
}
},
{
"term": {
"program.id": 2
}
},
{
"term": {
"program.id": 13
}
}
],
"minimum_should_match": 1
}
},
{
"term": {
"profile.filled_out": True
}
},
{
"bool": {
"must": [
{
"nested": {
"path": "program.courses",
"query": {
"bool": {
"must": [
{
"term": {
"program.courses.course_title":
"Supply Chain Fundamentals (SC1x)"
}
},
{
"term": {
"program.courses.payment_status": "Auditing"
}
}
]
}
}
}
},
{
'nested': {
'path': "program.course_runs",
'query': {
'term': {
'program.course_runs.semester': "2016 - Summer"
}
}
}
},
{
"term": {
"profile.birth_country": "DE"
}
},
{
"term": {
"profile.country": "US"
}
},
{
"term": {
"program.id": 1
}
}
]
}
}
]
}
}
}
PercolateQuery.objects.create(query=query, original_query=query)
reindex_test_es_data()
def test_create_backing_indices(self):
"""
Test that create_backing_indices creates the right backing indices for reindexing
"""
indices = create_backing_indices()
conn = get_conn(verify=False)
for index_name, _ in indices:
assert conn.indices.get_alias(index_name) is not None