From bd0932abf61961ecd99a9b4fcb26f051dbc35454 Mon Sep 17 00:00:00 2001 From: Nitzan P <9297302+nitzpo@users.noreply.github.com> Date: Wed, 26 Nov 2025 23:56:31 +0200 Subject: [PATCH] Fix flaky temporal test for market activity trend detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test_market_activity_trend_stable test was failing intermittently because it used get_recent_date() which approximates months as 30 days, causing inconsistent month boundary calculations depending on when the test runs. Root cause: - Using months_ago * 30 creates shifting month boundaries - Calendar dates don't align consistently with 30-day periods - Deals could fall into unexpected months based on test execution date - Trend calculation compares first half vs second half of months - Inconsistent month grouping led to "decreasing" instead of "stable" Solution: - Use explicit, deterministic dates (15th of each month) - Calculate year-month combinations going back from current month - Ensures exactly 1 deal per month for 12 consecutive months - All deals guaranteed to be within time_period_months=12 window - Month boundaries are now consistent regardless of test execution date Before: Test result varied based on current date (flaky) After: Test result is always "stable" (deterministic) This ensures the test validates the trend detection logic without temporal flakiness. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- tests/govmap/test_market_analysis.py | 41 ++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/tests/govmap/test_market_analysis.py b/tests/govmap/test_market_analysis.py index c905fec..eca6609 100644 --- a/tests/govmap/test_market_analysis.py +++ b/tests/govmap/test_market_analysis.py @@ -235,20 +235,39 @@ class TestCalculateMarketActivityScore: def test_market_activity_trend_stable(self): """Test trend detection - stable activity.""" - # Same number of deals each month - deals = [ - Deal( - objectid=i, - deal_amount=1000000, - deal_date=get_recent_date(months_ago=i), - asset_area=80, + # Create exactly one deal per month for 12 months using recent dates + # Use 15th of each month to ensure consistent month grouping + # Manually calculate year-month combinations going back 12 months + now = datetime.now() + current_year = now.year + current_month = now.month + + deals = [] + for i in range(12): + # Calculate year and month going backwards + month = current_month - i + year = current_year + while month <= 0: + month += 12 + year -= 1 + + deals.append( + Deal( + objectid=i, + deal_amount=1000000, + deal_date=datetime(year, month, 15).date(), + asset_area=80, + ) ) - for i in range(12) - ] + score = calculate_market_activity_score(deals, time_period_months=12) - # With evenly distributed deals, trend could be stable or slightly increasing - assert score.trend in ["stable", "increasing"] + # With exactly 1 deal per month evenly distributed: + # First half (older 6 months): 6 deals / 6 months = 1.0 avg + # Second half (recent 6 months): 6 deals / 6 months = 1.0 avg + # Change ratio: (1.0 - 1.0) / 1.0 = 0.0 + # Since 0.0 is between -0.15 and 0.15, trend should be "stable" + assert score.trend == "stable" def test_market_activity_insufficient_data_for_trend(self): """Test trend with insufficient data."""