fixes and refactor
This commit is contained in:
101
tests/test_search_sort_documents.py
Normal file
101
tests/test_search_sort_documents.py
Normal file
@@ -0,0 +1,101 @@
|
||||
import os
|
||||
import uuid
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
os.environ.setdefault("SECRET_KEY", "x" * 32)
|
||||
os.environ.setdefault("DATABASE_URL", "sqlite:////tmp/delphi_test.sqlite")
|
||||
|
||||
from app.main import app # noqa: E402
|
||||
from app.auth.security import get_current_user # noqa: E402
|
||||
|
||||
|
||||
class _User:
|
||||
def __init__(self):
|
||||
self.id = 1
|
||||
self.username = "tester"
|
||||
self.is_admin = True
|
||||
self.is_active = True
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def client():
|
||||
app.dependency_overrides[get_current_user] = lambda: _User()
|
||||
try:
|
||||
yield TestClient(app)
|
||||
finally:
|
||||
app.dependency_overrides.pop(get_current_user, None)
|
||||
|
||||
|
||||
def _create_customer_and_file(client: TestClient):
|
||||
cust_id = f"DOCSS-{uuid.uuid4().hex[:8]}"
|
||||
resp = client.post("/api/customers/", json={"id": cust_id, "last": "DocSS", "email": "dss@example.com"})
|
||||
assert resp.status_code == 200
|
||||
file_no = f"D-{uuid.uuid4().hex[:6]}"
|
||||
payload = {
|
||||
"file_no": file_no,
|
||||
"id": cust_id,
|
||||
"regarding": "Doc matter",
|
||||
"empl_num": "E01",
|
||||
"file_type": "CIVIL",
|
||||
"opened": date.today().isoformat(),
|
||||
"status": "ACTIVE",
|
||||
"rate_per_hour": 100.0,
|
||||
}
|
||||
resp = client.post("/api/files/", json=payload)
|
||||
assert resp.status_code == 200
|
||||
return cust_id, file_no
|
||||
|
||||
|
||||
def test_templates_tokenized_search_and_sort(client: TestClient):
|
||||
# Create templates
|
||||
t1 = f"TMP-{uuid.uuid4().hex[:6]}"
|
||||
t2 = f"TMP-{uuid.uuid4().hex[:6]}"
|
||||
|
||||
resp = client.post(
|
||||
"/api/documents/templates/",
|
||||
json={"form_id": t1, "form_name": "Alpha Letter", "category": "GENERAL", "content": "Hello"},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
resp = client.post(
|
||||
"/api/documents/templates/",
|
||||
json={"form_id": t2, "form_name": "Beta Memo", "category": "GENERAL", "content": "Hello"},
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
|
||||
# Tokenized search for both tokens only matches when both present
|
||||
resp = client.get("/api/documents/templates/", params={"search": "Alpha Letter"})
|
||||
assert resp.status_code == 200
|
||||
items = resp.json()
|
||||
ids = {i["form_id"] for i in items}
|
||||
assert t1 in ids and t2 not in ids
|
||||
|
||||
# Sorting by form_name desc
|
||||
resp = client.get("/api/documents/templates/", params={"sort_by": "form_name", "sort_dir": "desc"})
|
||||
assert resp.status_code == 200
|
||||
items = resp.json()
|
||||
if len(items) >= 2:
|
||||
assert items[0]["form_name"] >= items[1]["form_name"]
|
||||
|
||||
|
||||
def test_qdros_tokenized_search(client: TestClient):
|
||||
_, file_no = _create_customer_and_file(client)
|
||||
# Create QDROs
|
||||
q1 = {"file_no": file_no, "version": "01", "status": "DRAFT", "form_name": "Alpha Order", "notes": "Beta token present"}
|
||||
q2 = {"file_no": file_no, "version": "02", "status": "DRAFT", "form_name": "Gamma", "notes": "Beta only"}
|
||||
resp = client.post("/api/documents/qdros/", json=q1)
|
||||
assert resp.status_code == 200
|
||||
resp = client.post("/api/documents/qdros/", json=q2)
|
||||
assert resp.status_code == 200
|
||||
|
||||
# Only the one containing both tokens should match
|
||||
resp = client.get("/api/documents/qdros/", params={"search": "Alpha Beta"})
|
||||
assert resp.status_code == 200
|
||||
items = resp.json()
|
||||
names = {i.get("form_name") for i in items}
|
||||
assert "Alpha Order" in names
|
||||
assert "Gamma" not in names
|
||||
|
||||
|
||||
Reference in New Issue
Block a user