Metadata-Version: 2.4
Name: pytest-dst-vectors
Version: 0.1.0
Summary: pytest plugin: parametrize tests over DST gap, overlap and normal local times (dst-test-vectors). Offline, MIT.
Author-email: Plainfold <plainfoldhq@proton.me>
License-Expression: MIT
Project-URL: Homepage, https://github.com/plainfoldhq/pytest-dst-vectors
Project-URL: Source, https://github.com/plainfoldhq/pytest-dst-vectors
Project-URL: Issues, https://github.com/plainfoldhq/pytest-dst-vectors/issues
Keywords: pytest,dst,daylight saving time,timezone,time zone,zoneinfo,test vectors,ambiguous time,nonexistent time
Classifier: Framework :: Pytest
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: dst-test-vectors==0.1.1
Requires-Dist: pytest>=7
Dynamic: license-file

# pytest-dst-vectors

[![tests](https://github.com/plainfoldhq/pytest-dst-vectors/actions/workflows/tests.yml/badge.svg)](https://github.com/plainfoldhq/pytest-dst-vectors/actions/workflows/tests.yml)

A pytest plugin that runs your tests over DST edge cases: local wall times that do not exist (spring-forward gap), that happen twice (fall-back overlap), and the seconds right next to a change. Use it when you test code that converts local wall times to UTC around DST changes.

The cases come from [`dst-test-vectors`](https://pypi.org/project/dst-test-vectors/) (pinned to 0.1.1). This plugin adds no data of its own and never uses the network. Python 3.10+, pytest 7+.

## Install

```
pip install pytest-dst-vectors
```

The plugin loads itself (pytest11 entry point). A test that has a `dst_case` argument is run once per case.

## Examples

Every Python example below is run by the test suite (`tests/test_readme.py` extracts the code blocks and runs them with pytest).

### 1. Gaps and overlaps with `zoneinfo`

```python
from datetime import timezone
from zoneinfo import ZoneInfo

import pytest


@pytest.mark.dst_vectors(kind="gap")
def test_gap_fold(dst_case):
    zone = ZoneInfo(dst_case.zone)
    before = dst_case.local.replace(tzinfo=zone, fold=0).astimezone(timezone.utc)
    after = dst_case.local.replace(tzinfo=zone, fold=1).astimezone(timezone.utc)
    assert before == dst_case.compatible  # fold=0 uses the offset before the change
    assert after == dst_case.earlier      # fold=1 uses the offset after it


@pytest.mark.dst_vectors(kind="overlap", zone="America/New_York")
def test_overlap_fold(dst_case):
    zone = ZoneInfo(dst_case.zone)
    first = dst_case.local.replace(tzinfo=zone, fold=0).astimezone(timezone.utc)
    second = dst_case.local.replace(tzinfo=zone, fold=1).astimezone(timezone.utc)
    assert (first, second) == (dst_case.earlier, dst_case.later)
```

### 2. Test your own `to_utc` with `dst_check`

`dst_check(func, policy=..., **filters)` calls `func(zone, naive_local_datetime)` for every matching case and returns the mismatches (an empty list means all agree). Policies are `compatible` (default), `earlier`, `later` and `raise` (gaps must raise); see `dst-test-vectors` for details.

```python
from datetime import timezone
from zoneinfo import ZoneInfo


def to_utc(zone, local):
    return local.replace(tzinfo=ZoneInfo(zone)).astimezone(timezone.utc)


def to_utc_later(zone, local):
    return local.replace(tzinfo=ZoneInfo(zone), fold=1).astimezone(timezone.utc)


def test_to_utc_earlier(dst_check):
    assert dst_check(to_utc, policy="earlier") == []


def test_to_utc_later_in_overlaps(dst_check):
    assert dst_check(to_utc_later, policy="later", kind="overlap") == []
```

### 3. Run against a full vectors file

The plugin uses the bundled sample by default. To run the same tests against another file in the same format, such as the full 2000-2050 set (`dst_test_vectors.json`), pass it on the command line:

```
pytest --dst-vectors=dst_test_vectors.json
```

or set it in `pytest.ini` (`pyproject.toml` works the same under `[tool.pytest.ini_options]`):

```ini
[pytest]
dst_vectors_file = dst_test_vectors.json
```

```python
import pytest


@pytest.mark.dst_vectors(zone="Europe/London", year=2026)
def test_london_2026(dst_case):
    assert dst_case.zone == "Europe/London"
    assert dst_case.local.year == 2026
```

The test suite runs this example with a small stand-in file named `dst_test_vectors.json`, once with `--dst-vectors=dst_test_vectors.json` and once with the ini line above.

## Marker

`@pytest.mark.dst_vectors(kind=None, zone=None, tag=None, year=None)`, keyword arguments only:

| Argument | Meaning |
|---|---|
| `kind` | `"gap"`, `"overlap"` or `"normal"` |
| `zone` | IANA zone name, or a list of names |
| `tag` | e.g. `"midnight"`, `"odd_offset"`, `"delta_30min"`, `"dst"` |
| `year` | local year |

A test that takes `dst_case` without the marker gets every case. `dst_case` is a `dst_test_vectors.Case` (`zone`, `local`, `kind`, `utc`, `earlier`, `later`, `compatible`, `transition_utc`, `tags`, `note`).

If the filters match 0 cases, or `kind` is not one of the three kinds, collection of that test fails with a message that names the filters. Unknown marker arguments fail the same way.

## Test ids

Ids are `Case.id`: zone, local time, kind.

```
test_gap_fold[America/New_York 2026-03-08T02:00:00 gap]
test_overlap_fold[America/New_York 2026-11-01T01:30:00 overlap]
```

Select one with `-k "New_York and overlap"`.

## Options

| Option | Ini | Meaning |
|---|---|---|
| `--dst-vectors=PATH` | `dst_vectors_file` | Vectors JSON file in the `dst-test-vectors` format. Default: the bundled sample. A relative ini path is taken from the rootdir. |

The report header shows what is active, for example `dst-vectors: bundled sample, tzdata 2026f, 36 cases`. An unreadable file stops pytest with a usage error.

## What the bundled sample covers

36 cases for 2026 in 5 zones (America/New_York, Europe/London, America/Havana, Australia/Lord_Howe, Pacific/Chatham), generated from IANA tzdata 2026f: gaps, overlaps and the seconds next to each change, including a 30-minute change (Lord Howe), +12:45/+13:45 (Chatham) and changes at midnight (Havana). Cases are unchanged from the `dst-test-vectors` bundle. The expected values only match a machine whose tz database is as new as the vectors, so an older system tzdata can fail a few cases; install `tzdata` to pin one.

## Full set

The full set has 51,404 vectors for 2000-2050 in every zone, in the same JSON format: https://github.com/plainfoldhq/tz-transitions (paid; the sample above is free). DST dates by country: https://plainfoldhq.github.io/dst-dates/

## About

Plainfold is an AI-run business; this package was written and tested by AI agents.

MIT license. Questions: plainfoldhq@proton.me.
