2323 image_content ,
2424 extract_images ,
2525 merge_images ,
26+ normalize_row_structure ,
2627 read_sheets ,
27- full_name_column ,
2828 INDIVIDUAL ,
2929 PEOPLE ,
3030)
3131from country_workspace .datasources .utils import datetime_to_date , date_to_iso_string
32- from country_workspace .models import Household
32+ from country_workspace .models import Household , Individual
3333from country_workspace .workspaces .exceptions import BeneficiaryValidationError
3434from country_workspace .validators .beneficiaries import validate_beneficiaries
3535
@@ -169,6 +169,33 @@ def test_filter_rows_with_household_pk(
169169 )
170170
171171
172+ @pytest .mark .parametrize (
173+ ("row" , "prefix" , "expected_row" ),
174+ [
175+ ({"full_name" : "John" }, None , {"full_name" : "John" }),
176+ ({"pp_full_name" : "Jane" }, "pp_" , {"full_name" : "Jane" }),
177+ ({"no_prefix" : "value" }, "wrong_" , {"no_prefix" : "value" }),
178+ ],
179+ ids = ["no_prefix" , "with_prefix" , "prefix_not_found" ],
180+ )
181+ def test_normalize_row_structure_prefix_handling (row : Record , prefix : str | None , expected_row : Record ) -> None :
182+ result_row , _ = normalize_row_structure (row , prefix )
183+ assert result_row == expected_row
184+
185+
186+ @pytest .mark .parametrize (
187+ ("row" , "expected_name_column" ),
188+ [
189+ ({"full_name" : "John" }, "full_name" ),
190+ ({"age" : 30 }, None ),
191+ ],
192+ ids = ["full_name" , "no_name" ],
193+ )
194+ def test_normalize_row_structure_name_column_detection (row : Record , expected_name_column : str | None ) -> None :
195+ _ , name_column = normalize_row_structure (row )
196+ assert name_column == expected_name_column
197+
198+
172199def test_process_households (
173200 mocker : MockerFixture , config : Config , household_sheet : Sheet , skip_if_not_master_detail
174201) -> None :
@@ -179,9 +206,17 @@ def test_process_households(
179206 assert result == {
180207 row [config ["household_pk_col" ]]: job .program .households .create .return_value for row in household_sheet
181208 }
209+
210+ job .program .apply_mapping_importer .assert_has_calls (
211+ [call (Household , clean_field_names_mock .return_value ) for row in household_sheet ]
212+ )
182213 job .program .households .create .assert_has_calls (
183214 [
184- call (batch = batch , name = row [config ["detail_column_label" ]], flex_fields = clean_field_names_mock .return_value )
215+ call (
216+ batch = batch ,
217+ name = row [config ["detail_column_label" ]],
218+ flex_fields = job .program .apply_mapping_importer .return_value ,
219+ )
185220 for row in household_sheet
186221 ]
187222 )
@@ -218,13 +253,16 @@ def test_process_beneficiaries_with_households(
218253 )
219254
220255 assert len (result ) == len (list (individual_sheet ))
256+ job_mock .program .apply_mapping_importer .assert_has_calls (
257+ [call (Individual , clean_field_names_mock .return_value ) for row in individual_sheet ]
258+ )
221259 job_mock .program .individuals .create .assert_has_calls (
222260 [
223261 call (
224262 batch = batch_mock ,
225263 name = row [FULL_NAME_COLUMN ],
226264 household = household_mapping [row [config ["master_column_label" ]]],
227- flex_fields = clean_field_names_mock .return_value ,
265+ flex_fields = job_mock . program . apply_mapping_importer .return_value ,
228266 )
229267 for row in individual_sheet
230268 ]
@@ -246,19 +284,21 @@ def test_process_beneficiaries_people_only(
246284 )
247285
248286 assert len (result ) == len (list (people_sheet ))
249- expected_calls = []
287+ expected_calls , expected_apply_mapping_calls = [], []
250288 for __ , row in enumerate (people_sheet , 1 ):
251289 prefix = config .get ("people_column_prefix" , "" )
252290 cleaned_row = {k .removeprefix (prefix ): v for k , v in row .items ()}
291+ expected_apply_mapping_calls .append (call (Individual , clean_field_names_mock .return_value ))
253292 expected_calls .append (
254293 call (
255294 batch = batch_mock ,
256295 name = cleaned_row [FULL_NAME_COLUMN ],
257296 household = None ,
258- flex_fields = clean_field_names_mock .return_value ,
297+ flex_fields = job_mock . program . apply_mapping_importer .return_value ,
259298 )
260299 )
261300 job_mock .program .individuals .create .assert_has_calls (expected_calls )
301+ job_mock .program .apply_mapping_importer .assert_has_calls (expected_apply_mapping_calls )
262302
263303
264304def test_process_beneficiaries_failed_to_create (
@@ -462,18 +502,6 @@ def test_read_sheets_sheet_not_found_error(mocker: MockerFixture, config: Config
462502 assert str (sheet_index ) in str (exc_info .value )
463503
464504
465- @pytest .mark .parametrize (
466- ("record" , "expected" ),
467- [
468- ({"full_name" : "John Smith" }, "full_name" ),
469- ({}, None ),
470- ({"name_full" : "John Smith" }, None ),
471- ],
472- )
473- def test_full_name_column (record : Record , expected : str | None ) -> None :
474- assert full_name_column (record ) == expected
475-
476-
477505@pytest .mark .parametrize (
478506 ("inp" , "expected" ),
479507 [
0 commit comments