Skip to content

Commit

Permalink
Use pytest.approx(x) instead of int(round(x))
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Oct 11, 2023
1 parent 55f3fdc commit ecb8c25
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import logging
import os
import pytest

import pandas as pd

Expand Down Expand Up @@ -131,11 +132,11 @@ def test_connect_invest():
stor_res = views.node(results, "storage")["scalars"]
my_results["storage_in"] = stor_res[
[(("electricity1", "storage"), "invest")]
]
my_results["storage"] = stor_res[[(("storage", "None"), "invest")]]
].iloc[0]
my_results["storage"] = stor_res[[(("storage", "None"), "invest")]].iloc[0]
my_results["storage_out"] = stor_res[
[(("storage", "electricity1"), "invest")]
]
].iloc[0]

connect_invest_dict = {
"line12": 814705,
Expand All @@ -146,6 +147,6 @@ def test_connect_invest():
}

for key in connect_invest_dict.keys():
assert int(round(my_results[key])) == int(
round(connect_invest_dict[key])
assert my_results[key] == pytest.approx(
connect_invest_dict[key], abs=0.5
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os

import pandas as pd
import pytest

from oemof.solph import EnergySystem
from oemof.solph import Model
Expand Down Expand Up @@ -140,4 +141,4 @@ def test_gen_caes():
}

for key in test_dict.keys():
assert int(round(data[key])) == int(round(test_dict[key]))
assert data[key] == pytest.approx(test_dict[key])
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os

import pandas as pd
import pytest

from oemof import solph as solph
from oemof.solph import processing
Expand Down Expand Up @@ -124,4 +125,4 @@ def test_gen_chp():
}

for key in test_dict.keys():
assert int(round(data[key])) == int(round(test_dict[key]))
assert data[key] == pytest.approx(test_dict[key])
5 changes: 3 additions & 2 deletions tests/test_scripts/test_solph/test_lopf/test_lopf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging

import pandas as pd
import pytest

from oemof.solph import EnergySystem
from oemof.solph import Investment
Expand Down Expand Up @@ -124,8 +125,8 @@ def test_lopf(solver="cbc"):

for key in generators_test_results.keys():
logging.debug("Test genertor production of {0}".format(key))
assert int(round(generators[key])) == int(
round(generators_test_results[key])
assert generators[key].iloc[0] == pytest.approx(
generators_test_results[key]
)

assert (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os

import pandas as pd
import pytest

from oemof.solph import EnergySystem
from oemof.solph import Model
Expand Down Expand Up @@ -190,4 +191,4 @@ def test_dispatch_example(solver="cbc", periods=24 * 5):
}

for key in test_results.keys():
assert int(round(results[key])) == int(round(test_results[key]))
assert results[key] == pytest.approx(test_results[key], abs=0.5)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
SPDX-License-Identifier: MIT
"""

import pytest

from oemof.solph import EnergySystem
from oemof.solph import Model
from oemof.solph import processing
Expand Down Expand Up @@ -116,4 +118,4 @@ def test_dispatch_one_time_step(solver="cbc"):
}

for key in test_results.keys():
assert int(round(results[key])) == int(round(test_results[key]))
assert results[key] == pytest.approx(test_results[key], abs=0.5)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
SPDX-License-Identifier: MIT
"""

import pytest

from oemof.solph import EnergySystem
from oemof.solph import Model
from oemof.solph import processing
Expand Down Expand Up @@ -116,4 +118,4 @@ def test_dispatch_one_time_step(solver="cbc"):
}

for key in test_results.keys():
assert int(round(results[key])) == int(round(test_results[key]))
assert results[key] == pytest.approx(test_results[key], abs=0.5)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os

import pandas as pd
import pytest
from oemof.tools import economics

from oemof.solph import EnergySystem
Expand Down Expand Up @@ -208,4 +209,4 @@ def test_dispatch_example(solver="cbc", periods=24 * 5):
}

for key in test_results.keys():
assert int(round(comp_results[key])) == int(round(test_results[key]))
assert comp_results[key] == pytest.approx(test_results[key], abs=0.5)
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import os

import pandas as pd
import pytest
from oemof.tools import economics

from oemof import solph
Expand Down Expand Up @@ -179,7 +180,7 @@ def test_results_with_actual_dump():
}

for key in stor_invest_dict.keys():
assert int(round(my_results[key])) == int(round(stor_invest_dict[key]))
assert my_results[key] == pytest.approx(stor_invest_dict[key])

# Solver results
assert str(meta["solver"]["Termination condition"]) == "optimal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import logging
import os
import pytest
from collections import namedtuple

import pandas as pd
Expand Down Expand Up @@ -171,9 +172,11 @@ def test_tuples_as_labels_example(
my_results = electricity_bus["sequences"].sum(axis=0).to_dict()
storage = es.groups["storage_electricity_battery"]
storage_node = views.node(results, storage)
my_results["max_load"] = storage_node["sequences"].max()[
[((storage, None), "storage_content")]
]
my_results["max_load"] = (
storage_node["sequences"]
.max()[[((storage, None), "storage_content")]]
.iloc[0]
)
commodity_bus = views.node(results, "bus_natural_gas_None")

gas_usage = commodity_bus["sequences"][
Expand Down Expand Up @@ -201,7 +204,7 @@ def test_tuples_as_labels_example(
}

for key in stor_invest_dict.keys():
assert int(round(my_results[key])) == int(round(stor_invest_dict[key]))
assert my_results[key] == pytest.approx(stor_invest_dict[key])

# Solver results
assert str(meta["solver"]["Termination condition"]) == "optimal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os

import pandas as pd
import pytest

from oemof import solph
from oemof.solph import views
Expand Down Expand Up @@ -172,14 +173,14 @@ def test_variable_chp(filename="variable_chp.csv", solver="cbc"):

for key in variable_chp_dict_max.keys():
logging.debug("Test the maximum value of {0}".format(key))
assert int(round(maxresults[[key]])) == int(
round(variable_chp_dict_max[key])
assert maxresults[[key]].iloc[0] == pytest.approx(
variable_chp_dict_max[key]
)

for key in variable_chp_dict_sum.keys():
logging.debug("Test the summed up value of {0}".format(key))
assert int(round(sumresults[[key]])) == int(
round(variable_chp_dict_sum[key])
assert sumresults[[key]].iloc[0] == pytest.approx(
variable_chp_dict_sum[key]
)

assert (
Expand Down

0 comments on commit ecb8c25

Please sign in to comment.