From fa479adfb006056d8ab853cd44f43a82ea4546a8 Mon Sep 17 00:00:00 2001 From: Isaac Riley Date: Tue, 15 Mar 2022 12:17:09 +0100 Subject: [PATCH] manually added tests from test branch to avoid major conflicts --- tests/test_layout_parsing.py | 28 +++++++++++++++++++++++++ tests/test_redaction_detection.py | 34 +++++++++++++++++++++++++++++++ tests/test_table_parsing.py | 10 +++++++++ 3 files changed, 72 insertions(+) create mode 100644 tests/test_layout_parsing.py create mode 100644 tests/test_redaction_detection.py diff --git a/tests/test_layout_parsing.py b/tests/test_layout_parsing.py new file mode 100644 index 0000000..49eca55 --- /dev/null +++ b/tests/test_layout_parsing.py @@ -0,0 +1,28 @@ +from vidocp.layout_parsing import parse_layout +import pytest +import numpy as np +import pdf2image + + +@pytest.fixture() +def rects(): + page_index = 0 + pdf_path = "/home/lillian/vidocp/tests/VV-313450.pdf" + page = pdf2image.convert_from_path(pdf_path, first_page=page_index + 1, last_page=page_index + 1)[0] + page = np.array(page) + rectangles = parse_layout(page) + return rectangles + + +def test_number_of_layout_boxes(rects): + assert len([[x] for x in rects]) == 53 + + +def test_layout_rects(rects): + for rect in rects: + x, y, w, h = rect + possible_xrange = (x, x + w) + possible_yrange = (y, y + h) + assert all(x <= possible_xrange[0] and x + w >= possible_xrange[1] for (x, y, w, h) in rects) + assert all(x <= possible_yrange[0] and x + w >= possible_yrange[1] for (x, y, w, h) in rects) + \ No newline at end of file diff --git a/tests/test_redaction_detection.py b/tests/test_redaction_detection.py new file mode 100644 index 0000000..eb10263 --- /dev/null +++ b/tests/test_redaction_detection.py @@ -0,0 +1,34 @@ +from vidocp.redaction_detection import find_redactions +import numpy as np +import pytest +import pdf2image + + +@pytest.fixture() +def image(): + page_index = 0 + pdf_path = "/home/lillian/vidocp/tests/CropBoxNotEqualToMediabox_redacted.pdf" + page = pdf2image.convert_from_path(pdf_path, first_page=page_index + 1, last_page=page_index + 1)[0] + page = np.array(page) + return page + + +@pytest.fixture() +def contours(image): + redaction_contours = find_redactions(image) + return redaction_contours + + +def test_number_of_redaction(contours): + assert len(list(contours)) == 1 + + +def test_is_black(image, contours): + for contour in contours: + cont = contour.tolist() + x1, y1 = min(cont)[0] + x2, y2 = max(cont)[0] + redaction = image[y1:y2, x1:x2] + for pixel in redaction: + assert pixel.all() < 10 + \ No newline at end of file diff --git a/tests/test_table_parsing.py b/tests/test_table_parsing.py index 221ce72..8ac4bc0 100644 --- a/tests/test_table_parsing.py +++ b/tests/test_table_parsing.py @@ -26,3 +26,13 @@ def test_range_of_rects(rects): assert topleft >= expected_range[0] assert bottomright <= expected_range[1] + + +def test_rects_in_range(rects): + x, y, w, h = min(rects) + minimum = (x, y) + x, y, w, h = max(rects) + maximum = (x + w, y + h) + for (x, y, w, h) in rects: + assert minimum <= (x, y) <= maximum + \ No newline at end of file