File size: 6,711 Bytes
4d127e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
    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)
    #
#
###################################################################################################
###################################################################################################
###################################################################################################
#