Compare commits
2 Commits
fb5d0f7c0c
...
a672633526
| Author | SHA1 | Date | |
|---|---|---|---|
| a672633526 | |||
| e5b63ac6fb |
@@ -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
@@ -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")
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user