-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_cli.py
More file actions
198 lines (167 loc) · 6.87 KB
/
test_cli.py
File metadata and controls
198 lines (167 loc) · 6.87 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
import tempfile
from pathlib import Path
from typer.testing import CliRunner
from sqlite_rag.cli import app
from sqlite_rag.settings import Settings
class TestCLI:
def test_search_exact_match(self):
"""Test adding documents and searching for an exact match."""
doc1_content = "The quick brown fox jumps over the lazy dog"
doc2_content = (
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
)
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
runner = CliRunner()
model_path = Path(Settings().model_path).absolute()
result = runner.invoke(
app,
[
"--database",
tmp_db.name,
"configure",
"--model-path",
str(model_path),
"--no-prompt-templates",
"--other-vector-options",
"distance=cosine",
],
)
assert result.exit_code == 0
# Add
result = runner.invoke(
app,
[
"--database",
tmp_db.name,
"add-text",
doc1_content,
],
)
assert result.exit_code == 0
result = runner.invoke(
app,
[
"--database",
tmp_db.name,
"add-text",
doc2_content,
],
)
assert result.exit_code == 0
# Search
result = runner.invoke(
app,
[
"--database",
tmp_db.name,
"search",
doc1_content,
"--debug",
"--limit",
"1",
],
)
# Assert CLI command executed successfully
assert result.exit_code == 0
assert "Search Results (1 matches)" in result.stdout
# For exact match with cosine distance, we expect distance close to 0.0
assert "Vector: 0.000000" in result.stdout or "0.00000" in result.stdout
def test_set_settings(self):
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
runner = CliRunner()
model_path = "mypath/mymodel.gguf"
result = runner.invoke(
app,
[
"--database",
tmp_db.name,
"configure",
"--model-path",
model_path,
"--other-vector-options",
"distance=L2",
],
)
assert result.exit_code == 0
assert f"model_path: {model_path}" in result.stdout
assert "other_vector_options: distance=L2" in result.stdout
def test_change_database_path(self):
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
runner = CliRunner()
result = runner.invoke(
app,
["--database", tmp_db.name, "settings"],
)
assert result.exit_code == 0
assert f"Database: {tmp_db.name}" in result.stdout
def test_add_with_exclude_extensions(self):
with tempfile.TemporaryDirectory() as tmp_dir:
(Path(tmp_dir) / "file1.txt").write_text("This is a text file.")
(Path(tmp_dir) / "file2.md").write_text("# This is a markdown file.")
(Path(tmp_dir) / "file3.py").write_text("print('Hello, world!')")
(Path(tmp_dir) / "file4.js").write_text("console.log('Hello, world!');")
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
runner = CliRunner()
result = runner.invoke(
app,
["--database", tmp_db.name, "add", tmp_dir, "--exclude", "py,js"],
)
assert result.exit_code == 0
# Check that only .txt and .md files were added
assert "Processing 2 files" in result.stdout
assert "file1.txt" in result.stdout
assert "file2.md" in result.stdout
assert "file3.py" not in result.stdout
assert "file4.js" not in result.stdout
def test_add_with_only_extensions(self):
with tempfile.TemporaryDirectory() as tmp_dir:
(Path(tmp_dir) / "file1.txt").write_text("This is a text file.")
(Path(tmp_dir) / "file2.md").write_text("# This is a markdown file.")
(Path(tmp_dir) / "file3.py").write_text("print('Hello, world!')")
(Path(tmp_dir) / "file4.js").write_text("console.log('Hello, world!');")
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
runner = CliRunner()
result = runner.invoke(
app,
[
"--database",
tmp_db.name,
"add",
tmp_dir,
"--only",
"md,txt",
],
)
assert result.exit_code == 0
# Check that only .txt and .md files were added
assert "Processing 2 files" in result.stdout
assert "file1.txt" in result.stdout
assert "file2.md" in result.stdout
assert "file3.py" not in result.stdout
assert "file4.js" not in result.stdout
def test_add_with_only_and_exclude_extensions_are_normilized(self):
with tempfile.TemporaryDirectory() as tmp_dir:
(Path(tmp_dir) / "file1.txt").write_text("This is a text file.")
(Path(tmp_dir) / "file2.md").write_text("# This is a markdown file.")
(Path(tmp_dir) / "file3.py").write_text("print('Hello, world!')")
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
runner = CliRunner()
result = runner.invoke(
app,
[
"--database",
tmp_db.name,
"add",
tmp_dir,
"--only",
".md, .txt,py",
"--exclude",
".py ", # wins over --only
],
)
assert result.exit_code == 0
# Check that only .txt and .md files were added
assert "Processing 2 files" in result.stdout
assert "file1.txt" in result.stdout
assert "file2.md" in result.stdout
assert "file3.py" not in result.stdout