Skip to content

test: add property-based tests for compute_rsi, compute_roc, and classify_trend using hypothesis #61

@Codex-Crusader

Description

@Codex-Crusader

Summary

The ROADMAP.md v0.5 milestone explicitly calls for property-based tests for pure functions. The pulseengine/core/price.py module contains three pure functions (compute_rsi, compute_roc, classify_trend) that are ideal candidates for hypothesis-based property testing.

Current test coverage uses only hand-crafted example inputs. Property-based tests will catch edge cases that example tests miss.

Functions to cover

compute_rsi(series, period)

Properties to verify:

  • Output is always in [0.0, 100.0]
  • Returns 50.0 when len(series) < period + 1
  • Returns 100.0 when all deltas are positive gains

compute_roc(series, period)

Properties to verify:

  • Returns 0.0 when len(series) <= period
  • Returns 0.0 when the first value is near zero
  • Output is finite or 0.0 for all finite inputs

classify_trend(series)

Properties to verify:

  • Always returns one of {"uptrend", "downtrend", "sideways", "insufficient data"}
  • Returns "insufficient data" when len(series) < 8
  • A strictly monotonically increasing series with sufficient data always returns "uptrend"

Implementation

# tests/test_core.py
from hypothesis import given, strategies as st
import pandas as pd

@given(st.lists(st.floats(min_value=0.01, max_value=1e6, allow_nan=False), min_size=15, max_size=100))
def test_rsi_bounds(values):
    series = pd.Series(values)
    result = compute_rsi(series, period=14)
    assert 0.0 <= result <= 100.0

Requirements

Add hypothesis to requirements-dev.txt.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions