|
1 | 1 | from sqlite_rag.models.chunk import Chunk |
2 | 2 | from sqlite_rag.models.document import Document |
| 3 | +from sqlite_rag.models.sentence import Sentence |
3 | 4 | from sqlite_rag.repository import Repository |
4 | 5 |
|
5 | 6 |
|
@@ -153,35 +154,72 @@ def test_remove_document_success(self, db_conn): |
153 | 154 | conn, settings = db_conn |
154 | 155 | repo = Repository(conn, settings) |
155 | 156 |
|
156 | | - # Add a document with chunks |
| 157 | + # Add a document with chunks and sentences |
157 | 158 | doc = Document( |
158 | 159 | content="Test document content.", |
159 | 160 | uri="test.txt", |
160 | 161 | metadata={"author": "test"}, |
161 | 162 | ) |
162 | | - doc.chunks = [ |
163 | | - Chunk(content="Chunk 1", embedding=b"\x00" * 384), |
164 | | - Chunk(content="Chunk 2", embedding=b"\x00" * 384), |
| 163 | + chunk1 = Chunk(content="Chunk 1", embedding=b"\x00" * 384) |
| 164 | + chunk1.sentences = [ |
| 165 | + Sentence( |
| 166 | + content="Sentence 1", |
| 167 | + embedding=b"\x00" * 384, |
| 168 | + start_offset=0, |
| 169 | + end_offset=10, |
| 170 | + ), |
| 171 | + Sentence( |
| 172 | + content="Sentence 2", |
| 173 | + embedding=b"\x00" * 384, |
| 174 | + start_offset=11, |
| 175 | + end_offset=20, |
| 176 | + ), |
| 177 | + ] |
| 178 | + chunk2 = Chunk(content="Chunk 2", embedding=b"\x00" * 384) |
| 179 | + chunk2.sentences = [ |
| 180 | + Sentence( |
| 181 | + content="Sentence 3", |
| 182 | + embedding=b"\x00" * 384, |
| 183 | + start_offset=0, |
| 184 | + end_offset=10, |
| 185 | + ), |
165 | 186 | ] |
| 187 | + doc.chunks = [chunk1, chunk2] |
166 | 188 | doc_id = repo.add_document(doc) |
167 | 189 |
|
168 | | - # Verify document and chunks exist |
| 190 | + # Verify document, chunks, and sentences exist |
169 | 191 | cursor = conn.cursor() |
170 | 192 | cursor.execute("SELECT COUNT(*) FROM documents WHERE id = ?", (doc_id,)) |
171 | 193 | assert cursor.fetchone()[0] == 1 |
172 | 194 | cursor.execute("SELECT COUNT(*) FROM chunks WHERE document_id = ?", (doc_id,)) |
173 | 195 | assert cursor.fetchone()[0] == 2 |
| 196 | + cursor.execute( |
| 197 | + """ |
| 198 | + SELECT COUNT(*) FROM sentences |
| 199 | + WHERE chunk_id IN (SELECT id FROM chunks WHERE document_id = ?) |
| 200 | + """, |
| 201 | + (doc_id,), |
| 202 | + ) |
| 203 | + assert cursor.fetchone()[0] == 3 |
174 | 204 |
|
175 | 205 | # Remove document |
176 | 206 | success = repo.remove_document(doc_id) |
177 | 207 |
|
178 | 208 | assert success is True |
179 | 209 |
|
180 | | - # Verify document and chunks are removed |
| 210 | + # Verify document, chunks, and sentences are removed |
181 | 211 | cursor.execute("SELECT COUNT(*) FROM documents WHERE id = ?", (doc_id,)) |
182 | 212 | assert cursor.fetchone()[0] == 0 |
183 | 213 | cursor.execute("SELECT COUNT(*) FROM chunks WHERE document_id = ?", (doc_id,)) |
184 | 214 | assert cursor.fetchone()[0] == 0 |
| 215 | + cursor.execute( |
| 216 | + """ |
| 217 | + SELECT COUNT(*) FROM sentences |
| 218 | + WHERE chunk_id IN (SELECT id FROM chunks WHERE document_id = ?) |
| 219 | + """, |
| 220 | + (doc_id,), |
| 221 | + ) |
| 222 | + assert cursor.fetchone()[0] == 0 |
185 | 223 |
|
186 | 224 | def test_remove_document_not_found(self, db_conn): |
187 | 225 | conn, settings = db_conn |
|
0 commit comments