101 lines
2.8 KiB
Python
101 lines
2.8 KiB
Python
"""Tests for adapter base interface and result types."""
|
|
from datetime import datetime
|
|
|
|
from services.adapters.base import AdapterResult, BaseAdapter
|
|
|
|
|
|
class TestAdapterResult:
|
|
def test_ok_when_items_and_no_error(self):
|
|
r = AdapterResult(
|
|
source_type="market_api",
|
|
ticker="AAPL",
|
|
items=[{"price": 150}],
|
|
raw_payload=b'{"price":150}',
|
|
content_hash="abc123",
|
|
fetched_at=datetime(2026, 4, 11),
|
|
)
|
|
assert r.ok is True
|
|
assert r.item_count == 1
|
|
|
|
def test_not_ok_when_error(self):
|
|
r = AdapterResult(
|
|
source_type="market_api",
|
|
ticker="AAPL",
|
|
items=[],
|
|
raw_payload=b"",
|
|
content_hash="",
|
|
fetched_at=datetime(2026, 4, 11),
|
|
error="timeout",
|
|
)
|
|
assert r.ok is False
|
|
|
|
def test_not_ok_when_empty_items(self):
|
|
r = AdapterResult(
|
|
source_type="news_api",
|
|
ticker="MSFT",
|
|
items=[],
|
|
raw_payload=b"{}",
|
|
content_hash="def456",
|
|
fetched_at=datetime(2026, 4, 11),
|
|
)
|
|
assert r.ok is False
|
|
|
|
def test_http_metadata_defaults(self):
|
|
r = AdapterResult(
|
|
source_type="market_api",
|
|
ticker="AAPL",
|
|
items=[{"x": 1}],
|
|
raw_payload=b"x",
|
|
content_hash="h",
|
|
fetched_at=datetime(2026, 4, 11),
|
|
)
|
|
assert r.http_status is None
|
|
assert r.response_time_ms is None
|
|
assert r.metadata == {}
|
|
|
|
|
|
class _StubAdapter(BaseAdapter):
|
|
async def fetch(self, ticker, config):
|
|
return AdapterResult(
|
|
source_type="market_api",
|
|
ticker=ticker,
|
|
items=[],
|
|
raw_payload=b"",
|
|
content_hash="",
|
|
fetched_at=datetime(2026, 4, 11),
|
|
)
|
|
|
|
def source_type(self):
|
|
return "market_api"
|
|
|
|
|
|
class _FilingsStub(BaseAdapter):
|
|
async def fetch(self, ticker, config):
|
|
return AdapterResult(
|
|
source_type="filings_api",
|
|
ticker=ticker,
|
|
items=[],
|
|
raw_payload=b"",
|
|
content_hash="",
|
|
fetched_at=datetime(2026, 4, 11),
|
|
)
|
|
|
|
def source_type(self):
|
|
return "filings_api"
|
|
|
|
|
|
class TestBaseAdapterHelpers:
|
|
def test_bucket_name_market(self):
|
|
adapter = _StubAdapter()
|
|
assert adapter.bucket_name() == "stonks-raw-market"
|
|
|
|
def test_bucket_name_filings(self):
|
|
adapter = _FilingsStub()
|
|
assert adapter.bucket_name() == "stonks-raw-filings"
|
|
|
|
def test_artifact_path_format(self):
|
|
adapter = _StubAdapter()
|
|
now = datetime(2026, 4, 11, 14, 30)
|
|
path = adapter.artifact_path("AAPL", "doc-123", now)
|
|
assert path == "market_api/AAPL/2026/04/11/doc-123/raw.json"
|