feat: add theory guides (terminology, algorithms, testing, debugging) and 2 new chapters

This commit is contained in:
soda64 committed 2026-11-30 23:08:04 +01:00
1 parent ae4c421815
commit 0ac80d60d4
14 files changed
+380 -3

No files matched your search

@@ -0,0 +1,18 @@
# 05 — Rodzaje Testów: Jednostkowe, Integracyjne i Mutacyjne
Teoria: [notes/04-testing-pyramid-and-types.md](../../../notes/04-testing-pyramid-and-types.md)
W tym rozdziale ćwiczysz **3 warstwy testowania** i sprawdzasz, jak pisać testy odporne na mutanty (*mutation testing*).
## Zadania (`testing_types.py` + `test_testing_types.py`)
1. **Unit (logika z warunkami brzegowymi):** zaimplementuj `is_within_rate_limit(current_requests: int, limit: int) -> bool` (zwraca `True`, gdy `current_requests <= limit` oraz `limit > 0`, a dla ujemnego `current_requests` rzuca `ValueError`).
2. **Integration (prawdziwy system plików):** zaimplementuj `save_and_load_config(path: Path, data: dict[str, str]) -> dict[str, str]`, które zapisuje JSON do pliku na dysku i odczytuje go z powrotem.
3. **Mutation Testing (eksperyment ręczny):** po tym, jak wszystkie testy przejdą:
* zmień na chwilę w `testing_types.py` operator `<=` na `<` (to jest **mutant**!),
* uruchom `make test MOD=02-engineering-2026/05-testing-types-and-mutation`,
* upewnij się, że co najmniej jeden test **wykrył i zabił mutanta** (zakończył się błędem), a potem cofnij zmianę.
## Uruchomienie testów
```bash
make test MOD=02-engineering-2026/05-testing-types-and-mutation
```
@@ -0,0 +1,27 @@
from pathlib import Path
import pytest
from testing_types import is_within_rate_limit, save_and_load_config
# --- 1. Testy jednostkowe + granice zabijające mutanty (<= vs <, > 0 vs >= 0) ---
def test_rate_limit_boundaries_kill_mutants() -> None:
assert is_within_rate_limit(9, 10) is True
assert is_within_rate_limit(10, 10) is True # Zabija mutanta `<=` -> `<`
assert is_within_rate_limit(11, 10) is False
assert is_within_rate_limit(0, 0) is False # Zabija mutanta `limit > 0` -> `>= 0`
assert is_within_rate_limit(0, -1) is False
with pytest.raises(ValueError):
is_within_rate_limit(-1, 10)
# --- 2. Test integracyjny (prawdziwy zapis i odczyt z systemu plików) ---
def test_save_and_load_config_integration(tmp_path: Path) -> None:
cfg_file = tmp_path / "service-config.json"
payload = {"env": "prod", "region": "eu-central-1"}
loaded = save_and_load_config(cfg_file, payload)
assert cfg_file.exists()
assert loaded == payload
@@ -0,0 +1,15 @@
import json
from pathlib import Path
def is_within_rate_limit(current_requests: int, limit: int) -> bool:
"""Sprawdza, czy liczba zapytań mieści się w limicie (current_requests <= limit)."""
raise NotImplementedError(
"ValueError dla current_requests < 0; False dla limit <= 0; inaczej current <= limit"
)
def save_and_load_config(path: Path, data: dict[str, str]) -> dict[str, str]:
"""Zapisuje słownik jako JSON pod path i odczytuje go z powrotem z dysku."""
_ = json
raise NotImplementedError("Zapisz json.dumps(data) do path i odczytaj przez json.loads")