@@ -1009,10 +1009,10 @@ def test_dref_create_and_update_in_local_language(
10091009 response = self .client .patch (url , data = data_en , format = "json" , HTTP_ACCEPT_LANGUAGE = "es" )
10101010 self .assert_400 (response )
10111011
1012- @patch ("dref.views.transaction.on_commit" )
1012+ @patch ("dref.views.transaction.on_commit" , lambda func : func () )
10131013 @patch ("dref.views.process_translation.delay" )
10141014 @patch ("dref.views.is_translation_complete" )
1015- def test_update_and_finalize_dref (self , mock_on_commit , mock_is_translation_complete , mock_translation ):
1015+ def test_update_and_finalize_dref (self , mock_is_translation_complete , mock_translation ):
10161016 dref = DrefFactory .create (
10171017 title = "Título original en español" ,
10181018 type_of_dref = Dref .DrefType .IMMINENT ,
@@ -1028,39 +1028,42 @@ def test_update_and_finalize_dref(self, mock_on_commit, mock_is_translation_comp
10281028 response = self .client .patch (url , data = data_es , HTTP_ACCEPT_LANGUAGE = "es" )
10291029 self .assert_200 (response )
10301030 self .assertEqual (response .data ["title" ], "en español" )
1031- # update in French
1031+
1032+ # Update in French
10321033 data_fr = {"title" : "Titre en français" , "modified_at" : datetime .now ()}
10331034 response = self .client .patch (url , data = data_fr , format = "json" , HTTP_ACCEPT_LANGUAGE = "fr" )
10341035 self .assert_400 (response )
1035- # update in Arabic
1036+
1037+ # Update in Arabic
10361038 data_ar = {"title" : "العنوان بالعربية" , "modified_at" : datetime .now ()}
10371039 response = self .client .patch (url , data = data_ar , format = "json" , HTTP_ACCEPT_LANGUAGE = "ar" )
10381040 self .assert_400 (response )
1039- # update in English
1041+ # Update in English before translation complete
10401042 data_en = {"title" : "Updated title in English" , "modified_at" : datetime .now ()}
10411043 response = self .client .patch (url , data = data_en , HTTP_ACCEPT_LANGUAGE = "en" )
10421044 self .assert_400 (response )
1043-
10441045 finalize_url = f"/api/v2/dref/{ dref .id } /finalize/"
1045- # Test Finalize without Translation completion
10461046 mock_is_translation_complete .return_value = False
10471047 response = self .client .post (finalize_url )
10481048 self .assert_400 (response )
1049- mock_on_commit .side_effect = lambda func : func ()
1049+ dref .refresh_from_db ()
1050+ self .assertEqual (dref .status , Dref .Status .FINALIZING )
1051+
10501052 model_name = get_model_name (type (dref ))
10511053 mock_translation .assert_called_once_with (model_name , dref .pk )
1052- # Test finalize with Translation completion
1054+
10531055 mock_translation .reset_mock ()
10541056 mock_is_translation_complete .return_value = True
10551057 response = self .client .post (finalize_url )
10561058 self .assert_200 (response )
1057- mock_translation .assert_not_called ()
1058- self .assertEqual (response .data ["status" ], Dref .Status .FINALIZED )
1059- self .assertEqual (response .data ["translation_module_original_language" ], "en" )
10601059
1061- # Update in English
1062- finalized_dref_id = response .data ["id" ]
1063- url = f"/api/v2/dref/{ finalized_dref_id } /"
1060+ dref .refresh_from_db ()
1061+ self .assertEqual (dref .status , Dref .Status .FINALIZED )
1062+ self .assertEqual (dref .translation_module_original_language , "en" )
1063+ self .assertEqual (response .data ["status" ], Dref .Status .FINALIZED )
1064+ mock_translation .assert_not_called ()
1065+ # Update in English after finalization
1066+ url = f"/api/v2/dref/{ dref .id } /"
10641067 response = self .client .patch (url , data = data_en , HTTP_ACCEPT_LANGUAGE = "en" )
10651068 self .assert_200 (response )
10661069 self .assertEqual (response .data ["title" ], "Updated title in English" )
@@ -1220,10 +1223,10 @@ def test_create_and_update_operational_update(self):
12201223 response = self .client .patch (url , data = data , HTTP_ACCEPT_LANGUAGE = "fr" )
12211224 self .assert_400 (response )
12221225
1223- @patch ("dref.views.transaction.on_commit" )
1226+ @patch ("dref.views.transaction.on_commit" , lambda func : func () )
12241227 @patch ("dref.views.process_translation.delay" )
12251228 @patch ("dref.views.is_translation_complete" )
1226- def test_dref_operational_update_finalize (self , mock_on_commit , mock_is_translation_complete , mock_translation ):
1229+ def test_dref_operational_update_finalize (self , mock_is_translation_complete , mock_translation ):
12271230 # Create users
12281231 user1 , user2 = UserFactory .create_batch (2 )
12291232 dref = DrefFactory .create (
@@ -1240,7 +1243,6 @@ def test_dref_operational_update_finalize(self, mock_on_commit, mock_is_translat
12401243 modified_at = datetime .now (),
12411244 translation_module_original_language = "ar" ,
12421245 )
1243-
12441246 url = f"/api/v2/dref-op-update/{ op_update .id } /"
12451247 self .client .force_authenticate (user1 )
12461248
@@ -1257,20 +1259,20 @@ def test_dref_operational_update_finalize(self, mock_on_commit, mock_is_translat
12571259
12581260 # Finalize Operational Update
12591261 finalize_url = f"/api/v2/dref-op-update/{ op_update .id } /finalize/"
1260- # Test Finalize without Translation completion
12611262 mock_is_translation_complete .return_value = False
12621263 response = self .client .post (finalize_url )
12631264 self .assert_400 (response )
1264- mock_translation .assert_called_once_with (get_model_name (type (op_update )), op_update .pk )
1265- # Test Finalize with translation complete
1265+ model_name = get_model_name (type (op_update ))
1266+ mock_translation .assert_called_once_with (model_name , op_update .pk )
1267+
12661268 mock_translation .reset_mock ()
12671269 mock_is_translation_complete .return_value = True
12681270 response = self .client .post (finalize_url )
12691271 self .assert_200 (response )
1270- mock_on_commit .side_effect = lambda func : func ()
1272+ op_update .refresh_from_db ()
1273+ self .assertEqual (op_update .status , Dref .Status .FINALIZED )
1274+ self .assertEqual (op_update .translation_module_original_language , "en" )
12711275 mock_translation .assert_not_called ()
1272- self .assertEqual (response .data ["status" ], Dref .Status .FINALIZED )
1273- self .assertEqual (response .data ["translation_module_original_language" ], "en" )
12741276 # Update in English
12751277 finalized_op_update_id = response .data ["id" ]
12761278 url = f"/api/v2/dref-op-update/{ finalized_op_update_id } /"
@@ -2293,13 +2295,12 @@ def test_create_and_update_final_report(self):
22932295 self .assertEqual (response .data ["translation_module_original_language" ], "es" )
22942296 self .assertEqual (response .data ["title" ], "Título en español" )
22952297
2296- @patch ("dref.views.transaction.on_commit" )
2298+ @patch ("dref.views.transaction.on_commit" , lambda func : func () )
22972299 @patch ("dref.views.process_translation.delay" )
22982300 @patch ("dref.views.is_translation_complete" )
2299- def test_dref_final_report_finalize (self , mock_on_commit , mock_is_translation_complete , mock_translation ):
2301+ def test_dref_final_report_finalize (self , mock_is_translation_complete , mock_translation ):
23002302 region = Region .objects .create (name = RegionName .AFRICA )
23012303 country = Country .objects .create (name = "Test country12" , region = region )
2302- # Create users
23032304 user1 , user2 = UserFactory .create_batch (2 )
23042305 dref = DrefFactory .create (
23052306 title = "Test Title" ,
@@ -2323,7 +2324,6 @@ def test_dref_final_report_finalize(self, mock_on_commit, mock_is_translation_co
23232324 data_ar = {"title" : "العنوان بالعربية" , "modified_at" : datetime .now ()}
23242325 response = self .client .patch (url , data = data_ar , HTTP_ACCEPT_LANGUAGE = "ar" )
23252326 self .assert_400 (response )
2326-
23272327 # Update in Spanish (original language)
23282328 data_es = {"title" : "Título en español" , "modified_at" : datetime .now ()}
23292329 response = self .client .patch (url , data = data_es , HTTP_ACCEPT_LANGUAGE = "es" )
@@ -2337,22 +2337,25 @@ def test_dref_final_report_finalize(self, mock_on_commit, mock_is_translation_co
23372337 mock_is_translation_complete .return_value = False
23382338 response = self .client .post (finalize_url )
23392339 self .assert_400 (response )
2340- mock_on_commit .side_effect = lambda func : func ()
2341- mock_translation .assert_called_once_with (get_model_name (type (final_report )), final_report .pk )
2342- # Test finalize with Translation completion
2340+ model_name = get_model_name (type (final_report ))
2341+ mock_translation .assert_called_once_with (model_name , final_report .pk )
23432342 mock_translation .reset_mock ()
2343+
2344+ # Test finalize with Translation completion
23442345 mock_is_translation_complete .return_value = True
23452346 response = self .client .post (finalize_url )
23462347 self .assert_200 (response )
2347- mock_translation . assert_not_called ()
2348- self .assertEqual (response . data [ " status" ] , Dref .Status .FINALIZED )
2349- self .assertEqual (response . data [ " translation_module_original_language" ] , "en" )
2348+ final_report . refresh_from_db ()
2349+ self .assertEqual (final_report . status , Dref .Status .FINALIZED )
2350+ self .assertEqual (final_report . translation_module_original_language , "en" )
23502351
2351- # Update in English
2352+ mock_translation . assert_not_called ()
23522353 finalized_final_report_id = response .data ["id" ]
23532354 data_en = {"title" : "Updated title in English" , "modified_at" : datetime .now ()}
2355+ # Update in English
23542356 url = f"/api/v2/dref-final-report/{ finalized_final_report_id } /"
23552357 response = self .client .patch (url , data = data_en , HTTP_ACCEPT_LANGUAGE = "en" )
2358+ self .assert_200 (response )
23562359 self .assertEqual (response .data ["title" ], "Updated title in English" )
23572360
23582361
0 commit comments