/
exp_jobs_one_off_test.py
2806 lines (2431 loc) · 111 KB
/
exp_jobs_one_off_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
998
999
1000
# coding: utf-8
#
# Copyright 2014 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for Exploration-related jobs."""
from __future__ import absolute_import # pylint: disable=import-only-modules
from __future__ import unicode_literals # pylint: disable=import-only-modules
import ast
import datetime
import logging
import os
from core.domain import exp_domain
from core.domain import exp_fetchers
from core.domain import exp_jobs_one_off
from core.domain import exp_services
from core.domain import fs_domain
from core.domain import html_cleaner
from core.domain import html_validation_service
from core.domain import rights_manager
from core.domain import state_domain
from core.domain import user_services
from core.platform import models
from core.tests import test_utils
import feconf
import python_utils
import utils
(job_models, exp_models, base_models, classifier_models) = (
models.Registry.import_models([
models.NAMES.job, models.NAMES.exploration, models.NAMES.base_model,
models.NAMES.classifier]))
memcache_services = models.Registry.import_memcache_services()
search_services = models.Registry.import_search_services()
taskqueue_services = models.Registry.import_taskqueue_services()
# This mock should be used only in ExplorationContentValidationJobForCKEditor
# and InteractionCustomizationArgsValidationJob.
# The first job validates the html strings and produces as output the invalid
# strings. If we do not use mock validation for rte while updating
# states and saving exploration, the validation for subtitled html
# in state will fail, thereby resulting in failure of job.
# The second job validates the customization args in html and if the
# mock is not used while updating states and saving explorations,
# the validation for subtitled html in state will fail, thereby
# resulting in failure of job.
def mock_validate(unused_self):
pass
def run_job_for_deleted_exp(
self, job_class, check_error=False,
error_type=None, error_msg=None, function_to_be_called=None,
exp_id=None):
"""Helper function to run job for a deleted exploration and check the
output or error condition.
"""
job_id = job_class.create_new()
# Check there is one job in the taskqueue corresponding to
# delete_exploration_from_subscribed_users.
self.assertEqual(
self.count_jobs_in_taskqueue(
taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS), 1)
job_class.enqueue(job_id)
self.assertEqual(
self.count_jobs_in_taskqueue(
taskqueue_services.QUEUE_NAME_ONE_OFF_JOBS), 2)
self.process_and_flush_pending_tasks()
if check_error:
with self.assertRaisesRegexp(error_type, error_msg):
function_to_be_called(exp_id)
else:
self.assertEqual(job_class.get_output(job_id), [])
class MathExpressionValidationOneOffJobTests(test_utils.GenericTestBase):
ALBERT_EMAIL = 'albert@example.com'
ALBERT_NAME = 'albert'
VALID_EXP_ID = 'exp_id0'
NEW_EXP_ID = 'exp_id1'
EXP_TITLE = 'title'
def setUp(self):
super(MathExpressionValidationOneOffJobTests, self).setUp()
# Setup user who will own the test explorations.
self.signup(self.ALBERT_EMAIL, self.ALBERT_NAME)
self.albert_id = self.get_user_id_from_email(self.ALBERT_EMAIL)
self.process_and_flush_pending_tasks()
def test_exp_state_pairs_are_produced_only_for_desired_interactions(self):
"""Checks output is produced only for desired interactions."""
answer_groups_1 = [{
'outcome': {
'dest': 'Introduction',
'feedback': {
'content_id': 'feedback_1',
'html': '<p>Feedback</p>'
},
'labelled_as_correct': True,
'param_changes': [],
'refresher_exploration_id': None,
'missing_prerequisite_skill_id': None
},
'rule_input_translations': {},
'rule_types_to_inputs': {
'IsMathematicallyEquivalentTo': [{
'x': 'x+y'
}, {
'x': 'x=y'
}]
},
'training_data': [],
'tagged_skill_misconception_id': None
}]
states_dict = state_domain.State.from_dict({
'content': {
'content_id': 'content_1',
'html': 'Question 1'
},
'recorded_voiceovers': {
'voiceovers_mapping': {
'content_1': {},
'feedback_1': {},
'feedback_2': {},
'hint_1': {},
'content_2': {}
}
},
'written_translations': {
'translations_mapping': {
'content_1': {},
'feedback_1': {},
'feedback_2': {},
'hint_1': {},
'content_2': {}
}
},
'interaction': {
'answer_groups': answer_groups_1,
'confirmed_unclassified_answers': [],
'customization_args': {},
'default_outcome': {
'dest': 'Introduction',
'feedback': {
'content_id': 'feedback_2',
'html': 'Correct Answer'
},
'param_changes': [],
'refresher_exploration_id': None,
'labelled_as_correct': True,
'missing_prerequisite_skill_id': None
},
'hints': [{
'hint_content': {
'content_id': 'hint_1',
'html': 'Hint 1'
}
}],
'solution': {
'correct_answer': {
'ascii': 'x+y',
'latex': 'x+y'
},
'answer_is_exclusive': False,
'explanation': {
'html': 'Solution explanation',
'content_id': 'content_2'
}
},
'id': 'MathExpressionInput'
},
'next_content_id_index': 3,
'param_changes': [],
'solicit_answer_details': False,
'classifier_model_id': None
}).to_dict()
self.save_new_exp_with_custom_states_schema_version(
self.VALID_EXP_ID, 'user_id', states_dict, 34)
job_id = (
exp_jobs_one_off.MathExpressionValidationOneOffJob.create_new())
exp_jobs_one_off.MathExpressionValidationOneOffJob.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.MathExpressionValidationOneOffJob.get_output(
job_id))
expected_output = [
u'[u\'Invalid\', [u\'The exploration with ID: exp_id0 had some '
u'issues during migration. This is most likely due to the '
u'exploration having invalid solution(s).\']]']
self.assertEqual(actual_output, expected_output)
def test_no_action_is_performed_for_deleted_exploration(self):
"""Test that no action is performed on deleted explorations."""
exploration = exp_domain.Exploration.create_default_exploration(
self.VALID_EXP_ID, title='title', category='category')
exploration.add_states(['State1'])
state1 = exploration.states['State1']
state1.update_interaction_id('MathExpressionInput')
answer_group_list = [{
'rule_input_translations': {},
'rule_types_to_inputs': {
'IsMathematicallyEquivalentTo': [{
'x': u'[\'y=mx+c\']'
}]
},
'outcome': {
'dest': 'Introduction',
'feedback': {
'content_id': 'feedback',
'html': '<p>Outcome for state1</p>'
},
'param_changes': [],
'labelled_as_correct': False,
'refresher_exploration_id': None,
'missing_prerequisite_skill_id': None
},
'training_data': [],
'tagged_skill_misconception_id': None
}]
state1.update_interaction_answer_groups(answer_group_list)
exp_services.save_new_exploration(self.albert_id, exploration)
exp_services.delete_exploration(self.albert_id, self.VALID_EXP_ID)
run_job_for_deleted_exp(
self, exp_jobs_one_off.MathExpressionValidationOneOffJob)
class OneOffExplorationFirstPublishedJobTests(test_utils.GenericTestBase):
EXP_ID = 'exp_id'
def setUp(self):
super(OneOffExplorationFirstPublishedJobTests, self).setUp()
self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME)
self.admin_id = self.get_user_id_from_email(self.ADMIN_EMAIL)
self.set_admins([self.ADMIN_USERNAME])
self.admin = user_services.UserActionsInfo(self.admin_id)
self.signup(self.OWNER_EMAIL, self.OWNER_USERNAME)
self.owner_id = self.get_user_id_from_email(self.OWNER_EMAIL)
self.owner = user_services.UserActionsInfo(self.owner_id)
def test_first_published_time_of_exploration_that_is_unpublished(self):
"""This tests that, if an exploration is published, unpublished, and
then published again, the job uses the first publication time as the
value for first_published_msec.
"""
self.save_new_valid_exploration(
self.EXP_ID, self.owner_id, end_state_name='End')
rights_manager.publish_exploration(self.owner, self.EXP_ID)
job_class = exp_jobs_one_off.ExplorationFirstPublishedOneOffJob
job_id = job_class.create_new()
exp_jobs_one_off.ExplorationFirstPublishedOneOffJob.enqueue(job_id)
self.process_and_flush_pending_tasks()
exploration_rights = rights_manager.get_exploration_rights(self.EXP_ID)
# Test to see whether first_published_msec was correctly updated.
exp_first_published = exploration_rights.first_published_msec
exp_rights_model = exp_models.ExplorationRightsModel.get(self.EXP_ID)
last_updated_time_msec = utils.get_time_in_millisecs(
exp_rights_model.last_updated)
self.assertLess(
exp_first_published, last_updated_time_msec)
rights_manager.unpublish_exploration(self.admin, self.EXP_ID)
rights_manager.publish_exploration(self.owner, self.EXP_ID)
job_id = job_class.create_new()
exp_jobs_one_off.ExplorationFirstPublishedOneOffJob.enqueue(job_id)
self.process_and_flush_pending_tasks()
# Test to see whether first_published_msec remains the same despite the
# republication.
exploration_rights = rights_manager.get_exploration_rights(self.EXP_ID)
self.assertEqual(
exp_first_published, exploration_rights.first_published_msec)
def test_no_action_is_performed_for_deleted_exploration(self):
"""Test that no action is performed on deleted explorations."""
self.save_new_valid_exploration(
self.EXP_ID, self.owner_id, end_state_name='End')
rights_manager.publish_exploration(self.owner, self.EXP_ID)
exp_services.delete_exploration(self.owner_id, self.EXP_ID)
run_job_for_deleted_exp(
self, exp_jobs_one_off.ExplorationFirstPublishedOneOffJob,
check_error=True,
error_type=base_models.BaseModel.EntityNotFoundError,
error_msg=(
'Entity for class ExplorationRightsModel with id '
'exp_id not found'),
function_to_be_called=rights_manager.get_exploration_rights,
exp_id=self.EXP_ID)
class ExplorationValidityJobManagerTests(test_utils.GenericTestBase):
ALBERT_EMAIL = 'albert@example.com'
ALBERT_NAME = 'albert'
VALID_EXP_ID = 'exp_id0'
NEW_EXP_ID = 'exp_id1'
EXP_TITLE = 'title'
def setUp(self):
super(ExplorationValidityJobManagerTests, self).setUp()
# Setup user who will own the test explorations.
self.signup(self.ALBERT_EMAIL, self.ALBERT_NAME)
self.albert_id = self.get_user_id_from_email(self.ALBERT_EMAIL)
self.process_and_flush_pending_tasks()
def test_validation_errors_are_not_raised_for_valid_exploration(self):
"""Checks validation errors are not raised for a valid exploration."""
exploration = exp_domain.Exploration.create_default_exploration(
self.VALID_EXP_ID, title='title', category='category',
objective='Test Exploration')
exploration.add_states(['End'])
intro_state = exploration.states['Introduction']
end_state = exploration.states['End']
self.set_interaction_for_state(intro_state, 'TextInput')
self.set_interaction_for_state(end_state, 'EndExploration')
default_outcome = state_domain.Outcome(
'End', state_domain.SubtitledHtml(
'default_outcome', '<p>Introduction</p>'
), False, [], None, None
)
intro_state.update_interaction_default_outcome(default_outcome)
end_state.update_interaction_default_outcome(None)
exp_services.save_new_exploration(self.albert_id, exploration)
# Start ExplorationValidityJobManager job on unpublished exploration.
job_id = exp_jobs_one_off.ExplorationValidityJobManager.create_new()
exp_jobs_one_off.ExplorationValidityJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationValidityJobManager.get_output(
job_id))
self.assertEqual(actual_output, [])
self.set_admins([self.ALBERT_NAME])
owner = user_services.UserActionsInfo(self.albert_id)
rights_manager.publish_exploration(owner, self.VALID_EXP_ID)
# Start ExplorationValidityJobManager job on published exploration.
job_id = exp_jobs_one_off.ExplorationValidityJobManager.create_new()
exp_jobs_one_off.ExplorationValidityJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationValidityJobManager.get_output(
job_id))
self.assertEqual(actual_output, [])
def test_strict_validation_errors_are_raised_for_published_exploration(
self):
"""Checks validation errors are not present for valid exploration."""
exploration = exp_domain.Exploration.create_default_exploration(
self.VALID_EXP_ID, title='title', category='category')
exp_services.save_new_exploration(self.albert_id, exploration)
# Start ExplorationValidityJobManager job on unpublished exploration.
job_id = exp_jobs_one_off.ExplorationValidityJobManager.create_new()
exp_jobs_one_off.ExplorationValidityJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationValidityJobManager.get_output(
job_id))
self.assertEqual(actual_output, [])
self.set_admins([self.ALBERT_NAME])
owner = user_services.UserActionsInfo(self.albert_id)
rights_manager.publish_exploration(owner, self.VALID_EXP_ID)
# Start ExplorationValidityJobManager job on published exploration.
job_id = exp_jobs_one_off.ExplorationValidityJobManager.create_new()
exp_jobs_one_off.ExplorationValidityJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationValidityJobManager.get_output(
job_id))
expected_output = [(
'[u\'exp_id0\', '
'[u\'This state does not have any interaction specified.\']]'
)]
self.assertEqual(actual_output, expected_output)
self.set_interaction_for_state(
exploration.states['Introduction'], 'TextInput')
exp_services.save_new_exploration(self.albert_id, exploration)
rights_manager.publish_exploration(owner, self.VALID_EXP_ID)
# Start ExplorationValidityJobManager job on published exploration.
job_id = exp_jobs_one_off.ExplorationValidityJobManager.create_new()
exp_jobs_one_off.ExplorationValidityJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off
.ExplorationValidityJobManager.get_output(job_id))
expected_output = [(
'[u\'exp_id0\', '
'[u"Please fix the following issues before saving this '
'exploration: 1. It is impossible to complete the exploration '
'from the following states: Introduction '
'2. An objective must be specified (in the \'Settings\' tab). "]]')]
self.assertEqual(actual_output, expected_output)
def test_no_action_is_performed_for_deleted_exploration(self):
"""Test that no action is performed on deleted explorations."""
exploration = exp_domain.Exploration.create_default_exploration(
self.VALID_EXP_ID, title='title', category='category')
exp_services.save_new_exploration(self.albert_id, exploration)
self.set_admins([self.ALBERT_NAME])
owner = user_services.UserActionsInfo(self.albert_id)
rights_manager.publish_exploration(owner, self.VALID_EXP_ID)
exp_services.delete_exploration(self.albert_id, self.VALID_EXP_ID)
run_job_for_deleted_exp(
self, exp_jobs_one_off.ExplorationValidityJobManager)
class ExplorationMigrationAuditJobTests(test_utils.GenericTestBase):
"""Tests for ExplorationMigrationAuditJob."""
ALBERT_EMAIL = 'albert@example.com'
ALBERT_NAME = 'albert'
VALID_EXP_ID = 'exp_id0'
NEW_EXP_ID = 'exp_id1'
EXP_TITLE = 'title'
def setUp(self):
super(ExplorationMigrationAuditJobTests, self).setUp()
# Setup user who will own the test explorations.
self.signup(self.ALBERT_EMAIL, self.ALBERT_NAME)
self.albert_id = self.get_user_id_from_email(self.ALBERT_EMAIL)
self.process_and_flush_pending_tasks()
def create_exploration_with_states_schema_version(
self, states_schema_version, exp_id, user_id, states_dict):
"""Saves a new default exploration with the given states dictionary in
the given state schema version. All passed state dictionaries in
'states_dict' must have the states schema version indicated by
'states_schema_version'.
Note that it makes an explicit commit to the datastore instead of using
the usual functions for updating and creating explorations. This is
because the latter approach would result in an exploration with the
*current* states schema version.
Args:
states_schema_version: int. The state schema version.
exp_id: str. The exploration ID.
user_id: str. The user_id of the creator.
states_dict: dict. The dict representation of all the states, in the
given states schema version.
"""
exp_model = exp_models.ExplorationModel(
id=exp_id,
category='category',
title='title',
objective='Old objective',
language_code='en',
tags=[],
blurb='',
author_notes='',
states_schema_version=states_schema_version,
init_state_name=feconf.DEFAULT_INIT_STATE_NAME,
states=states_dict,
param_specs={},
param_changes=[]
)
rights_manager.create_new_exploration_rights(exp_id, user_id)
commit_message = 'New exploration created with title \'title\'.'
exp_model.commit(
user_id, commit_message, [{
'cmd': 'create_new',
'title': 'title',
'category': 'category',
}])
exp_rights = exp_models.ExplorationRightsModel.get_by_id(exp_id)
exp_summary_model = exp_models.ExpSummaryModel(
id=exp_id,
title='title',
category='category',
objective='Old objective',
language_code='en',
tags=[],
ratings=feconf.get_empty_ratings(),
scaled_average_rating=feconf.EMPTY_SCALED_AVERAGE_RATING,
status=exp_rights.status,
community_owned=exp_rights.community_owned,
owner_ids=exp_rights.owner_ids,
contributor_ids=[],
contributors_summary={},
)
exp_summary_model.put()
def test_migration_audit_job_skips_deleted_explorations(self):
"""Tests that the exploration migration job skips deleted explorations
and does not attempt to migrate.
"""
self.save_new_exp_with_states_schema_v0(
self.NEW_EXP_ID, self.albert_id, self.EXP_TITLE)
# Note: This creates a summary based on the upgraded model (which is
# fine). A summary is needed to delete the exploration.
exp_services.create_exploration_summary(
self.NEW_EXP_ID, None)
# Delete the exploration before migration occurs.
exp_services.delete_exploration(self.albert_id, self.NEW_EXP_ID)
# Ensure the exploration is deleted.
with self.assertRaisesRegexp(Exception, 'Entity .* not found'):
exp_fetchers.get_exploration_by_id(self.NEW_EXP_ID)
# Start migration job on sample exploration.
job_id = exp_jobs_one_off.ExplorationMigrationAuditJob.create_new()
exp_jobs_one_off.ExplorationMigrationAuditJob.enqueue(job_id)
# This running without errors indicates the deleted exploration is
# being ignored, since otherwise exp_fetchers.get_exploration_by_id
# (used within the job) will raise an error.
self.process_and_flush_pending_tasks()
# Ensure the exploration is still deleted.
with self.assertRaisesRegexp(Exception, 'Entity .* not found'):
exp_fetchers.get_exploration_by_id(self.NEW_EXP_ID)
def test_audit_job_runs_for_any_state_schema_version(self):
"""Tests that the exploration migration converts older explorations to a
previous state schema version before running the audit job.
"""
self.save_new_exp_with_states_schema_v0(
self.NEW_EXP_ID, self.albert_id, self.EXP_TITLE)
job_id = exp_jobs_one_off.ExplorationMigrationAuditJob.create_new()
exp_jobs_one_off.ExplorationMigrationAuditJob.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationMigrationAuditJob.get_output(job_id))
self.assertEqual(actual_output, [u'[u\'SUCCESS\', 1]'])
def test_migration_job_audit_success(self):
"""Test that the audit job runs correctly on explorations of the
previous state schema.
"""
states_dict = {
'content': {
'content_id': 'content_1',
'html': 'Question 1'
},
'recorded_voiceovers': {
'voiceovers_mapping': {
'content_1': {},
'feedback_2': {},
'hint_1': {},
'content_2': {}
}
},
'written_translations': {
'translations_mapping': {
'content_1': {},
'feedback_2': {},
'hint_1': {},
'content_2': {}
}
},
'interaction': {
'answer_groups': [],
'confirmed_unclassified_answers': [],
'customization_args': {},
'default_outcome': {
'dest': 'Introduction',
'feedback': {
'content_id': 'feedback_2',
'html': 'Correct Answer'
},
'param_changes': [],
'refresher_exploration_id': None,
'labelled_as_correct': True,
'missing_prerequisite_skill_id': None
},
'hints': [{
'hint_content': {
'content_id': 'hint_1',
'html': 'Hint 1'
}
}],
'solution': {
'correct_answer': {
'ascii': 'x+y',
'latex': 'x+y'
},
'answer_is_exclusive': False,
'explanation': {
'html': 'Solution explanation',
'content_id': 'content_2'
}
},
'id': 'MathExpressionInput'
},
'next_content_id_index': 0,
'param_changes': [],
'solicit_answer_details': False,
'classifier_model_id': None
}
self.create_exploration_with_states_schema_version(
36,
self.NEW_EXP_ID,
self.albert_id,
{'Introduction': states_dict}
)
swap_states_schema_version = self.swap(
feconf, 'CURRENT_STATE_SCHEMA_VERSION', 37)
swap_exp_schema_version = self.swap(
exp_domain.Exploration, 'CURRENT_EXP_SCHEMA_VERSION', 42)
with swap_states_schema_version, swap_exp_schema_version:
job_id = exp_jobs_one_off.ExplorationMigrationAuditJob.create_new()
exp_jobs_one_off.ExplorationMigrationAuditJob.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationMigrationAuditJob.get_output(
job_id)
)
expected_output = ['[u\'SUCCESS\', 1]']
self.assertEqual(actual_output, expected_output)
def test_migration_job_audit_failure(self):
"""Test that the audit job runs correctly on explorations of the
previous state schema and catches any errors that occur during the
migration.
"""
states_dict = {
'content': {
'content_id': 'content_1',
'html': 'Question 1'
},
'recorded_voiceovers': {
'voiceovers_mapping': {
'content_1': {},
'feedback_2': {},
'hint_1': {},
'content_2': {}
}
},
'written_translations': {
'translations_mapping': {
'content_1': {},
'feedback_2': {},
'hint_1': {},
'content_2': {}
}
},
'interaction': {
'answer_groups': [],
'confirmed_unclassified_answers': [],
'customization_args': {},
'default_outcome': {
'dest': 'Introduction',
'feedback': {
'content_id': 'feedback_2',
'html': 'Correct Answer'
},
'param_changes': [],
'refresher_exploration_id': None,
'labelled_as_correct': True,
'missing_prerequisite_skill_id': None
},
'hints': [{
'hint_content': {
'content_id': 'hint_1',
'html': 'Hint 1'
}
}],
'solution': {
'correct_answer': {
'ascii': 'x+y',
'latex': 'x+y'
},
'answer_is_exclusive': False,
'explanation': {
'html': 'Solution explanation',
'content_id': 'content_2'
}
},
'id': 'MathExpressionInput'
},
'next_content_id_index': 0,
'param_changes': [],
'solicit_answer_details': False,
'classifier_model_id': None
}
self.create_exploration_with_states_schema_version(
36,
self.NEW_EXP_ID,
self.albert_id,
{'Introduction': states_dict}
)
# Make a mock conversion function that raises an error.
mock_conversion = classmethod(
lambda cls, exploration_dict: exploration_dict['property_that_dne'])
swap_states_schema_version = self.swap(
feconf, 'CURRENT_STATE_SCHEMA_VERSION', 37)
swap_exp_schema_version = self.swap(
exp_domain.Exploration, 'CURRENT_EXP_SCHEMA_VERSION', 42)
with swap_states_schema_version, swap_exp_schema_version, self.swap(
exp_domain.Exploration,
'_convert_states_v36_dict_to_v37_dict',
mock_conversion
):
job_id = exp_jobs_one_off.ExplorationMigrationAuditJob.create_new()
exp_jobs_one_off.ExplorationMigrationAuditJob.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationMigrationAuditJob.get_output(
job_id)
)
expected_output = [
u'[u\'MIGRATION_ERROR\', [u"Exploration exp_id1 failed migratio'
'n to states v37: u\'property_that_dne\'"]]'
]
self.assertEqual(actual_output, expected_output)
class ExplorationMigrationJobTests(test_utils.GenericTestBase):
ALBERT_EMAIL = 'albert@example.com'
ALBERT_NAME = 'albert'
VALID_EXP_ID = 'exp_id0'
NEW_EXP_ID = 'exp_id1'
EXP_TITLE = 'title'
def setUp(self):
super(ExplorationMigrationJobTests, self).setUp()
# Setup user who will own the test explorations.
self.signup(self.ALBERT_EMAIL, self.ALBERT_NAME)
self.albert_id = self.get_user_id_from_email(self.ALBERT_EMAIL)
self.process_and_flush_pending_tasks()
def test_migration_job_does_not_convert_up_to_date_exp(self):
"""Tests that the exploration migration job does not convert an
exploration that is already the latest states schema version.
"""
# Create a new, default exploration that should not be affected by the
# job.
exploration = exp_domain.Exploration.create_default_exploration(
self.VALID_EXP_ID, title='title', category='category')
init_state = exploration.states[exploration.init_state_name]
self.set_interaction_for_state(init_state, 'EndExploration')
init_state.update_interaction_default_outcome(None)
exp_services.save_new_exploration(self.albert_id, exploration)
self.assertEqual(
exploration.states_schema_version,
feconf.CURRENT_STATE_SCHEMA_VERSION)
yaml_before_migration = exploration.to_yaml()
# Start migration job on sample exploration.
job_id = exp_jobs_one_off.ExplorationMigrationJobManager.create_new()
exp_jobs_one_off.ExplorationMigrationJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
# Verify the exploration is exactly the same after migration.
updated_exp = exp_fetchers.get_exploration_by_id(self.VALID_EXP_ID)
self.assertEqual(
updated_exp.states_schema_version,
feconf.CURRENT_STATE_SCHEMA_VERSION)
after_converted_yaml = updated_exp.to_yaml()
self.assertEqual(after_converted_yaml, yaml_before_migration)
def test_migration_job_does_not_have_validation_fail_on_default_exp(self):
"""Tests that the exploration migration job does not have a validation
failure for a default exploration (of states schema version 0), due to
the exploration having a null interaction ID in its initial state.
"""
self.save_new_exp_with_states_schema_v0(
self.NEW_EXP_ID, self.albert_id, self.EXP_TITLE)
# Start migration job on sample exploration.
job_id = exp_jobs_one_off.ExplorationMigrationJobManager.create_new()
exp_jobs_one_off.ExplorationMigrationJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
# Verify the new exploration has been migrated by the job.
updated_exp = exp_fetchers.get_exploration_by_id(self.NEW_EXP_ID)
self.assertEqual(
updated_exp.states_schema_version,
feconf.CURRENT_STATE_SCHEMA_VERSION)
# Ensure the states structure within the exploration was changed.
self.assertNotEqual(
updated_exp.to_dict()['states'], self.VERSION_0_STATES_DICT)
def test_migration_job_skips_deleted_explorations(self):
"""Tests that the exploration migration job skips deleted explorations
and does not attempt to migrate.
"""
self.save_new_exp_with_states_schema_v0(
self.NEW_EXP_ID, self.albert_id, self.EXP_TITLE)
# Note: This creates a summary based on the upgraded model (which is
# fine). A summary is needed to delete the exploration.
exp_services.create_exploration_summary(
self.NEW_EXP_ID, None)
# Delete the exploration before migration occurs.
exp_services.delete_exploration(self.albert_id, self.NEW_EXP_ID)
# Ensure the exploration is deleted.
with self.assertRaisesRegexp(Exception, 'Entity .* not found'):
exp_fetchers.get_exploration_by_id(self.NEW_EXP_ID)
# Start migration job on sample exploration.
job_id = exp_jobs_one_off.ExplorationMigrationJobManager.create_new()
exp_jobs_one_off.ExplorationMigrationJobManager.enqueue(job_id)
# This running without errors indicates the deleted exploration is
# being ignored, since otherwise exp_fetchers.get_exploration_by_id
# (used within the job) will raise an error.
self.process_and_flush_pending_tasks()
# Ensure the exploration is still deleted.
with self.assertRaisesRegexp(Exception, 'Entity .* not found'):
exp_fetchers.get_exploration_by_id(self.NEW_EXP_ID)
def test_exploration_migration_job_output(self):
"""Test that Exploration Migration job output is correct."""
exploration = exp_domain.Exploration.create_default_exploration(
self.VALID_EXP_ID, title='title', category='category')
exp_services.save_new_exploration(self.albert_id, exploration)
self.save_new_exp_with_states_schema_v0(
self.NEW_EXP_ID, self.albert_id, self.EXP_TITLE)
# Start migration job on sample exploration.
job_id = exp_jobs_one_off.ExplorationMigrationJobManager.create_new()
exp_jobs_one_off.ExplorationMigrationJobManager.enqueue(job_id)
self.process_and_flush_pending_tasks()
actual_output = (
exp_jobs_one_off.ExplorationMigrationJobManager.get_output(job_id))
expected_output = ['[u\'SUCCESS\', 1]']
self.assertEqual(actual_output, expected_output)
def test_migration_job_creates_appropriate_classifier_models(self):
"""Tests that the exploration migration job creates appropriate
classifier data models for explorations.
"""
self.save_new_exp_with_states_schema_v21(
self.NEW_EXP_ID, self.albert_id, self.EXP_TITLE)
exploration = exp_fetchers.get_exploration_by_id(self.NEW_EXP_ID)
initial_state_name = list(exploration.states.keys())[0]
# Store classifier model for the new exploration.
classifier_model_id = classifier_models.ClassifierTrainingJobModel.create( # pylint: disable=line-too-long
'TextClassifier', 'TextInput', self.NEW_EXP_ID, exploration.version,
datetime.datetime.utcnow(), {}, initial_state_name,
feconf.TRAINING_JOB_STATUS_COMPLETE, 1)
# Store training job model for the classifier model.
classifier_models.TrainingJobExplorationMappingModel.create(
self.NEW_EXP_ID, exploration.version, initial_state_name,
classifier_model_id)
# Start migration job on sample exploration.
job_id = exp_jobs_one_off.ExplorationMigrationJobManager.create_new()
exp_jobs_one_off.ExplorationMigrationJobManager.enqueue(job_id)
with self.swap(feconf, 'ENABLE_ML_CLASSIFIERS', True):
with self.swap(feconf, 'MIN_TOTAL_TRAINING_EXAMPLES', 2):
with self.swap(feconf, 'MIN_ASSIGNED_LABELS', 1):
self.process_and_flush_pending_tasks()
new_exploration = exp_fetchers.get_exploration_by_id(self.NEW_EXP_ID)
initial_state_name = list(new_exploration.states.keys())[0]
self.assertLess(exploration.version, new_exploration.version)
classifier_exp_mapping_model = classifier_models.TrainingJobExplorationMappingModel.get_models( # pylint: disable=line-too-long
self.NEW_EXP_ID, new_exploration.version,
[initial_state_name])[0]
self.assertEqual(
classifier_exp_mapping_model.job_id, classifier_model_id)
def test_migration_job_fails_with_invalid_exploration(self):
observed_log_messages = []
def _mock_logging_function(msg, *args):
"""Mocks logging.error()."""
observed_log_messages.append(msg % args)
exploration = exp_domain.Exploration.create_default_exploration(
self.VALID_EXP_ID, title='title', category='category')
exp_services.save_new_exploration(self.albert_id, exploration)
exploration_model = exp_models.ExplorationModel.get(self.VALID_EXP_ID)
exploration_model.language_code = 'invalid_language_code'
exploration_model.commit(
self.albert_id, 'Changed language_code.', [])
memcache_services.delete('exploration:%s' % self.VALID_EXP_ID)
job_id = exp_jobs_one_off.ExplorationMigrationJobManager.create_new()
exp_jobs_one_off.ExplorationMigrationJobManager.enqueue(job_id)
with self.swap(logging, 'error', _mock_logging_function):
self.process_and_flush_pending_tasks()
self.assertEqual(
observed_log_messages,
['Exploration %s failed non-strict validation: '
'Invalid language_code: invalid_language_code'
% (self.VALID_EXP_ID)])
class ViewableExplorationsAuditJobTests(test_utils.GenericTestBase):
ALBERT_EMAIL = 'albert@example.com'
ALBERT_NAME = 'albert'
VALID_EXP_ID = 'exp_id0'
NEW_EXP_ID = 'exp_id1'
EXP_TITLE = 'title'
def setUp(self):
super(ViewableExplorationsAuditJobTests, self).setUp()
# Setup user who will own the test explorations.
self.signup(self.ALBERT_EMAIL, self.ALBERT_NAME)
self.albert_id = self.get_user_id_from_email(self.ALBERT_EMAIL)
self.process_and_flush_pending_tasks()
def test_output_contains_only_viewable_private_explorations(self):
"""Checks that only viewable private explorations are present