From 3bcd7a686844826aa40df44b557916de16ff4128 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20Porte=C5=A1?= <michalportes1@gmail.com>
Date: Fri, 16 Apr 2021 17:37:54 +0200
Subject: [PATCH] Factor out part of public method

---
 pictureshow/core.py | 17 +++++---
 tests/unit_test.py  | 99 ++++++++++++++++++---------------------------
 2 files changed, 50 insertions(+), 66 deletions(-)

diff --git a/pictureshow/core.py b/pictureshow/core.py
index 051bc1e..f343766 100644
--- a/pictureshow/core.py
+++ b/pictureshow/core.py
@@ -28,12 +28,7 @@ def __init__(self, *pic_files):
 
     def save_pdf(self, pdf_file, page_size='A4', landscape=False, margin=72,
                  layout=(1, 1), stretch_small=False, force_overwrite=False):
-        target_str = str(pdf_file)
-        target_path = Path(pdf_file)
-
-        if target_path.exists() and not force_overwrite:
-            raise FileExistsError(f'file {target_str!r} exists')
-
+        target_str = self._validate_target_path(pdf_file, force_overwrite)
         page_size = self._validate_page_size(page_size, landscape)
         layout = self._validate_layout(layout)
 
@@ -64,6 +59,16 @@ def _save_pdf(self, pdf_file, page_size, margin, layout, stretch_small):
                 num_ok += 1
             pdf_canvas.showPage()
 
+    @staticmethod
+    def _validate_target_path(file_path, force_overwrite):
+        target_str = str(file_path)
+        target_path = Path(file_path)
+
+        if target_path.exists() and not force_overwrite:
+            raise FileExistsError(f'file {target_str!r} exists')
+
+        return target_str
+
     @staticmethod
     def _validate_page_size(page_size, landscape):
         if isinstance(page_size, str):
diff --git a/tests/unit_test.py b/tests/unit_test.py
index 009f814..8b8ae2c 100644
--- a/tests/unit_test.py
+++ b/tests/unit_test.py
@@ -27,66 +27,6 @@ def picture():
     return image_reader
 
 
-class TestPictureShowSavePdf:
-    """Test core.PictureShow.save_pdf"""
-
-    def test_nonexistent_target_file(self, mocker):
-        Path = mocker.patch('pictureshow.core.Path', autospec=True)
-        Path.return_value.exists.return_value = False
-        _save_pdf = mocker.patch('pictureshow.core.PictureShow._save_pdf',
-                                 autospec=True)
-        fake_pdf_name = 'foo.pdf'
-        PictureShow().save_pdf(fake_pdf_name)
-
-        Path.assert_called_once_with(fake_pdf_name)
-        Path().exists.assert_called_once_with()
-        _save_pdf.assert_called_once()
-        # test only the second positional arg (first after self)
-        assert _save_pdf.call_args[0][1] == fake_pdf_name
-
-    def test_existing_target_file_raises_error(self, mocker):
-        Path = mocker.patch('pictureshow.core.Path', autospec=True)
-        Path.return_value.exists.return_value = True
-        _save_pdf = mocker.patch('pictureshow.core.PictureShow._save_pdf',
-                                 autospec=True)
-        fake_pdf_name = 'foo.pdf'
-        with pytest.raises(FileExistsError, match="file '.*' exists"):
-            PictureShow().save_pdf(fake_pdf_name)
-
-        Path.assert_called_once_with(fake_pdf_name)
-        Path().exists.assert_called_once_with()
-        _save_pdf.assert_not_called()
-
-    def test_force_overwrite_existing_file(self, mocker):
-        Path = mocker.patch('pictureshow.core.Path', autospec=True)
-        Path.return_value.exists.return_value = True
-        _save_pdf = mocker.patch('pictureshow.core.PictureShow._save_pdf',
-                                 autospec=True)
-        fake_pdf_name = 'foo.pdf'
-        PictureShow().save_pdf(fake_pdf_name, force_overwrite=True)
-
-        Path.assert_called_once_with(fake_pdf_name)
-        Path().exists.assert_called_once_with()
-        _save_pdf.assert_called_once()
-        # test only the second positional arg (first after self)
-        assert _save_pdf.call_args[0][1] == fake_pdf_name
-
-    def test_target_path_as_pathlike(self, mocker):
-        """Call `save_pdf` with a Path object as target file,
-        assert that `_save_pdf` was called with a str.
-        """
-        Path_mock = mocker.patch('pictureshow.core.Path', autospec=True)
-        Path_mock.return_value.exists.return_value = False
-        _save_pdf = mocker.patch('pictureshow.core.PictureShow._save_pdf',
-                                 autospec=True)
-        fake_pdf_name = 'foo.pdf'
-        PictureShow().save_pdf(Path(fake_pdf_name))
-
-        _save_pdf.assert_called_once()
-        # test only the second positional arg (first after self)
-        assert _save_pdf.call_args[0][1] == fake_pdf_name
-
-
 class TestSavePdf:
     """Test core.PictureShow._save_pdf"""
 
@@ -189,6 +129,45 @@ def test_invalid_input_calls(self, mocker, reader_side_effects):
         canvas.save.assert_not_called()
 
 
+class TestValidateTargetPath:
+    """Test core.PictureShow._validate_target_path"""
+
+    def test_nonexistent_target_file(self, mocker):
+        Path = mocker.patch('pictureshow.core.Path', autospec=True)
+        Path.return_value.exists.return_value = False
+        pdf_file = 'foo.pdf'
+
+        result = PictureShow()._validate_target_path(pdf_file,
+                                                     force_overwrite=False)
+        assert result == pdf_file
+
+    def test_existing_target_file_raises_error(self, mocker):
+        Path = mocker.patch('pictureshow.core.Path', autospec=True)
+        Path.return_value.exists.return_value = True
+
+        with pytest.raises(FileExistsError, match="file '.*' exists"):
+            PictureShow()._validate_target_path('foo.pdf',
+                                                force_overwrite=False)
+
+    def test_force_overwrite_existing_file(self, mocker):
+        Path = mocker.patch('pictureshow.core.Path', autospec=True)
+        Path.return_value.exists.return_value = True
+        pdf_file = 'foo.pdf'
+
+        result = PictureShow()._validate_target_path(pdf_file,
+                                                     force_overwrite=True)
+        assert result == pdf_file
+
+    def test_target_path_as_pathlike(self, mocker):
+        Path_mock = mocker.patch('pictureshow.core.Path', autospec=True)
+        Path_mock.return_value.exists.return_value = False
+        pdf_file = 'foo.pdf'
+
+        result = PictureShow()._validate_target_path(Path(pdf_file),
+                                                     force_overwrite=False)
+        assert result == pdf_file
+
+
 class TestValidatePageSize:
     """Test core.PictureShow._validate_page_size"""