From a943f53ab636a4b8cd2e0b78c59441e24a0587db Mon Sep 17 00:00:00 2001 From: Fabrizio Finozzi Date: Tue, 3 Dec 2024 12:25:35 +0100 Subject: [PATCH] code: unit test for _load_converters_from_eg and _load_transformers --- test/conftest.py | 37 ++++++++++++++------- test/test_base_network.py | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 11 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index f59d08c9b..d1ff9fa62 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -32,6 +32,24 @@ def config(): return config_dict +@pytest.fixture(scope="function") +def buses_dataframe(): + return pd.DataFrame( + { + "bus_id": [5231, 5232], + "voltage": [380.0, 380.0], + "dc": ["f", "f"], + "symbol": ["Substation", "Substation"], + "under_construction": ["f", "f"], + "x": [6.8884, 6.8894], + "y": [45.6783, 45.6793], + "country": ["IT", "IT"], + "geometry": ["POINT (6.8884 45.6783)", "POINT (6.8894 45.6793)"], + }, + index=[5090, 5091], + ) + + @pytest.fixture(scope="function") def converters_dataframe(): return pd.DataFrame( @@ -48,18 +66,15 @@ def converters_dataframe(): @pytest.fixture(scope="function") -def buses_dataframe(): +def transformers_dataframe(): return pd.DataFrame( { - "bus_id": [5231, 5232], - "voltage": [380.0, 380.0], - "dc": ["f", "f"], - "symbol": ["Substation", "Substation"], - "under_construction": ["f", "f"], - "x": [6.8884, 6.8894], - "y": [45.6783, 45.6793], - "country": ["IT", "IT"], - "geometry": ["POINT (6.8884 45.6783)", "POINT (6.8894 45.6793)"], + "transformer_id": "transf_5231_5232", + "bus0": 5231, + "bus1": 5232, + "voltage": 380.0, + "geometry": "'LINESTRING(6.8884 45.6783 ", + "": "6.8894 45.6793)'", }, - index=[5090, 5091], + index=[0], ) diff --git a/test/test_base_network.py b/test/test_base_network.py index fed81be22..76e0f6790 100644 --- a/test/test_base_network.py +++ b/test/test_base_network.py @@ -23,7 +23,9 @@ _get_linetypes_config, _get_oid, _load_buses, + _load_converters_from_eg, _load_converters_from_osm, + _load_transformers, ) path_cwd = pathlib.Path.cwd() @@ -137,6 +139,38 @@ def test_load_buses(tmpdir, config, buses_dataframe): assert df_comparison.empty +def test_load_converters_from_eg(tmpdir, buses_dataframe, config, converters_dataframe): + """ + Verify what returned by _load_converters_from_eg. + """ + df_converters_reference = pd.DataFrame( + { + "converter_id": "convert_5231_5232", + "bus0": "5231", + "bus1": "5232", + "voltage": 380.0, + "geometry": "LINESTRING(6.8884 45.6783 ,6.8894 45.6793)", + "carrier": "B2B", + }, + index=[0], + ) + buses_path = pathlib.Path(tmpdir, "buses.csv") + buses_dataframe.to_csv(buses_path, index=False) + countries = config["countries"] + italy_shape = pathlib.Path(path_cwd, "test", "test_data", "italy_shape.geojson") + df_buses = _load_buses(buses_path, italy_shape, countries, config) + converters_path = pathlib.Path(tmpdir, "converters_exercise.csv") + converters_dataframe.to_csv(converters_path, index=False) + df_converters_output = ( + _load_converters_from_eg(df_buses, converters_path) + .drop("Unnamed: 5", axis=1) + .reset_index() + ) + df_converters_comparison = df_converters_output.compare(df_converters_reference) + pathlib.Path.unlink(converters_path) + assert df_converters_comparison.empty + + def test_load_converters_from_osm( tmpdir, buses_dataframe, config, converters_dataframe ): @@ -169,3 +203,36 @@ def test_load_converters_from_osm( df_converters_comparison = df_converters_output.compare(df_converters_reference) pathlib.Path.unlink(converters_path) assert df_converters_comparison.empty + + +def test_load_transformers(tmpdir, buses_dataframe, config, transformers_dataframe): + """ + Verify what returned by _load_transformers. + """ + df_transformers_reference = pd.DataFrame( + { + "transformer_id": "transf_5231_5232", + "bus0": "5231", + "bus1": "5232", + "voltage": 380.0, + "geometry": "LINESTRING(6.8884 45.6783 ,6.8894 45.6793)", + }, + index=[0], + ) + buses_path = pathlib.Path(tmpdir, "buses.csv") + buses_dataframe.to_csv(buses_path, index=False) + countries = config["countries"] + italy_shape = pathlib.Path(path_cwd, "test", "test_data", "italy_shape.geojson") + df_buses = _load_buses(buses_path, italy_shape, countries, config) + transformers_path = pathlib.Path(tmpdir, "transformers_exercise.csv") + transformers_dataframe.to_csv(transformers_path, index=False) + df_transformers_output = ( + _load_transformers(df_buses, transformers_path) + .drop("Unnamed: 5", axis=1) + .reset_index() + ) + df_transformers_comparison = df_transformers_output.compare( + df_transformers_reference + ) + pathlib.Path.unlink(transformers_path) + assert df_transformers_comparison.empty