feat: Add currency management feature with CRUD operations
Some checks failed
Run Tests / test (push) Failing after 5m2s
Some checks failed
Run Tests / test (push) Failing after 5m2s
- Introduced a new template for currency overview and management (`currencies.html`). - Updated footer to include attribution to AllYouCanGET. - Added "Currencies" link to the main navigation header. - Implemented end-to-end tests for currency creation, update, and activation toggling. - Created unit tests for currency API endpoints, including creation, updating, and activation toggling. - Added a fixture to seed default currencies for testing. - Enhanced database setup tests to ensure proper seeding and migration handling.
This commit is contained in:
101
tests/unit/test_currencies.py
Normal file
101
tests/unit/test_currencies.py
Normal file
@@ -0,0 +1,101 @@
|
||||
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."
|
||||
Reference in New Issue
Block a user