You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.4 KiB
Python

"""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/api/v1/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
@pytest.mark.asyncio
async def test_connection_error_raises_oracle_connection_error(httpx_mock: HTTPXMock):
"""ConnectError 시 OracleConnectionError가 발생한다 (3회 재시도 후)."""
import httpx
from libs.oracle_client.client import OracleClient
from libs.oracle_client.exceptions import OracleConnectionError
# 3번 모두 ConnectError (max_attempts=3)
httpx_mock.add_exception(httpx.ConnectError("connection refused"))
httpx_mock.add_exception(httpx.ConnectError("connection refused"))
httpx_mock.add_exception(httpx.ConnectError("connection refused"))
async with OracleClient("http://oracle:18001") as client:
with pytest.raises(OracleConnectionError):
await client.get("/health")
@pytest.mark.asyncio
async def test_timeout_raises_oracle_timeout_error(httpx_mock: HTTPXMock):
"""ReadTimeout 시 OracleTimeoutError가 발생한다 (3회 재시도 후)."""
import httpx
from libs.oracle_client.client import OracleClient
from libs.oracle_client.exceptions import OracleTimeoutError
httpx_mock.add_exception(httpx.ReadTimeout("read timeout"))
httpx_mock.add_exception(httpx.ReadTimeout("read timeout"))
httpx_mock.add_exception(httpx.ReadTimeout("read timeout"))
async with OracleClient("http://oracle:18001") as client:
with pytest.raises(OracleTimeoutError):
await client.get("/health")
@pytest.mark.asyncio
async def test_client_without_context_manager_raises():
"""context manager 없이 get() 호출 시 RuntimeError가 발생한다."""
from libs.oracle_client.client import OracleClient
client = OracleClient("http://oracle:18001")
with pytest.raises(RuntimeError, match="async context manager"):
await client.get("/health")