Skip to content

Commit

Permalink
Factor out part of public method
Browse files Browse the repository at this point in the history
  • Loading branch information
mportesdev committed Apr 16, 2021
1 parent 2dbbd12 commit 3bcd7a6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 66 deletions.
17 changes: 11 additions & 6 deletions pictureshow/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
99 changes: 39 additions & 60 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down Expand Up @@ -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"""

Expand Down

0 comments on commit 3bcd7a6

Please sign in to comment.