Skip to content

Commit

Permalink
Improve testability of installation test
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Jul 2, 2024
1 parent ed0d677 commit 4f11cb6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
50 changes: 33 additions & 17 deletions src/oemof/solph/_console_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from oemof import solph


def check_oemof_installation(silent=False):
def _check_oemof_installation(solvers):
logging.disable(logging.CRITICAL)

date_time_index = pd.date_range("1/1/2012", periods=6, freq="h")
Expand Down Expand Up @@ -48,22 +48,38 @@ def check_oemof_installation(silent=False):
om = solph.Model(energysystem)

# check solvers
solver = dict()
for s in ["cbc", "glpk", "gurobi", "cplex"]:
solver_status = dict()
for s in solvers:
try:
om.solve(solver=s)
solver[s] = "working"
solver_status[s] = True
except Exception:
solver[s] = "not working"

if not silent:
print()
print("*****************************")
print("Solver installed with oemof:")
print()
for s, t in solver.items():
print("{0}: {1}".format(s, t))
print()
print("*****************************")
print("oemof successfully installed.")
print("*****************************")
solver_status[s] = False

return solver_status


def check_oemof_installation():

solvers_to_test = ["cbc", "glpk", "gurobi", "cplex"]

solver_status = _check_oemof_installation(solvers_to_test)

print_text = (
"***********************************\n"
"Solver installed with oemof.solph:\n"
"\n"
)
for solver, works in solver_status.items():
if works:
print_text += f"{solver}: installed and working\n"
else:
print_text += f"{solver}: not installed/ not working\n"
print_text += (
"\n"
"***********************************\n"
"oemof.solph successfully installed.\n"
"***********************************\n"
)

print(print_text)
12 changes: 11 additions & 1 deletion tests/test_console_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@


def test_console_scripts():
console_scripts.check_oemof_installation(silent=False)
# this is just a smoke test
console_scripts.check_oemof_installation()


def test_solver_check():
solver_list = ["cbc", "invalid solver"]
results = console_scripts._check_oemof_installation(solver_list)

assert isinstance(results, dict)
assert list(results.keys()) == solver_list
assert results["invalid solver"] == False

0 comments on commit 4f11cb6

Please sign in to comment.