feat: add SQL/SQLite, AppSec, Capstone incident-tracker, Gitea Actions CI and spaced repetition
Test and Lint / quality (push) Successful in 34s
Test and Lint / quality (push) Successful in 34s
This commit is contained in:
1 parent
0ac80d60d4
commit
9430a194c3
18 files changed
+422
-7
No files matched your search
@@ -0,0 +1,21 @@
|
||||
# Capstone Mini-Projekt: `incident-tracker` (CLI + SQLite + JSON logs)
|
||||
|
||||
Ten sam projekt SRE realizujesz w Pythonie, Go i Ruście, żeby porównać ergonomię i architekturę wszystkich trzech języków.
|
||||
|
||||
## Wymagania
|
||||
Zaimplementuj aplikację `incident-tracker` (`tracker.py`):
|
||||
1. **Model domenowy:** `Incident(id, title, severity, status)` gdzie `severity` ∈ `{"low", "medium", "high", "critical"}`, `status` ∈ `{"open", "resolved"}`.
|
||||
2. **Warstwa persystencji (`IncidentStore` na SQLite):**
|
||||
* `init_schema()` — idempotentne tworzenie tabeli `incidents`.
|
||||
* `add(title: str, severity: str) -> Incident` — walidacja niepustego tytułu i dozwolonego `severity`, zapis przez parametryzowane zapytanie.
|
||||
* `list_incidents(severity: str | None = None) -> list[Incident]` — opcjonalne filtrowanie.
|
||||
* `resolve(incident_id: int) -> Incident` — zmiana statusu na `"resolved"` (lub `IncidentNotFoundError`).
|
||||
* `stats() -> dict[str, int]` — zliczenie incydentów po `status` (`{"open": ..., "resolved": ...}`).
|
||||
3. **Warstwa CLI (`build_cli(db_path: Path)` w `typer`):**
|
||||
* Podkomendy: `add <title> --severity <sev>`, `list [--severity <sev>]`, `resolve <id>`, `stats`.
|
||||
* Wynik wypisywany jako czysty JSON na `stdout`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test-project
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from tracker import (
|
||||
IncidentNotFoundError,
|
||||
IncidentStore,
|
||||
InvalidIncidentError,
|
||||
build_cli,
|
||||
)
|
||||
from typer.testing import CliRunner
|
||||
|
||||
|
||||
def test_incident_store_sqlite_lifecycle(tmp_path: Path) -> None:
|
||||
store = IncidentStore(tmp_path / "incidents.db")
|
||||
store.init_schema()
|
||||
|
||||
with pytest.raises(InvalidIncidentError):
|
||||
store.add(" ", "high")
|
||||
with pytest.raises(InvalidIncidentError):
|
||||
store.add("DB down", "urgent")
|
||||
|
||||
inc1 = store.add("K8s node NotReady", "critical")
|
||||
inc2 = store.add("High disk usage", "medium")
|
||||
assert inc1.status == "open"
|
||||
assert len(store.list_incidents()) == 2
|
||||
assert [i.id for i in store.list_incidents(severity="critical")] == [inc1.id]
|
||||
|
||||
resolved = store.resolve(inc1.id)
|
||||
assert resolved.status == "resolved"
|
||||
assert store.stats() == {"open": 1, "resolved": 1}
|
||||
|
||||
with pytest.raises(IncidentNotFoundError):
|
||||
store.resolve(9999)
|
||||
assert inc2.id != inc1.id
|
||||
|
||||
|
||||
def test_incident_cli_end_to_end(tmp_path: Path) -> None:
|
||||
db_path = tmp_path / "cli.db"
|
||||
app = build_cli(db_path)
|
||||
runner = CliRunner()
|
||||
|
||||
res_add = runner.invoke(app, ["add", "API 502 spike", "--severity", "high"])
|
||||
assert res_add.exit_code == 0
|
||||
created = json.loads(res_add.stdout)
|
||||
assert created["severity"] == "high"
|
||||
|
||||
res_stats = runner.invoke(app, ["stats"])
|
||||
assert res_stats.exit_code == 0
|
||||
assert json.loads(res_stats.stdout) == {"open": 1, "resolved": 0}
|
||||
@@ -0,0 +1,50 @@
|
||||
import json
|
||||
import sqlite3
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
import typer
|
||||
|
||||
VALID_SEVERITIES = frozenset({"low", "medium", "high", "critical"})
|
||||
|
||||
|
||||
class InvalidIncidentError(ValueError):
|
||||
"""Niepoprawne dane wejściowe incydentu."""
|
||||
|
||||
|
||||
class IncidentNotFoundError(KeyError):
|
||||
"""Nie znaleziono incydentu o podanym ID."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Incident:
|
||||
id: int
|
||||
title: str
|
||||
severity: str
|
||||
status: str
|
||||
|
||||
|
||||
class IncidentStore:
|
||||
def __init__(self, db_path: Path) -> None:
|
||||
self._db_path = db_path
|
||||
|
||||
def init_schema(self) -> None:
|
||||
_ = sqlite3
|
||||
raise NotImplementedError("Utwórz tabelę incidents (id, title, severity, status)")
|
||||
|
||||
def add(self, title: str, severity: str) -> Incident:
|
||||
raise NotImplementedError("Zwaliduj title i severity, zapisz w SQLite i zwróć Incident")
|
||||
|
||||
def list_incidents(self, severity: str | None = None) -> list[Incident]:
|
||||
raise NotImplementedError("Zwróć incydenty (opcjonalnie przefiltrowane po severity)")
|
||||
|
||||
def resolve(self, incident_id: int) -> Incident:
|
||||
raise NotImplementedError("Zmień status na 'resolved' lub rzuć IncidentNotFoundError")
|
||||
|
||||
def stats(self) -> dict[str, int]:
|
||||
raise NotImplementedError("Zwróć {'open': X, 'resolved': Y}")
|
||||
|
||||
|
||||
def build_cli(db_path: Path) -> typer.Typer:
|
||||
_ = (json, db_path, typer.Typer)
|
||||
raise NotImplementedError("Zarejestruj podkomendy add, list, resolve, stats wypisujące JSON")
|
||||
Reference in new issue
Block a user