Files
vault-dash/tests/test_data_service_quote_units.py

60 lines
1.6 KiB
Python

from __future__ import annotations
import pytest
from app.services.cache import CacheService
from app.services.data_service import DataService
class _CacheStub(CacheService):
def __init__(self, initial: dict[str, object] | None = None) -> None:
self._store = dict(initial or {})
async def get_json(self, key: str): # type: ignore[override]
return self._store.get(key)
async def set_json(self, key: str, value): # type: ignore[override]
self._store[key] = value
return True
@pytest.mark.asyncio
async def test_get_quote_upgrades_cached_gld_quote_with_missing_quote_unit() -> None:
cache = _CacheStub(
{
"quote:GLD": {
"symbol": "GLD",
"price": 404.19,
"change": 0.0,
"change_percent": 0.0,
"updated_at": "2026-03-25T00:00:00+00:00",
"source": "cache",
}
}
)
service = DataService(cache=cache)
quote = await service.get_quote("GLD")
assert quote["quote_unit"] == "share"
assert cache._store["quote:GLD"]["quote_unit"] == "share"
@pytest.mark.asyncio
async def test_get_quote_preserves_missing_quote_unit_for_unsupported_symbols() -> None:
cache = _CacheStub(
{
"quote:BTC-USD": {
"symbol": "BTC-USD",
"price": 70000.0,
"updated_at": "2026-03-25T00:00:00+00:00",
"source": "cache",
}
}
)
service = DataService(cache=cache, default_underlying="BTC-USD")
quote = await service.get_quote("BTC-USD")
assert "quote_unit" not in quote