Skip to content

Commit 3ddf7af

Browse files
committed
feat(template): add sample tests and document quality checks
- add tests/sample/test_main.py with pytest examples - document running pytest, ruff check, and ruff format --check in README.md - include tests/**/*.py in Ruff scope - add Ruff per-file ignores for tests (S101, PLR2004) and fix test docstring lintk
1 parent 6b2bf32 commit 3ddf7af

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ docker compose build dev
204204
docker compose run --rm dev
205205
```
206206

207-
Examples:
207+
Examples (quality checks): run tests with pytest, then run Ruff lint and
208+
format checks.
208209

209210
```bash
211+
docker compose run --rm dev pytest -q
210212
docker compose run --rm dev ruff check
211213
docker compose run --rm dev ruff format --check
212214
```

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ include = [
4444
"**/pyproject.toml",
4545
"src/**/*.py",
4646
"src/**/*.pyi",
47+
"tests/**/*.py",
4748
]
4849
line-length = 79
4950
indent-width = 4
@@ -60,6 +61,10 @@ ignore = [
6061
]
6162

6263
[tool.ruff.lint.per-file-ignores]
64+
"tests/**/*.py" = [
65+
"S101",
66+
"PLR2004",
67+
]
6368

6469
[tool.ruff.lint.isort]
6570

src/sample/main.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
from numpy.random import random
1+
"""Main template."""
22

3+
import numpy as np
34

4-
def run():
5-
sample = random(size=100)
6-
print("Sample mean is ", sample.mean())
5+
6+
def get_sample(k: int) -> list[float]:
7+
"""Example of random sample.
8+
9+
Args:
10+
k: how much samples to get
11+
12+
Returns:
13+
list of sample of size k
14+
"""
15+
rng = np.random.default_rng(seed=42)
16+
floats = rng.random(k)
17+
return floats.tolist()
18+
19+
20+
def run(k: int) -> list[float]:
21+
"""Example of run.
22+
23+
Args:
24+
k: samples to get
25+
26+
Returns:
27+
list samples of size k
28+
"""
29+
return get_sample(k)

tests/sample/test_main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Tests for sample.main module."""
2+
3+
import pytest
4+
5+
from sample.main import get_sample, run
6+
7+
8+
def test_get_sample_returns_requested_number_of_values() -> None:
9+
"""get_sample returns exactly k values in [0, 1]."""
10+
sample = get_sample(5)
11+
assert len(sample) == 5
12+
assert all(0.0 <= value <= 1.0 for value in sample)
13+
14+
15+
def test_get_sample_is_deterministic_for_same_k() -> None:
16+
"""Template behavior is deterministic because seed is fixed."""
17+
assert get_sample(3) == pytest.approx(
18+
[0.7739560485559633, 0.4388784397520523, 0.8585979199113825],
19+
)
20+
21+
22+
def test_run_uses_get_sample(monkeypatch: pytest.MonkeyPatch) -> None:
23+
"""Run delegates to get_sample and returns its result unchanged."""
24+
expected = [0.1, 0.2]
25+
26+
def fake_get_sample(k: int) -> list[float]:
27+
assert k == 2
28+
return expected
29+
30+
monkeypatch.setattr("sample.main.get_sample", fake_get_sample)
31+
assert run(2) == expected

0 commit comments

Comments
 (0)