diff --git a/tests/conftest.py b/tests/conftest.py index ff11187e..23e69733 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,24 @@ import pandas as pd + +def _check_value(key, detected, expected, i): + """ + Helper function to check a single value (start or end) against expected value. + + Args: + key: The key name ('start' or 'end') + detected: The detected value + expected: The expected value + i: Segment index for error messages + """ + if isinstance(detected, float): + assert round(detected, 6) == round(expected, 6), \ + f"Segment {i}: Expected {key} '{expected}', got '{detected}'" + else: + assert detected == expected, \ + f"Segment {i}: Expected {key} '{expected}', got '{detected}'" + def assert_segments_match(detected_segments, expected_segments): """ Helper function to validate that detected segments match expected segments. @@ -38,19 +56,8 @@ def assert_segments_match(detected_segments, expected_segments): assert detected['direction'] == expected['direction'], \ f"Segment {i}: Expected direction '{expected['direction']}', got '{detected['direction']}'" - if isinstance(detected['start'], float): - assert round(detected['start'], 6) == round(expected['start'], 6), \ - f"Segment {i}: Expected start '{expected['start']}', got '{detected['start']}'" - else: - assert detected['start'] == expected['start'], \ - f"Segment {i}: Expected start '{expected['start']}', got '{detected['start']}'" - - if isinstance(detected['end'], float): - assert round(detected['end'], 6) == round(expected['end'], 6), \ - f"Segment {i}: Expected end '{expected['end']}', got '{detected['end']}'" - else: - assert detected['end'] == expected['end'], \ - f"Segment {i}: Expected end '{expected['end']}', got '{detected['end']}'" + _check_value('start', detected['start'], expected['start'], i) + _check_value('end', detected['end'], expected['end'], i) def assert_segments_in_a_haystack(detected_segments, expected_segments): diff --git a/tests/test_non_dates.py b/tests/test_non_dates.py index 1d1c0be5..782c00f5 100644 --- a/tests/test_non_dates.py +++ b/tests/test_non_dates.py @@ -91,4 +91,33 @@ def test_string_index(self): {'direction': 'Flat', 'start': 'Step 168', 'end': 'Step 180'}, ] + assert_segments_match(results.segments, expected_segments) + + @pytest.mark.core + def test_weekly_date_index(self): + """Test standard gradual trend with weekly-spaced dates.""" + df = pt.load_data('series_synthetic') + # Create weekly dates starting from 2026-01-01 + df['weekly_date'] = pd.date_range(start='2026-01-01', periods=len(df), freq='W') + results = pt.detect_trends( + df, + value_col='gradual', + date_col='weekly_date', + plot=False, + method_params={'is_abrupt_padded': False} + ) + + # Expected segments based on current behavior + expected_segments = [ + {'direction': 'Up', 'start': pd.Timestamp('2026-01-11'), 'end': pd.Timestamp('2026-06-14')}, + {'direction': 'Down', 'start': pd.Timestamp('2026-06-21'), 'end': pd.Timestamp('2026-09-06')}, + {'direction': 'Flat', 'start': pd.Timestamp('2026-09-13'), 'end': pd.Timestamp('2026-10-04')}, + {'direction': 'Up', 'start': pd.Timestamp('2026-10-11'), 'end': pd.Timestamp('2027-05-23')}, + {'direction': 'Flat', 'start': pd.Timestamp('2027-05-30'), 'end': pd.Timestamp('2027-06-13')}, + {'direction': 'Down', 'start': pd.Timestamp('2027-06-20'), 'end': pd.Timestamp('2027-09-26')}, + {'direction': 'Up', 'start': pd.Timestamp('2027-10-03'), 'end': pd.Timestamp('2028-06-11')}, + {'direction': 'Down', 'start': pd.Timestamp('2028-06-18'), 'end': pd.Timestamp('2029-03-18')}, + {'direction': 'Flat', 'start': pd.Timestamp('2029-03-25'), 'end': pd.Timestamp('2029-06-17')}, + ] + assert_segments_match(results.segments, expected_segments) \ No newline at end of file