exchange-data-manager/tests/test_request_modes.py

137 lines
3.7 KiB
Python

"""Tests for request mode handling."""
import pytest
from exchange_data_manager.candles.models import CandleRequest, RequestMode
class TestRequestMode:
"""Tests for RequestMode enum and mode detection."""
def test_range_mode(self):
"""Test RANGE mode when both start and end provided."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
start=1709337600,
end=1709341200,
)
assert request.mode == RequestMode.RANGE
def test_since_mode(self):
"""Test SINCE mode when only start provided."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
start=1709337600,
)
assert request.mode == RequestMode.SINCE
def test_last_n_mode(self):
"""Test LAST_N mode when only limit provided."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
limit=100,
)
assert request.mode == RequestMode.LAST_N
def test_open_mode(self):
"""Test OPEN mode when neither start nor end nor limit provided."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
)
assert request.mode == RequestMode.OPEN
def test_range_takes_precedence_over_limit(self):
"""Test that RANGE mode is detected even with limit."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
start=1709337600,
end=1709341200,
limit=50,
)
assert request.mode == RequestMode.RANGE
def test_since_takes_precedence_over_limit(self):
"""Test that SINCE mode is detected even with limit."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
start=1709337600,
limit=50,
)
assert request.mode == RequestMode.SINCE
class TestMsConversion:
"""Tests for start_ms and end_ms properties."""
def test_start_ms_converts(self):
"""Test start_ms converts seconds to milliseconds."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
start=1709337600,
)
assert request.start_ms == 1709337600000
def test_end_ms_converts(self):
"""Test end_ms converts seconds to milliseconds."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
end=1709341200,
)
assert request.end_ms == 1709341200000
def test_start_ms_none(self):
"""Test start_ms is None when start not provided."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
)
assert request.start_ms is None
def test_end_ms_none(self):
"""Test end_ms is None when end not provided."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
)
assert request.end_ms is None
def test_zero_start_converts(self):
"""Test that start=0 (Unix epoch) converts correctly."""
request = CandleRequest(
exchange="binance",
symbol="BTC/USDT",
timeframe="1m",
start=0,
)
assert request.start_ms == 0
assert request.mode == RequestMode.SINCE # 0 is truthy for mode detection