- Updated test functions in various test files to enhance code clarity by formatting long lines and improving indentation. - Adjusted assertions to use multi-line formatting for better readability. - Added new test cases for theme settings API to ensure proper functionality. - Ensured consistent use of line breaks and spacing across test files for uniformity.
126 lines
3.3 KiB
Python
126 lines
3.3 KiB
Python
from typing import Dict
|
|
|
|
import pytest
|
|
|
|
from models.currency import Currency
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _cleanup_currencies(db_session):
|
|
db_session.query(Currency).delete()
|
|
db_session.commit()
|
|
yield
|
|
db_session.query(Currency).delete()
|
|
db_session.commit()
|
|
|
|
|
|
def _assert_currency(
|
|
payload: Dict[str, object],
|
|
code: str,
|
|
name: str,
|
|
symbol: str | None,
|
|
is_active: bool,
|
|
) -> None:
|
|
assert payload["code"] == code
|
|
assert payload["name"] == name
|
|
assert payload["is_active"] is is_active
|
|
if symbol is None:
|
|
assert payload["symbol"] is None
|
|
else:
|
|
assert payload["symbol"] == symbol
|
|
|
|
|
|
def test_list_returns_default_currency(api_client, db_session):
|
|
response = api_client.get("/api/currencies/")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert any(item["code"] == "USD" for item in data)
|
|
|
|
|
|
def test_create_currency_success(api_client, db_session):
|
|
payload = {"code": "EUR", "name": "Euro", "symbol": "€", "is_active": True}
|
|
response = api_client.post("/api/currencies/", json=payload)
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
_assert_currency(data, "EUR", "Euro", "€", True)
|
|
|
|
stored = db_session.query(Currency).filter_by(code="EUR").one()
|
|
assert stored.name == "Euro"
|
|
assert stored.symbol == "€"
|
|
assert stored.is_active is True
|
|
|
|
|
|
def test_create_currency_conflict(api_client, db_session):
|
|
api_client.post(
|
|
"/api/currencies/",
|
|
json={
|
|
"code": "CAD",
|
|
"name": "Canadian Dollar",
|
|
"symbol": "$",
|
|
"is_active": True,
|
|
},
|
|
)
|
|
duplicate = api_client.post(
|
|
"/api/currencies/",
|
|
json={
|
|
"code": "CAD",
|
|
"name": "Canadian Dollar",
|
|
"symbol": "$",
|
|
"is_active": True,
|
|
},
|
|
)
|
|
assert duplicate.status_code == 409
|
|
|
|
|
|
def test_update_currency_fields(api_client, db_session):
|
|
api_client.post(
|
|
"/api/currencies/",
|
|
json={
|
|
"code": "GBP",
|
|
"name": "British Pound",
|
|
"symbol": "£",
|
|
"is_active": True,
|
|
},
|
|
)
|
|
|
|
response = api_client.put(
|
|
"/api/currencies/GBP",
|
|
json={"name": "Pound Sterling", "symbol": "£", "is_active": False},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
_assert_currency(data, "GBP", "Pound Sterling", "£", False)
|
|
|
|
|
|
def test_toggle_currency_activation(api_client, db_session):
|
|
api_client.post(
|
|
"/api/currencies/",
|
|
json={
|
|
"code": "AUD",
|
|
"name": "Australian Dollar",
|
|
"symbol": "A$",
|
|
"is_active": True,
|
|
},
|
|
)
|
|
|
|
response = api_client.patch(
|
|
"/api/currencies/AUD/activation",
|
|
json={"is_active": False},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
_assert_currency(data, "AUD", "Australian Dollar", "A$", False)
|
|
|
|
|
|
def test_default_currency_cannot_be_deactivated(api_client, db_session):
|
|
api_client.get("/api/currencies/")
|
|
response = api_client.patch(
|
|
"/api/currencies/USD/activation",
|
|
json={"is_active": False},
|
|
)
|
|
assert response.status_code == 400
|
|
assert (
|
|
response.json()["detail"]
|
|
== "The default currency cannot be deactivated."
|
|
)
|