-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_sqlite_rag.py
More file actions
763 lines (576 loc) · 24.2 KB
/
test_sqlite_rag.py
File metadata and controls
763 lines (576 loc) · 24.2 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
import json
import os
import tempfile
from pathlib import Path
import pytest
from sqlite_rag import SQLiteRag
from sqlite_rag.settings import Settings
class TestSQLiteRagAdd:
def test_add_simple_text_file(self):
# test file
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write(
"This is a test document with some content. And this is another very long sentence."
)
temp_file_path = f.name
rag = SQLiteRag.create(":memory:")
rag.add(temp_file_path)
conn = rag._conn
cursor = conn.execute("SELECT COUNT(*) FROM documents")
doc_count = cursor.fetchone()[0]
assert doc_count == 1
cursor = conn.execute("SELECT COUNT(*) FROM chunks")
chunk_count = cursor.fetchone()[0]
assert chunk_count > 0
def test_add_unsupported_file_type(self):
# Create a temporary file with an unsupported extension
with tempfile.NamedTemporaryFile(
mode="w", suffix=".unsupported", delete=False
) as f:
f.write("This is a test document with unsupported file type.")
rag = SQLiteRag.create(":memory:")
# Attempt to add the unsupported file
processed = rag.add(f.name)
assert processed == 0
def test_add_directory(self):
with tempfile.TemporaryDirectory() as temp_dir:
file1 = Path(temp_dir) / "file1.txt"
file2 = Path(temp_dir) / "file2.txt"
file1.write_text("This is the first test document.")
file2.write_text("This is the second test document.")
rag = SQLiteRag.create(db_path=":memory:")
rag.add(temp_dir)
conn = rag._conn
cursor = conn.execute("SELECT COUNT(*) AS total FROM documents")
doc_count = cursor.fetchone()[0]
assert doc_count == 2
cursor = conn.execute("SELECT COUNT(*) AS total FROM chunks")
chunk_count = cursor.fetchone()[0]
assert chunk_count > 0
def test_add_directory_recursively(self):
with tempfile.TemporaryDirectory() as temp_dir:
sub_dir = Path(temp_dir) / "subdir"
sub_dir.mkdir()
file1 = Path(temp_dir) / "file1.txt"
file2 = sub_dir / "file2.txt"
file1.write_text("This is the first test document.")
file2.write_text("This is the second test document in a subdirectory.")
rag = SQLiteRag.create(":memory:")
rag.add(temp_dir, recursive=True)
conn = rag._conn
cursor = conn.execute("SELECT COUNT(*) FROM documents")
doc_count = cursor.fetchone()[0]
assert doc_count == 2
cursor = conn.execute("SELECT COUNT(*) FROM chunks")
chunk_count = cursor.fetchone()[0]
assert chunk_count > 0
def test_add_with_absolute_paths(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write("This is a test document with absolute path option.")
temp_file_path = f.name
rag = SQLiteRag.create(":memory:")
rag.add(temp_file_path, use_relative_paths=False)
conn = rag._conn
cursor = conn.execute("SELECT uri FROM documents")
doc = cursor.fetchone()
assert doc
assert doc[0] == str(Path(temp_file_path).absolute())
def test_add_with_relative_paths(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write("This is a test document with relative path option.")
temp_file_path = Path(f.name)
rag = SQLiteRag.create(":memory:")
rag.add(str(temp_file_path), use_relative_paths=True)
conn = rag._conn
cursor = conn.execute("SELECT uri FROM documents")
doc = cursor.fetchone()
assert doc
assert doc[0] == str(temp_file_path.relative_to(temp_file_path.parent))
def test_add_file_with_metadata(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write("This is a test document with metadata.")
temp_file_path = f.name
rag = SQLiteRag.create(":memory:")
metadata = {"author": "test", "date": "2023-10-01"}
rag.add(
temp_file_path,
metadata=metadata,
)
conn = rag._conn
cursor = conn.execute("SELECT content, metadata FROM documents")
doc = cursor.fetchone()
assert doc
assert doc[0] == "This is a test document with metadata."
assert doc[1] == json.dumps(
{
**metadata,
"generated": {"title": "This is a test document with metadata."},
}
)
def test_add_documents_with_generated_title(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as doc1:
doc1.write("# Title 1\nThis is the first test document.")
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as doc2:
doc2.write("# Title 2\nThis is the second test document.")
doc3 = "# Title 3\nThis is the third test document."
doc4 = "# Title 4\nThis is the fourth test document."
rag = SQLiteRag.create(db_path=":memory:")
rag.add(doc1.name)
rag.add(doc2.name)
rag.add_text(doc3)
rag.add_text(doc4)
conn = rag._conn
cursor = conn.execute("SELECT metadata FROM documents")
docs = cursor.fetchall()
assert len(docs) == 4
titles = [json.loads(doc[0]).get("generated", {}).get("title") for doc in docs]
assert "Title 1" in titles
assert "Title 2" in titles
assert "Title 3" in titles
assert "Title 4" in titles
def test_add_empty_file(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
temp_file_path = f.name
rag = SQLiteRag.create(":memory:")
processed = rag.add(temp_file_path)
assert processed == 0
def test_add_unchanged_file_twice(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
f.write("This is a test document that will be added twice.")
temp_file_path = f.name
rag = SQLiteRag.create(":memory:")
# Add the file once
rag.add(temp_file_path)
conn = rag._conn
cursor = conn.execute("SELECT COUNT(*) FROM documents")
doc_count = cursor.fetchone()[0]
assert doc_count == 1
# Add the same file again
rag.add(temp_file_path)
# Still should be only one document
cursor = conn.execute("SELECT COUNT(*) FROM documents")
doc_count = cursor.fetchone()[0]
assert doc_count == 1
def test_add_text(self):
rag = SQLiteRag.create(":memory:")
rag.add_text(
"This is a test document content with some text to be indexed.",
uri="test_doc.txt",
metadata={"author": "test"},
)
conn = rag._conn
cursor = conn.execute("SELECT content, uri, metadata FROM documents")
doc = cursor.fetchone()
assert doc
assert doc[0] == "This is a test document content with some text to be indexed."
assert doc[1] == "test_doc.txt"
assert (
doc[2]
== '{"author": "test", "generated": {"title": "This is a test document content with some text to be indexed."}}'
)
cursor = conn.execute("SELECT COUNT(*) FROM chunks")
chunk_count = cursor.fetchone()[0]
assert chunk_count > 0
def test_add_text_without_options(self):
rag = SQLiteRag.create(":memory:")
rag.add_text("This is a test document content without options.")
conn = rag._conn
cursor = conn.execute("SELECT content FROM documents")
doc = cursor.fetchone()
assert doc
assert doc[0] == "This is a test document content without options."
cursor = conn.execute("SELECT COUNT(*) FROM chunks")
chunk_count = cursor.fetchone()[0]
assert chunk_count > 0
def test_add_text_with_metadata(self):
rag = SQLiteRag.create(":memory:")
metadata = {"author": "test", "date": "2023-10-01"}
rag.add_text(
"This is a test document content with metadata.",
uri="test_doc_with_metadata.txt",
metadata=metadata,
)
conn = rag._conn
cursor = conn.execute("SELECT content, uri, metadata FROM documents")
doc = cursor.fetchone()
assert doc
assert doc[0] == "This is a test document content with metadata."
assert doc[1] == "test_doc_with_metadata.txt"
assert doc[2] == json.dumps(
{
**metadata,
"generated": {
"title": "This is a test document content with metadata."
},
}
)
def test_add_markdown_with_frontmatter(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write(
"""---
title: Sample Document
author: Test Author
---
# Heading 1
This is a sample markdown document.
"""
)
temp_file_path = f.name
rag = SQLiteRag.create(":memory:")
rag.add(temp_file_path)
conn = rag._conn
cursor = conn.execute("SELECT content, metadata FROM documents")
doc = cursor.fetchone()
assert doc
assert "# Heading 1" in doc[0]
assert "This is a sample markdown document." in doc[0]
metadata = json.loads(doc[1])
assert "extracted" in metadata
assert "title" in metadata["extracted"]
assert metadata["extracted"]["title"] == "Sample Document"
assert "author" in metadata["extracted"]
assert metadata["extracted"]["author"] == "Test Author"
class TestSQLiteRag:
def test_list_documents(self):
rag = SQLiteRag.create(":memory:")
rag.add_text("Document 1 content.")
rag.add_text("Document 2 content.")
documents = rag.list_documents()
assert len(documents) == 2
assert documents[0].content == "Document 1 content."
assert documents[1].content == "Document 2 content."
def test_find_document_by_id(self):
rag = SQLiteRag.create(":memory:")
rag.add_text(
"Test document content.", uri="test.txt", metadata={"author": "test"}
)
documents = rag.list_documents()
doc_id = documents[0].id
# Find by ID
assert doc_id is not None
found_doc = rag.find_document(doc_id)
assert found_doc is not None
assert found_doc.id == doc_id
assert found_doc.content == "Test document content."
assert found_doc.uri == "test.txt"
assert found_doc.metadata == {
"author": "test",
"generated": {"title": "Test document content."},
}
def test_find_document_by_uri(self):
rag = SQLiteRag.create(":memory:")
rag.add_text(
"Test document content.", uri="test.txt", metadata={"author": "test"}
)
# Find by URI
found_doc = rag.find_document("test.txt")
assert found_doc is not None
assert found_doc.content == "Test document content."
assert found_doc.uri == "test.txt"
assert found_doc.metadata == {
"author": "test",
"generated": {"title": "Test document content."},
}
def test_find_document_not_found(self):
rag = SQLiteRag.create(":memory:")
found_doc = rag.find_document("nonexistent")
assert found_doc is None
def test_remove_document_by_id(self):
rag = SQLiteRag.create(":memory:")
rag.add_text(
"Test document content.", uri="test.txt", metadata={"author": "test"}
)
documents = rag.list_documents()
doc_id = documents[0].id
# Verify document exists
assert len(documents) == 1
# Remove by ID
assert doc_id is not None
success = rag.remove_document(doc_id)
assert success is True
# Verify document is removed
documents = rag.list_documents()
assert len(documents) == 0
def test_remove_document_by_uri(self):
rag = SQLiteRag.create(":memory:")
rag.add_text(
"Test document content.", uri="test.txt", metadata={"author": "test"}
)
# Verify document exists
documents = rag.list_documents()
assert len(documents) == 1
# Remove by URI
success = rag.remove_document("test.txt")
assert success is True
# Verify document is removed
documents = rag.list_documents()
assert len(documents) == 0
def test_remove_document_not_found(self):
rag = SQLiteRag.create(":memory:")
success = rag.remove_document("nonexistent")
assert success is False
def test_remove_document_with_chunks(self):
rag = SQLiteRag.create(":memory:")
# Add document that will create chunks
rag.add_text(
"This is a longer document that should create multiple chunks when processed by the chunker.",
uri="test.txt",
)
# Verify document and chunks exist
documents = rag.list_documents()
assert len(documents) == 1
doc_id = documents[0].id
cursor = rag._conn.cursor()
cursor.execute("SELECT COUNT(*) FROM chunks WHERE document_id = ?", (doc_id,))
chunk_count = cursor.fetchone()[0]
assert chunk_count > 0
# Remove document
assert doc_id is not None
success = rag.remove_document(doc_id)
assert success is True
# Verify document and chunks are removed
documents = rag.list_documents()
assert len(documents) == 0
cursor.execute("SELECT COUNT(*) FROM chunks WHERE document_id = ?", (doc_id,))
chunk_count = cursor.fetchone()[0]
assert chunk_count == 0
def test_rebuild_with_existing_files(self):
"""Test rebuild with files that still exist"""
# Arrange
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f1:
f1.write("Original content for file 1")
file1_path = f1.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f2:
f2.write("Original content for file 2")
file2_path = f2.name
rag = SQLiteRag.create(":memory:")
rag.add(file1_path)
rag.add(file2_path)
documents = rag.list_documents()
assert len(documents) == 2
cursor = rag._conn.cursor()
cursor.execute("SELECT COUNT(*) FROM chunks")
initial_chunk_count = cursor.fetchone()[0]
assert initial_chunk_count > 0
# Act
Path(file1_path).write_text("Modified content for file 1")
Path(file2_path).write_text("Modified content for file 2")
result = rag.rebuild()
# Assert
assert result["total"] == 2
assert result["reprocessed"] == 2
assert result["not_found"] == 0
assert result["removed"] == 0
documents = rag.list_documents()
assert len(documents) == 2
# Check that content was updated
found_file1 = None
found_file2 = None
for doc in documents:
if doc.uri == file1_path:
found_file1 = doc
elif doc.uri == file2_path:
found_file2 = doc
assert found_file1 is not None
assert found_file2 is not None
assert "Modified content for file 1" in found_file1.content
assert "Modified content for file 2" in found_file2.content
def test_rebuild_with_missing_files(self):
"""Test rebuild when some files are missing"""
# Arrange
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f1:
f1.write("Content for file 1")
file1_path = f1.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f2:
f2.write("Content for file 2")
file2_path = f2.name
rag = SQLiteRag.create(":memory:")
# Add files
rag.add(file1_path)
rag.add(file2_path)
# Make file missing
os.unlink(file2_path)
# Act
result = rag.rebuild(remove_missing=False)
# Assert
assert result["total"] == 2
assert result["reprocessed"] == 1 # Only file1 was reprocessed
assert result["not_found"] == 1 # file2 was not found
assert result["removed"] == 0 # Nothing removed
# Both documents should still exist in database
documents = rag.list_documents()
assert len(documents) == 2
def test_rebuild_remove_missing_files(self):
"""Test rebuild with remove_missing=True"""
# Arrange
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f1:
f1.write("Content for file 1")
file1_path = f1.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f2:
f2.write("Content for file 2")
file2_path = f2.name
rag = SQLiteRag.create(":memory:")
rag.add(file1_path)
rag.add(file2_path)
# Make it missing
os.unlink(file2_path)
# Act
result = rag.rebuild(remove_missing=True)
# Assert
assert result["total"] == 2
assert result["reprocessed"] == 1 # Only file1 was reprocessed
assert result["not_found"] == 1 # file2 was not found
assert result["removed"] == 1 # file2 was removed
# Only one document should remain
documents = rag.list_documents()
assert len(documents) == 1
assert documents[0].uri == file1_path
def test_rebuild_text_documents(self):
"""Test rebuild with text documents (no URI)"""
rag = SQLiteRag.create(":memory:")
rag.add_text("Text document 1 content")
result = rag.rebuild()
assert result["total"] == 1
assert result["reprocessed"] == 1
assert result["not_found"] == 0
assert result["removed"] == 0
documents = rag.list_documents()
assert len(documents) == 1
def test_rebuild_with_md_frontmatter(self):
"""Test rebuild with markdown file that have frontmatter"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f1:
f1.write(
"""---
title: Document 1
author: Author 1
---
# Heading 1
Content of document 1.
"""
)
file1_path = f1.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f2:
f2.write(
"""# Heading 2
Content of document 2.
"""
)
file2_path = f2.name
rag = SQLiteRag.create(":memory:")
rag.add(file1_path)
rag.add(file2_path)
result = rag.rebuild()
assert result["total"] == 2
assert result["reprocessed"] == 2
assert result["not_found"] == 0
assert result["removed"] == 0
documents = rag.list_documents()
assert len(documents) == 2
titles = [
doc.metadata.get("extracted", {}).get("title", "") for doc in documents
]
assert "Document 1" in titles
assert "Document 2" not in titles # No frontmatter title
def test_reset_database(self):
temp_file_path = os.path.join(tempfile.mkdtemp(), "something")
rag = SQLiteRag.create(temp_file_path)
rag.add_text("Test document 1")
rag.add_text("Test document 2", uri="test.txt")
documents = rag.list_documents()
assert len(documents) == 2
success = rag.reset()
assert success is True
assert not Path(temp_file_path).exists()
class TestSQLiteRagSearch:
def test_search_exact_match(self):
# cosin distance for searching embedding is exact 0.0 when strings match
settings = {
"use_prompt_templates": False,
"other_vector_options": "distance=cosine",
}
temp_file_path = os.path.join(tempfile.mkdtemp(), "something")
rag = SQLiteRag.create(temp_file_path, settings=settings)
expected_string = "The quick brown fox jumps over the lazy dog"
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f1:
f1.write(expected_string)
file1_path = f1.name
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f2:
f2.write(
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
)
file2_path = f2.name
rag.add(file1_path)
rag.add(file2_path)
# Act
results = rag.search(expected_string)
assert len(results) > 0
assert expected_string == results[0].document.content
assert 1 == results[0].vec_rank
assert 0.0 == results[0].vec_distance
@pytest.mark.parametrize(
"quantize_scan", [True, False], ids=["quantize", "no-quantize"]
)
def test_search_samples_exact_match_by_scan_type(self, quantize_scan: bool):
# Test that searching for exact content from sample files returns distance 0
# FTS not included in the combined score
settings = {
"use_prompt_templates": False,
"other_vector_options": "distance=cosine",
"weight_fts": 0.0,
"quantize_scan": quantize_scan,
}
temp_file_path = os.path.join(tempfile.mkdtemp(), "mydb.db")
rag = SQLiteRag.create(temp_file_path, settings=settings)
# Index all sample files
samples_dir = Path(__file__).parent / "assets" / "samples"
rag.add(str(samples_dir))
# Get all sample files and test each one
sample_files = list(samples_dir.glob("*.txt"))
for sample_file in sample_files:
file_content = sample_file.read_text(encoding="utf-8").rstrip("\n")
# Search for the exact content
results = rag.search(file_content, top_k=2)
assert len(results) == 2
first_result = results[0]
assert first_result.vec_distance == 0.0
assert first_result.document.content == file_content
# Second result should have distance > 0
second_result = results[1]
assert second_result.vec_distance and second_result.vec_distance > 0.0
def test_search_uses_retrieval_query_template(self, mocker):
template = "task: search | Do something with {content}"
settings = {"prompt_template_retrieval_query": template}
rag = SQLiteRag.create(":memory:", settings=settings)
mock_engine = mocker.Mock()
mock_engine.search.return_value = []
rag._engine = mock_engine
query = "test query"
rag.search(query)
# Assert that engine.search was called with the formatted template
expected_query = rag._settings.prompt_template_retrieval_query.format(
content=query
)
mock_engine.search.assert_called_once_with(expected_query, top_k=10)
@pytest.mark.parametrize("use_prompt_templates", [True, False])
def test_search_with_prompt_template(self, mocker, use_prompt_templates):
# Arrange
settings = Settings(
use_prompt_templates=use_prompt_templates,
prompt_template_retrieval_query="task: search result | query: {content}",
)
# Mock engine and its search method
mock_engine = mocker.Mock()
mock_engine.search.return_value = [] # Empty search results
# Create SQLiteRag instance with mocked dependencies
rag = SQLiteRag(mocker.Mock(), settings)
rag._engine = mock_engine
mocker.patch.object(rag, "_ensure_initialized")
# Act
rag.search("test query", new_context=False)
# Assert - verify engine.search was called with correct formatted query
expected_query = (
"task: search result | query: test query"
if use_prompt_templates
else "test query"
)
mock_engine.search.assert_called_once_with(expected_query, top_k=10)