-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_backend.py
More file actions
200 lines (153 loc) · 6.04 KB
/
test_backend.py
File metadata and controls
200 lines (153 loc) · 6.04 KB
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
#!/usr/bin/env python3
"""
Test Backend for RAG Chatbot Knowledge Base
Tests the ChromaDB vector database logic directly, bypassing the Streamlit UI.
Run with: python test_backend.py
"""
import os
import shutil
import sys
TEST_DB_PATH = "./test_chroma_db"
def setup_test_db():
if os.path.exists(TEST_DB_PATH):
shutil.rmtree(TEST_DB_PATH)
import vector_db
vector_db.PERSIST_DIRECTORY = TEST_DB_PATH
vector_db._client = None
vector_db._collection = None
def cleanup_test_db():
vector_db._client = None
vector_db._collection = None
if os.path.exists(TEST_DB_PATH):
shutil.rmtree(TEST_DB_PATH)
def test_case_1_fresh_ingestion():
print("\n" + "="*60)
print("TEST CASE 1: Fresh Ingestion & Metadata")
print("="*60)
setup_test_db()
chunk_count = vector_db.ingest_text(
text="Refunds take 5 days.",
source_id="test_doc_1",
product="RazorpayX",
feature="General",
access_level="INTERNAL"
)
collection = vector_db.get_collection()
results = collection.get(where={"source_id": "test_doc_1"})
doc_count = len(results["ids"])
assert doc_count == 1, f"Expected 1 document, got {doc_count}"
metadata = results["metadatas"][0]
assert metadata["access_level"] == "INTERNAL", f"Expected INTERNAL, got {metadata['access_level']}"
assert metadata["product"] == "RazorpayX", f"Expected RazorpayX, got {metadata['product']}"
print(f" [PASS] Document count: {doc_count}")
print(f" [PASS] access_level: {metadata['access_level']}")
print(f" [PASS] product: {metadata['product']}")
print(" TEST CASE 1: PASSED")
return True
def test_case_2_overwrite_logic():
print("\n" + "="*60)
print("TEST CASE 2: Overwrite Logic (Upsert)")
print("="*60)
chunk_count = vector_db.ingest_text(
text="Refunds take 10 minutes.",
source_id="test_doc_1",
product="RazorpayX",
feature="Payouts",
access_level="EXTERNAL"
)
collection = vector_db.get_collection()
results = collection.get(where={"source_id": "test_doc_1"})
doc_count = len(results["ids"])
assert doc_count == 1, f"Expected 1 document (not 2), got {doc_count}"
content = results["documents"][0]
assert "10 minutes" in content, f"Expected new content with '10 minutes', got: {content}"
assert "5 days" not in content, f"Old content '5 days' should be deleted, but found in: {content}"
print(f" [PASS] Document count after overwrite: {doc_count} (not duplicated)")
print(f" [PASS] Content updated: '{content[:50]}...'")
print(" TEST CASE 2: PASSED")
return True
def test_case_3_delete_logic():
print("\n" + "="*60)
print("TEST CASE 3: Delete Logic")
print("="*60)
deleted_count = vector_db.delete_by_source_id("test_doc_1")
print(f" Deleted {deleted_count} chunks")
collection = vector_db.get_collection()
results = collection.get(where={"source_id": "test_doc_1"})
doc_count = len(results["ids"])
assert doc_count == 0, f"Expected 0 documents after deletion, got {doc_count}"
print(f" [PASS] Document count after deletion: {doc_count}")
print(" TEST CASE 3: PASSED")
return True
def test_case_4_access_level_filtering():
print("\n" + "="*60)
print("TEST CASE 4: Access Level Filtering (Security Check)")
print("="*60)
vector_db.ingest_text(
text="Public Info - This is visible to everyone.",
source_id="doc_public",
product="RazorpayX",
feature="General",
access_level="EXTERNAL"
)
vector_db.ingest_text(
text="Secret Info - This is private and confidential.",
source_id="doc_private",
product="RazorpayX",
feature="General",
access_level="INTERNAL"
)
collection = vector_db.get_collection()
all_docs = collection.get()
print(f" Total documents in DB: {len(all_docs['ids'])}")
results = collection.query(
query_texts=["information"],
where={"access_level": "EXTERNAL"},
n_results=10
)
returned_docs = results["documents"][0] if results["documents"] else []
returned_metadatas = results["metadatas"][0] if results["metadatas"] else []
print(f" Filtered query returned {len(returned_docs)} document(s)")
for i, (doc, meta) in enumerate(zip(returned_docs, returned_metadatas)):
assert meta["access_level"] == "EXTERNAL", f"Security breach! INTERNAL doc returned: {doc[:50]}"
assert "Secret" not in doc, f"Security breach! Secret content visible: {doc[:50]}"
print(f" Doc {i+1}: access_level={meta['access_level']}, content='{doc[:30]}...'")
has_public = any("Public" in doc for doc in returned_docs)
assert has_public, "Public document not found in results"
print(" [PASS] Only EXTERNAL documents returned")
print(" [PASS] INTERNAL documents properly hidden")
print(" TEST CASE 4: PASSED")
return True
def run_all_tests():
print("\n" + "#"*60)
print("# RAG CHATBOT - VECTOR DATABASE TEST SUITE")
print("#"*60)
all_passed = True
try:
if not test_case_1_fresh_ingestion():
all_passed = False
if not test_case_2_overwrite_logic():
all_passed = False
if not test_case_3_delete_logic():
all_passed = False
if not test_case_4_access_level_filtering():
all_passed = False
except AssertionError as e:
print(f"\n [FAIL] Assertion Error: {e}")
all_passed = False
except Exception as e:
print(f"\n [FAIL] Unexpected Error: {e}")
all_passed = False
finally:
cleanup_test_db()
print("\n" + "#"*60)
if all_passed:
print("# ALL SYSTEMS GO: Database Logic is Secure")
print("#"*60)
return 0
else:
print("# SOME TESTS FAILED - Please review the errors above")
print("#"*60)
return 1
if __name__ == "__main__":
sys.exit(run_all_tests())