diff --git a/tests/pricehist/sources/test_coindesk.py b/tests/pricehist/sources/test_coindesk.py index 98d3d6f..6987c83 100644 --- a/tests/pricehist/sources/test_coindesk.py +++ b/tests/pricehist/sources/test_coindesk.py @@ -35,6 +35,11 @@ def currencies_url(): return "https://api.coindesk.com/v1/bpi/supported-currencies.json" +@pytest.fixture +def fetch_url(): + return "https://api.coindesk.com/v1/bpi/historical/close.json" + + @pytest.fixture def currencies_json(): dir = Path(os.path.splitext(__file__)[0]) @@ -43,54 +48,29 @@ def currencies_json(): @pytest.fixture def currencies_response_ok(requests_mock, currencies_url, currencies_json): - requests_mock.add( - responses.GET, - currencies_url, - body=currencies_json, - status=200, - ) + requests_mock.add(responses.GET, currencies_url, body=currencies_json, status=200) yield requests_mock @pytest.fixture -def recent_json(): - dir = Path(os.path.splitext(__file__)[0]) - return (dir / "recent.json").read_text() - - -@pytest.fixture -def recent_response_ok(requests_mock, recent_json): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - body=recent_json, - status=200, - ) +def recent_response_ok(requests_mock, fetch_url): + json = (Path(os.path.splitext(__file__)[0]) / "recent.json").read_text() + requests_mock.add(responses.GET, fetch_url, body=json, status=200) yield requests_mock @pytest.fixture -def all_json(): - dir = Path(os.path.splitext(__file__)[0]) - return (dir / "all-partial.json").read_text() - - -@pytest.fixture -def all_response_ok(requests_mock, all_json): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - body=all_json, - status=200, - ) +def all_response_ok(requests_mock, fetch_url): + json = (Path(os.path.splitext(__file__)[0]) / "all-partial.json").read_text() + requests_mock.add(responses.GET, fetch_url, body=json, status=200) yield requests_mock @pytest.fixture -def not_found_response(requests_mock): +def not_found_response(requests_mock, fetch_url): requests_mock.add( responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", + fetch_url, status=404, body="Sorry, that currency was not found", ) @@ -139,12 +119,7 @@ def test_symbols_requests_logged(src, currencies_response_ok, caplog): def test_symbols_not_found(src, requests_mock, currencies_url): - requests_mock.add( - responses.GET, - currencies_url, - body="[]", - status=200, - ) + requests_mock.add(responses.GET, currencies_url, body="[]", status=200) with pytest.raises(exceptions.ResponseParsingError) as e: src.symbols() assert "data not found" in str(e.value) @@ -162,22 +137,14 @@ def test_symbols_network_issue(src, requests_mock, currencies_url): def test_symbols_bad_status(src, requests_mock, currencies_url): - requests_mock.add( - responses.GET, - currencies_url, - status=500, - ) + requests_mock.add(responses.GET, currencies_url, status=500) with pytest.raises(exceptions.BadResponse) as e: src.symbols() assert "Server Error" in str(e.value) def test_symbols_parsing_error(src, requests_mock, currencies_url): - requests_mock.add( - responses.GET, - currencies_url, - body="NOT JSON", - ) + requests_mock.add(responses.GET, currencies_url, body="NOT JSON") with pytest.raises(exceptions.ResponseParsingError) as e: src.symbols() assert "while parsing data" in str(e.value) @@ -209,13 +176,9 @@ def test_fetch_long_hist_from_start(src, type, all_response_ok): assert len(series.prices) > 13 -def test_fetch_from_before_start(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - status=404, - body="Sorry, the CoinDesk BPI only covers data from 2010-07-17 onwards.", - ) +def test_fetch_from_before_start(src, type, requests_mock, fetch_url): + body = "Sorry, the CoinDesk BPI only covers data from 2010-07-17 onwards." + requests_mock.add(responses.GET, fetch_url, status=404, body=body) with pytest.raises(exceptions.BadResponse) as e: src.fetch(Series("BTC", "AUD", type, "2010-01-01", "2010-07-24")) assert "only covers data from" in str(e.value) @@ -226,47 +189,31 @@ def test_fetch_to_future(src, type, all_response_ok): assert len(series.prices) > 0 -def test_wrong_dates_order(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - status=404, - body="Sorry, but your specified end date is before your start date.", - ) +def test_wrong_dates_order(src, type, requests_mock, fetch_url): + body = "Sorry, but your specified end date is before your start date." + requests_mock.add(responses.GET, fetch_url, status=404, body=body) with pytest.raises(exceptions.BadResponse) as e: src.fetch(Series("BTC", "AUD", type, "2021-01-07", "2021-01-01")) assert "End date is before start date." in str(e.value) -def test_fetch_in_future(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - status=404, - body="Sorry, but your specified end date is before your start date.", - ) +def test_fetch_in_future(src, type, requests_mock, fetch_url): + body = "Sorry, but your specified end date is before your start date." + requests_mock.add(responses.GET, fetch_url, status=404, body=body) with pytest.raises(exceptions.BadResponse) as e: src.fetch(Series("BTC", "AUD", type, "2030-01-01", "2030-01-07")) assert "start date must be in the past" in str(e.value) -def test_fetch_empty(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - body="{}", - ) +def test_fetch_empty(src, type, requests_mock, fetch_url): + requests_mock.add(responses.GET, fetch_url, body="{}") series = src.fetch(Series("BTC", "AUD", type, "2010-07-17", "2010-07-17")) assert len(series.prices) == 0 -def test_fetch_known_pair_no_data(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - status=500, - body="No results returned from database", - ) +def test_fetch_known_pair_no_data(src, type, requests_mock, fetch_url): + body = "No results returned from database" + requests_mock.add(responses.GET, fetch_url, status=500, body=body) with pytest.raises(exceptions.BadResponse) as e: src.fetch(Series("BTC", "CUP", type, "2010-07-17", "2010-07-23")) assert "No results returned from database" in str(e.value) @@ -292,35 +239,23 @@ def test_fetch_unknown_pair(src, type): src.fetch(Series("ABC", "XZY", type, "2021-01-01", "2021-01-07")) -def test_fetch_network_issue(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - body=requests.exceptions.ConnectionError("Network issue"), - ) +def test_fetch_network_issue(src, type, requests_mock, fetch_url): + body = requests.exceptions.ConnectionError("Network issue") + requests_mock.add(responses.GET, fetch_url, body=body) with pytest.raises(exceptions.RequestError) as e: src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07")) assert "Network issue" in str(e.value) -def test_fetch_bad_status(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - status=500, - body="Some other reason", - ) +def test_fetch_bad_status(src, type, requests_mock, fetch_url): + requests_mock.add(responses.GET, fetch_url, status=500, body="Some other reason") with pytest.raises(exceptions.BadResponse) as e: src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07")) assert "Internal Server Error" in str(e.value) -def test_fetch_parsing_error(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://api.coindesk.com/v1/bpi/historical/close.json", - body="NOT JSON", - ) +def test_fetch_parsing_error(src, type, requests_mock, fetch_url): + requests_mock.add(responses.GET, fetch_url, body="NOT JSON") with pytest.raises(exceptions.ResponseParsingError) as e: src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07")) assert "while parsing data" in str(e.value) diff --git a/tests/pricehist/sources/test_ecb.py b/tests/pricehist/sources/test_ecb.py index c68db11..2d747fe 100644 --- a/tests/pricehist/sources/test_ecb.py +++ b/tests/pricehist/sources/test_ecb.py @@ -25,15 +25,19 @@ def type(src): @pytest.fixture -def xml(): - dir = Path(os.path.splitext(__file__)[0]) - return (dir / "eurofxref-hist-partial.xml").read_text() +def url(): + return "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml" @pytest.fixture -def empty_xml(): +def url_90d(): + return "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml" + + +@pytest.fixture +def xml(): dir = Path(os.path.splitext(__file__)[0]) - return (dir / "eurofxref-hist-empty.xml").read_text() + return (dir / "eurofxref-hist-partial.xml").read_text() @pytest.fixture @@ -43,35 +47,23 @@ def requests_mock(): @pytest.fixture -def response_ok(requests_mock, xml): - requests_mock.add( - responses.GET, - "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml", - body=xml, - status=200, - ) +def response_ok(requests_mock, url, xml): + requests_mock.add(responses.GET, url, body=xml, status=200) yield requests_mock @pytest.fixture -def response_ok_90d(requests_mock, xml): - requests_mock.add( - responses.GET, - "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml", - body=xml, - status=200, - ) +def response_ok_90d(requests_mock, url_90d, xml): + requests_mock.add(responses.GET, url_90d, body=xml, status=200) yield requests_mock @pytest.fixture -def response_empty_xml(requests_mock, empty_xml): - requests_mock.add( - responses.GET, - "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml", - body=empty_xml, - status=200, - ) +def response_empty_xml(requests_mock, url): + empty_xml = ( + Path(os.path.splitext(__file__)[0]) / "eurofxref-hist-empty.xml" + ).read_text() + requests_mock.add(responses.GET, url, body=empty_xml, status=200) yield requests_mock @@ -198,34 +190,23 @@ def test_fetch_unknown_pair(src, type): src.fetch(Series("ABC", "XZY", type, "2021-01-04", "2021-01-08")) -def test_fetch_network_issue(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml", - body=requests.exceptions.ConnectionError("Network issue"), - ) +def test_fetch_network_issue(src, type, requests_mock, url): + err = requests.exceptions.ConnectionError("Network issue") + requests_mock.add(responses.GET, url, body=err) with pytest.raises(exceptions.RequestError) as e: src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08")) assert "Network issue" in str(e.value) -def test_fetch_bad_status(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml", - status=500, - ) +def test_fetch_bad_status(src, type, requests_mock, url): + requests_mock.add(responses.GET, url, status=500) with pytest.raises(exceptions.BadResponse) as e: src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08")) assert "Server Error" in str(e.value) -def test_fetch_parsing_error(src, type, requests_mock): - requests_mock.add( - responses.GET, - "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml", - body="NOT XML", - ) +def test_fetch_parsing_error(src, type, requests_mock, url): + requests_mock.add(responses.GET, url, body="NOT XML") with pytest.raises(exceptions.ResponseParsingError) as e: src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08")) assert "while parsing data" in str(e.value)