11import json
2+ from contextlib import nullcontext
23from copy import deepcopy
34from datetime import date , datetime , timedelta
45from pathlib import Path
2324 UserProfile ,
2425)
2526from evap .evaluation .models_logging import LogEntry
27+ from evap .evaluation .tests .tools import assert_no_database_modifications
2628
2729EXAMPLE_DATA = json .loads (
2830 Path (evap .cms .fixtures .__file__ ).with_name ("import_example_data.json" ).read_text (encoding = "utf-8" )
@@ -313,17 +315,21 @@ class TestImportEvents(TestCase):
313315 def setUpTestData (cls ):
314316 cls .semester = baker .make (Semester )
315317
316- def _import (self , data = None ):
318+ def _import (self , data = None , assert_nop = False ):
317319 if not data :
318320 data = EXAMPLE_DATA
319321 data = json .dumps (data )
320- importer = JSONImporter (self .semester , date (2000 , 1 , 1 ))
321- importer .import_json (data )
322+
323+ cm = assert_no_database_modifications () if assert_nop else nullcontext ()
324+ with cm :
325+ importer = JSONImporter (self .semester , date (2000 , 1 , 1 ))
326+ importer .import_json (data )
322327 return importer
323328
324329 @override_settings (EXAM_EVALUATION_DEFAULT_DURATION = timedelta (days = 3 ))
325330 def test_import_courses (self ):
326331 importer = self ._import ()
332+ self ._import (assert_nop = True )
327333
328334 course = Course .objects .get ()
329335
@@ -632,11 +638,7 @@ def test_import_courses_evaluation_not_new(self):
632638 evaluation .course .name_en = "Change"
633639 evaluation .course .save ()
634640
635- importer = self ._import ()
636-
637- evaluation = Evaluation .objects .get (pk = evaluation .pk )
638- self .assertFalse (evaluation .is_rewarded )
639- self .assertEqual (evaluation .course .name_en , "Change" )
641+ importer = self ._import (assert_nop = True )
640642 self .assertEqual (len (importer .statistics .attempted_evaluation_changes ), 1 )
641643 self .assertEqual (len (importer .statistics .attempted_course_changes ), 1 )
642644
@@ -663,12 +665,8 @@ def test_import_courses_evaluation_approved(self):
663665 evaluation .is_rewarded = False
664666 evaluation .save ()
665667
666- importer = self ._import ()
667-
668- evaluation = Evaluation .objects .get (pk = evaluation .pk )
669- self .assertFalse (evaluation .is_rewarded )
668+ importer = self ._import (assert_nop = True )
670669 self .assertEqual (len (importer .statistics .attempted_evaluation_changes ), 1 )
671- self .assertEqual (evaluation .participants .count (), 2 )
672670
673671 evaluation .participants .clear ()
674672 evaluation = Evaluation .objects .get (pk = evaluation .pk )
@@ -695,12 +693,10 @@ def test_import_courses_evaluation_running(self):
695693
696694 self .assertEqual (evaluation .participants .count (), 0 )
697695
698- importer = self ._import ()
699-
696+ importer = self ._import (assert_nop = True )
700697 self .assertEqual (len (importer .statistics .attempted_evaluation_changes ), 1 )
701698 self .assertEqual (len (importer .statistics .updated_participants ), 0 )
702699 self .assertEqual (len (importer .statistics .attempted_participant_changes ), 1 )
703- self .assertEqual (evaluation .participants .count (), 0 )
704700
705701 def test_import_courses_update (self ):
706702 self ._import ()
@@ -740,7 +736,8 @@ def test_importer_log_email_sent_no_recipients(self):
740736 def test_importer_wrong_data (self ):
741737 wrong_data = deepcopy (EXAMPLE_DATA )
742738 wrong_data ["events" ][0 ]["isexam" ] = "false"
743- with self .assertRaises (ValidationError ):
739+ # Note: not using assert_nop because the assert_no_database_modifications should include the assertRaises
740+ with assert_no_database_modifications (), self .assertRaises (ValidationError ):
744741 self ._import (wrong_data )
745742
746743 data_with_additional_attribute = deepcopy (EXAMPLE_DATA )
0 commit comments