diff --git a/.travis.yml b/.travis.yml index 3ac5038..ab6efe4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,8 @@ python: - "3.6" - "3.7" - "2.7" - - "3.4" - "3.5" - - "3.8-dev" + - "3.8" install: # command to install dependencies - pip install -r requirements-dev.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index b9a414a..85701e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +0.1.1 (2020-04-05) +================== + +Added +----- + +- When an error occur, print the URL that was used for the request (#1) +- Test that data for all the indicators (but `TRD-CMPLMNTRTY-NDX`) can be downloaded (#1) + + 0.1.0 (2019-11-25) ================== diff --git a/tests/test_get_indicator.py b/tests/test_get_indicator.py new file mode 100644 index 0000000..ea11d94 --- /dev/null +++ b/tests/test_get_indicator.py @@ -0,0 +1,44 @@ +import pytest +import requests +import pandas as pd +from world_trade_data import get_indicator, get_indicators + + +def test_get_indicators(): + df = get_indicators() + assert isinstance(df, pd.DataFrame) + assert len(df.index) + assert 'TRD-CMPLMNTRTY-NDX' in df.index + + +@pytest.mark.parametrize('indicator', ['MPRT-TRD-VL', 'XPRT-TRD-VL', 'WRLD-GRWTH']) +def test_get_indicator(indicator): + df = get_indicator(indicator, 'usa') + assert isinstance(df, pd.DataFrame) + assert len(df.index) + + +def test_error_on_wrong_indicator(indicator='INCORRECT'): + with pytest.raises(requests.exceptions.RequestException, match='http://wits.worldbank.org'): + get_indicator(indicator, 'usa') + + +def test_error_on_wrong_partner(indicator='NMBR-XPRT-PRTNR'): + with pytest.raises(requests.exceptions.RequestException, match='http://wits.worldbank.org'): + get_indicator(indicator, 'usa') + + +@pytest.mark.parametrize('indicator', get_indicators().query('SDMX_partnervalue==""').index) +def test_get_all_indicators(indicator): + if indicator == 'TRD-CMPLMNTRTY-NDX': + pytest.xfail('TRD-CMPLMNTRTY-NDX is not available - cf. https://github.com/mwouts/world_trade_data/issues/1') + df = get_indicator(indicator, 'usa') + assert isinstance(df, pd.DataFrame) + assert len(df.index) + + +@pytest.mark.parametrize('indicator', get_indicators().query('SDMX_partnervalue=="999"').index) +def test_get_all_indicators_no_partner(indicator): + df = get_indicator(indicator, 'usa', partner='999') + assert isinstance(df, pd.DataFrame) + assert len(df.index) diff --git a/world_trade_data/data.py b/world_trade_data/data.py index 43a324d..63b386b 100644 --- a/world_trade_data/data.py +++ b/world_trade_data/data.py @@ -79,8 +79,11 @@ def _get_data(reporter, partner, product, year, datasource, name_or_id, **kwargs if ('all' in reporter.lower() and 'all' in partner.lower()) or sum(['all' in args[k] for k in args]) >= 3: LOGGER.warning(LIMITATIONS) - response = requests.get('http://wits.worldbank.org/API/V1/SDMX/V21/{}?format=JSON' - .format('/'.join(list_args))) + url = 'http://wits.worldbank.org/API/V1/SDMX/V21/{}?format=JSON'.format('/'.join(list_args)) + try: + response = requests.get(url) + except requests.exceptions.RequestException as err: + raise requests.exceptions.RequestException('While requesting {}, an error occured: {}'.format(url, err)) response.raise_for_status() data = response.json() return _wits_data_to_df(data, name_or_id=name_or_id) diff --git a/world_trade_data/version.py b/world_trade_data/version.py index 973e52c..7076bb0 100644 --- a/world_trade_data/version.py +++ b/world_trade_data/version.py @@ -1,3 +1,3 @@ """version number""" -__version__ = '0.1.0' +__version__ = '0.1.1'