File size: 8,114 Bytes
2d7bf65 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | # scripts/test_e2e_queries.py
"""
End-to-end query tests across all 4 states.
Tests vector retrieval, graph retrieval, and multi-jurisdiction paths.
Run:
uv run python scripts/test_e2e_queries.py
"""
from __future__ import annotations
import sys, json, time
import io
from pathlib import Path
if sys.stdout.encoding != "utf-8":
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8")
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
import structlog
from civicsetu.agent.graph import get_compiled_graph
from civicsetu.models.enums import Jurisdiction
log = structlog.get_logger(__name__)
# ββ Test cases ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Format: (label, query, jurisdiction_filter, expected_query_type, must_contain_section)
TEST_CASES = [
# ββ Fact lookup β vector path βββββββββββββββββββββββββββββββββββββββββββββ
(
"FACT_CENTRAL_01",
"What are the obligations of a promoter under RERA?",
None,
"fact_lookup",
"4", # RERA Act Section 4 β promoter obligations
),
(
"FACT_MH_01",
"What documents must a promoter submit for project registration in Maharashtra?",
Jurisdiction.MAHARASHTRA,
"fact_lookup",
None,
),
(
"FACT_UP_01",
"What is the timeline for refund of amount paid by allottee in Uttar Pradesh?",
Jurisdiction.UTTAR_PRADESH,
"fact_lookup",
None,
),
(
"FACT_KA_01",
"What are the grounds for revocation of project registration in Karnataka?",
Jurisdiction.KARNATAKA,
"fact_lookup",
None,
),
(
"FACT_TN_01",
"What information must a real estate agent provide to buyers in Tamil Nadu?",
Jurisdiction.TAMIL_NADU,
"fact_lookup",
None,
),
# ββ Cross-reference β graph path ββββββββββββββββββββββββββββββββββββββββββ
(
"XREF_01",
"What does section 18 of RERA say about refund obligations?",
None,
"cross_reference",
"18",
),
(
"XREF_02",
"Which state rules implement section 9 of the RERA Act on agent registration?",
None,
"cross_reference",
"9",
),
(
"XREF_MH_01",
"Which MahaRERA rule derives from section 4 of the central RERA Act?",
Jurisdiction.MAHARASHTRA,
"cross_reference",
None,
),
# ββ Penalty lookup β graph path βββββββββββββββββββββββββββββββββββββββββββ
(
"PENALTY_01",
"What is the penalty for non-registration of a real estate project under RERA?",
None,
"penalty_lookup",
None,
),
(
"PENALTY_02",
"What are the penalties for a promoter who fails to register under RERA?",
None,
"penalty_lookup",
None,
),
# ββ Multi-jurisdiction comparison βββββββββββββββββββββββββββββββββββββββββ
(
"MULTI_01",
"How does Karnataka handle extension of project registration compared to the central RERA Act?",
None,
"cross_reference",
None,
),
(
"MULTI_02",
"What is the rate of interest payable on refunds under Tamil Nadu RERA rules?",
Jurisdiction.TAMIL_NADU,
"fact_lookup",
None,
),
]
def run_test(graph, label, query, jurisdiction_filter, expected_type, must_contain):
start = time.time()
state = {
"query": query,
"jurisdiction_filter": jurisdiction_filter,
"top_k": 5,
"session_id": f"e2e_{label}",
"retrieved_chunks": [],
"reranked_chunks": [],
"citations": [],
"confidence_score": 0.0,
"conflict_warnings": [],
"amendment_notice": None,
"retry_count": 0,
"hallucination_flag": False,
"error": None,
}
try:
result = graph.invoke(state)
elapsed = time.time() - start
query_type = result.get("query_type", "UNKNOWN")
confidence = result.get("confidence_score", 0.0)
answer = result.get("raw_response", "") or ""
citations = result.get("citations", [])
error = result.get("error")
# Checks
type_ok = expected_type in str(query_type).lower() if expected_type else True
citation_ok = len(citations) > 0
section_ok = True
if must_contain:
section_ok = any(
must_contain == str(c.section_id) for c in citations
) or must_contain in answer
status = "PASS" if (citation_ok and section_ok and not error) else "FAIL"
return {
"label": label,
"status": status,
"query_type": str(query_type),
"type_match": type_ok,
"confidence": round(confidence, 3),
"citations": len(citations),
"section_check": f"{must_contain}={'OK' if section_ok else 'MISSING'}" if must_contain else "N/A",
"elapsed_s": round(elapsed, 2),
"error": error,
"answer_preview": answer[:120].replace("\n", " ") if answer else "",
}
except Exception as e:
return {
"label": label,
"status": "ERROR",
"query_type": "N/A",
"type_match": False,
"confidence": 0.0,
"citations": 0,
"section_check": "N/A",
"elapsed_s": round(time.time() - start, 2),
"error": str(e),
"answer_preview": "",
}
def main():
print("Compiling LangGraph...")
graph = get_compiled_graph()
print(f"Running {len(TEST_CASES)} test cases...\n")
results = []
for label, query, jfilter, etype, must_section in TEST_CASES:
print(f" [{label}] ...", end=" ", flush=True)
r = run_test(graph, label, query, jfilter, etype, must_section)
results.append(r)
status_icon = "PASS" if r["status"] == "PASS" else "FAIL"
print(f"{status_icon} ({r['elapsed_s']}s, conf={r['confidence']}, cit={r['citations']})")
# ββ Summary βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
passed = sum(1 for r in results if r["status"] == "PASS")
failed = sum(1 for r in results if r["status"] == "FAIL")
errors = sum(1 for r in results if r["status"] == "ERROR")
avg_conf = sum(r["confidence"] for r in results) / len(results)
avg_time = sum(r["elapsed_s"] for r in results) / len(results)
print("\n" + "=" * 70)
print(f"E2E Test Results: {passed} PASS {failed} FAIL {errors} ERROR")
print(f"Avg confidence : {avg_conf:.3f}")
print(f"Avg latency : {avg_time:.2f}s")
print("=" * 70)
# Print failures detail
for r in results:
if r["status"] != "PASS":
print(f"\n{r['status']}: {r['label']}")
print(f" query_type : {r['query_type']}")
print(f" section_check: {r['section_check']}")
print(f" citations : {r['citations']}")
print(f" error : {r['error']}")
print(f" answer : {r['answer_preview']}")
# Save full results
out = Path("e2e_results.json")
out.write_text(json.dumps(results, indent=2, default=str))
print(f"\nFull results β {out}")
if failed + errors > 0:
sys.exit(1)
if __name__ == "__main__":
main()
|