Skip to content

Commit

Permalink
Fix failing UNFCCC & IIASA API tests (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Jun 28, 2022
1 parent 516da60 commit 52b4d03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
15 changes: 7 additions & 8 deletions pyam/iiasa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)}
Expand Down
34 changes: 12 additions & 22 deletions tests/test_iiasa.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
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 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, TEST_API, TEST_API_NAME


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

# 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
Expand Down Expand Up @@ -58,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():
Expand All @@ -77,24 +80,11 @@ 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")
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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_unfccc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand Down

0 comments on commit 52b4d03

Please sign in to comment.