Skip to content

Commit 65fb654

Browse files
author
Daniele Briggi
committed
chore: tests
1 parent 3ce34c6 commit 65fb654

4 files changed

Lines changed: 69 additions & 58 deletions

File tree

src/sqlite_rag/cli.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
22
import shlex
33
import sys
4-
from pathlib import Path
54
from typing import Optional
65

76
import typer
@@ -75,7 +74,10 @@ def list_documents():
7574

7675

7776
@app.command()
78-
def remove(identifier: str):
77+
def remove(
78+
identifier: str,
79+
yes: bool = typer.Option(False, "-y", "--yes", help="Skip confirmation prompt")
80+
):
7981
"""Remove document by path or UUID"""
8082
rag = SQLiteRag()
8183

@@ -93,11 +95,12 @@ def remove(identifier: str):
9395
typer.echo(f"Content preview: {document.content[:200]}{'...' if len(document.content) > 200 else ''}")
9496
typer.echo()
9597

96-
# Ask for confirmation
97-
confirm = typer.confirm("Are you sure you want to delete this document?")
98-
if not confirm:
99-
typer.echo("Cancelled.")
100-
return
98+
# Ask for confirmation unless -y flag is used
99+
if not yes:
100+
confirm = typer.confirm("Are you sure you want to delete this document?")
101+
if not confirm:
102+
typer.echo("Cancelled.")
103+
return
101104

102105
# Remove the document
103106
success = rag.remove_document(identifier)

src/sqlite_rag/engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def generate_embedding(self, chunks: list[Chunk]) -> list[Chunk]:
4646
cursor = self._conn.cursor()
4747

4848
for chunk in chunks:
49-
cursor.execute("SELECT llm_embed_generate(?, 'json_output=0')", (chunk.content,))
49+
cursor.execute("SELECT llm_embed_generate(?)", (chunk.content,))
5050
result = cursor.fetchone()
5151

5252
if result is None:

tests/test_engine.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_search_with_semantic_and_fts(self, db_conn):
4949
uri="document2.txt",
5050
)
5151
doc3 = Document(
52-
content="This document contains the phrase 'tongue-twister' and discusses woodcutters and wood.",
52+
content="This document discusses about woodcutters and wood.",
5353
uri="document3.txt",
5454
)
5555

@@ -59,16 +59,16 @@ def test_search_with_semantic_and_fts(self, db_conn):
5959

6060
repository = Repository(conn, settings)
6161
repository.add_document(doc1)
62-
doc2_id = repository.add_document(doc2)
63-
repository.add_document(doc3)
62+
repository.add_document(doc2)
63+
doc3_id = repository.add_document(doc3)
6464

6565
engine.quantize()
6666

6767
# Act
68-
results = engine.search("wood woodchuck", limit=5)
68+
results = engine.search("wood lumberjack", limit=5)
6969

7070
assert len(results) > 0
71-
assert doc2_id == results[0].id
71+
assert doc3_id == results[0].id
7272

7373
def test_search_semantic_result(self, db_conn):
7474
# Arrange
@@ -86,7 +86,7 @@ def test_search_semantic_result(self, db_conn):
8686
uri="document2.txt",
8787
)
8888
doc3 = Document(
89-
content="This document contains the phrase 'tongue-twister' and discusses woodcutters and wood.",
89+
content="This document discusses about woodcutters and wood.",
9090
uri="document3.txt",
9191
)
9292

@@ -96,16 +96,16 @@ def test_search_semantic_result(self, db_conn):
9696

9797
repository = Repository(conn, settings)
9898
repository.add_document(doc1)
99-
doc2_id = repository.add_document(doc2)
100-
repository.add_document(doc3)
99+
repository.add_document(doc2)
100+
doc3_id = repository.add_document(doc3)
101101

102102
engine.quantize()
103103

104104
# Act
105-
results = engine.search("tongue-twister", limit=5)
105+
results = engine.search("lumberjack", limit=5)
106106

107107
assert len(results) > 0
108-
assert doc2_id == results[0].id
108+
assert doc3_id == results[0].id
109109

110110
def test_search_fts_results(self, db_conn):
111111
# Arrange
@@ -123,7 +123,7 @@ def test_search_fts_results(self, db_conn):
123123
uri="document2.txt",
124124
)
125125
doc3 = Document(
126-
content="This document contains the phrase 'tongue-twister' and discusses woodcutters and wood.",
126+
content="This document discusses about woodcutters and wood.",
127127
uri="document3.txt",
128128
)
129129

@@ -132,14 +132,14 @@ def test_search_fts_results(self, db_conn):
132132
engine.process(doc3)
133133

134134
repository = Repository(conn, settings)
135-
repository.add_document(doc1)
135+
doc1_id = repository.add_document(doc1)
136136
repository.add_document(doc2)
137-
doc3_id = repository.add_document(doc3)
137+
repository.add_document(doc3)
138138

139139
engine.quantize()
140140

141141
# Act
142-
results = engine.search("woodcutters", limit=5)
142+
results = engine.search("brown fox animal", limit=5)
143143

144144
assert len(results) > 0
145-
assert doc3_id == results[0].id
145+
assert doc1_id == results[0].id

tests/test_sqlite_rag.py

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ def test_add_simple_text_file(self, db_settings):
1313
)
1414
temp_file_path = f.name
1515

16-
db_settings.chunk_size = 2
17-
db_settings.chunk_overlap = 0
18-
1916
rag = SQLiteRag(db_settings)
2017

2118
rag.add(temp_file_path)
@@ -102,15 +99,17 @@ def test_list_documents(self, db_settings):
10299

103100
def test_find_document_by_id(self, db_settings):
104101
rag = SQLiteRag(db_settings)
105-
106-
rag.add_text("Test document content.", uri="test.txt", metadata={"author": "test"})
102+
103+
rag.add_text(
104+
"Test document content.", uri="test.txt", metadata={"author": "test"}
105+
)
107106
documents = rag.list_documents()
108107
doc_id = documents[0].id
109-
108+
110109
# Find by ID
111110
assert doc_id is not None
112111
found_doc = rag.find_document(doc_id)
113-
112+
114113
assert found_doc is not None
115114
assert found_doc.id == doc_id
116115
assert found_doc.content == "Test document content."
@@ -119,95 +118,104 @@ def test_find_document_by_id(self, db_settings):
119118

120119
def test_find_document_by_uri(self, db_settings):
121120
rag = SQLiteRag(db_settings)
122-
123-
rag.add_text("Test document content.", uri="test.txt", metadata={"author": "test"})
124-
121+
122+
rag.add_text(
123+
"Test document content.", uri="test.txt", metadata={"author": "test"}
124+
)
125+
125126
# Find by URI
126127
found_doc = rag.find_document("test.txt")
127-
128+
128129
assert found_doc is not None
129130
assert found_doc.content == "Test document content."
130131
assert found_doc.uri == "test.txt"
131132
assert found_doc.metadata == {"author": "test"}
132133

133134
def test_find_document_not_found(self, db_settings):
134135
rag = SQLiteRag(db_settings)
135-
136+
136137
found_doc = rag.find_document("nonexistent")
137-
138+
138139
assert found_doc is None
139140

140141
def test_remove_document_by_id(self, db_settings):
141142
rag = SQLiteRag(db_settings)
142-
143-
rag.add_text("Test document content.", uri="test.txt", metadata={"author": "test"})
143+
144+
rag.add_text(
145+
"Test document content.", uri="test.txt", metadata={"author": "test"}
146+
)
144147
documents = rag.list_documents()
145148
doc_id = documents[0].id
146-
149+
147150
# Verify document exists
148151
assert len(documents) == 1
149-
152+
150153
# Remove by ID
151154
assert doc_id is not None
152155
success = rag.remove_document(doc_id)
153-
156+
154157
assert success is True
155-
158+
156159
# Verify document is removed
157160
documents = rag.list_documents()
158161
assert len(documents) == 0
159162

160163
def test_remove_document_by_uri(self, db_settings):
161164
rag = SQLiteRag(db_settings)
162-
163-
rag.add_text("Test document content.", uri="test.txt", metadata={"author": "test"})
164-
165+
166+
rag.add_text(
167+
"Test document content.", uri="test.txt", metadata={"author": "test"}
168+
)
169+
165170
# Verify document exists
166171
documents = rag.list_documents()
167172
assert len(documents) == 1
168-
173+
169174
# Remove by URI
170175
success = rag.remove_document("test.txt")
171-
176+
172177
assert success is True
173-
178+
174179
# Verify document is removed
175180
documents = rag.list_documents()
176181
assert len(documents) == 0
177182

178183
def test_remove_document_not_found(self, db_settings):
179184
rag = SQLiteRag(db_settings)
180-
185+
181186
success = rag.remove_document("nonexistent")
182-
187+
183188
assert success is False
184189

185190
def test_remove_document_with_chunks(self, db_settings):
186191
rag = SQLiteRag(db_settings)
187-
192+
188193
# Add document that will create chunks
189-
rag.add_text("This is a longer document that should create multiple chunks when processed by the chunker.", uri="test.txt")
190-
194+
rag.add_text(
195+
"This is a longer document that should create multiple chunks when processed by the chunker.",
196+
uri="test.txt",
197+
)
198+
191199
# Verify document and chunks exist
192200
documents = rag.list_documents()
193201
assert len(documents) == 1
194202
doc_id = documents[0].id
195-
203+
196204
cursor = rag._conn.cursor()
197205
cursor.execute("SELECT COUNT(*) FROM chunks WHERE document_id = ?", (doc_id,))
198206
chunk_count = cursor.fetchone()[0]
199207
assert chunk_count > 0
200-
208+
201209
# Remove document
202210
assert doc_id is not None
203211
success = rag.remove_document(doc_id)
204-
212+
205213
assert success is True
206-
214+
207215
# Verify document and chunks are removed
208216
documents = rag.list_documents()
209217
assert len(documents) == 0
210-
218+
211219
cursor.execute("SELECT COUNT(*) FROM chunks WHERE document_id = ?", (doc_id,))
212220
chunk_count = cursor.fetchone()[0]
213221
assert chunk_count == 0

0 commit comments

Comments
 (0)