chore: initialize containerized Python learning workbook (2026 DevOps/SRE)
This commit is contained in:
commit
ae4c421815
63 files changed
+1273
No files matched your search
@@ -0,0 +1,12 @@
|
||||
# 04 — Dobre Praktyki Testowania (`pytest`, In-Memory Fakes)
|
||||
|
||||
Zamiast kruchych mocków (`MagicMock`), w testach jednostkowych stosuj czyste **In-Memory Fakes** implementujące ten sam kontrakt (`Protocol`).
|
||||
|
||||
## Zadania (`testing_ex.py`)
|
||||
1. Zaimplementuj `InMemoryKvStore` spełniający `KvStore`.
|
||||
2. Zaimplementuj `FeatureFlagService.is_enabled(flag: str) -> bool` (zwraca `True`, jeśli wartość w store to `"true"` lub `"1"`, case-insensitive).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=02-engineering-2026/04-testing-best-practices
|
||||
```
|
||||
@@ -0,0 +1,14 @@
|
||||
import pytest
|
||||
from testing_ex import FeatureFlagService, InMemoryKvStore
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("raw_value", "expected"),
|
||||
[("true", True), ("TRUE", True), ("1", True), ("false", False), ("0", False), (None, False)],
|
||||
)
|
||||
def test_feature_flag_service_with_in_memory_fake(raw_value: str | None, expected: bool) -> None:
|
||||
store = InMemoryKvStore()
|
||||
if raw_value is not None:
|
||||
store.set("new_ui", raw_value)
|
||||
svc = FeatureFlagService(store)
|
||||
assert svc.is_enabled("new_ui") is expected
|
||||
@@ -0,0 +1,25 @@
|
||||
from typing import Protocol
|
||||
|
||||
|
||||
class KvStore(Protocol):
|
||||
def get(self, key: str) -> str | None: ...
|
||||
def set(self, key: str, value: str) -> None: ...
|
||||
|
||||
|
||||
class InMemoryKvStore:
|
||||
def __init__(self) -> None:
|
||||
self._data: dict[str, str] = {}
|
||||
|
||||
def get(self, key: str) -> str | None:
|
||||
raise NotImplementedError("Pobierz wartość lub None")
|
||||
|
||||
def set(self, key: str, value: str) -> None:
|
||||
raise NotImplementedError("Zapisz wartość w słowniku")
|
||||
|
||||
|
||||
class FeatureFlagService:
|
||||
def __init__(self, store: KvStore) -> None:
|
||||
self._store = store
|
||||
|
||||
def is_enabled(self, flag: str) -> bool:
|
||||
raise NotImplementedError("Zwróć True jeśli wartość flagi to 'true' lub '1' (lowercase)")
|
||||
Reference in new issue
Block a user