Tidy tests for coindesk, ecb.
This commit is contained in:
parent
fadeee4870
commit
73c5f3a1c6
2 changed files with 63 additions and 147 deletions
|
@ -35,6 +35,11 @@ def currencies_url():
|
||||||
return "https://api.coindesk.com/v1/bpi/supported-currencies.json"
|
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
|
@pytest.fixture
|
||||||
def currencies_json():
|
def currencies_json():
|
||||||
dir = Path(os.path.splitext(__file__)[0])
|
dir = Path(os.path.splitext(__file__)[0])
|
||||||
|
@ -43,54 +48,29 @@ def currencies_json():
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def currencies_response_ok(requests_mock, currencies_url, currencies_json):
|
def currencies_response_ok(requests_mock, currencies_url, currencies_json):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, currencies_url, body=currencies_json, status=200)
|
||||||
responses.GET,
|
|
||||||
currencies_url,
|
|
||||||
body=currencies_json,
|
|
||||||
status=200,
|
|
||||||
)
|
|
||||||
yield requests_mock
|
yield requests_mock
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def recent_json():
|
def recent_response_ok(requests_mock, fetch_url):
|
||||||
dir = Path(os.path.splitext(__file__)[0])
|
json = (Path(os.path.splitext(__file__)[0]) / "recent.json").read_text()
|
||||||
return (dir / "recent.json").read_text()
|
requests_mock.add(responses.GET, fetch_url, body=json, status=200)
|
||||||
|
|
||||||
|
|
||||||
@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,
|
|
||||||
)
|
|
||||||
yield requests_mock
|
yield requests_mock
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def all_json():
|
def all_response_ok(requests_mock, fetch_url):
|
||||||
dir = Path(os.path.splitext(__file__)[0])
|
json = (Path(os.path.splitext(__file__)[0]) / "all-partial.json").read_text()
|
||||||
return (dir / "all-partial.json").read_text()
|
requests_mock.add(responses.GET, fetch_url, body=json, status=200)
|
||||||
|
|
||||||
|
|
||||||
@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,
|
|
||||||
)
|
|
||||||
yield requests_mock
|
yield requests_mock
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def not_found_response(requests_mock):
|
def not_found_response(requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
requests_mock.add(
|
||||||
responses.GET,
|
responses.GET,
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
fetch_url,
|
||||||
status=404,
|
status=404,
|
||||||
body="Sorry, that currency was not found",
|
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):
|
def test_symbols_not_found(src, requests_mock, currencies_url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, currencies_url, body="[]", status=200)
|
||||||
responses.GET,
|
|
||||||
currencies_url,
|
|
||||||
body="[]",
|
|
||||||
status=200,
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.ResponseParsingError) as e:
|
with pytest.raises(exceptions.ResponseParsingError) as e:
|
||||||
src.symbols()
|
src.symbols()
|
||||||
assert "data not found" in str(e.value)
|
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):
|
def test_symbols_bad_status(src, requests_mock, currencies_url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, currencies_url, status=500)
|
||||||
responses.GET,
|
|
||||||
currencies_url,
|
|
||||||
status=500,
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.BadResponse) as e:
|
with pytest.raises(exceptions.BadResponse) as e:
|
||||||
src.symbols()
|
src.symbols()
|
||||||
assert "Server Error" in str(e.value)
|
assert "Server Error" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
def test_symbols_parsing_error(src, requests_mock, currencies_url):
|
def test_symbols_parsing_error(src, requests_mock, currencies_url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, currencies_url, body="NOT JSON")
|
||||||
responses.GET,
|
|
||||||
currencies_url,
|
|
||||||
body="NOT JSON",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.ResponseParsingError) as e:
|
with pytest.raises(exceptions.ResponseParsingError) as e:
|
||||||
src.symbols()
|
src.symbols()
|
||||||
assert "while parsing data" in str(e.value)
|
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
|
assert len(series.prices) > 13
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_from_before_start(src, type, requests_mock):
|
def test_fetch_from_before_start(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
body = "Sorry, the CoinDesk BPI only covers data from 2010-07-17 onwards."
|
||||||
responses.GET,
|
requests_mock.add(responses.GET, fetch_url, status=404, body=body)
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
status=404,
|
|
||||||
body="Sorry, the CoinDesk BPI only covers data from 2010-07-17 onwards.",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.BadResponse) as e:
|
with pytest.raises(exceptions.BadResponse) as e:
|
||||||
src.fetch(Series("BTC", "AUD", type, "2010-01-01", "2010-07-24"))
|
src.fetch(Series("BTC", "AUD", type, "2010-01-01", "2010-07-24"))
|
||||||
assert "only covers data from" in str(e.value)
|
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
|
assert len(series.prices) > 0
|
||||||
|
|
||||||
|
|
||||||
def test_wrong_dates_order(src, type, requests_mock):
|
def test_wrong_dates_order(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
body = "Sorry, but your specified end date is before your start date."
|
||||||
responses.GET,
|
requests_mock.add(responses.GET, fetch_url, status=404, body=body)
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
status=404,
|
|
||||||
body="Sorry, but your specified end date is before your start date.",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.BadResponse) as e:
|
with pytest.raises(exceptions.BadResponse) as e:
|
||||||
src.fetch(Series("BTC", "AUD", type, "2021-01-07", "2021-01-01"))
|
src.fetch(Series("BTC", "AUD", type, "2021-01-07", "2021-01-01"))
|
||||||
assert "End date is before start date." in str(e.value)
|
assert "End date is before start date." in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_in_future(src, type, requests_mock):
|
def test_fetch_in_future(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
body = "Sorry, but your specified end date is before your start date."
|
||||||
responses.GET,
|
requests_mock.add(responses.GET, fetch_url, status=404, body=body)
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
status=404,
|
|
||||||
body="Sorry, but your specified end date is before your start date.",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.BadResponse) as e:
|
with pytest.raises(exceptions.BadResponse) as e:
|
||||||
src.fetch(Series("BTC", "AUD", type, "2030-01-01", "2030-01-07"))
|
src.fetch(Series("BTC", "AUD", type, "2030-01-01", "2030-01-07"))
|
||||||
assert "start date must be in the past" in str(e.value)
|
assert "start date must be in the past" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_empty(src, type, requests_mock):
|
def test_fetch_empty(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, fetch_url, body="{}")
|
||||||
responses.GET,
|
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
body="{}",
|
|
||||||
)
|
|
||||||
series = src.fetch(Series("BTC", "AUD", type, "2010-07-17", "2010-07-17"))
|
series = src.fetch(Series("BTC", "AUD", type, "2010-07-17", "2010-07-17"))
|
||||||
assert len(series.prices) == 0
|
assert len(series.prices) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_known_pair_no_data(src, type, requests_mock):
|
def test_fetch_known_pair_no_data(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
body = "No results returned from database"
|
||||||
responses.GET,
|
requests_mock.add(responses.GET, fetch_url, status=500, body=body)
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
status=500,
|
|
||||||
body="No results returned from database",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.BadResponse) as e:
|
with pytest.raises(exceptions.BadResponse) as e:
|
||||||
src.fetch(Series("BTC", "CUP", type, "2010-07-17", "2010-07-23"))
|
src.fetch(Series("BTC", "CUP", type, "2010-07-17", "2010-07-23"))
|
||||||
assert "No results returned from database" in str(e.value)
|
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"))
|
src.fetch(Series("ABC", "XZY", type, "2021-01-01", "2021-01-07"))
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_network_issue(src, type, requests_mock):
|
def test_fetch_network_issue(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
body = requests.exceptions.ConnectionError("Network issue")
|
||||||
responses.GET,
|
requests_mock.add(responses.GET, fetch_url, body=body)
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
body=requests.exceptions.ConnectionError("Network issue"),
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.RequestError) as e:
|
with pytest.raises(exceptions.RequestError) as e:
|
||||||
src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07"))
|
src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07"))
|
||||||
assert "Network issue" in str(e.value)
|
assert "Network issue" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_bad_status(src, type, requests_mock):
|
def test_fetch_bad_status(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, fetch_url, status=500, body="Some other reason")
|
||||||
responses.GET,
|
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
status=500,
|
|
||||||
body="Some other reason",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.BadResponse) as e:
|
with pytest.raises(exceptions.BadResponse) as e:
|
||||||
src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07"))
|
src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07"))
|
||||||
assert "Internal Server Error" in str(e.value)
|
assert "Internal Server Error" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_parsing_error(src, type, requests_mock):
|
def test_fetch_parsing_error(src, type, requests_mock, fetch_url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, fetch_url, body="NOT JSON")
|
||||||
responses.GET,
|
|
||||||
"https://api.coindesk.com/v1/bpi/historical/close.json",
|
|
||||||
body="NOT JSON",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.ResponseParsingError) as e:
|
with pytest.raises(exceptions.ResponseParsingError) as e:
|
||||||
src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07"))
|
src.fetch(Series("BTC", "AUD", type, "2021-01-01", "2021-01-07"))
|
||||||
assert "while parsing data" in str(e.value)
|
assert "while parsing data" in str(e.value)
|
||||||
|
|
|
@ -25,15 +25,19 @@ def type(src):
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def xml():
|
def url():
|
||||||
dir = Path(os.path.splitext(__file__)[0])
|
return "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml"
|
||||||
return (dir / "eurofxref-hist-partial.xml").read_text()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@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])
|
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
|
@pytest.fixture
|
||||||
|
@ -43,35 +47,23 @@ def requests_mock():
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def response_ok(requests_mock, xml):
|
def response_ok(requests_mock, url, xml):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, url, body=xml, status=200)
|
||||||
responses.GET,
|
|
||||||
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml",
|
|
||||||
body=xml,
|
|
||||||
status=200,
|
|
||||||
)
|
|
||||||
yield requests_mock
|
yield requests_mock
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def response_ok_90d(requests_mock, xml):
|
def response_ok_90d(requests_mock, url_90d, xml):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, url_90d, body=xml, status=200)
|
||||||
responses.GET,
|
|
||||||
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml",
|
|
||||||
body=xml,
|
|
||||||
status=200,
|
|
||||||
)
|
|
||||||
yield requests_mock
|
yield requests_mock
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def response_empty_xml(requests_mock, empty_xml):
|
def response_empty_xml(requests_mock, url):
|
||||||
requests_mock.add(
|
empty_xml = (
|
||||||
responses.GET,
|
Path(os.path.splitext(__file__)[0]) / "eurofxref-hist-empty.xml"
|
||||||
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml",
|
).read_text()
|
||||||
body=empty_xml,
|
requests_mock.add(responses.GET, url, body=empty_xml, status=200)
|
||||||
status=200,
|
|
||||||
)
|
|
||||||
yield requests_mock
|
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"))
|
src.fetch(Series("ABC", "XZY", type, "2021-01-04", "2021-01-08"))
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_network_issue(src, type, requests_mock):
|
def test_fetch_network_issue(src, type, requests_mock, url):
|
||||||
requests_mock.add(
|
err = requests.exceptions.ConnectionError("Network issue")
|
||||||
responses.GET,
|
requests_mock.add(responses.GET, url, body=err)
|
||||||
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml",
|
|
||||||
body=requests.exceptions.ConnectionError("Network issue"),
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.RequestError) as e:
|
with pytest.raises(exceptions.RequestError) as e:
|
||||||
src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08"))
|
src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08"))
|
||||||
assert "Network issue" in str(e.value)
|
assert "Network issue" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_bad_status(src, type, requests_mock):
|
def test_fetch_bad_status(src, type, requests_mock, url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, url, status=500)
|
||||||
responses.GET,
|
|
||||||
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml",
|
|
||||||
status=500,
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.BadResponse) as e:
|
with pytest.raises(exceptions.BadResponse) as e:
|
||||||
src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08"))
|
src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08"))
|
||||||
assert "Server Error" in str(e.value)
|
assert "Server Error" in str(e.value)
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_parsing_error(src, type, requests_mock):
|
def test_fetch_parsing_error(src, type, requests_mock, url):
|
||||||
requests_mock.add(
|
requests_mock.add(responses.GET, url, body="NOT XML")
|
||||||
responses.GET,
|
|
||||||
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml",
|
|
||||||
body="NOT XML",
|
|
||||||
)
|
|
||||||
with pytest.raises(exceptions.ResponseParsingError) as e:
|
with pytest.raises(exceptions.ResponseParsingError) as e:
|
||||||
src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08"))
|
src.fetch(Series("EUR", "AUD", type, "2021-01-04", "2021-01-08"))
|
||||||
assert "while parsing data" in str(e.value)
|
assert "while parsing data" in str(e.value)
|
||||||
|
|
Loading…
Add table
Reference in a new issue