Skip to content

Commit 3a44b2f

Browse files
author
Daniele Briggi
committed
chore(tests): add cli tests
1 parent 6f6d3d3 commit 3a44b2f

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

src/sqlite_rag/cli.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,19 @@ def add(
257257
rag_context = ctx.obj["rag_context"]
258258
start_time = time.time()
259259

260-
# Parse and normalize extension lists
261260
only_list = (
262261
[e.strip().lstrip(".").lower() for e in only_extensions.split(",") if e.strip()]
263-
if only_extensions else None
262+
if only_extensions
263+
else None
264264
)
265265
exclude_list = (
266-
[e.strip().lstrip(".").lower() for e in exclude_extensions.split(",") if e.strip()]
267-
if exclude_extensions else None
266+
[
267+
e.strip().lstrip(".").lower()
268+
for e in exclude_extensions.split(",")
269+
if e.strip()
270+
]
271+
if exclude_extensions
272+
else None
268273
)
269274

270275
rag = rag_context.get_rag()

src/sqlite_rag/reader.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ def is_supported(
4646
only_extensions: Optional[list[str]] = None,
4747
exclude_extensions: Optional[list[str]] = None,
4848
) -> bool:
49-
"""Check if the file extension is supported"""
49+
"""Check if the file extension is supported.
50+
51+
Parameters:
52+
path (Path): The file path to check.
53+
only_extensions (Optional[list[str]]): If provided, only files with these extensions are considered.
54+
exclude_extensions (Optional[list[str]]): If provided, files with these extensions are excluded.
55+
"""
5056
extension = path.suffix.lower().lstrip(".")
5157

5258
supported_extensions = set(FileReader.extensions)

tests/integration/test_cli.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,86 @@ def test_change_database_path(self):
113113
assert result.exit_code == 0
114114

115115
assert f"Database: {tmp_db.name}" in result.stdout
116+
117+
def test_add_with_exclude_extensions(self):
118+
with tempfile.TemporaryDirectory() as tmp_dir:
119+
(Path(tmp_dir) / "file1.txt").write_text("This is a text file.")
120+
(Path(tmp_dir) / "file2.md").write_text("# This is a markdown file.")
121+
(Path(tmp_dir) / "file3.py").write_text("print('Hello, world!')")
122+
(Path(tmp_dir) / "file4.js").write_text("console.log('Hello, world!');")
123+
124+
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
125+
runner = CliRunner()
126+
127+
result = runner.invoke(
128+
app,
129+
["--database", tmp_db.name, "add", tmp_dir, "--exclude", "py,js"],
130+
)
131+
assert result.exit_code == 0
132+
133+
# Check that only .txt and .md files were added
134+
assert "Processing 2 files" in result.stdout
135+
assert "file1.txt" in result.stdout
136+
assert "file2.md" in result.stdout
137+
assert "file3.py" not in result.stdout
138+
assert "file4.js" not in result.stdout
139+
140+
def test_add_with_only_extensions(self):
141+
with tempfile.TemporaryDirectory() as tmp_dir:
142+
(Path(tmp_dir) / "file1.txt").write_text("This is a text file.")
143+
(Path(tmp_dir) / "file2.md").write_text("# This is a markdown file.")
144+
(Path(tmp_dir) / "file3.py").write_text("print('Hello, world!')")
145+
(Path(tmp_dir) / "file4.js").write_text("console.log('Hello, world!');")
146+
147+
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
148+
runner = CliRunner()
149+
150+
result = runner.invoke(
151+
app,
152+
[
153+
"--database",
154+
tmp_db.name,
155+
"add",
156+
tmp_dir,
157+
"--only",
158+
"md,txt",
159+
],
160+
)
161+
assert result.exit_code == 0
162+
163+
# Check that only .txt and .md files were added
164+
assert "Processing 2 files" in result.stdout
165+
assert "file1.txt" in result.stdout
166+
assert "file2.md" in result.stdout
167+
assert "file3.py" not in result.stdout
168+
assert "file4.js" not in result.stdout
169+
170+
def test_add_with_only_and_exclude_extensions_are_normilized(self):
171+
with tempfile.TemporaryDirectory() as tmp_dir:
172+
(Path(tmp_dir) / "file1.txt").write_text("This is a text file.")
173+
(Path(tmp_dir) / "file2.md").write_text("# This is a markdown file.")
174+
(Path(tmp_dir) / "file3.py").write_text("print('Hello, world!')")
175+
176+
with tempfile.NamedTemporaryFile(suffix=".tempdb") as tmp_db:
177+
runner = CliRunner()
178+
179+
result = runner.invoke(
180+
app,
181+
[
182+
"--database",
183+
tmp_db.name,
184+
"add",
185+
tmp_dir,
186+
"--only",
187+
".md, .txt,py",
188+
"--exclude",
189+
".py ", # wins over --only
190+
],
191+
)
192+
assert result.exit_code == 0
193+
194+
# Check that only .txt and .md files were added
195+
assert "Processing 2 files" in result.stdout
196+
assert "file1.txt" in result.stdout
197+
assert "file2.md" in result.stdout
198+
assert "file3.py" not in result.stdout

0 commit comments

Comments
 (0)