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.

188 lines
6.1 KiB
Python

"""
Test financial data endpoints
"""
import pytest
from fastapi.testclient import TestClient
from datetime import datetime, timedelta
def test_get_financial_data_post_valid_request(client: TestClient, sample_financial_data):
"""Test POST financial data endpoint with valid request"""
response = client.post("/api/v1/financial/data", json=sample_financial_data)
# Should succeed or return specific error
assert response.status_code in [200, 404, 500]
if response.status_code == 200:
data = response.json()
# Check response structure
assert "company" in data
assert "financial_data" in data
assert "metadata" in data
# Check company info
company = data["company"]
assert "ticker" in company
assert "name" in company
# Check metadata
metadata = data["metadata"]
assert "request_id" in metadata
assert "last_updated" in metadata
def test_get_financial_data_post_invalid_dates(client: TestClient):
"""Test POST endpoint with invalid date range"""
invalid_data = {
"ticker": "AAPL",
"start_date": "2023-12-31T00:00:00",
"end_date": "2023-01-01T00:00:00", # End before start
"period_type": "quarterly"
}
response = client.post("/api/v1/financial/data", json=invalid_data)
assert response.status_code == 400
data = response.json()
assert "detail" in data
assert "error_type" in data["detail"]
assert data["detail"]["error_type"] == "VALIDATION_ERROR"
def test_get_financial_data_post_future_dates(client: TestClient):
"""Test POST endpoint with future dates"""
future_date = datetime.now() + timedelta(days=365)
future_data = {
"ticker": "AAPL",
"start_date": future_date.isoformat(),
"end_date": (future_date + timedelta(days=30)).isoformat(),
"period_type": "quarterly"
}
response = client.post("/api/v1/financial/data", json=future_data)
assert response.status_code == 400
data = response.json()
assert "detail" in data
assert "error_type" in data["detail"]
assert data["detail"]["error_type"] == "INVALID_PERIOD"
def test_get_financial_data_post_old_dates(client: TestClient):
"""Test POST endpoint with dates before SEC data availability"""
old_data = {
"ticker": "AAPL",
"start_date": "1990-01-01T00:00:00",
"end_date": "1990-12-31T00:00:00",
"period_type": "quarterly"
}
response = client.post("/api/v1/financial/data", json=old_data)
assert response.status_code == 400
data = response.json()
assert "detail" in data
assert "error_type" in data["detail"]
assert data["detail"]["error_type"] == "INVALID_PERIOD"
def test_get_financial_data_get_valid_request(client: TestClient):
"""Test GET financial data endpoint with valid parameters"""
params = {
"start_date": "2023-01-01T00:00:00",
"end_date": "2023-12-31T23:59:59",
"period_type": "quarterly",
"include_metrics": True,
"force_refresh": False
}
response = client.get("/api/v1/financial/data/AAPL", params=params)
# Should succeed or return specific error
assert response.status_code in [200, 404, 500]
if response.status_code == 200:
data = response.json()
assert "company" in data
assert "financial_data" in data
def test_get_financial_data_get_missing_params(client: TestClient):
"""Test GET endpoint with missing required parameters"""
# Missing start_date and end_date
response = client.get("/api/v1/financial/data/AAPL")
assert response.status_code == 422 # Validation error
def test_get_financial_data_invalid_ticker(client: TestClient, sample_financial_data):
"""Test with invalid ticker"""
invalid_data = sample_financial_data.copy()
invalid_data["ticker"] = "INVALID_TICKER_123"
response = client.post("/api/v1/financial/data", json=invalid_data)
# Should return 404 or 500 depending on implementation
assert response.status_code in [404, 500]
def test_financial_data_request_validation(client: TestClient):
"""Test request validation"""
# Empty ticker
invalid_requests = [
{
"ticker": "",
"start_date": "2023-01-01T00:00:00",
"end_date": "2023-12-31T00:00:00"
},
{
"ticker": "A" * 20, # Too long
"start_date": "2023-01-01T00:00:00",
"end_date": "2023-12-31T00:00:00"
},
{
# Missing required fields
"ticker": "AAPL"
}
]
for invalid_request in invalid_requests:
response = client.post("/api/v1/financial/data", json=invalid_request)
assert response.status_code == 422
def test_financial_data_period_type_validation(client: TestClient):
"""Test period type validation"""
valid_periods = ["quarterly", "annual", "all"]
for period in valid_periods:
data = {
"ticker": "AAPL",
"start_date": "2023-01-01T00:00:00",
"end_date": "2023-12-31T00:00:00",
"period_type": period
}
response = client.post("/api/v1/financial/data", json=data)
# Should not fail on validation
assert response.status_code != 422
def test_financial_data_optional_parameters(client: TestClient):
"""Test optional parameters"""
# Test with all optional parameters
data = {
"ticker": "AAPL",
"start_date": "2023-01-01T00:00:00",
"end_date": "2023-12-31T00:00:00",
"period_type": "quarterly",
"include_metrics": False,
"force_refresh": True
}
response = client.post("/api/v1/financial/data", json=data)
# Should not fail on validation
assert response.status_code != 422
# Test with minimal parameters
minimal_data = {
"ticker": "AAPL",
"start_date": "2023-01-01T00:00:00",
"end_date": "2023-12-31T00:00:00"
}
response = client.post("/api/v1/financial/data", json=minimal_data)
# Should not fail on validation
assert response.status_code != 422