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.
125 lines
3.9 KiB
Python
125 lines
3.9 KiB
Python
"""
|
|
Test OHLCV endpoints
|
|
"""
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
def test_get_ohlcv_data_post(client: TestClient, sample_ohlcv_data):
|
|
"""Test POST OHLCV endpoint"""
|
|
response = client.post("/api/v1/market/ohlcv", json=sample_ohlcv_data)
|
|
|
|
# Should return 501 (not implemented) or 200/404
|
|
assert response.status_code in [200, 404, 501]
|
|
|
|
if response.status_code == 501:
|
|
data = response.json()
|
|
assert "detail" in data
|
|
assert "error_type" in data["detail"]
|
|
assert data["detail"]["error_type"] == "DATA_NOT_FOUND"
|
|
assert "not yet implemented" in data["detail"]["message"]
|
|
|
|
def test_get_ohlcv_data_get(client: TestClient):
|
|
"""Test GET OHLCV endpoint"""
|
|
params = {
|
|
"start_date": "2023-01-01T00:00:00",
|
|
"end_date": "2023-12-31T23:59:59",
|
|
"interval": "1d"
|
|
}
|
|
|
|
response = client.get("/api/v1/market/ohlcv/AAPL", params=params)
|
|
|
|
# Should return 501 (not implemented) or 200/404
|
|
assert response.status_code in [200, 404, 501]
|
|
|
|
def test_ohlcv_invalid_dates(client: TestClient):
|
|
"""Test OHLCV with invalid dates"""
|
|
invalid_data = {
|
|
"ticker": "AAPL",
|
|
"start_date": "2023-12-31T00:00:00",
|
|
"end_date": "2023-01-01T00:00:00", # End before start
|
|
"interval": "1d"
|
|
}
|
|
|
|
response = client.post("/api/v1/market/ohlcv", 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_ohlcv_invalid_interval(client: TestClient):
|
|
"""Test OHLCV with invalid interval"""
|
|
invalid_data = {
|
|
"ticker": "AAPL",
|
|
"start_date": "2023-01-01T00:00:00",
|
|
"end_date": "2023-12-31T00:00:00",
|
|
"interval": "5min" # Invalid interval
|
|
}
|
|
|
|
response = client.post("/api/v1/market/ohlcv", json=invalid_data)
|
|
|
|
assert response.status_code == 422 # Validation error
|
|
|
|
def test_ohlcv_valid_intervals(client: TestClient):
|
|
"""Test OHLCV with all valid intervals"""
|
|
valid_intervals = ["1d", "1w", "1m"]
|
|
|
|
for interval in valid_intervals:
|
|
data = {
|
|
"ticker": "AAPL",
|
|
"start_date": "2023-01-01T00:00:00",
|
|
"end_date": "2023-12-31T00:00:00",
|
|
"interval": interval
|
|
}
|
|
|
|
response = client.post("/api/v1/market/ohlcv", json=data)
|
|
# Should not fail on validation
|
|
assert response.status_code != 422
|
|
|
|
def test_ohlcv_request_validation(client: TestClient):
|
|
"""Test OHLCV request validation"""
|
|
# Empty ticker
|
|
invalid_requests = [
|
|
{
|
|
"ticker": "",
|
|
"start_date": "2023-01-01T00:00:00",
|
|
"end_date": "2023-12-31T00:00:00",
|
|
"interval": "1d"
|
|
},
|
|
{
|
|
"ticker": "A" * 20, # Too long
|
|
"start_date": "2023-01-01T00:00:00",
|
|
"end_date": "2023-12-31T00:00:00",
|
|
"interval": "1d"
|
|
},
|
|
{
|
|
# Missing required fields
|
|
"ticker": "AAPL",
|
|
"interval": "1d"
|
|
}
|
|
]
|
|
|
|
for invalid_request in invalid_requests:
|
|
response = client.post("/api/v1/market/ohlcv", json=invalid_request)
|
|
assert response.status_code == 422
|
|
|
|
def test_ohlcv_get_missing_params(client: TestClient):
|
|
"""Test GET OHLCV endpoint with missing parameters"""
|
|
# Missing required parameters
|
|
response = client.get("/api/v1/market/ohlcv/AAPL")
|
|
|
|
assert response.status_code == 422
|
|
|
|
def test_ohlcv_get_invalid_interval_param(client: TestClient):
|
|
"""Test GET OHLCV endpoint with invalid interval parameter"""
|
|
params = {
|
|
"start_date": "2023-01-01T00:00:00",
|
|
"end_date": "2023-12-31T00:00:00",
|
|
"interval": "invalid"
|
|
}
|
|
|
|
response = client.get("/api/v1/market/ohlcv/AAPL", params=params)
|
|
|
|
assert response.status_code == 422 |