Skip to content

Commit

Permalink
Added more tests code coverage at 89%
Browse files Browse the repository at this point in the history
  • Loading branch information
amaank404 committed Oct 25, 2024
1 parent 31a392e commit 1db0905
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 6 deletions.
28 changes: 24 additions & 4 deletions tests/data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# pragma: no cover

import logging
import os
import shutil
Expand Down Expand Up @@ -68,14 +70,26 @@ def getfile(url, file):
print("- Sample PDF data")
for x in tqdm.tqdm(d["data"]):
fpath = x["path"]
getfile(
repo.get_contents(fpath).download_url,
root / "cache" / "pdf" / Path(fpath).name,
)
if "password" not in Path(fpath).name:
getfile(
repo.get_contents(fpath).download_url,
root / "cache" / "pdf" / Path(fpath).name,
)

datefile.write_text(str(int(time.time())), "utf-8")


def _batched(iter, batch_size):
batch = []
for x in iter:
batch.append(x)
if len(batch) > batch_size:
yield batch
batch = []
if batch:
yield batch


class TestData:
def __init__(self, data_folder):
self.data_folder = Path(data_folder)
Expand All @@ -91,5 +105,11 @@ def __init__(self, data_folder):
def filter_extension(self, extensions):
return filter(lambda x: x.suffix.strip(".") in extensions, self.files)

def filter_extension_batched(self, extensions, batch_size=10):
return _batched(self.filter_extension(extensions), batch_size)

def batched(self, batch_size=10):
return _batched(self.files, batch_size)


dataset = TestData(root)
61 changes: 61 additions & 0 deletions tests/test_get_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import logging

import data
import pytest

import homepress.renderer
from homepress.renderer import get_renderer

# Uncomment this if u wish to run the code
# at test_get_renderer_files_unsupported_with_ignore_errors
#
# def _flatten(e, d=None):
# if d is None:
# d = []
# for x in e:
# if hasattr(x, "renderers"):
# _flatten(x.renderers, d)
# elif hasattr(x, "renderer"):
# _flatten([x.renderer], d)
# else:
# d.append(x)
#
# return d


def test_get_renderer_files():
get_renderer(list(data.dataset.filter_extension(["pdf", "svg", "xps"])))


def test_get_renderer_files_unsupported():
with pytest.raises(TypeError):
get_renderer(list(data.dataset.filter_extension(["bin"])))


def test_get_renderer_files_unsupported_with_ignore_errors():
a = get_renderer([data.root], True)
b = get_renderer(list(data.dataset.filter_extension(homepress.renderer.formats)))

# Code below was used to find differences as to where the file might've been and where it might've not been
# taken up by the get_renderer method

# sortbyfname = lambda x: str(x.file.resolve())
# sortbyfname2 = lambda x: str(x.file.resolve().name)

# a_flat = _flatten(a.renderers)
# b_flat = _flatten(b.renderers)

# a_flat.sort(key=sortbyfname)
# b_flat.sort(key=sortbyfname)

# a_n = [sortbyfname2(x) for x in a_flat]
# b_n = [sortbyfname2(x) for x in b_flat]

# for x, y in zip(a_n, b_n):
# logging.warning(f"{x} {y}")

assert len(a) == len(b)


def test_get_renderer_files_folder():
assert len(get_renderer([data.root / "testdata" / "filesamples.com"])) != 0
26 changes: 26 additions & 0 deletions tests/test_multi_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from test_renderer import TestRenderer

from homepress.renderer.multi_renderer import MultiRenderer


@pytest.fixture(scope="module")
def multi_renderer():
return MultiRenderer([TestRenderer() for _ in range(10)])


def test_multi_renderer_out_of_range(multi_renderer: MultiRenderer):
with pytest.raises(IndexError):
multi_renderer.render(17, (10, 10))


def test_multi_renderer_in_range(multi_renderer: MultiRenderer):
assert "RES" == multi_renderer.render(8, (10, 10))


def test_multi_renderer_preview(multi_renderer: MultiRenderer):
assert "RESPREV" == multi_renderer.render_preview(7)


def test_multi_renderer_get_text(multi_renderer: MultiRenderer):
assert TestRenderer().get_text(0) == multi_renderer.get_text(1)
33 changes: 33 additions & 0 deletions tests/test_page_range_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from test_renderer import TestRenderer

from homepress.renderer import MultiRenderer
from homepress.renderer.page_range_renderer import PageRangeRenderer


@pytest.fixture(scope="module")
def page_renderer():
return PageRangeRenderer(
MultiRenderer([TestRenderer(render_res=x) for x in range(10)]), range(2, 8)
)


def test_page_renderer_out_of_range(page_renderer: PageRangeRenderer):
with pytest.raises(IndexError):
page_renderer.render(6, (10, 10))


def test_page_renderer_in_range(page_renderer: PageRangeRenderer):
assert 7 == page_renderer.render(5, (10, 10))


def test_page_renderer_preview(page_renderer: PageRangeRenderer):
assert "RESPREV" == page_renderer.render_preview(2)


def test_page_renderer_get_text(page_renderer: PageRangeRenderer):
assert TestRenderer().get_text(0) == page_renderer.get_text(1)


def test_page_renderer_len(page_renderer: PageRangeRenderer):
assert 6 == len(page_renderer)
100 changes: 100 additions & 0 deletions tests/test_press.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import logging
from pathlib import Path

import data
import pytest

import homepress.renderer
from homepress import Press


@pytest.fixture(
scope="module",
params=list(data.dataset.filter_extension_batched(homepress.renderer.formats, 10)),
)
def press_10(request):
return Press(request.param)


@pytest.fixture(
scope="module",
params=list(data.dataset.filter_extension_batched(homepress.renderer.formats, 10)),
)
def press_30(request):
return Press(request.param)


def test_press_text(press_10: Press):
assert isinstance(press_10.text(), list)


def test_press_images_png(press_10: Press, tmpdir: Path):
press_10.images(
tmpdir, format="png", file_prefix="test_png_image", resolution=(64, 64)
)


def test_press_images_webp(press_10: Press, tmpdir: Path):
press_10.images(
tmpdir, format="webp", file_prefix="test_webp_image", resolution=(64, 64)
)


def test_merge(press_10: Press, tmpdir: Path):
press_10.merge(tmpdir / "merge_test.pdf", resolution=(64, 64))


@pytest.mark.parametrize(
"size,margin,ppi,rtl,flip_even",
[
("A4", [0], 10, False, False),
("Legal", [2], 10, True, False),
("Ledger", [4, 3], 10, False, True),
("Letter", [0, 2, 3], 10, True, True),
((1 / 2, 5), [0, 4, 5, 6], 10, False, False),
],
)
def test_midpage(press_10: Press, tmpdir: Path, size, margin, ppi, rtl, flip_even):
press_10.midpage(
tmpdir / "midpage_test.pdf",
size=size,
margin=margin,
ppi=ppi,
rtl=rtl,
flip_even=flip_even,
)


@pytest.mark.parametrize(
"size,margin,ppi,rtl,flip_even,separate_stacks,stack_prefix,stack_size",
[
("A4", [0], 10, False, False, False, "stack_", 12),
("Legal", [2], 10, True, False, True, "stack_", 12),
("Ledger", [4, 3], 10, False, True, True, "st12_", 12),
("Letter", [0, 2, 3], 10, True, True, False, "", 16),
((1 / 2, 5), [0, 4, 5, 6], 10, False, False, True, "_p", 4),
],
)
def test_midpage_multi(
press_30: Press,
tmpdir: Path,
size,
margin,
ppi,
rtl,
flip_even,
separate_stacks,
stack_prefix,
stack_size,
):
press_30.midpage_multi(
tmpdir / "midpage_test.pdf",
size=size,
margin=margin,
ppi=ppi,
rtl=rtl,
flip_even=flip_even,
separate_stacks=separate_stacks,
stack_prefix=stack_prefix,
stack_size=stack_size,
)
29 changes: 29 additions & 0 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from homepress.renderer import Renderer


class TestRenderer(Renderer):
supported_extensions = []

def __init__(
self,
len=1,
render_res="RES",
render_preview_res="RESPREV",
get_text_res="The quick brown fox jumps tonsss",
):
self.len = len
self.render_res = render_res
self.get_text_res = get_text_res
self.render_preview_res = render_preview_res

def __len__(self):
return self.len

def render(self, page, res):
return self.render_res

def render_preview(self, page):
return self.render_preview_res

def get_text(self, page):
return self.get_text_res
1 change: 0 additions & 1 deletion tests/test_renderer_mupdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ def test_file_exists():
renderer = MuPDFRenderer(
"asbsladoibibqwiogriobibhgioiovhaoivhgiovhgiovhari.pdf"
)
renderer.render(1, (1000, 1000))
1 change: 0 additions & 1 deletion tests/test_renderer_pil.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ def test_out_of_range(pil_renderer: PILRenderer):
def test_file_exists():
with pytest.raises(FileNotFoundError):
renderer = PILRenderer("asbsladoibibqwiogriobibhgioiovhaoivhgiovhgiovhari.png")
renderer.render(1, (1000, 1000))
Empty file added tests/testdata/unsupported.bin
Empty file.

0 comments on commit 1db0905

Please sign in to comment.