> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# tests/test_expand_events.py

> The date-rule expander (common.contracts.expand_events) turns event_types.csv rules and event_dates.csv listed dates into actual per-year occurrences.

`tests/test_expand_events.py` · chapter [Tests and team tooling](https://calendar-effects.mintlify.site/tour/10-tests-and-tooling.md), file 4 · 104 lines · [open in the repository](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/tests/test_expand_events.py)

## What it does

The date-rule expander (common.contracts.expand\_events) turns event\_types.csv rules
and event\_dates.csv listed dates into actual per-year occurrences.

## What is inside

* **`_by_type_and_year(rows, event_type, year)`** · [line 12](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/tests/test_expand_events.py#L12)

## What it checks

* output matches the events contract columns (line 16)
* black friday and cyber monday 2024 are correct (line 22)
* fixed rule repeats every year (line 30)
* listed events come from event dates csv and are year filtered (line 36)
* nth weekday rolls to the 5th occurrence when it exists (line 51)
* nth weekday raises instead of silently rolling into the next month (line 56)
* offset anchor outside event types csv raises a clear error (line 62)
* offset event near year boundary lands in the right year (line 68)
* one day events have equal start and end (line 78)
* every row is market wide by default (line 85)
* expand events finds offset occurrences anchored in the prior year (line 90)

## Connections

**Uses:** [`src/common/contracts.py`](https://calendar-effects.mintlify.site/reference/src/common/contracts)

## Source

```python title="tests/test_expand_events.py" lines expandable theme={null}
"""The date-rule expander (common.contracts.expand_events) turns event_types.csv rules
and event_dates.csv listed dates into actual per-year occurrences."""
from __future__ import annotations

from datetime import date

import pytest

from common.contracts import CONTRACTS, _nth_weekday_of_month, _resolve_date_for_year, expand_events


def _by_type_and_year(rows, event_type, year):
    return [r for r in rows if r["event_type"] == event_type and r["event_date"].year == year]


def test_output_matches_the_events_contract_columns():
    rows = expand_events(2024, 2024)
    assert rows
    assert set(rows[0]) == set(CONTRACTS["events"])


def test_black_friday_and_cyber_monday_2024_are_correct():
    rows = expand_events(2024, 2024)
    [black_friday] = _by_type_and_year(rows, "black_friday", 2024)
    [cyber_monday] = _by_type_and_year(rows, "cyber_monday", 2024)
    assert black_friday["event_date"] == date(2024, 11, 29)
    assert cyber_monday["event_date"] == date(2024, 12, 2)


def test_fixed_rule_repeats_every_year():
    rows = expand_events(2016, 2025)
    halloweens = {r["event_date"] for r in rows if r["event_type"] == "halloween"}
    assert halloweens == {date(y, 10, 31) for y in range(2016, 2026)}


def test_listed_events_come_from_event_dates_csv_and_are_year_filtered():
    rows = expand_events(2016, 2025)
    prime_days = _by_type_and_year(rows, "prime_day", 2025)
    assert len(prime_days) == 1
    assert prime_days[0]["event_date"] == date(2025, 7, 8)
    assert prime_days[0]["event_end_date"] == date(2025, 7, 11)

    # A narrower range must actually exclude listed rows just outside it, on both ends.
    narrowed = expand_events(2020, 2021)
    assert not _by_type_and_year(narrowed, "prime_day", 2019)
    assert not _by_type_and_year(narrowed, "prime_day", 2022)
    assert _by_type_and_year(narrowed, "prime_day", 2020)
    assert _by_type_and_year(narrowed, "prime_day", 2021)


def test_nth_weekday_rolls_to_the_5th_occurrence_when_it_exists():
    # November 2024 has five Fridays (1, 8, 15, 22, 29).
    assert _nth_weekday_of_month(2024, 11, "fri", 5) == date(2024, 11, 29)


def test_nth_weekday_raises_instead_of_silently_rolling_into_the_next_month():
    # November 2024 has only four Thursdays, so there is no 5th.
    with pytest.raises(ValueError):
        _nth_weekday_of_month(2024, 11, "thu", 5)


def test_offset_anchor_outside_event_types_csv_raises_a_clear_error():
    types_by_id = {"made_up": {"date_rule": "offset:nonexistent_anchor:+1"}}
    with pytest.raises(ValueError, match="nonexistent_anchor"):
        _resolve_date_for_year("made_up", 2024, types_by_id, {}, set())


def test_offset_event_near_year_boundary_lands_in_the_right_year():
    # A hypothetical event 10 days after Christmas (Dec 25) falls in January of next year.
    types_by_id = {
        "christmas": {"date_rule": "fixed:12-25"},
        "post_christmas": {"date_rule": "offset:christmas:+10"},
    }
    d = _resolve_date_for_year("post_christmas", 2024, types_by_id, {}, set())
    assert d == date(2025, 1, 4)


def test_one_day_events_have_equal_start_and_end():
    rows = expand_events(2024, 2024)
    for r in rows:
        if r["event_type"] not in {"prime_day"}:
            assert r["event_date"] == r["event_end_date"]


def test_every_row_is_market_wide_by_default():
    rows = expand_events(2016, 2025)
    assert {r["ticker"] for r in rows} == {"ALL"}


def test_expand_events_finds_offset_occurrences_anchored_in_the_prior_year(tmp_path):
    # "post_christmas" is 10 days after Christmas, so its 2024-anchored occurrence
    # (2025-01-04) must show up when asking for 2025 alone, not just 2024.
    types_csv = tmp_path / "event_types.csv"
    types_csv.write_text(
        "event_type,name,category,region,date_rule,in_scope,notes\n"
        "christmas,Christmas,holiday,global,fixed:12-25,yes,\n"
        "post_christmas,Post-Christmas,shopping,global,offset:christmas:+10,yes,\n"
    )
    dates_csv = tmp_path / "event_dates.csv"
    dates_csv.write_text("event_type,event_date,event_end_date,source\n")

    rows = expand_events(2025, 2025, event_types_path=types_csv, event_dates_path=dates_csv)
    post_christmas_dates = {r["event_date"] for r in rows if r["event_type"] == "post_christmas"}
    assert post_christmas_dates == {date(2025, 1, 4)}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.