From 8c5a46f84415c1412a15bf2376a6e1b646231601 Mon Sep 17 00:00:00 2001 From: Nemo Date: Sun, 4 Jul 2021 01:46:56 +0530 Subject: [PATCH] Adds initial tests --- setup.cfg | 6 ++-- tests/{min.md => book-min.md} | 0 tests/{title.md => book-title.md} | 0 tests/test_integration.py | 49 +++++++++++++++++++++++++++++++ tests/test_skeleton.py | 25 ---------------- 5 files changed, 52 insertions(+), 28 deletions(-) rename tests/{min.md => book-min.md} (100%) rename tests/{title.md => book-title.md} (100%) create mode 100644 tests/test_integration.py delete mode 100644 tests/test_skeleton.py diff --git a/setup.cfg b/setup.cfg index 3576488..fde2ac6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -82,9 +82,9 @@ console_scripts = # in order to write a coverage file that can be read by Jenkins. # CAUTION: --cov flags may prohibit setting breakpoints while debugging. # Comment those flags to avoid this py.test issue. -addopts = - --cov pystitcher --cov-report term-missing - --verbose +addopts = --verbose + # --cov pystitcher --cov-report term-missing + norecursedirs = dist build diff --git a/tests/min.md b/tests/book-min.md similarity index 100% rename from tests/min.md rename to tests/book-min.md diff --git a/tests/title.md b/tests/book-title.md similarity index 100% rename from tests/title.md rename to tests/book-title.md diff --git a/tests/test_integration.py b/tests/test_integration.py new file mode 100644 index 0000000..19e4c9f --- /dev/null +++ b/tests/test_integration.py @@ -0,0 +1,49 @@ +import pytest +import os +import PyPDF3 +from pystitcher.stitcher import Stitcher +from itertools import chain + +ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) + "/../" + +TEST_DATA = [ + ("clean",6, [('Super Potato Book', 0), ('Volume 1', 0), ('Part 1', 0), ('Volume 2', 3), ('Part 2', 3)]), + ("keep",6, [('Super Potato Book', 0), ('Volume 1', 0), ('Part 1', 0), ('Chapter 1', 0), ('Chapter 2', 1), ('Scene 1', 1), ('Scene 2', 2), ('Volume 2', 3), ('Part 3', 3), ('Chapter 3', 3), ('Chapter 4', 4), ('Scene 3', 4), ('Scene 4', 5)]), + ("flatten", 6, [('Super Potato Book', 0), ('Volume 1', 0), ('Part 1', 0), ('Chapter 1', 0), ('Chapter 2', 1), ('Scene 1', 1), ('Scene 2', 2), ('Volume 2', 3), ('Part 3', 3), ('Chapter 3', 3), ('Chapter 4', 4), ('Scene 3', 4), ('Scene 4', 5)]), + ("rotate", 9, [('Super Potato Book', 0), ('Volume 1', 0), ('Part 1', 0), ('Volume 2', 3), ('Part 2', 3), ('Volume 3', 6), ('Part 3', 6)]), + ("min",3, [('Part 1', 0), ('Chapter 1', 0), ('Chapter 2', 1), ('Scene 1', 1), ('Scene 2', 2)]) +] + +def pdf_name(name): + return "tests/%s.pdf" % name + +def render(name): + input_file = open("tests/book-%s.md" % name, 'r') + output_file = "%s.pdf" % name + stitcher = Stitcher(input_file) + stitcher.generate(output_file, True) + # Switch back to main directory + os.chdir(ROOT_DIR) + return pdf_name(name) + +def flatten_bookmarks(bookmarks): + """Given a list, possibly nested to any level, return it flattened.""" + output = [] + for destination in bookmarks: + if type(destination) == type([]): + output.extend(flatten_bookmarks(destination)) + else: + output.append(destination) + return output + +def get_all_bookmarks(pdf): + """ Returns a list of all flattened bookmarks with title and page number in a PDF file""" + return [(d['/Title'], pdf.getDestinationPageNumber(d)) for d in flatten_bookmarks(pdf.getOutlines())] + +@pytest.mark.parametrize("name,pages,bookmarks", TEST_DATA) +def test_book(name, pages, bookmarks): + output_file = render(name) + pdf = PyPDF3.PdfFileReader(output_file) + assert pdf.getNumPages() == pages + assert get_all_bookmarks(pdf) == bookmarks + diff --git a/tests/test_skeleton.py b/tests/test_skeleton.py deleted file mode 100644 index 3470f08..0000000 --- a/tests/test_skeleton.py +++ /dev/null @@ -1,25 +0,0 @@ -import pytest - -from pystitcher.skeleton import fib, main - -__author__ = "Nemo" -__copyright__ = "Nemo" -__license__ = "MIT" - - -def test_fib(): - """API Tests""" - assert fib(1) == 1 - assert fib(2) == 1 - assert fib(7) == 13 - with pytest.raises(AssertionError): - fib(-10) - - -def test_main(capsys): - """CLI Tests""" - # capsys is a pytest fixture that allows asserts agains stdout/stderr - # https://docs.pytest.org/en/stable/capture.html - main(["7"]) - captured = capsys.readouterr() - assert "The 7-th Fibonacci number is 13" in captured.out