Compare commits

...

2 Commits

Author SHA1 Message Date
mrosati a672633526 extract _get_absolute_path
build / build (push) Successful in 11s
2026-04-23 20:46:26 +02:00
mrosati e5b63ac6fb use absolute paths 2026-04-23 19:56:11 +02:00
3 changed files with 39 additions and 3 deletions
+21 -1
View File
@@ -1,9 +1,29 @@
from __future__ import annotations
import os
from pathlib import Path
from chromy.utilities import ingest_file
def _get_absolute_path(file: str) -> str:
"""
A helper method that, given a valid relative path to a file, returns its
absolute path.
Args:
file (str): The relative path to the file.
Raises:
FileNotFoundError(): If the file does not exist.
"""
if not os.path.exists(file):
raise FileNotFoundError()
file_path = Path(file)
return str(file_path.resolve(file_path))
def handle_import(collection: str, file: str) -> int:
records_added = ingest_file(collection, file)
records_added = ingest_file(collection, _get_absolute_path(file))
print(f"Added {records_added} records to collection '{collection}'.")
return 0
+9 -1
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import unittest
from collections.abc import Sequence
from pathlib import Path
from unittest.mock import patch
from click.testing import Result
@@ -11,6 +12,10 @@ from chromy.cli import app
class CliTests(unittest.TestCase):
@staticmethod
def _fixture_path(path: str) -> str:
return str(Path(path).resolve())
def test_list_collections(self) -> None:
with patch(
"chromy.handlers.list_collections.list_collections",
@@ -60,7 +65,10 @@ class CliTests(unittest.TestCase):
) as ingest_file:
result = _invoke(["import", "notes", "romeo_and_juliet.txt"])
ingest_file.assert_called_once_with("notes", "romeo_and_juliet.txt")
ingest_file.assert_called_once_with(
"notes",
self._fixture_path("romeo_and_juliet.txt"),
)
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.stdout, "Added 3 records to collection 'notes'.\n")
+9 -1
View File
@@ -4,6 +4,7 @@ import io
import unittest
from collections.abc import Callable
from contextlib import redirect_stdout
from pathlib import Path
from typing import TypeVar
from unittest.mock import patch
@@ -21,6 +22,10 @@ CommandT = TypeVar("CommandT")
class HandlerTests(unittest.TestCase):
@staticmethod
def _fixture_path(path: str) -> str:
return str(Path(path).resolve())
def test_list_collections_prints_empty_message(self) -> None:
with patch(
"chromy.handlers.list_collections.list_collections", return_value=[]
@@ -94,7 +99,10 @@ class HandlerTests(unittest.TestCase):
"romeo_and_juliet.txt",
)
ingest_file.assert_called_once_with("notes", "romeo_and_juliet.txt")
ingest_file.assert_called_once_with(
"notes",
self._fixture_path("romeo_and_juliet.txt"),
)
self.assertEqual(exit_code, 0)
self.assertEqual(output, "Added 3 records to collection 'notes'.\n")