44from pathlib import Path
55from unittest .mock import MagicMock , mock_open , patch
66
7+ import pandas as pd
8+
79from power_grid_model_io .data_stores .vision_excel_file_store import VisionExcelFileStore
810
911
@@ -20,3 +22,55 @@ def test_header_rows(mock_excel_file: MagicMock):
2022
2123 # Assert
2224 assert mock_excel_file .return_value .parse .call_count == 2
25+
26+
27+ @patch ("power_grid_model_io.data_stores.excel_file_store.pd.ExcelFile" )
28+ @patch ("power_grid_model_io.data_stores.excel_file_store.Path.open" , mock_open ())
29+ def test_name_column_dtype_conversion (mock_excel_file : MagicMock ):
30+ store = VisionExcelFileStore (file_path = Path ("dummy.xlsx" ))
31+ mock_excel_file .return_value .sheet_names = ["test_sheet" ]
32+
33+ preview_df = pd .DataFrame (columns = ["Mock.Name" , "Other.Column" , "ID" ])
34+
35+ def mock_parse (* args , ** kwargs ):
36+ if kwargs .get ("nrows" ) == 0 :
37+ return preview_df
38+ else :
39+ actual_data = {
40+ "Mock.Name" : [123456789 , 987.654 ],
41+ "Other.Column" : ["value1" , "value2" ],
42+ "ID" : [1 , 2 ],
43+ "ratio" : [0.1 , 0.2 ],
44+ }
45+ df = pd .DataFrame (actual_data )
46+
47+ if "dtype" in kwargs :
48+ for col , dtype_val in kwargs ["dtype" ].items ():
49+ if col in df .columns and dtype_val is str :
50+ df [col ] = df [col ].apply (lambda x : str (int (x )) if float (x ).is_integer () else str (x ))
51+
52+ return df
53+
54+ mock_excel_file .return_value .parse .side_effect = mock_parse
55+
56+ data = store .load ()
57+ result_df = data ["test_sheet" ]
58+
59+ assert mock_excel_file .return_value .parse .call_count == 2
60+
61+ first_call = mock_excel_file .return_value .parse .call_args_list [0 ]
62+ assert first_call [1 ]["nrows" ] == 0
63+
64+ second_call = mock_excel_file .return_value .parse .call_args_list [1 ]
65+ assert "dtype" in second_call [1 ]
66+ assert "Mock.Name" in second_call [1 ]["dtype" ]
67+ assert second_call [1 ]["dtype" ]["Mock.Name" ] is str
68+
69+ assert result_df ["Mock.Name" ][0 ] == "123456789" # Long int as string
70+ assert result_df ["Mock.Name" ][1 ] == "987.654" # Float as string
71+ assert result_df ["Other.Column" ][0 ] == "value1"
72+ assert result_df ["Other.Column" ][1 ] == "value2"
73+ assert result_df ["ID" ][0 ] == 1
74+ assert result_df ["ID" ][1 ] == 2
75+ assert result_df ["ratio" ][0 ] == 0.1
76+ assert result_df ["ratio" ][1 ] == 0.2
0 commit comments