pytest-dst-vectors 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_dst_vectors/__init__.py +2 -0
- pytest_dst_vectors/plugin.py +96 -0
- pytest_dst_vectors-0.1.0.dist-info/METADATA +167 -0
- pytest_dst_vectors-0.1.0.dist-info/RECORD +8 -0
- pytest_dst_vectors-0.1.0.dist-info/WHEEL +5 -0
- pytest_dst_vectors-0.1.0.dist-info/entry_points.txt +2 -0
- pytest_dst_vectors-0.1.0.dist-info/licenses/LICENSE +21 -0
- pytest_dst_vectors-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""pytest plugin: `dst_case` parametrization, `dst_check` fixture, `--dst-vectors` option.
|
|
2
|
+
|
|
3
|
+
Adds no data of its own and never uses the network; all cases come from dst_test_vectors.
|
|
4
|
+
"""
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
import dst_test_vectors as dtv
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
_FILTERS = ("kind", "zone", "tag", "year")
|
|
11
|
+
_PATH_KEY = pytest.StashKey[object]()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def pytest_addoption(parser):
|
|
15
|
+
group = parser.getgroup("dst-vectors", "DST test vectors")
|
|
16
|
+
group.addoption("--dst-vectors", dest="dst_vectors", default=None, metavar="PATH",
|
|
17
|
+
help="vectors JSON file in the dst-test-vectors format "
|
|
18
|
+
"(default: the sample bundled with dst-test-vectors)")
|
|
19
|
+
parser.addini("dst_vectors_file", "vectors JSON file (same as --dst-vectors; "
|
|
20
|
+
"relative paths are taken from the rootdir)")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _resolve_path(config):
|
|
24
|
+
"""Absolute path of the vectors file, or None for the bundled sample."""
|
|
25
|
+
cli = config.getoption("dst_vectors")
|
|
26
|
+
if cli:
|
|
27
|
+
return os.path.abspath(cli)
|
|
28
|
+
ini = config.getini("dst_vectors_file")
|
|
29
|
+
if ini:
|
|
30
|
+
return os.path.normpath(os.path.join(str(config.rootpath), ini))
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def pytest_configure(config):
|
|
35
|
+
config.addinivalue_line(
|
|
36
|
+
"markers",
|
|
37
|
+
"dst_vectors(kind=None, zone=None, tag=None, year=None): parametrize the test's "
|
|
38
|
+
"`dst_case` argument over the matching DST test vectors")
|
|
39
|
+
path = _resolve_path(config)
|
|
40
|
+
try:
|
|
41
|
+
meta, loaded = dtv.load(path)
|
|
42
|
+
except (OSError, ValueError, KeyError) as e:
|
|
43
|
+
raise pytest.UsageError(f"dst-vectors: cannot read vectors file {path!r}: {e!r}") from e
|
|
44
|
+
config.stash[_PATH_KEY] = path
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _path(config):
|
|
48
|
+
return config.stash[_PATH_KEY]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def pytest_report_header(config):
|
|
52
|
+
path = _path(config)
|
|
53
|
+
meta, loaded = dtv.load(path)
|
|
54
|
+
return (f"dst-vectors: {path or 'bundled sample'}, tzdata {meta.get('tzdata_version')}, "
|
|
55
|
+
f"{len(loaded)} cases")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _filters(metafunc):
|
|
59
|
+
marker = metafunc.definition.get_closest_marker("dst_vectors")
|
|
60
|
+
if marker is None:
|
|
61
|
+
return {}
|
|
62
|
+
if marker.args:
|
|
63
|
+
pytest.fail(f"{metafunc.definition.nodeid}: dst_vectors takes keyword arguments only "
|
|
64
|
+
f"({', '.join(_FILTERS)}), got positional {marker.args!r}", pytrace=False)
|
|
65
|
+
unknown = sorted(set(marker.kwargs) - set(_FILTERS))
|
|
66
|
+
if unknown:
|
|
67
|
+
pytest.fail(f"{metafunc.definition.nodeid}: unknown dst_vectors argument(s) "
|
|
68
|
+
f"{', '.join(unknown)}; allowed: {', '.join(_FILTERS)}", pytrace=False)
|
|
69
|
+
return {k: v for k, v in marker.kwargs.items() if v is not None}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def pytest_generate_tests(metafunc):
|
|
73
|
+
if "dst_case" not in metafunc.fixturenames:
|
|
74
|
+
return
|
|
75
|
+
filters = _filters(metafunc)
|
|
76
|
+
where = metafunc.definition.nodeid
|
|
77
|
+
try:
|
|
78
|
+
selected = dtv.cases(path=_path(metafunc.config), **filters)
|
|
79
|
+
except ValueError as e:
|
|
80
|
+
pytest.fail(f"{where}: bad dst_vectors filter {filters!r}: {e}", pytrace=False)
|
|
81
|
+
if not selected:
|
|
82
|
+
pytest.fail(f"{where}: dst_vectors filters {filters!r} match 0 cases in "
|
|
83
|
+
f"{_path(metafunc.config) or 'the bundled sample'}", pytrace=False)
|
|
84
|
+
metafunc.parametrize("dst_case", selected, ids=[c.id for c in selected])
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@pytest.fixture
|
|
88
|
+
def dst_check(request):
|
|
89
|
+
"""dst_test_vectors.check bound to the active vectors file:
|
|
90
|
+
dst_check(func, policy="compatible", **filters) -> list of mismatches."""
|
|
91
|
+
path = _path(request.config)
|
|
92
|
+
|
|
93
|
+
def bound(func, policy="compatible", **filters):
|
|
94
|
+
return dtv.check(func, policy, path=path, **filters)
|
|
95
|
+
|
|
96
|
+
return bound
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-dst-vectors
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: pytest plugin: parametrize tests over DST gap, overlap and normal local times (dst-test-vectors). Offline, MIT.
|
|
5
|
+
Author-email: Plainfold <plainfoldhq@proton.me>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/plainfoldhq/pytest-dst-vectors
|
|
8
|
+
Project-URL: Source, https://github.com/plainfoldhq/pytest-dst-vectors
|
|
9
|
+
Project-URL: Issues, https://github.com/plainfoldhq/pytest-dst-vectors/issues
|
|
10
|
+
Keywords: pytest,dst,daylight saving time,timezone,time zone,zoneinfo,test vectors,ambiguous time,nonexistent time
|
|
11
|
+
Classifier: Framework :: Pytest
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Topic :: Software Development :: Testing
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: dst-test-vectors==0.1.1
|
|
20
|
+
Requires-Dist: pytest>=7
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# pytest-dst-vectors
|
|
24
|
+
|
|
25
|
+
[](https://github.com/plainfoldhq/pytest-dst-vectors/actions/workflows/tests.yml)
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
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+.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
pip install pytest-dst-vectors
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The plugin loads itself (pytest11 entry point). A test that has a `dst_case` argument is run once per case.
|
|
38
|
+
|
|
39
|
+
## Examples
|
|
40
|
+
|
|
41
|
+
Every Python example below is run by the test suite (`tests/test_readme.py` extracts the code blocks and runs them with pytest).
|
|
42
|
+
|
|
43
|
+
### 1. Gaps and overlaps with `zoneinfo`
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from datetime import timezone
|
|
47
|
+
from zoneinfo import ZoneInfo
|
|
48
|
+
|
|
49
|
+
import pytest
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@pytest.mark.dst_vectors(kind="gap")
|
|
53
|
+
def test_gap_fold(dst_case):
|
|
54
|
+
zone = ZoneInfo(dst_case.zone)
|
|
55
|
+
before = dst_case.local.replace(tzinfo=zone, fold=0).astimezone(timezone.utc)
|
|
56
|
+
after = dst_case.local.replace(tzinfo=zone, fold=1).astimezone(timezone.utc)
|
|
57
|
+
assert before == dst_case.compatible # fold=0 uses the offset before the change
|
|
58
|
+
assert after == dst_case.earlier # fold=1 uses the offset after it
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@pytest.mark.dst_vectors(kind="overlap", zone="America/New_York")
|
|
62
|
+
def test_overlap_fold(dst_case):
|
|
63
|
+
zone = ZoneInfo(dst_case.zone)
|
|
64
|
+
first = dst_case.local.replace(tzinfo=zone, fold=0).astimezone(timezone.utc)
|
|
65
|
+
second = dst_case.local.replace(tzinfo=zone, fold=1).astimezone(timezone.utc)
|
|
66
|
+
assert (first, second) == (dst_case.earlier, dst_case.later)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2. Test your own `to_utc` with `dst_check`
|
|
70
|
+
|
|
71
|
+
`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.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from datetime import timezone
|
|
75
|
+
from zoneinfo import ZoneInfo
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def to_utc(zone, local):
|
|
79
|
+
return local.replace(tzinfo=ZoneInfo(zone)).astimezone(timezone.utc)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def to_utc_later(zone, local):
|
|
83
|
+
return local.replace(tzinfo=ZoneInfo(zone), fold=1).astimezone(timezone.utc)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_to_utc_earlier(dst_check):
|
|
87
|
+
assert dst_check(to_utc, policy="earlier") == []
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_to_utc_later_in_overlaps(dst_check):
|
|
91
|
+
assert dst_check(to_utc_later, policy="later", kind="overlap") == []
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 3. Run against a full vectors file
|
|
95
|
+
|
|
96
|
+
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:
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
pytest --dst-vectors=dst_test_vectors.json
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
or set it in `pytest.ini` (`pyproject.toml` works the same under `[tool.pytest.ini_options]`):
|
|
103
|
+
|
|
104
|
+
```ini
|
|
105
|
+
[pytest]
|
|
106
|
+
dst_vectors_file = dst_test_vectors.json
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import pytest
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@pytest.mark.dst_vectors(zone="Europe/London", year=2026)
|
|
114
|
+
def test_london_2026(dst_case):
|
|
115
|
+
assert dst_case.zone == "Europe/London"
|
|
116
|
+
assert dst_case.local.year == 2026
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
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.
|
|
120
|
+
|
|
121
|
+
## Marker
|
|
122
|
+
|
|
123
|
+
`@pytest.mark.dst_vectors(kind=None, zone=None, tag=None, year=None)`, keyword arguments only:
|
|
124
|
+
|
|
125
|
+
| Argument | Meaning |
|
|
126
|
+
|---|---|
|
|
127
|
+
| `kind` | `"gap"`, `"overlap"` or `"normal"` |
|
|
128
|
+
| `zone` | IANA zone name, or a list of names |
|
|
129
|
+
| `tag` | e.g. `"midnight"`, `"odd_offset"`, `"delta_30min"`, `"dst"` |
|
|
130
|
+
| `year` | local year |
|
|
131
|
+
|
|
132
|
+
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`).
|
|
133
|
+
|
|
134
|
+
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.
|
|
135
|
+
|
|
136
|
+
## Test ids
|
|
137
|
+
|
|
138
|
+
Ids are `Case.id`: zone, local time, kind.
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
test_gap_fold[America/New_York 2026-03-08T02:00:00 gap]
|
|
142
|
+
test_overlap_fold[America/New_York 2026-11-01T01:30:00 overlap]
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Select one with `-k "New_York and overlap"`.
|
|
146
|
+
|
|
147
|
+
## Options
|
|
148
|
+
|
|
149
|
+
| Option | Ini | Meaning |
|
|
150
|
+
|---|---|---|
|
|
151
|
+
| `--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. |
|
|
152
|
+
|
|
153
|
+
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.
|
|
154
|
+
|
|
155
|
+
## What the bundled sample covers
|
|
156
|
+
|
|
157
|
+
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.
|
|
158
|
+
|
|
159
|
+
## Full set
|
|
160
|
+
|
|
161
|
+
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/
|
|
162
|
+
|
|
163
|
+
## About
|
|
164
|
+
|
|
165
|
+
Plainfold is an AI-run business; this package was written and tested by AI agents.
|
|
166
|
+
|
|
167
|
+
MIT license. Questions: plainfoldhq@proton.me.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
pytest_dst_vectors/__init__.py,sha256=lsVLa1pwVvHMntfMzXzEcIzjt0qFTndMZMA11ckIWHc,113
|
|
2
|
+
pytest_dst_vectors/plugin.py,sha256=QfrQ1vMPfsir2iPg5SOkHjwy5i6DDGtf_xT1P-Pdcm4,3579
|
|
3
|
+
pytest_dst_vectors-0.1.0.dist-info/licenses/LICENSE,sha256=VTJIN4C0LmNas6-7WtMHv4heNx9GXkvDFiegdCS7up8,1066
|
|
4
|
+
pytest_dst_vectors-0.1.0.dist-info/METADATA,sha256=11J9FUrKleemA1KS4eghzMZwAXUOxhoA-vagqQUeUZM,6846
|
|
5
|
+
pytest_dst_vectors-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
pytest_dst_vectors-0.1.0.dist-info/entry_points.txt,sha256=APkEYhyAqYXIWnY9I7tXma-LG5GZYGSHfPV1uSXQl6U,51
|
|
7
|
+
pytest_dst_vectors-0.1.0.dist-info/top_level.txt,sha256=86e3WE3mfFJEegTFlRTZ_EglofV_3MpzAhmrQbUNCGs,19
|
|
8
|
+
pytest_dst_vectors-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Plainfold
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pytest_dst_vectors
|