Download test/core/test_timeframe.py from MostafaRoohy/Project_CandleFetcher: direct link, hf CLI and curl.
- Browser
- Download file 6.71 kB
-
https://huggingface.co/datasets/MostafaRoohy/Project_CandleFetcher/resolve/main/test/core/test_timeframe.py
- Command line
-
hf download hf://datasets/MostafaRoohy/Project_CandleFetcher/test/core/test_timeframe.py
-
curl -L -o test_timeframe.py https://huggingface.co/datasets/MostafaRoohy/Project_CandleFetcher/resolve/main/test/core/test_timeframe.py
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 | |
| # | |
| 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') | |
| # | |
| 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) | |
| # | |
| # | |
| ################################################################################################### | |
| ################################################################################################### | |
| ################################################################################################### | |
| # | |