Skip to content

Commit

Permalink
code: add unit test for _load_links_from_osm and _load_links_from_eg
Browse files Browse the repository at this point in the history
  • Loading branch information
finozzifa committed Dec 3, 2024
1 parent a943f53 commit 429fee8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ def converters_dataframe():
)


@pytest.fixture(scope="function")
def links_dataframe():
return pd.DataFrame(
{
"link_id": "link_5231_5232",
"bus0": 5231,
"bus1": 5232,
"voltage": 380.0,
"p_nom": 600.0,
"length": 1000.0,
"underground": "t",
"under_construction": "f",
"geometry": "'LINESTRING(6.8884 45.6783 ",
"": "6.8894 45.6793)'",
},
index=[0],
)


@pytest.fixture(scope="function")
def transformers_dataframe():
return pd.DataFrame(
Expand Down
74 changes: 74 additions & 0 deletions test/test_base_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
_load_buses,
_load_converters_from_eg,
_load_converters_from_osm,
_load_links_from_eg,
_load_links_from_osm,
_load_transformers,
)

Expand Down Expand Up @@ -205,6 +207,78 @@ def test_load_converters_from_osm(
assert df_converters_comparison.empty


def test_load_links_from_eg(tmpdir, buses_dataframe, config, links_dataframe):
"""
Verify what returned by _load_links_from_eg.
"""
df_links_reference = pd.DataFrame(
{
"link_id": "link_5231_5232",
"bus0": "5231",
"bus1": "5232",
"voltage": 380.0,
"p_nom": 600.0,
"length": 1.0,
"underground": True,
"under_construction": False,
"geometry": "LINESTRING(6.8884 45.6783 ,6.8894 45.6793)",
"carrier": "DC",
},
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)
links_path = pathlib.Path(tmpdir, "links_exercise.csv")
links_dataframe.to_csv(links_path, index=False)
df_links_output = (
_load_links_from_eg(df_buses, links_path)
.drop("Unnamed: 9", axis=1)
.reset_index()
)
df_links_comparison = df_links_output.compare(df_links_reference)
pathlib.Path.unlink(links_path)
assert df_links_comparison.empty


def test_load_links_from_osm(tmpdir, buses_dataframe, config, links_dataframe):
"""
Verify what returned by _load_links_from_osm.
"""
df_links_reference = pd.DataFrame(
{
"link_id": "link_5231_5232",
"bus0": "5231",
"bus1": "5232",
"voltage": 380.0,
"p_nom": 600.0,
"length": 1.0,
"underground": True,
"under_construction": False,
"geometry": "LINESTRING(6.8884 45.6783 ,6.8894 45.6793)",
"carrier": "DC",
},
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)
links_path = pathlib.Path(tmpdir, "links_exercise.csv")
links_dataframe.to_csv(links_path, index=False)
df_links_output = (
_load_links_from_osm(df_buses, links_path)
.drop("Unnamed: 9", axis=1)
.reset_index()
)
df_links_comparison = df_links_output.compare(df_links_reference)
pathlib.Path.unlink(links_path)
assert df_links_comparison.empty


def test_load_transformers(tmpdir, buses_dataframe, config, transformers_dataframe):
"""
Verify what returned by _load_transformers.
Expand Down

0 comments on commit 429fee8

Please sign in to comment.