Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a672633526 | |||
| e5b63ac6fb |
@@ -1,9 +1,29 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from chromy.utilities import ingest_file
|
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:
|
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}'.")
|
print(f"Added {records_added} records to collection '{collection}'.")
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
+9
-1
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
from pathlib import Path
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from click.testing import Result
|
from click.testing import Result
|
||||||
@@ -11,6 +12,10 @@ from chromy.cli import app
|
|||||||
|
|
||||||
|
|
||||||
class CliTests(unittest.TestCase):
|
class CliTests(unittest.TestCase):
|
||||||
|
@staticmethod
|
||||||
|
def _fixture_path(path: str) -> str:
|
||||||
|
return str(Path(path).resolve())
|
||||||
|
|
||||||
def test_list_collections(self) -> None:
|
def test_list_collections(self) -> None:
|
||||||
with patch(
|
with patch(
|
||||||
"chromy.handlers.list_collections.list_collections",
|
"chromy.handlers.list_collections.list_collections",
|
||||||
@@ -60,7 +65,10 @@ class CliTests(unittest.TestCase):
|
|||||||
) as ingest_file:
|
) as ingest_file:
|
||||||
result = _invoke(["import", "notes", "romeo_and_juliet.txt"])
|
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.exit_code, 0)
|
||||||
self.assertEqual(result.stdout, "Added 3 records to collection 'notes'.\n")
|
self.assertEqual(result.stdout, "Added 3 records to collection 'notes'.\n")
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import io
|
|||||||
import unittest
|
import unittest
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from contextlib import redirect_stdout
|
from contextlib import redirect_stdout
|
||||||
|
from pathlib import Path
|
||||||
from typing import TypeVar
|
from typing import TypeVar
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@@ -21,6 +22,10 @@ CommandT = TypeVar("CommandT")
|
|||||||
|
|
||||||
|
|
||||||
class HandlerTests(unittest.TestCase):
|
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:
|
def test_list_collections_prints_empty_message(self) -> None:
|
||||||
with patch(
|
with patch(
|
||||||
"chromy.handlers.list_collections.list_collections", return_value=[]
|
"chromy.handlers.list_collections.list_collections", return_value=[]
|
||||||
@@ -94,7 +99,10 @@ class HandlerTests(unittest.TestCase):
|
|||||||
"romeo_and_juliet.txt",
|
"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(exit_code, 0)
|
||||||
self.assertEqual(output, "Added 3 records to collection 'notes'.\n")
|
self.assertEqual(output, "Added 3 records to collection 'notes'.\n")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user