matplotlib.use("agg") from pathlib import Path import os from requests.exceptions import ConnectionError import pytest import numpy as np import pandas as pd from datetime import datetime from pyam import IamDataFrame, META_IDX, IAMC_IDX, iiasa # verify whether IIASA database API can be reached, skip tests otherwise try: iiasa.Connection() IIASA_UNAVAILABLE = False 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" TEST_YEARS = [2005, 2010] TEST_DTS = [datetime(2005, 6, 17), datetime(2010, 7, 21)] TEST_TIME_STR = ["2005-06-17", "2010-07-21"] TEST_TIME_STR_HR = ["2005-06-17 00:00:00", "2010-07-21 12:00:00"]
def conn(): if not IIASA_UNAVAILABLE: return iiasa.Connection(TEST_API)
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
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_regions(): conn = iiasa.Connection('IXSE_SR15') obs = conn.regions().values assert 'World' in obs
def test_regions_with_synonyms(): conn = iiasa.Connection('IXSE_SR15') obs = conn.regions(include_synonyms=True) assert 'synonym' in obs.columns assert ( obs[obs.region == 'R5ROWO'].synonym == 'Rest of the World (R5)').all()
def test_conn_creds_tuple(): user, pw = os.environ[TEST_ENV_USER], os.environ[TEST_ENV_PW] conn = iiasa.Connection('IXSE_SR15', creds=(user, pw)) assert conn.current_connection == 'IXSE_SR15'
def test_auth(): conn = iiasa.Connection('iamc15') assert conn.base_url == 'https://db1.ene.iiasa.ac.at/iamc15-api/rest/v2.1/' conn.auth()
def test_anon_conn(): conn = iiasa.Connection('IXSE_SR15') assert conn.current_connection == 'IXSE_SR15'
def test_anon_conn_warning(): conn = iiasa.Connection('iamc15') assert conn.current_connection == 'IXSE_SR15'
def test_query_data_model_scen(): conn = iiasa.Connection('IXSE_SR15') obs = conn._query_post_data(model='AIM*', scenario='ADVANCE_2020_Med2C') exp = copy.deepcopy(QUERY_DATA_EXP) exp['filters']['runs'] = [2] assert obs == exp
def test_available_indicators(): conn = iiasa.Connection('IXSE_SR15') obs = conn.available_metadata() assert 'carbon price|2050' in list(obs)
def test_metadata(): conn = iiasa.Connection('IXSE_SR15') obs = conn.scenario_list()['model'].values assert 'MESSAGEix-GLOBIOM 1.0' in obs
def test_valid_connections(): # connecting to an unknown API raises an error assert TEST_API in iiasa.Connection().valid_connections
def test_variables(): conn = iiasa.Connection('IXSE_SR15') obs = conn.variables().values assert 'Emissions|CO2' in obs
def test_conn_creds_config(): iiasa.set_config(os.environ[TEST_ENV_USER], os.environ[TEST_ENV_PW]) conn = iiasa.Connection(TEST_API) assert conn.current_connection == TEST_API_NAME
def test_metadata(): conn = iiasa.Connection('iamc15') obs = conn.metadata()['model'].values assert 'MESSAGEix-GLOBIOM 1.0' in obs