Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/argus/clients/exchangerate_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def get_rates(curr1: str, curr2: str):
resp.raise_for_status()
payload = resp.json()

if payload["result"] == "success":
data["result"] = "success"
data["conversion_rate"] = payload["conversion_rate"]
return data
else:
data["result"] = "error"
data["error_type"] = payload.get("error_type")
check_error(data["error_type"])
return None

except req.exceptions.Timeout:
print("API hat zu lange gebraucht.")
return None
Expand All @@ -41,16 +51,6 @@ def get_rates(curr1: str, curr2: str):
print("Unerwartete API-Antwortstruktur.")
return None

if payload.get("result") == "success":
data["result"] = "success"
data["conversion_rate"] = payload.get("conversion_rate")
return data
else:
data["result"] = "error"
data["error_type"] = payload.get("error_type")
check_error(data["error_type"])
return None


def check_error(err_type: str) -> None:
"""
Expand Down
36 changes: 29 additions & 7 deletions tests/test_exchangerate_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from argus.clients.exchangerate_client import get_rates, check_error


def test_check_currency_timeout(monkeypatch):
def test_check_currency_timeout(monkeypatch, capsys):
def test_get_resp(url, timeout):
raise req.exceptions.Timeout()

Expand All @@ -12,8 +12,11 @@ def test_get_resp(url, timeout):
data = get_rates("EUR", "USD")
assert data is None

captured = capsys.readouterr()
assert "API hat zu lange gebraucht." in captured.out


def test_check_currency_connection_error(monkeypatch):
def test_check_currency_connection_error(monkeypatch, capsys):
def test_get_resp(url, timeout):
raise req.exceptions.ConnectionError()

Expand All @@ -22,8 +25,11 @@ def test_get_resp(url, timeout):
data = get_rates("EUR", "USD")
assert data is None

captured = capsys.readouterr()
assert "Keine Verbindung zur API." in captured.out


def test_check_currency_request_exception(monkeypatch):
def test_check_currency_request_exception(monkeypatch, capsys):
def test_get_resp(url, timeout):
raise req.exceptions.RequestException("Testfehler")

Expand All @@ -32,8 +38,11 @@ def test_get_resp(url, timeout):
data = get_rates("EUR", "USD")
assert data is None

captured = capsys.readouterr()
assert "Request fehlgeschlagen:" in captured.out


def test_check_currency_value_error(monkeypatch):
def test_check_currency_value_error(monkeypatch, capsys):
test_resp = Mock()
test_resp.raise_for_status.return_value = None
test_resp.json.side_effect = ValueError("Ungültige JSON-Antwort")
Expand All @@ -46,12 +55,15 @@ def test_get_resp(url, timeout):
data = get_rates("EUR", "USD")
assert data is None

captured = capsys.readouterr()
assert "Fehler beim Verarbeiten der API-Antwort." in captured.out


def test_check_currency_key_error(monkeypatch):
def test_check_currency_key_error(monkeypatch, capsys):
test_resp = Mock()
test_resp.raise_for_status.return_value = None
test_resp.json.return_value = {
"result": "",
"result": "success", # not passing "success" bypases the "conversion_rate" checking
"error_type": "",
# "conversion_rate" fehlt absichtlich
}
Expand All @@ -64,6 +76,9 @@ def test_get_resp(url, timeout):
data = get_rates("EUR", "USD")
assert data is None

captured = capsys.readouterr()
assert "Unerwartete API-Antwortstruktur." in captured.out


def test_check_currency_valid(monkeypatch):
test_resp = Mock()
Expand All @@ -83,7 +98,7 @@ def test_get_resp(url, timeout):
assert data == {"result": "success", "error_type": "", "conversion_rate": 1.2}


def test_check_currency_invalid(monkeypatch):
def test_check_currency_invalid(monkeypatch, capsys):
test_resp = Mock()
test_resp.raise_for_status.return_value = None
test_resp.json.return_value = {
Expand All @@ -100,6 +115,9 @@ def test_get_resp(url, timeout):
data = get_rates("EUR", "USD")
assert data is None

captured = capsys.readouterr()
assert "Invalid request! Please try again later." in captured.out


def test_check_error(capsys):
check_error("unsupported-code")
Expand All @@ -123,3 +141,7 @@ def test_check_error(capsys):
captured.out
== "Request limit reached! Please try again later or upgrade to exchangerate-api.com.\n"
)

check_error("Some unknown Error")
captured = capsys.readouterr()
assert captured.out == ""
5 changes: 3 additions & 2 deletions tests/test_timeseries_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def test_get_a_full_timeseries():
"max_rate": [1.1055831909179688],
}
result = prepare_trend_analysis(test_curr, test_start, test_end, test_interval)
if result is None:
return False

assert result is not None

result_df, result_dict = result
result_df["date"] = result_df["date"].astype("str")
result_dict["min_date"] = [str(result_dict["min_date"][0])]
Expand Down
8 changes: 6 additions & 2 deletions tests/test_validation_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@


def test_op_is_valid():
data = is_valid_op("+")

assert data is True
assert is_valid_op("+") is True
assert is_valid_op("-") is True
assert is_valid_op("*") is True
assert is_valid_op("/") is True
assert is_valid_op("%") is True
assert is_valid_op("**") is True


def test_op_is_not_valid():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_yfinance_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_error_raise(monkeypatch):
def fake_yfinance_download(
tickers=test_curr, start=test_start, end=test_end, interval=test_interval
):
return Exception("fake yfinance error")
raise Exception("fake yfinance error")

monkeypatch.setattr("yfinance.download", fake_yfinance_download)

Expand Down
Loading