configurable directory
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from chromy.chroma_functions import get_client
|
||||
from chromy.errors import ChromaPathError
|
||||
|
||||
|
||||
class ChromaFunctionsTests(unittest.TestCase):
|
||||
def test_get_client_uses_default_when_env_is_unset(self) -> None:
|
||||
with (
|
||||
patch.dict(os.environ, {}, clear=True),
|
||||
patch("chromy.chroma_functions.chromadb.PersistentClient") as persistent,
|
||||
):
|
||||
get_client()
|
||||
|
||||
persistent.assert_called_once_with()
|
||||
|
||||
def test_get_client_uses_chroma_folder_override(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
configured_parent = Path(temp_dir) / "data"
|
||||
|
||||
with (
|
||||
patch.dict(os.environ, {"CHROMA_FOLDER": str(configured_parent)}),
|
||||
patch(
|
||||
"chromy.chroma_functions.chromadb.PersistentClient"
|
||||
) as persistent,
|
||||
):
|
||||
get_client()
|
||||
|
||||
expected_path = configured_parent.resolve() / "chroma"
|
||||
persistent.assert_called_once_with(path=str(expected_path))
|
||||
self.assertTrue(expected_path.is_dir())
|
||||
|
||||
def test_get_client_resolves_relative_chroma_folder_from_cwd(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
working_dir = Path(temp_dir)
|
||||
previous_cwd = Path.cwd()
|
||||
|
||||
try:
|
||||
os.chdir(working_dir)
|
||||
with (
|
||||
patch.dict(os.environ, {"CHROMA_FOLDER": "relative-parent"}),
|
||||
patch(
|
||||
"chromy.chroma_functions.chromadb.PersistentClient"
|
||||
) as persistent,
|
||||
):
|
||||
get_client()
|
||||
finally:
|
||||
os.chdir(previous_cwd)
|
||||
|
||||
expected_path = (working_dir / "relative-parent").resolve() / "chroma"
|
||||
persistent.assert_called_once_with(path=str(expected_path))
|
||||
|
||||
def test_get_client_fails_when_configured_path_is_not_usable(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
invalid_parent = Path(temp_dir) / "not-a-directory"
|
||||
invalid_parent.write_text("x", encoding="utf-8")
|
||||
|
||||
with (
|
||||
patch.dict(os.environ, {"CHROMA_FOLDER": str(invalid_parent)}),
|
||||
self.assertRaisesRegex(
|
||||
ChromaPathError,
|
||||
"Could not create or access Chroma directory",
|
||||
),
|
||||
):
|
||||
get_client()
|
||||
|
||||
def test_get_client_wraps_client_initialization_failures(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
configured_parent = Path(temp_dir)
|
||||
|
||||
with (
|
||||
patch.dict(os.environ, {"CHROMA_FOLDER": str(configured_parent)}),
|
||||
patch(
|
||||
"chromy.chroma_functions.chromadb.PersistentClient",
|
||||
side_effect=RuntimeError("boom"),
|
||||
),
|
||||
self.assertRaisesRegex(
|
||||
ChromaPathError,
|
||||
"Could not initialize Chroma client",
|
||||
),
|
||||
):
|
||||
get_client()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -10,6 +10,7 @@ from click.testing import Result
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from chromy.cli import app
|
||||
from chromy.errors import ChromaPathError
|
||||
|
||||
|
||||
class CliTests(unittest.TestCase):
|
||||
@@ -251,6 +252,19 @@ class CliTests(unittest.TestCase):
|
||||
self.assertNotEqual(result.exit_code, 0)
|
||||
self.assertIn("Missing option", result.output)
|
||||
|
||||
def test_cli_surfaces_chroma_path_errors(self) -> None:
|
||||
with patch(
|
||||
"chromy.handlers.list_collections.list_collections",
|
||||
side_effect=ChromaPathError("configured path is not writable"),
|
||||
):
|
||||
result = _invoke(["list-collections"])
|
||||
|
||||
self.assertEqual(result.exit_code, 1)
|
||||
self.assertEqual(
|
||||
result.stdout,
|
||||
"Error: configured path is not writable\n",
|
||||
)
|
||||
|
||||
|
||||
def _invoke(arguments: Sequence[str]) -> Result:
|
||||
return CliRunner().invoke(app, list(arguments))
|
||||
|
||||
Reference in New Issue
Block a user