Skip to content

Commit

Permalink
add unit tests for document.py
Browse files Browse the repository at this point in the history
  • Loading branch information
deeplow committed Sep 19, 2022
1 parent 5ab94c0 commit dd2c1a9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dangerzone/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ def output_filename(self, filename: str) -> None:


class DocumentFilenameException(Exception):
def __init__(self, message):
def __init__(self, message: str) -> None:
super().__init__(message)
5 changes: 5 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@

class TestBase:
sample_doc = str(test_docs_dir.joinpath(BASIC_SAMPLE))


@pytest.fixture
def sample_doc():
return str(test_docs_dir.joinpath(BASIC_SAMPLE))
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import os
import shutil
import tempfile
from pathlib import Path

import pytest
from click.testing import CliRunner, Result
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_lang_eng(self):
)
def test_filenames(self, filename):
tempdir = tempfile.mkdtemp(prefix="dangerzone-")
doc_path = os.path.join(filename)
doc_path = str(Path(tempdir).joinpath(filename))
shutil.copyfile(self.sample_doc, doc_path)
result = self.run_cli(doc_path)
shutil.rmtree(tempdir)
Expand Down
87 changes: 87 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import tempfile

import pytest

from dangerzone.document import DocumentFilenameException, DocumentHolder

from . import sample_doc


@pytest.fixture
def unwriteable_pdf():
file_path = tempfile.mktemp(suffix=".pdf")
with open(file_path, "w+"):
# create file
pass
os.chmod(file_path, mode=600)
return file_path


@pytest.fixture
def unreadable_pdf():
file_path = tempfile.mktemp(suffix=".pdf")
with open(file_path, "w+"):
# create file
pass
os.chmod(file_path, mode=0)
return file_path


def test_input_sample_init(sample_doc):
DocumentHolder(sample_doc)


def test_input_sample_after(sample_doc):
d = DocumentHolder()
d.input_filename = sample_doc


def test_input_file_none():
"""
Attempts to read a document's filename when no doc has been set
"""
d = DocumentHolder()
with pytest.raises(DocumentFilenameException):
d.input_filename


def test_input_file_non_existing():
with pytest.raises(DocumentFilenameException):
DocumentHolder("fake-dir/non-existing-file.pdf")


def test_input_file_unreadable(unreadable_pdf):
with pytest.raises(DocumentFilenameException):
DocumentHolder(unreadable_pdf)


def test_output_file_unwriteable(unwriteable_pdf):
d = DocumentHolder()
with pytest.raises(DocumentFilenameException):
d.output_filename = unwriteable_pdf


def test_output():
pdf_file = tempfile.mktemp(suffix=".pdf")
d = DocumentHolder()
d.output_filename = pdf_file


def test_output_file_none():
"""
Attempts to read a document's filename when no doc has been set
"""
d = DocumentHolder()
with pytest.raises(DocumentFilenameException):
d.output_filename(self, filename)


def test_output_file_not_pdf():
docx_file = tempfile.mktemp(suffix=".docx")
d = DocumentHolder()

with pytest.raises(DocumentFilenameException):
d.output_filename = docx_file

assert not os.path.exists(docx_file)

0 comments on commit dd2c1a9

Please sign in to comment.