Skip to content

Commit

Permalink
code: unit test for _load_converters_from_eg and _load_transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
finozzifa committed Dec 3, 2024
1 parent 151214a commit a943f53
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 11 deletions.
37 changes: 26 additions & 11 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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],
)
67 changes: 67 additions & 0 deletions test/test_base_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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

0 comments on commit a943f53

Please sign in to comment.