test_vocabulary.py¶
tests/test_vocabulary.py
Tests for the central schema vocabulary (clew.vocabulary).
test_apostrophe_is_escaped_by_doubling
None test_vocabulary.test_apostrophe_is_escaped_by_doubling()
`repr` quotes an apostrophe-bearing value with DOUBLE quotes, which SQLite reads as an identifier, not a string. No shipped value contains one today; the generator must not be the reason it can never be added.
Checks
assert vocab.check("c") == "CHECK(c IN ('it''s', 'plain'))"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-SCHEMA-012 | measured |
Source · 14 lines
def test_apostrophe_is_escaped_by_doubling() -> None:
"""`repr` quotes an apostrophe-bearing value with DOUBLE quotes, which
SQLite reads as an identifier, not a string. No shipped value contains one
today; the generator must not be the reason it can never be added."""
vocab = Vocabulary(id="t", values=("it's", "plain"), means="test")
assert vocab.check("c") == "CHECK(c IN ('it''s', 'plain'))"
conn = sqlite3.connect(":memory:")
conn.execute(f"CREATE TABLE t (c TEXT {vocab.check('c')})")
conn.execute('INSERT INTO t VALUES ("it\'s")')
with pytest.raises(sqlite3.IntegrityError):
conn.execute("INSERT INTO t VALUES ('nope')")
conn.close()
Activity · 8 steps
test_every_registered_column_has_a_registered_vocabulary
None test_vocabulary.test_every_registered_column_has_a_registered_vocabulary()
`check()` refuses an unregistered column instead of returning an empty string: a silently-absent CHECK is the hole this module exists to close.
Checks
assert check(table, column).startswith("CHECK(") with pytest.raises(KeyError): check("call_edges", "not_a_column")
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-SCHEMA-012 | measured |
Source · 9 lines
def test_every_registered_column_has_a_registered_vocabulary() -> None:
"""`check()` refuses an unregistered column instead of returning an empty
string: a silently-absent CHECK is the hole this module exists to close."""
for table, column in COLUMNS:
assert check(table, column).startswith("CHECK(")
with pytest.raises(KeyError):
check("call_edges", "not_a_column")
Activity · 4 steps
test_every_registry_value_satisfies_its_generated_check
None test_vocabulary.test_every_registry_value_satisfies_its_generated_check()
Round-trip: every registered value is accepted by the clause generated for it, and a non-member is rejected. Catches a registry that is a superset of what the SQL actually admits.
Checks
assert conn.execute("SELECT COUNT(*) FROM probe").fetchone()[0] == len(vocab.values), (f"{table}.{column} lost a value round-tripping" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-SCHEMA-012 | measured |
Source · 18 lines
def test_every_registry_value_satisfies_its_generated_check() -> None:
"""Round-trip: every registered value is accepted by the clause generated
for it, and a non-member is rejected. Catches a registry that is a superset
of what the SQL actually admits."""
conn = sqlite3.connect(":memory:")
for (table, column), vocab in COLUMNS.items():
conn.execute(f"CREATE TABLE probe (c TEXT {vocab.check('c')})")
for value in vocab.values:
conn.execute("INSERT INTO probe VALUES (?)", (value,))
with pytest.raises(sqlite3.IntegrityError, match="CHECK constraint failed"):
conn.execute("INSERT INTO probe VALUES ('__not_a_member__')")
assert conn.execute("SELECT COUNT(*) FROM probe").fetchone()[0] == len(vocab.values), (
f"{table}.{column} lost a value round-tripping"
)
conn.execute("DROP TABLE probe")
conn.close()
Activity · 10 steps
test_single_value_vocabulary_generates_valid_sql
None test_vocabulary.test_single_value_vocabulary_generates_valid_sql()
The idiom this replaces used `{tuple!r}`, which emits `IN ('x',)` for a one-value set — SQLite rejects the trailing comma. It survived only because every set it was applied to happened to have >= 2 values; `boundary_source` was the live one-value case until the declared-dispatch terminus widened it.
Checks
assert one.check("c") == "CHECK(c IN ('only'))"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-SCHEMA-012 | measured |
Source · 24 lines
def test_single_value_vocabulary_generates_valid_sql() -> None:
"""The idiom this replaces used `{tuple!r}`, which emits `IN ('x',)` for a
one-value set — SQLite rejects the trailing comma. It survived only because
every set it was applied to happened to have >= 2 values; `boundary_source`
was the live one-value case until the declared-dispatch terminus widened it.
So the case is exercised SYNTHETICALLY now, not through whichever registry
entry happens to be short this week — the generator is what is under test,
and binding the assertion to a real vocabulary made a legitimate widening
look like a regression. Every registered one-value vocabulary is still
checked, so the real case is covered the moment one exists again.
"""
one = Vocabulary(id="one", values=("only",), means="test")
assert one.check("c") == "CHECK(c IN ('only'))"
conn = sqlite3.connect(":memory:")
for vocab in [one, *(v for v in VOCABULARIES.values() if len(v.values) == 1)]:
conn.execute(f"CREATE TABLE probe (c TEXT {vocab.check('c')})")
conn.execute("INSERT INTO probe VALUES (?)", (vocab.values[0],))
with pytest.raises(sqlite3.IntegrityError):
conn.execute("INSERT INTO probe VALUES ('__not_a_member__')")
conn.execute("DROP TABLE probe")
conn.close()
Activity · 10 steps
test_the_five_strength_vocabularies_are_distinct_objects
None test_vocabulary.test_the_five_strength_vocabularies_are_distinct_objects()
Five columns share the tuple ('low','medium','high') and mean five different things — spawn-detection strength, inferred-vs-declared provenance, lock-identity certainty, acquisition-resolution success, and a constant on external_boundaries. One shared object would mean adding a value for ONE silently widens the CHECK on the other four, with every registry-vs-registry test still green.
Checks
assert all(v.values == ("low", "medium", "high") for v in five)assert len({id(v) for v in five}) == 5assert len({v.id for v in five}) == 5assert len({v.means for v in five}) == 5
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-WIKI-054 | inferred | test_the_five_strength_vocabularies_are_distinct_objects → all |
Source · 14 lines
def test_the_five_strength_vocabularies_are_distinct_objects() -> None:
"""Five columns share the tuple ('low','medium','high') and mean five
different things — spawn-detection strength, inferred-vs-declared
provenance, lock-identity certainty, acquisition-resolution success, and a
constant on external_boundaries. One shared object would mean adding a value
for ONE silently widens the CHECK on the other four, with every
registry-vs-registry test still green."""
five = [THREAD_STRENGTH, KEY_STRENGTH, LOCK_IDENTITY, ACQ_STRENGTH, BOUNDARY_STRENGTH]
assert all(v.values == ("low", "medium", "high") for v in five)
assert len({id(v) for v in five}) == 5
assert len({v.id for v in five}) == 5
assert len({v.means for v in five}) == 5
Activity · 5 steps
test_validated_returns_members_and_raises_on_anything_else
None test_vocabulary.test_validated_returns_members_and_raises_on_anything_else()
The message must name the origin, the token and the full allowed set — that triple is what makes a fail-closed refusal cheaper to fix than a silent normalization.
Checks
assert THREAD_KIND.validated("pthread", owner="x", field="kind") == "pthread"assert ".clew.yaml [thread_patterns]" in messageassert "'pthred'" in messageassert "task, pthread, win32, timer, isr, main, oneshot, process, coroutine, unknown" in message
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-SCHEMA-012 | measured |
Source · 19 lines
def test_validated_returns_members_and_raises_on_anything_else() -> None:
"""The message must name the origin, the token and the full allowed set —
that triple is what makes a fail-closed refusal cheaper to fix than a
silent normalization."""
assert THREAD_KIND.validated("pthread", owner="x", field="kind") == "pthread"
with pytest.raises(DeclarationError) as exc:
THREAD_KIND.validated("pthred", owner=".clew.yaml [thread_patterns]", field="kind")
message = str(exc.value)
assert ".clew.yaml [thread_patterns]" in message
assert "'pthred'" in message
# Spelled out in full, in declared order, and updated DELIBERATELY when the
# vocabulary grows — #58 added `process`/`coroutine` for Python's spawn
# primitives, and `win32` arrived with the Windows ones. The point of the
# literal is that widening a fail-closed vocabulary is a decision someone has
# to make on purpose, not a diff that slips through because the assertion was
# derived from the thing it checks.
assert "task, pthread, win32, timer, isr, main, oneshot, process, coroutine, unknown" in message