test_enrichment.py¶
tests/test_enrichment.py
Tests for clew.enrichment (the optional `--enrich` curriculum path).
test_enrich_is_idempotent
None test_enrichment.test_enrich_is_idempotent(Path tmp_path)
Re-running enrichment replaces rows rather than accumulating them.
Checks
assert rows == [("only",)]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_enrich_is_idempotent → enrich_database |
Source · 19 lines
def test_enrich_is_idempotent(tmp_path: Path) -> None:
"""The docstring promises re-running the build idempotently overwrites the
table (DELETE before insert). A second enrich with fewer topics must leave
exactly those topics, not the union.
@brief Re-enrich replaces, never accumulates.
@version 1
"""
db = _enrich(tmp_path, _CURRICULUM) # 2 topics
yaml_path = tmp_path / "topics.yaml"
yaml_path.write_text("- topic: only\n title: Only\n summary: S\n", encoding="utf-8")
enrich_database(db, yaml_path) # re-run with 1 topic
conn = sqlite3.connect(str(db))
rows = conn.execute("SELECT topic FROM architecture_topics").fetchall()
conn.close()
assert rows == [("only",)]
Activity · 8 steps
test_enrich_optional_field_defaults
None test_enrichment.test_enrich_optional_field_defaults(Path tmp_path)
Missing optional fields fall back to empty lists / difficulty 1.
Checks
assert json.loads(row[0]) == []assert json.loads(row[1]) == []assert json.loads(row[2]) == []assert row[3] == 1
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_enrich_optional_field_defaults → _enrich → enrich_database |
Source · 20 lines
def test_enrich_optional_field_defaults(tmp_path: Path) -> None:
"""A minimal entry (only the required scalar fields) inserts cleanly: the
absent list fields default to `[]` and difficulty to 1.
@brief Optional fields get sane defaults.
@version 1
"""
minimal = "- topic: t\n title: T\n summary: S\n"
db = _enrich(tmp_path, minimal)
conn = sqlite3.connect(str(db))
row = conn.execute(
"SELECT key_files, key_concepts, prerequisite_topics, difficulty FROM architecture_topics"
).fetchone()
conn.close()
assert json.loads(row[0]) == []
assert json.loads(row[1]) == []
assert json.loads(row[2]) == []
assert row[3] == 1
Activity · 9 steps
test_enrich_populates_topics
None test_enrichment.test_enrich_populates_topics(Path tmp_path)
A curriculum round-trips into architecture_topics with list fields JSON-encoded.
Checks
assert len(rows) == 2assert event_bus[0] == "event_bus"assert event_bus[1] == "The Event Bus"assert json.loads(event_bus[3]) == ["src/event_bus/event_bus.c"]assert json.loads(event_bus[4]) == ["callback", "dispatch"]assert event_bus[6] == 2assert dataflow[0] == "dataflow"assert json.loads(dataflow[5]) == ["event_bus"] # prerequisite_topicsassert dataflow[6] == 3
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_enrich_populates_topics → _enrich → enrich_database |
Source · 28 lines
def test_enrich_populates_topics(tmp_path: Path) -> None:
"""Each YAML entry becomes one row; scalar fields land verbatim and the
list fields (key_files / key_concepts / prerequisite_topics) are stored as
JSON that decodes back to the original lists.
@brief Curriculum round-trips into the table.
@version 1
"""
db = _enrich(tmp_path, _CURRICULUM)
conn = sqlite3.connect(str(db))
rows = conn.execute(
"SELECT topic, title, summary, key_files, key_concepts, "
"prerequisite_topics, difficulty FROM architecture_topics ORDER BY topic"
).fetchall()
conn.close()
assert len(rows) == 2
dataflow, event_bus = rows # 'dataflow' < 'event_bus'
assert event_bus[0] == "event_bus"
assert event_bus[1] == "The Event Bus"
assert json.loads(event_bus[3]) == ["src/event_bus/event_bus.c"]
assert json.loads(event_bus[4]) == ["callback", "dispatch"]
assert event_bus[6] == 2
assert dataflow[0] == "dataflow"
assert json.loads(dataflow[5]) == ["event_bus"] # prerequisite_topics
assert dataflow[6] == 3
Activity · 14 steps
test_enrich_rejects_non_list_yaml
None test_enrichment.test_enrich_rejects_non_list_yaml(Path tmp_path)
A YAML file that isn't a list is rejected (not silently accepted).
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_enrich_rejects_non_list_yaml → enrich_database |
Source · 14 lines
def test_enrich_rejects_non_list_yaml(tmp_path: Path) -> None:
"""The contract requires a top-level YAML list; a mapping (or any non-list)
must fail loudly via `sys.exit(1)` rather than silently produce an empty or
malformed table.
@brief Non-list curriculum YAML exits non-zero.
@version 1
"""
db = tmp_path / "clew.db"
sqlite3.connect(str(db)).close()
yaml_path = tmp_path / "topics.yaml"
yaml_path.write_text("topic: not_a_list\ntitle: T\n", encoding="utf-8")
with pytest.raises(SystemExit):
enrich_database(db, yaml_path)1 more lines, not shown