> ## 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_event_calendar.py

> The event calendar (config/event_types.csv and config/event_dates.csv) must follow its contract, so anyone can add an event by adding a row without breaking the pipeline.

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

## What it does

The event calendar (config/event\_types.csv and config/event\_dates.csv) must follow
its contract, so anyone can add an event by adding a row without breaking the pipeline.

## What is inside

* **`SNAKE_CASE`** · [line 18](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/tests/test_event_calendar.py#L18)
* **`header(path)`** · [line 21](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/tests/test_event_calendar.py#L21)
* **`listed_dates()`** · [line 26](https://github.sfu.ca/aba395/Calendar-Effects/blob/main/tests/test_event_calendar.py#L26)

## What it checks

* config files have the contract columns in order (line 31)
* event types are unique snake case ids (line 36)
* every date rule is valid (line 44)
* offset rules point at known events (line 49)
* in scope is yes or no (line 57)
* in scope only filter (line 61)
* listed dates belong to listed events and have sources (line 66)
* in scope listed events have one date per study year (line 74)

## Connections

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

## Source

```python title="tests/test_event_calendar.py" lines expandable theme={null}
"""The event calendar (config/event_types.csv and config/event_dates.csv) must follow
its contract, so anyone can add an event by adding a row without breaking the pipeline."""
from __future__ import annotations

import csv
import re
from datetime import date

from common.contracts import (
    CONTRACTS,
    DATE_RULE,
    EVENT_DATES_CSV,
    EVENT_TYPES_CSV,
    load_event_types,
)
from common.study import STUDY_END_YEAR, STUDY_START_YEAR

SNAKE_CASE = re.compile(r"^[a-z][a-z0-9_]*$")


def header(path) -> list[str]:
    with open(path, newline="", encoding="utf-8-sig") as f:
        return next(csv.reader(f))


def listed_dates() -> list[dict[str, str]]:
    with open(EVENT_DATES_CSV, newline="", encoding="utf-8-sig") as f:
        return list(csv.DictReader(f))


def test_config_files_have_the_contract_columns_in_order():
    assert header(EVENT_TYPES_CSV) == list(CONTRACTS["event_types"])
    assert header(EVENT_DATES_CSV) == list(CONTRACTS["event_dates"])


def test_event_types_are_unique_snake_case_ids():
    ids = [row["event_type"] for row in load_event_types()]
    duplicates = {i for i in ids if ids.count(i) > 1}
    assert not duplicates, f"duplicate event_type: {duplicates}"
    bad = [i for i in ids if not SNAKE_CASE.match(i)]
    assert not bad, f"event_type must be snake_case: {bad}"


def test_every_date_rule_is_valid():
    bad = [(r["event_type"], r["date_rule"]) for r in load_event_types() if not DATE_RULE.match(r["date_rule"])]
    assert not bad, f"invalid date_rule (see docs/data-contracts.md): {bad}"


def test_offset_rules_point_at_known_events():
    ids = {row["event_type"] for row in load_event_types()}
    for row in load_event_types():
        if row["date_rule"].startswith("offset:"):
            anchor = row["date_rule"].split(":")[1]
            assert anchor in ids, f"{row['event_type']} is offset from unknown event {anchor!r}"


def test_in_scope_is_yes_or_no():
    assert {row["in_scope"] for row in load_event_types()} <= {"yes", "no"}


def test_in_scope_only_filter():
    in_scope = load_event_types(in_scope_only=True)
    assert in_scope and all(row["in_scope"] == "yes" for row in in_scope)


def test_listed_dates_belong_to_listed_events_and_have_sources():
    listed = {r["event_type"] for r in load_event_types() if r["date_rule"] == "listed"}
    for row in listed_dates():
        assert row["event_type"] in listed, f"{row['event_type']} has listed dates but its rule is not 'listed'"
        assert row["event_date"] <= row["event_end_date"], f"{row['event_type']}: end before start"
        assert row["source"].strip(), f"{row['event_type']} {row['event_date']}: missing source"


def test_in_scope_listed_events_have_one_date_per_study_year():
    years = range(STUDY_START_YEAR, STUDY_END_YEAR + 1)
    for event in load_event_types(in_scope_only=True):
        if event["date_rule"] != "listed":
            continue
        starts = [date.fromisoformat(r["event_date"]).year for r in listed_dates()
                  if r["event_type"] == event["event_type"]]
        wrong = {year: starts.count(year) for year in years if starts.count(year) != 1}
        assert not wrong, (
            f"{event['event_type']} is in scope with date_rule 'listed', so config/event_dates.csv needs "
            f"exactly one row for each year {years[0]}-{years[-1]} (a missing year silently has no event). "
            f"Rows per year where that is not true: {wrong}"
        )
```


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