Project_CandleFetcher / test /core /test_timeframe.py
MostafaRoohy's picture
Snapshot: candle dataset tip (16 Oct 2026)
4d127e7
Raw History Blame Contribute Delete
6.71 kB
"""
Tests for the canonical timeframe vocabulary and candle-track source mapping.
`TimeFrame` values are Unix seconds by contract and `CandleSource` numeric values are
stable serialized identifiers, so these tests pin both against accidental drift.
"""
from __future__ import annotations
#
###################################################################################################
###################################################################################################
################################################################################################### Standard Modules
#
import pytest
#
###################################################################################################
###################################################################################################
################################################################################################### My Modules
#
from candlefetcher.core.exceptions import ValidationError
from candlefetcher.core.timeframe import CandleSource, TimeFrame
#
###################################################################################################
###################################################################################################
################################################################################################### TimeFrame Durations
#
@pytest.mark.parametrize(
'timeframe, expected_seconds',
[
(TimeFrame.S01, 1.0),
(TimeFrame.S30, 30.0),
(TimeFrame.M01, 60.0),
(TimeFrame.M15, 900.0),
(TimeFrame.M30, 1800.0),
(TimeFrame.M45, 2700.0),
(TimeFrame.H01, 3600.0),
(TimeFrame.H04, 14400.0),
(TimeFrame.H12, 43200.0),
(TimeFrame.D01, 86400.0),
(TimeFrame.D06, 518400.0),
(TimeFrame.W01, 604800.0),
],
)
def test_timeframe_seconds(timeframe:TimeFrame, expected_seconds:float) -> None:
"""Every duration is exact Unix seconds, always returned as a fresh float."""
seconds = timeframe.seconds
assert (seconds == expected_seconds)
assert (isinstance(seconds, float))
assert (seconds == timeframe.value)
#
def test_timeframe_enum_identity() -> None:
"""Timeframes compare by identity and never silently create new members."""
assert (TimeFrame.M01 is TimeFrame['M01'])
assert (TimeFrame.M01 is TimeFrame(60.0))
assert (TimeFrame.M01 is not TimeFrame.M02)
with pytest.raises(KeyError):
TimeFrame['M07']
#
with pytest.raises(ValueError):
TimeFrame(601.0)
#
#
def test_timeframe_members_are_unique_in_seconds() -> None:
"""No two timeframes may claim the same duration."""
seconds = [timeframe.seconds for timeframe in TimeFrame]
assert (len(seconds) == len(set(seconds)))
#
def test_timeframe_covers_a_contiguous_practical_grid() -> None:
"""The catalog contains the seconds/minutes/hours/days/week members fetchers rely on."""
expected = {
'S01', 'S05', 'S15', 'S30', 'S50',
'M01', 'M02', 'M03', 'M04', 'M05', 'M10', 'M15', 'M30', 'M45',
'H01', 'H02', 'H03', 'H04', 'H06', 'H08', 'H12',
'D01', 'D02', 'D03', 'D04', 'D05', 'D06',
'W01',
}
assert (expected.issubset({timeframe.name for timeframe in TimeFrame}))
#
###################################################################################################
###################################################################################################
################################################################################################### Candle Source Mapping
#
def test_required_sources_map_to_canonical_columns() -> None:
"""Required TOHLCV sources map to their lowercase dataframe column names."""
assert (CandleSource.TIMESTAMP.column == 'timestamp')
assert (CandleSource.OPEN.column == 'open')
assert (CandleSource.HIGH.column == 'high')
assert (CandleSource.LOW.column == 'low')
assert (CandleSource.CLOSE.column == 'close')
assert (CandleSource.VOLUME.column == 'volume')
#
def test_optional_sources_map_to_canonical_columns() -> None:
"""Optional provider tracks keep their established lowercase names."""
assert (CandleSource.TIMESTAMP_CLOSE.column == 'timestamp_close')
assert (CandleSource.QUOTE_VOLUME.column == 'quote_volume')
assert (CandleSource.TRADE_COUNT.column == 'trade_count')
assert (CandleSource.TAKER_BUY_VOLUME.column == 'taker_buy_volume')
assert (CandleSource.TAKER_BUY_QUOTE_VOLUME.column == 'taker_buy_quote_volume')
#
def test_derived_sources_map_to_canonical_columns() -> None:
"""Derived price tracks expose the same lowercase naming convention."""
assert (CandleSource.HL2.column == 'hl2')
assert (CandleSource.HLC3.column == 'hlc3')
assert (CandleSource.OHLC4.column == 'ohlc4')
#
@pytest.mark.parametrize(
'source, expected_value',
[
(CandleSource.TIMESTAMP , 0),
(CandleSource.OPEN , 1),
(CandleSource.HIGH , 2),
(CandleSource.LOW , 3),
(CandleSource.CLOSE , 4),
(CandleSource.VOLUME , 5),
(CandleSource.TIMESTAMP_CLOSE , 6),
(CandleSource.QUOTE_VOLUME , 7),
(CandleSource.TRADE_COUNT , 8),
(CandleSource.TAKER_BUY_VOLUME , 9),
(CandleSource.TAKER_BUY_QUOTE_VOLUME , 10),
(CandleSource.HL2 , 22),
(CandleSource.HLC3 , 333),
(CandleSource.OHLC4 , 4444),
],
)
def test_source_numeric_values_are_stable_serialization_identifiers(source:CandleSource, expected_value:int) -> None:
"""Numeric source identifiers are a serialization contract and must not drift."""
assert (source.value == expected_value)
#
def test_invalid_tolerance_for_detection_is_rejected() -> None:
"""Negative tolerance inputs fail validation instead of silently passing everything."""
from candlefetcher.core.candle import _detect_timeframe_from_timestamps
with pytest.raises(ValidationError, match='tolerance must be >= 0'):
_detect_timeframe_from_timestamps(__import__('numpy').array([0.0, 60.0]), tolerance=-0.1)
#
#
###################################################################################################
###################################################################################################
###################################################################################################
#