From e0a3eca658d76e8814a069665e977ca013c93178 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 08:18:19 +0200 Subject: [PATCH 1/8] Update expected UNFCCC emissions inventory values --- tests/test_unfccc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_unfccc.py b/tests/test_unfccc.py index 649213b04..d44bd1e00 100644 --- a/tests/test_unfccc.py +++ b/tests/test_unfccc.py @@ -4,7 +4,7 @@ UNFCCC_DF = pd.DataFrame( - [[1990, 1609.25345], [1991, 1434.21149], [1992, 1398.38269]], + [[1990, 1638.57], [1991, 1460.31], [1992, 1429.20]], columns=["year", "value"], ) From a3159ab86646a62f01f2522034d8e769a6f9b4df Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 08:21:13 +0200 Subject: [PATCH 2/8] Change url of the IIASA-auth-api --- pyam/iiasa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyam/iiasa.py b/pyam/iiasa.py index 630cf2344..eaa20af23 100644 --- a/pyam/iiasa.py +++ b/pyam/iiasa.py @@ -24,7 +24,7 @@ # set requests-logger to WARNING only logging.getLogger("requests").setLevel(logging.WARNING) -_AUTH_URL = "https://db1.ene.iiasa.ac.at/EneAuth/config/v1" +_AUTH_URL = "https://api.manager.ece.iiasa.ac.at/legacy/" _CITE_MSG = """ You are connected to the {} scenario explorer hosted by IIASA. If you use this data in any published format, please cite the From 115a0a5b3edbf5212f5e5c5f9d5099d23557dd3c Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 08:27:50 +0200 Subject: [PATCH 3/8] Fix the auth-url format --- pyam/iiasa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyam/iiasa.py b/pyam/iiasa.py index eaa20af23..62812f59f 100644 --- a/pyam/iiasa.py +++ b/pyam/iiasa.py @@ -24,7 +24,7 @@ # set requests-logger to WARNING only logging.getLogger("requests").setLevel(logging.WARNING) -_AUTH_URL = "https://api.manager.ece.iiasa.ac.at/legacy/" +_AUTH_URL = "https://api.manager.ece.iiasa.ac.at/legacy" _CITE_MSG = """ You are connected to the {} scenario explorer hosted by IIASA. If you use this data in any published format, please cite the From 63c01405b782d22d24f527b403b18986c5d17436 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 10:21:10 +0200 Subject: [PATCH 4/8] Migrate IIASA test-config to specific testing module --- tests/conftest.py | 10 ---------- tests/test_iiasa.py | 12 ++++++++---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index f596779ee..c82bdfee2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,10 +21,6 @@ except ConnectionError: # pragma: no cover IIASA_UNAVAILABLE = True -TEST_API = "integration-test" -TEST_API_NAME = "IXSE_INTEGRATION_TEST" - - here = Path(__file__).parent IMAGE_BASELINE_DIR = here / "expected_figs" TEST_DATA_DIR = here / "data" @@ -262,9 +258,3 @@ def recursive_df(request): def plot_stackplot_df(): df = IamDataFrame(TEST_STACKPLOT_DF) yield df - - -@pytest.fixture(scope="session") -def conn(): - if not IIASA_UNAVAILABLE: - return iiasa.Connection(TEST_API) diff --git a/tests/test_iiasa.py b/tests/test_iiasa.py index 16ad8f0df..8d86fc346 100644 --- a/tests/test_iiasa.py +++ b/tests/test_iiasa.py @@ -1,20 +1,24 @@ import os import pytest import pandas as pd +import pandas.testing as pdt import numpy as np - import numpy.testing as npt -import pandas.testing as pdt + +from requests.exceptions import ConnectionError from pyam import IamDataFrame, iiasa, read_iiasa, META_IDX from pyam.testing import assert_iamframe_equal -from .conftest import IIASA_UNAVAILABLE, META_COLS, TEST_API, TEST_API_NAME +from .conftest import META_COLS, IIASA_UNAVAILABLE +# check if connection to IIASA database API is available, skip tests otherwise if IIASA_UNAVAILABLE: pytest.skip("IIASA database API unavailable", allow_module_level=True) -# check to see if we can do online testing of db authentication +TEST_API = "integration-test" +TEST_API_NAME = "IXSE_INTEGRATION_TEST" + TEST_ENV_USER = "IIASA_CONN_TEST_USER" TEST_ENV_PW = "IIASA_CONN_TEST_PW" CONN_ENV_AVAILABLE = TEST_ENV_USER in os.environ and TEST_ENV_PW in os.environ From ca8ee65653d0d5b8c2b74c59fb2e0c59ad3a8b6c Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 10:31:30 +0200 Subject: [PATCH 5/8] Remove skipped tests for deprecated features --- tests/test_iiasa.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/test_iiasa.py b/tests/test_iiasa.py index 8d86fc346..74ac06176 100644 --- a/tests/test_iiasa.py +++ b/tests/test_iiasa.py @@ -5,8 +5,6 @@ import numpy as np import numpy.testing as npt -from requests.exceptions import ConnectionError - from pyam import IamDataFrame, iiasa, read_iiasa, META_IDX from pyam.testing import assert_iamframe_equal @@ -81,20 +79,6 @@ def test_conn_creds_config(): assert conn.current_connection == TEST_API_NAME -@pytest.mark.skipif(not CONN_ENV_AVAILABLE, reason=CONN_ENV_REASON) -def test_conn_creds_tuple(): - user, pw = os.environ[TEST_ENV_USER], os.environ[TEST_ENV_PW] - conn = iiasa.Connection(TEST_API, creds=(user, pw)) - assert conn.current_connection == TEST_API_NAME - - -@pytest.mark.skipif(not CONN_ENV_AVAILABLE, reason=CONN_ENV_REASON) -def test_conn_creds_dict(): - user, pw = os.environ[TEST_ENV_USER], os.environ[TEST_ENV_PW] - conn = iiasa.Connection(TEST_API, creds={"username": user, "password": pw}) - assert conn.current_connection == TEST_API_NAME - - def test_conn_cleartext_raises(): # connecting with invalid credentials raises an error creds = ("_foo", "_bar") From 09d3a188a446a62e90c42aed096a5854298e1974 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 10:46:04 +0200 Subject: [PATCH 6/8] Revert moving IIASA config to specific test-module --- tests/conftest.py | 10 ++++++++++ tests/test_iiasa.py | 7 +++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index c82bdfee2..f596779ee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,6 +21,10 @@ except ConnectionError: # pragma: no cover IIASA_UNAVAILABLE = True +TEST_API = "integration-test" +TEST_API_NAME = "IXSE_INTEGRATION_TEST" + + here = Path(__file__).parent IMAGE_BASELINE_DIR = here / "expected_figs" TEST_DATA_DIR = here / "data" @@ -258,3 +262,9 @@ def recursive_df(request): def plot_stackplot_df(): df = IamDataFrame(TEST_STACKPLOT_DF) yield df + + +@pytest.fixture(scope="session") +def conn(): + if not IIASA_UNAVAILABLE: + return iiasa.Connection(TEST_API) diff --git a/tests/test_iiasa.py b/tests/test_iiasa.py index 74ac06176..aaa7b74a7 100644 --- a/tests/test_iiasa.py +++ b/tests/test_iiasa.py @@ -8,15 +8,14 @@ from pyam import IamDataFrame, iiasa, read_iiasa, META_IDX from pyam.testing import assert_iamframe_equal -from .conftest import META_COLS, IIASA_UNAVAILABLE +from .conftest import META_COLS, IIASA_UNAVAILABLE, TEST_API, TEST_API_NAME + -# check if connection to IIASA database API is available, skip tests otherwise if IIASA_UNAVAILABLE: pytest.skip("IIASA database API unavailable", allow_module_level=True) -TEST_API = "integration-test" -TEST_API_NAME = "IXSE_INTEGRATION_TEST" +# TODO environment variables are currently not set up on GitHub Actions TEST_ENV_USER = "IIASA_CONN_TEST_USER" TEST_ENV_PW = "IIASA_CONN_TEST_PW" CONN_ENV_AVAILABLE = TEST_ENV_USER in os.environ and TEST_ENV_PW in os.environ From 4d873659ecb8acc87f036f8190074375b963d8da Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 12:20:37 +0200 Subject: [PATCH 7/8] Change error message for IIASA-connection trouble --- pyam/iiasa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyam/iiasa.py b/pyam/iiasa.py index 62812f59f..350259122 100644 --- a/pyam/iiasa.py +++ b/pyam/iiasa.py @@ -56,9 +56,9 @@ def _get_config(file=None): return yaml.safe_load(stream) -def _check_response(r, msg="Trouble with request", error=RuntimeError): +def _check_response(r, msg="Error connecting to IIASA database", error=RuntimeError): if not r.ok: - raise error("{}: {}".format(msg, str(r.text))) + raise error(f"{msg}: {r.text}") def _get_token(creds, base_url): From a7b17f025dc8fd68a4c00b37f8d4a9378a713505 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 28 Jun 2022 12:33:35 +0200 Subject: [PATCH 8/8] Improve error messages and add explicit error-message tests --- pyam/iiasa.py | 9 ++++----- tests/test_iiasa.py | 11 +++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pyam/iiasa.py b/pyam/iiasa.py index 350259122..83a59f4f9 100644 --- a/pyam/iiasa.py +++ b/pyam/iiasa.py @@ -184,11 +184,10 @@ def connect(self, name): ) if name not in valid: - msg = """ - {} not recognized as a valid connection name. - Choose from one of the supported connections for your user: {}. - """ - raise ValueError(msg.format(name, self._connection_map.keys())) + raise ValueError( + f"You do not have access to instance '{name}' or it does not exist. " + "Use `Connection.valid_connections` for a list of accessible services." + ) url = "/".join([self._auth_url, "applications", name, "config"]) headers = {"Authorization": "Bearer {}".format(self._token)} diff --git a/tests/test_iiasa.py b/tests/test_iiasa.py index aaa7b74a7..8f832d67a 100644 --- a/tests/test_iiasa.py +++ b/tests/test_iiasa.py @@ -59,7 +59,9 @@ def test_unknown_conn(): # connecting to an unknown API raises an error - pytest.raises(ValueError, iiasa.Connection, "foo") + match = "You do not have access to instance 'foo' or it does not exist." + with pytest.raises(ValueError, match=match): + iiasa.Connection("foo") def test_valid_connections(): @@ -79,9 +81,10 @@ def test_conn_creds_config(): def test_conn_cleartext_raises(): - # connecting with invalid credentials raises an error - creds = ("_foo", "_bar") - pytest.raises(DeprecationWarning, iiasa.Connection, TEST_API, creds=creds) + # connecting with clear-text credentials raises an error + match = "Passing credentials as clear-text is not allowed." + with pytest.raises(DeprecationWarning, match=match): + iiasa.Connection(TEST_API, creds=("user", "password")) def test_variables(conn):