From 9cb2a9a8f79dd152e8e2bd5fb0d7e3f1ceba207e Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Thu, 26 Mar 2020 18:49:33 +0100 Subject: [PATCH 1/5] Create test_get_indicator.py --- tests/test_get_indicator.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/test_get_indicator.py diff --git a/tests/test_get_indicator.py b/tests/test_get_indicator.py new file mode 100644 index 0000000..ccf6840 --- /dev/null +++ b/tests/test_get_indicator.py @@ -0,0 +1,17 @@ +import pytest +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', 'TRD-CMPLMNTRTY-NDX']) +def test_get_indicator(indicator): + df = get_indicator(indicator, 'usa') + assert isinstance(df, pd.DataFrame) + assert len(df.index) From 4bb73da6cc2bb3831b5a67b0d531cd9502fd41d4 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 5 Apr 2020 20:22:50 +0200 Subject: [PATCH 2/5] Print the URL if an error occured --- world_trade_data/data.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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) From 684fa601eb627e8157e45420f0fe349c330fa7a9 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 5 Apr 2020 20:22:59 +0200 Subject: [PATCH 3/5] More tests --- tests/test_get_indicator.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_get_indicator.py b/tests/test_get_indicator.py index ccf6840..ea11d94 100644 --- a/tests/test_get_indicator.py +++ b/tests/test_get_indicator.py @@ -1,4 +1,5 @@ import pytest +import requests import pandas as pd from world_trade_data import get_indicator, get_indicators @@ -10,8 +11,34 @@ def test_get_indicators(): assert 'TRD-CMPLMNTRTY-NDX' in df.index -@pytest.mark.parametrize('indicator', ['MPRT-TRD-VL', 'XPRT-TRD-VL', 'WRLD_GRWTH', 'TRD-CMPLMNTRTY-NDX']) +@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) From 171dfd054a2454cf22841318f062763826f2ba4d Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 5 Apr 2020 20:23:06 +0200 Subject: [PATCH 4/5] Version 0.1.1 --- CHANGELOG.md | 10 ++++++++++ world_trade_data/version.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) 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/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' From ddbcb83eb14c243657f5e0ada7b82823438d9ea0 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 5 Apr 2020 20:31:27 +0200 Subject: [PATCH 5/5] Skip Python 3.4 as pyyaml is not available --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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