3131from country_workspace .datasources .utils import datetime_to_date , date_to_iso_string
3232from country_workspace .models import Household , Individual
3333from country_workspace .workspaces .exceptions import BeneficiaryValidationError
34- from country_workspace .validators .beneficiaries import validate_beneficiaries
3534
3635
3736HOUSEHOLD_1_PK = 1
4544def config (request ) -> Config :
4645 return {
4746 "batch_name" : "batch_name" ,
47+ "validate_mode" : "none" ,
4848 "master_detail" : request .param ,
4949 "household_pk_col" : "household_pk" ,
5050 "master_column_label" : "master_column" ,
5151 "detail_column_label" : "detail_column" ,
5252 "people_column_prefix" : "pp_" ,
53- "check_before" : False ,
54- "fail_if_alien" : False ,
5553 }
5654
5755
@@ -110,6 +108,11 @@ def household_mapping() -> Mapping[int, Mock]:
110108 }
111109
112110
111+ @pytest .fixture
112+ def people_mapping (people_sheet : Sheet ) -> Mapping [int , Mock ]:
113+ return {i : Mock () for i in range (len (list (people_sheet )))}
114+
115+
113116def test_column_configuration_error_format () -> None :
114117 error = ColumnConfigurationError (column_name := "test_column" )
115118 assert column_name in str (error )
@@ -317,60 +320,34 @@ def test_process_beneficiaries_failed_to_create(
317320 assert exc_info .value .sheet_name == expected_sheet_name
318321
319322
320- def test_validate_beneficiaries (config : Config , household_mapping : Mapping [int , Mock ]) -> None :
321- config ["check_before" ] = True
322-
323- validate_beneficiaries (config , household_mapping )
324-
325- for household in household_mapping .values ():
326- household .validate_with_checker .assert_called_once ()
327-
328-
329- def test_validate_beneficiaries_raises_exception_on_failed_validation (
330- config : Config , household_mapping : Mapping [int , Mock ]
331- ) -> None :
332- config ["check_before" ] = True
333- household_mapping [HOUSEHOLD_1_PK ].validate_with_checker .return_value = False
334-
335- with pytest .raises (BeneficiaryValidationError ):
336- validate_beneficiaries (config , household_mapping )
337-
338-
339- def test_validate_beneficiaries_check_before_is_false (config : Config , household_mapping : Mapping [int , Mock ]) -> None :
340- config ["check_before" ] = False
341-
342- validate_beneficiaries (config , household_mapping )
343-
344- for household in household_mapping .values ():
345- household .validate_with_checker .assert_not_called ()
346-
347-
348323def test_import_from_rdi (
349324 mocker : MockerFixture ,
350325 config : Config ,
351326 household_sheet : Sheet ,
352327 individual_sheet : Sheet ,
353328 people_sheet : Sheet ,
354329 household_mapping : Mapping [int , Mock ],
330+ people_mapping : Mapping [int , Mock ],
355331) -> None :
356332 job = Mock ()
357333 job .config = config
358334 batch_class_mock = mocker .patch ("country_workspace.datasources.rdi.Batch" )
359335 read_sheets_mock = mocker .patch ("country_workspace.datasources.rdi.read_sheets" )
360336 process_beneficiaries_mock = mocker .patch ("country_workspace.datasources.rdi.process_beneficiaries" )
361337 validate_beneficiaries_mock = mocker .patch ("country_workspace.datasources.rdi.validate_beneficiaries" )
338+ partial_mock = mocker .patch ("country_workspace.datasources.rdi.partial" )
362339 if config ["master_detail" ]:
363340 read_sheets_mock .return_value = household_sheet , individual_sheet
364341 process_households_mock = mocker .patch ("country_workspace.datasources.rdi.process_households" )
365342 process_households_mock .return_value = household_mapping
366343 process_beneficiaries_mock .return_value = (processed_individuals := list (individual_sheet ))
367344 else :
368345 read_sheets_mock .return_value = (people_sheet ,)
369- people_mapping = {i : Mock () for i in range (len (list (people_sheet )))}
370346 process_beneficiaries_mock .return_value = people_mapping
371347
372348 result = import_from_rdi (job )
373349
350+ partial_mock .assert_called_once_with (validate_beneficiaries_mock , config = config , office = job .program .country_office )
374351 if config ["master_detail" ]:
375352 assert result == {"household" : len (household_mapping ), "individual" : len (processed_individuals )}
376353 process_households_mock .assert_called_once_with (
@@ -383,7 +360,7 @@ def test_import_from_rdi(
383360 config ,
384361 household_mapping ,
385362 )
386- validate_beneficiaries_mock . assert_called_once_with (config , household_mapping )
363+ partial_mock . return_value . assert_called_once_with (household_mapping )
387364 else :
388365 assert result == {"people" : len (people_mapping )}
389366 process_beneficiaries_mock .assert_called_once_with (
@@ -392,7 +369,7 @@ def test_import_from_rdi(
392369 batch_class_mock .objects .create .return_value ,
393370 config ,
394371 )
395- validate_beneficiaries_mock . assert_called_once_with (config , people_mapping )
372+ partial_mock . return_value . assert_called_once_with (people_mapping )
396373
397374 batch_class_mock .objects .create .assert_called_once_with (
398375 name = config ["batch_name" ],
0 commit comments