Skip to content

Commit 78495a9

Browse files
committed
tests(db): skip tests when dependency's not met
1 parent dad1eb9 commit 78495a9

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
.PHONY: multitest
2-
31
EXTRA_LOCK_ARGS?=
4-
EXTRA_DEPS?=""
2+
EXTRA_DEPS?=
3+
4+
LOADED_DOT_ENV=@if [ -f .env ] ; then source .env; fi;
5+
56
DEFAULT_GROUPS=--group dev --group lsp --group mcp --group debug $(EXTRA_LOCK_ARGS)
67

8+
.PHONY: multitest
9+
710
deps:
811
pdm lock $(DEFAULT_GROUPS) || pdm lock $(DEFAULT_GROUPS) --group legacy; \
912
pdm install

tests/database/test_chroma0.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44

55
import httpx
66
import pytest
7+
8+
try:
9+
import chromadb
10+
11+
if not chromadb.__version__.startswith("0.6.3"):
12+
pytest.skip(
13+
f"Found chromadb {chromadb.__version__}. Skipping chroma0 tests.",
14+
allow_module_level=True,
15+
)
16+
except ModuleNotFoundError:
17+
pytest.skip(
18+
"ChromaDB 0.6.3 not found. Skipping choma0 tests.",
19+
allow_module_level=True,
20+
)
21+
722
from chromadb.api.types import QueryResult
823
from chromadb.errors import InvalidCollectionException
924
from tree_sitter import Point

tests/database/test_db_init.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1+
import sys
2+
from unittest.mock import MagicMock, patch
3+
14
import pytest
25

36
from vectorcode.cli_utils import Config
47
from vectorcode.database import get_database_connector
5-
from vectorcode.database.chroma0 import ChromaDB0Connector
68

79

8-
def test_get_database_connector():
9-
assert isinstance(
10-
get_database_connector(Config(db_type="ChromaDB0")), ChromaDB0Connector
11-
)
10+
@pytest.mark.parametrize(
11+
"db_type, module_to_mock, class_name",
12+
[
13+
("ChromaDB0", "vectorcode.database.chroma0", "ChromaDB0Connector"),
14+
# To test a new connector, add a tuple here following the same pattern.
15+
# e.g. ("NewDB", "vectorcode.database.newdb", "NewDBConnector"),
16+
],
17+
)
18+
def test_get_database_connector(db_type, module_to_mock, class_name):
19+
"""
20+
Tests that get_database_connector can correctly return a connector
21+
for a given db_type. This test is parameterized to be easily
22+
extensible for new database connectors.
23+
"""
24+
mock_connector_class = MagicMock()
25+
mock_module = MagicMock()
26+
setattr(mock_module, class_name, mock_connector_class)
27+
28+
# Use patch.dict to temporarily replace the module in sys.modules.
29+
# This prevents the actual module from being imported, avoiding
30+
# errors if its dependencies are not installed.
31+
with patch.dict(sys.modules, {module_to_mock: mock_module}):
32+
config = Config(db_type=db_type)
33+
connector = get_database_connector(config)
34+
35+
# Verify that the create method was called on our mock class
36+
mock_connector_class.create.assert_called_once_with(config)
37+
38+
# Verify that the returned connector is the one from our mock
39+
assert connector == mock_connector_class.create.return_value
1240

1341

1442
def test_get_database_connector_invalid_type():

0 commit comments

Comments
 (0)