feat(slice3): pure facet field-set + filter/count helper (§22.4a)
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
"""§22.4a SLICE-3 — pure facet field-set + filter/count (PUC-3).
|
||||
|
||||
Per docs/design/2026-06-06-configurable-collection-metadata.md §5.1, §6.4.
|
||||
"""
|
||||
from app import facets
|
||||
|
||||
|
||||
PRIORITY = {"priority": {"type": "enum", "values": ["P0", "P1", "P2"]}}
|
||||
SCHEMA = {
|
||||
"priority": {"type": "enum", "values": ["P0", "P1", "P2"]},
|
||||
"tags": {"type": "tags"},
|
||||
"owner": {"type": "text"},
|
||||
}
|
||||
|
||||
|
||||
def _e(slug, state="active", malformed=False, **meta):
|
||||
return {"slug": slug, "state": state, "metadata_malformed": malformed, "meta": meta}
|
||||
|
||||
|
||||
def test_facet_fields_orders_declared_then_state_skips_text():
|
||||
# enum + tags in declaration order, text skipped, state appended last.
|
||||
assert facets.facet_fields(SCHEMA) == [
|
||||
("priority", "enum"), ("tags", "tags"), ("state", "enum")]
|
||||
|
||||
|
||||
def test_no_schema_yields_no_facets():
|
||||
# INV-5: a collection with no fields has no facets (frontend keeps chips).
|
||||
assert facets.facet_fields(None) == []
|
||||
assert facets.facet_fields({}) == []
|
||||
|
||||
|
||||
def test_filter_and_count_basic_counts():
|
||||
entries = [
|
||||
_e("a", priority="P0", tags=["checkout"]),
|
||||
_e("b", priority="P0", tags=["cart"]),
|
||||
_e("c", priority="P1", tags=["checkout", "cart"]),
|
||||
]
|
||||
items, fac = facets.filter_and_count(entries, SCHEMA, {})
|
||||
assert {i["slug"] for i in items} == {"a", "b", "c"}
|
||||
assert fac["priority"] == {"P0": 2, "P1": 1}
|
||||
assert fac["tags"] == {"checkout": 2, "cart": 2}
|
||||
assert fac["state"] == {"active": 3}
|
||||
|
||||
|
||||
def test_filter_compose_or_within_and_across():
|
||||
entries = [
|
||||
_e("a", priority="P0", tags=["checkout"]), # P0 + checkout
|
||||
_e("b", priority="P0", tags=["cart"]), # P0, no checkout
|
||||
_e("c", priority="P1", tags=["checkout"]), # checkout, not P0
|
||||
]
|
||||
# priority=P0 AND tags=checkout → only "a".
|
||||
items, _ = facets.filter_and_count(
|
||||
entries, SCHEMA, {"priority": {"P0"}, "tags": {"checkout"}})
|
||||
assert {i["slug"] for i in items} == {"a"}
|
||||
|
||||
|
||||
def test_drilldown_counts_exclude_own_field_selection():
|
||||
entries = [
|
||||
_e("a", priority="P0"),
|
||||
_e("b", priority="P1"),
|
||||
_e("c", priority="P1"),
|
||||
]
|
||||
# With P0 selected, the priority facet still counts P1 over the set that
|
||||
# ignores priority's own selection — so P1 stays switchable.
|
||||
_, fac = facets.filter_and_count(entries, SCHEMA, {"priority": {"P0"}})
|
||||
assert fac["priority"] == {"P0": 1, "P1": 2}
|
||||
|
||||
|
||||
def test_malformed_toggle_narrows_items_and_counts():
|
||||
entries = [
|
||||
_e("a", state="active", malformed=True, priority="P9"),
|
||||
_e("b", state="active", malformed=False, priority="P0"),
|
||||
]
|
||||
items, fac = facets.filter_and_count(entries, SCHEMA, {}, only_malformed=True)
|
||||
assert {i["slug"] for i in items} == {"a"}
|
||||
assert fac["priority"] == {"P9": 1}
|
||||
|
||||
|
||||
def test_missing_value_contributes_no_facet_value():
|
||||
entries = [_e("a", priority="P0"), _e("b")] # b has no priority
|
||||
_, fac = facets.filter_and_count(entries, SCHEMA, {})
|
||||
assert fac["priority"] == {"P0": 1}
|
||||
|
||||
|
||||
def test_allowed_filter_keys():
|
||||
assert facets.allowed_filter_keys(PRIORITY) == {"priority", "state",
|
||||
"unreviewed", "malformed"}
|
||||
Reference in New Issue
Block a user