"""Unit tests for Oracle client using pytest-httpx.""" import json from pathlib import Path import pytest from pytest_httpx import HTTPXMock FIXTURES_DIR = Path(__file__).parent.parent / "fixtures" def load_fixture(name: str) -> dict: return json.loads((FIXTURES_DIR / name).read_text()) @pytest.mark.asyncio async def test_search_filings(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.filings import FilingsService data = load_fixture("filing_search.json") httpx_mock.add_response(json=data, url="http://oracle:18001/filings/search/AAPL") async with OracleClient("http://oracle:18001") as client: svc = FilingsService(client) result = await svc.search_filings("AAPL") assert result.ticker == "AAPL" assert len(result.filings) == 2 assert result.filings[0].form_type == "8-K" @pytest.mark.asyncio async def test_get_exhibit(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.filings import FilingsService data = load_fixture("exhibit_content.json") httpx_mock.add_response(json=data) async with OracleClient("http://oracle:18001") as client: svc = FilingsService(client) result = await svc.get_exhibit("0000320193-26-000001") assert result.accession_no == "0000320193-26-000001" assert len(result.content) > 0 @pytest.mark.asyncio async def test_get_daily_bars(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.price import PriceService data = load_fixture("price_data.json") httpx_mock.add_response(json=data) async with OracleClient("http://oracle:18001") as client: svc = PriceService(client) result = await svc.get_daily_bars("AAPL") assert result.ticker == "AAPL" assert len(result.bars) > 0 assert result.bars[0].close > 0 @pytest.mark.asyncio async def test_not_found_raises_oracle_not_found(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.exceptions import OracleNotFoundError from libs.oracle_client.filings import FilingsService httpx_mock.add_response(status_code=404) async with OracleClient("http://oracle:18001") as client: svc = FilingsService(client) with pytest.raises(OracleNotFoundError): await svc.get_exhibit("NONEXISTENT") @pytest.mark.asyncio async def test_server_error_raises_oracle_server_error(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.exceptions import OracleServerError from libs.oracle_client.filings import FilingsService # Must provide one response per retry attempt (3 total) httpx_mock.add_response(status_code=500) httpx_mock.add_response(status_code=500) httpx_mock.add_response(status_code=500) async with OracleClient("http://oracle:18001") as client: svc = FilingsService(client) with pytest.raises(OracleServerError): await svc.get_exhibit("ACC123") @pytest.mark.asyncio async def test_get_retries_on_transient_error_then_succeeds(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.filings import FilingsService data = load_fixture("exhibit_content.json") httpx_mock.add_response(status_code=500) # attempt 1 fails httpx_mock.add_response(status_code=500) # attempt 2 fails httpx_mock.add_response(json=data) # attempt 3 succeeds async with OracleClient("http://oracle:18001") as client: svc = FilingsService(client) result = await svc.get_exhibit("0000320193-26-000001") assert result.accession_no == "0000320193-26-000001" @pytest.mark.asyncio async def test_get_short_volume(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.finra import FinraService data = load_fixture("short_volume.json") httpx_mock.add_response(json=data) async with OracleClient("http://oracle:18001") as client: svc = FinraService(client) result = await svc.get_short_volume("AAPL") assert result.symbol == "AAPL" assert len(result.data) == 3 @pytest.mark.asyncio async def test_get_financial_data(httpx_mock: HTTPXMock): from libs.oracle_client.client import OracleClient from libs.oracle_client.financial import FinancialService data = load_fixture("financial_data.json") httpx_mock.add_response(json=data) async with OracleClient("http://oracle:18001") as client: svc = FinancialService(client) result = await svc.get_financial_data("AAPL") assert result.ticker == "AAPL" assert len(result.periods) == 2