Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/tenantq/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def build_filter(
created_before: Optional[int] = None,
) -> models.Filter:
"""Build a tenant-scoped filter, optionally narrowed by metadata."""
if tenant_id is None or not str(tenant_id).strip():
raise ValueError(
"tenant_id is required: request was not scoped to a tenant "
"(empty or whitespace-only tenant_id)"
)
tenant_id = str(tenant_id).strip()
must: List[models.FieldCondition] = [
models.FieldCondition(key=TENANT_FIELD, match=models.MatchValue(value=tenant_id))
]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ def test_created_at_range_filter(ingested, settings, embedder, dataset):
by_id = {d.id: d for d in dataset.documents}
for h in hits:
assert lo <= by_id[h.id].created_at <= hi


import pytest
from tenantq.search import build_filter


@pytest.mark.parametrize("bad", ["", " ", "\t"])
def test_build_filter_rejects_empty_tenant_id(bad):
with pytest.raises(ValueError, match="not scoped"):
build_filter(bad)


def test_build_filter_accepts_tenant_with_internal_spaces():
f = build_filter("acme corp")
assert f.must
Loading