Skip to content

Commit

Permalink
code: unit test for _load_converters_from_osm
Browse files Browse the repository at this point in the history
  • Loading branch information
finozzifa committed Dec 2, 2024
1 parent 2dc6b9a commit bdcffae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
19 changes: 9 additions & 10 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pathlib

import numpy as np
import pandas as pd
import pypsa
import pytest
Expand Down Expand Up @@ -35,25 +36,23 @@ def config():
def converters_dataframe():
return pd.DataFrame(
{
"index": 0,
"converter_id": "convert_20_41",
"bus0": "41",
"bus1": "42",
"underground": False,
"under_construction": False,
"country": "US",
"geometry": "LINESTRING(-122.3787 37.6821, -122.3777 37.6831)",
"converter_id": "convert_5231_5232",
"bus0": 5231,
"bus1": 5232,
"voltage": 380.0,
"geometry": "'LINESTRING(6.8884 45.6783 ",
"": "6.8894 45.6793)'",
},
index=[0],
).set_index("converter_id")
)


@pytest.fixture(scope="function")
def buses_dataframe():
return pd.DataFrame(
{
"bus_id": [5231, 5232],
"voltage": [380.0, 400.0],
"voltage": [380.0, 380.0],
"dc": ["f", "f"],
"symbol": ["Substation", "Substation"],
"under_construction": ["f", "f"],
Expand Down
71 changes: 35 additions & 36 deletions test/test_base_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,6 @@
path_cwd = pathlib.Path.cwd()


df_converters_reference = pd.DataFrame(
{
"converter_id": "convert_20_41",
"index": 0,
"bus0": "41",
"bus1": "42",
"underground": False,
"under_construction": False,
"country": "US",
"geometry": "LINESTRING(-122.3787 37.6821, -122.3777 37.6831)",
"carrier": "B2B",
"dc": True,
},
index=[0],
).set_index("converter_id")


@pytest.mark.parametrize(
"column_name, expected",
[
Expand Down Expand Up @@ -121,36 +104,18 @@ def test_get_oid(column_name, expected):
data_list = [['"oid"=>"12"'], ['"oid"=>"12345"'], ['"oid"=>"9876654"']]
df_exercise = pd.DataFrame(data_list, columns=[column_name])
output_series = _get_oid(df_exercise)
print(output_series)
comparison_series = output_series.compare(expected)
assert comparison_series.size == 0


# def test_load_converters_from_osm(tmpdir, buses_dataframe, config, converters_dataframe):
# """
# Verify what returned by _load_converters_from_osm.
# """
# buses_path = pathlib.Path(tmpdir, "buses.csv")
# buses_dataframe.to_csv(buses_path, index=False)
# countries = config["countries"]
# europe_shape = pathlib.Path(path_cwd, "resources", "europe_shape.geojson")
# df_buses = _load_buses(buses_path, europe_shape, countries, config).reset_index()
# converters_path = pathlib.Path(tmpdir, "converters_exercise.csv")
# converters_dataframe.to_csv(converters_path, index=False)
# df_converters_output = _load_converters_from_osm(df_buses, converters_path)
# df_converters_comparison = df_converters_output.compare(df_converters_reference)
# pathlib.Path.unlink(df_converters_comparison)
# assert df_converters_comparison.empty


def test_load_buses(tmpdir, config, buses_dataframe):
"""
Verify what returned by _load_buses.
"""
df_buses_reference = pd.DataFrame(
{
"bus_id": ["5231", "5232"],
"v_nom": [380.0, 400.0],
"v_nom": [380.0, 380.0],
"symbol": ["Substation", "Substation"],
"under_construction": [False, False],
"x": [6.8884, 6.8894],
Expand All @@ -170,3 +135,37 @@ def test_load_buses(tmpdir, config, buses_dataframe):
pathlib.Path.unlink(buses_path)
df_comparison = df_buses_output.compare(df_buses_reference)
assert df_comparison.empty


def test_load_converters_from_osm(
tmpdir, buses_dataframe, config, converters_dataframe
):
"""
Verify what returned by _load_converters_from_osm.
"""
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": "",
},
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_osm(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

0 comments on commit bdcffae

Please sign in to comment.