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.

tests/test_enrichment.py:118

Show on the map →

Called by none

Calls _enrich, enrich_database

Checks

  • assert rows == [("only",)]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-001measuredtest_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
lines 126–126db = _enrich(tmp_path, _CURRICULUM)lines 127–127yaml_path = tmp_path / "topics.yaml"lines 128–128yaml_path.write_text("- topic: only\n title: Only\n summary: S\n", encoding="utf-8")lines 129–129enrich_database(db, yaml_path)lines 131–131conn = sqlite3.connect(str(db))lines 132–132rows = conn.execute("SELECT topic FROM architecture_topics").fetchall()lines 133–133conn.close()lines 134–134assert rows == [("only",)]

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.

tests/test_enrichment.py:96

Show on the map →

Called by none

Calls _enrich

Checks

  • assert json.loads(row[0]) == []
  • assert json.loads(row[1]) == []
  • assert json.loads(row[2]) == []
  • assert row[3] == 1

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-001measuredtest_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
lines 103–103minimal = "- topic: t\n title: T\n summary: S\n"lines 104–104db = _enrich(tmp_path, minimal)lines 105–105conn = sqlite3.connect(str(db))lines 106–108row = conn.execute( "SELECT key_files, key_concepts, prerequisite_topics, difficulty FROM architecture_topics" ).fetchone()lines 109–109conn.close()lines 110–110assert json.loads(row[0]) == []lines 111–111assert json.loads(row[1]) == []lines 112–112assert json.loads(row[2]) == []lines 113–113assert row[3] == 1

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.

tests/test_enrichment.py:66

Show on the map →

Called by none

Calls _enrich

Checks

  • assert len(rows) == 2
  • 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

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-001measuredtest_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
lines 74–74db = _enrich(tmp_path, _CURRICULUM)lines 75–75conn = sqlite3.connect(str(db))lines 76–79rows = conn.execute( "SELECT topic, title, summary, key_files, key_concepts, " "prerequisite_topics, difficulty FROM architecture_topics ORDER BY topic" ).fetchall()lines 80–80conn.close()lines 82–82assert len(rows) == 2lines 83–83dataflow, event_bus = rowslines 84–84assert event_bus[0] == "event_bus"lines 85–85assert event_bus[1] == "The Event Bus"lines 86–86assert json.loads(event_bus[3]) == ["src/event_bus/event_bus.c"]lines 87–87assert json.loads(event_bus[4]) == ["callback", "dispatch"]lines 88–88assert event_bus[6] == 2lines 89–89assert dataflow[0] == "dataflow"lines 90–90assert json.loads(dataflow[5]) == ["event_bus"]lines 91–91assert dataflow[6] == 3

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).

tests/test_enrichment.py:139

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-001measuredtest_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

Activity · 6 steps
lines 147–147db = tmp_path / "clew.db"lines 148–148sqlite3.connect(str(db)).close()lines 149–149yaml_path = tmp_path / "topics.yaml"lines 150–150yaml_path.write_text("topic: not_a_list\ntitle: T\n", encoding="utf-8")lines 151–151with pytest.raises(SystemExit)lines 152–152enrich_database(db, yaml_path)