decouple core data from CLI formatting
build / build (push) Successful in 49s
pytest / pytest (push) Successful in 30s

This commit is contained in:
Matteo Rosati
2026-04-29 12:44:28 +02:00
parent 615ab14a1a
commit d1b1238897
12 changed files with 142 additions and 87 deletions
+19 -3
View File
@@ -31,11 +31,11 @@ class CliTests(unittest.TestCase):
with patch(
"chromy.handlers.list_collections.list_collections",
return_value=["books", "code"],
):
):
result = _invoke(["list-collections"])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.stdout, "books\ncode\n")
self.assertEqual(result.stdout, "· books\n· code\n")
def test_create_collection(self) -> None:
with patch(
@@ -89,7 +89,10 @@ class CliTests(unittest.TestCase):
count_collection.assert_called_once_with("notes")
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.stdout, "7\n")
self.assertEqual(
result.stdout,
"The 'notes' collection contains 7 records.\n",
)
def test_import_data(self) -> None:
with patch(
@@ -105,6 +108,19 @@ class CliTests(unittest.TestCase):
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.stdout, "Added 3 records to collection 'notes'.\n")
def test_import_data_rejects_non_text_files(self) -> None:
with patch(
"chromy.handlers.import_data.is_probably_text_file",
return_value=False,
):
result = _invoke(["import", "notes", "romeo_and_juliet.txt"])
self.assertEqual(result.exit_code, 1)
self.assertEqual(
result.stdout,
"Error: The file 'romeo_and_juliet.txt' is not a text file.\n",
)
def test_query(self) -> None:
query_result = {"ids": [["1"]], "documents": [["hello"]]}
+20 -2
View File
@@ -1,11 +1,29 @@
from __future__ import annotations
import unittest
from unittest.mock import patch
from chromy.embed import embed
class EmbedTest(unittest.TestCase):
def test_embed_function(self) -> None:
self.assertEqual(0, 0)
def test_embed_returns_empty_list_for_empty_chunks(self) -> None:
self.assertEqual(embed([]), [])
def test_embed_pairs_text_with_list_embeddings(self) -> None:
with patch(
"chromy.embed.DefaultEmbeddingFunction",
return_value=lambda chunks: ((1.0, 2.0), (3.0, 4.0)),
):
result = embed(["first", "second"])
self.assertEqual(
result,
[
{"text": "first", "embedding": [1.0, 2.0]},
{"text": "second", "embedding": [3.0, 4.0]},
],
)
if __name__ == "__main__":
+13 -2
View File
@@ -8,6 +8,7 @@ from pathlib import Path
from typing import TypeVar
from unittest.mock import patch
from chromy.errors import UnsupportedTextFileError
from chromy.handlers.count_collection import handle_count_collection
from chromy.handlers.create_collection import handle_create_collection
from chromy.handlers.delete_collection import (
@@ -47,7 +48,7 @@ class HandlerTests(unittest.TestCase):
)
self.assertEqual(exit_code, 0)
self.assertEqual(output, "notes\nplays\n")
self.assertEqual(output, "· notes\n· plays\n")
def test_create_collection_uses_typed_input(self) -> None:
with patch(
@@ -86,7 +87,7 @@ class HandlerTests(unittest.TestCase):
count.assert_called_once_with("notes")
self.assertEqual(exit_code, 0)
self.assertEqual(output, "7\n")
self.assertEqual(output, "The 'notes' collection contains 7 records.\n")
def test_import_data_uses_typed_input(self) -> None:
with patch(
@@ -106,6 +107,16 @@ class HandlerTests(unittest.TestCase):
self.assertEqual(exit_code, 0)
self.assertEqual(output, "Added 3 records to collection 'notes'.\n")
def test_import_data_rejects_non_text_files(self) -> None:
with (
patch(
"chromy.handlers.import_data.is_probably_text_file",
return_value=False,
),
self.assertRaises(UnsupportedTextFileError),
):
handle_import("notes", "romeo_and_juliet.txt")
def test_query_uses_typed_input(self) -> None:
query_result = {"ids": [["1"]], "documents": [["hello"]]}
with (