From 4f11cb6b8eb5022c66a7fbbab883c9589f8a5080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Sch=C3=B6nfeldt?= Date: Tue, 2 Jul 2024 17:36:28 +0200 Subject: [PATCH] Improve testability of installation test --- src/oemof/solph/_console_scripts.py | 50 +++++++++++++++++++---------- tests/test_console_scripts.py | 12 ++++++- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/oemof/solph/_console_scripts.py b/src/oemof/solph/_console_scripts.py index da0d66a78..ecc15c77b 100644 --- a/src/oemof/solph/_console_scripts.py +++ b/src/oemof/solph/_console_scripts.py @@ -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") @@ -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) diff --git a/tests/test_console_scripts.py b/tests/test_console_scripts.py index ff03710b8..c016bc9e5 100644 --- a/tests/test_console_scripts.py +++ b/tests/test_console_scripts.py @@ -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