Commit dca12e1d authored by Jamy Mahabier's avatar Jamy Mahabier Committed by Justin van der Krieken

Fix imports in tests

parent 9e79acab
import pytest
import numpy as np
from zesje.helpers import image_helper
from zesje import scans
# Tests
......@@ -21,7 +21,7 @@ def test_runtime_error_check_corner_marker(keypoints_input, error_expected):
image_data = np.zeros((1000, 1000))
if error_expected:
with pytest.raises(RuntimeError):
image_helper.check_corner_keypoints(image_data, keypoints_input)
scans.check_corner_keypoints(image_data, keypoints_input)
else:
image_helper.check_corner_keypoints(image_data, keypoints_input)
scans.check_corner_keypoints(image_data, keypoints_input)
assert True
......@@ -2,7 +2,7 @@ import os
import pytest
from zesje.helpers import scan_helper, image_helper
from zesje import scans, images
# Mocks
......@@ -12,8 +12,7 @@ from zesje.helpers import scan_helper, image_helper
def mock_get_box_return_original(monkeypatch, datadir):
def mock_return(image, widget, padding):
return image
monkeypatch.setattr(image_helper, 'get_box',
mock_return)
monkeypatch.setattr(images, 'get_box', mock_return)
# Tests
......@@ -36,5 +35,5 @@ def test_get_studentnumber_precision(datadir, mock_get_box_return_original):
im_path = os.path.join(datadir, 'studentnumbers', f'{filename_full}')
filename_short, _ = os.path.splitext(filename_full)
expected_number = int(filename_short)
detected_number = scan_helper.get_student_number(im_path, MockExamConfig)
detected_number = scans.get_student_number(im_path, MockExamConfig)
assert(expected_number == detected_number)
......@@ -2,7 +2,7 @@ import random
import pytest
from zesje.models import Exam, _generate_exam_token
from zesje.database import Exam, _generate_exam_token
# I couldn't figure out how to make the mock return True on the first call and False on the second call. Therefore,
......
......@@ -10,7 +10,7 @@ from reportlab.pdfgen.canvas import Canvas as RLCanvas
from wand.color import Color as WandColor
from wand.image import Image as WandImage
from zesje.helpers import pdf_generation_helper
from zesje import pdf_generation
# Mock fixtures #
......@@ -20,8 +20,7 @@ from zesje.helpers import pdf_generation_helper
def mock_generate_datamatrix(monkeypatch, datadir):
def mock_return(exam_id, page_num, copy_num):
return PIL.Image.open(os.path.join(datadir, 'datamatrix.png'))
monkeypatch.setattr(pdf_generation_helper, 'generate_datamatrix',
mock_return)
monkeypatch.setattr(pdf_generation, 'generate_datamatrix', mock_return)
@pytest.fixture
......@@ -29,7 +28,7 @@ def mock_generate_id_grid(monkeypatch):
def mock_return(canv, x, y):
canv.setFont('Helvetica', 14)
canv.drawString(x * mm, y * mm, 'Beautiful ID Grid')
monkeypatch.setattr(pdf_generation_helper, 'generate_id_grid', mock_return)
monkeypatch.setattr(pdf_generation, 'generate_id_grid', mock_return)
# Utility methods #
......@@ -72,7 +71,7 @@ def test_add_corner_markers_and_bottom_bar(datadir, tmpdir, pagesize, name):
pdf_filename = os.path.join(tmpdir, 'file.pdf')
canv = RLCanvas(pdf_filename, pagesize=pagesize)
pdf_generation_helper._add_corner_markers_and_bottom_bar(canv, pagesize)
pdf_generation._add_corner_markers_and_bottom_bar(canv, pagesize)
canv.save()
image_filename = os.path.join(datadir, 'cornermarkers', f'{name}.png')
......@@ -84,7 +83,7 @@ def test_generate_id_grid(datadir, tmpdir):
pdf_filename = os.path.join(tmpdir, 'file.pdf')
canv = RLCanvas(pdf_filename, pagesize=A4)
pdf_generation_helper.generate_id_grid(canv, 0, 0)
pdf_generation.generate_id_grid(canv, 0, 0)
canv.save()
image_filename = os.path.join(datadir, 'idwidgettest-1.png')
......@@ -102,8 +101,7 @@ def test_generate_overlay(mock_generate_datamatrix, mock_generate_id_grid,
filename = os.path.join(str(tmpdir), 'file.pdf')
canv = RLCanvas(filename, pagesize=pagesize)
pdf_generation_helper._generate_overlay(canv, pagesize, 'ABCDEFGHIJKL', 1,
2, 25, 150, 125, 150)
pdf_generation._generate_overlay(canv, pagesize, 'ABCDEFGHIJKL', 1, 2, 25, 150, 125, 150)
canv.save()
img_filenames = [os.path.join(datadir, 'overlays', f'{name}-{i}.png')
......@@ -117,8 +115,7 @@ def test_generate_pdfs_num_files(datadir, tmpdir):
num_copies = 3
pdf_generation_helper.generate_pdfs(blank_pdf, 'ABCDEFGHIJKL', str(tmpdir),
num_copies, 25, 270, 150, 270)
pdf_generation.generate_pdfs(blank_pdf, 'ABCDEFGHIJKL', str(tmpdir), num_copies, 25, 270, 150, 270)
assert len(tmpdir.listdir()) == num_copies
......@@ -131,8 +128,7 @@ def test_generate_pdfs_blank(mock_generate_datamatrix, mock_generate_id_grid,
datadir, tmpdir, pagesize, name):
blank_pdf = os.path.join(datadir, f'blank-{name}-2pages.pdf')
pdf_generation_helper.generate_pdfs(blank_pdf, 'ABCDEFGHIJKL', str(tmpdir),
2, 25, 150, 125, 150)
pdf_generation.generate_pdfs(blank_pdf, 'ABCDEFGHIJKL', str(tmpdir), 2, 25, 150, 125, 150)
img_filenames = [os.path.join(datadir, 'overlays', f'{name}-{i}.png')
for i in [0, 1]]
......@@ -146,8 +142,7 @@ def test_generate_pdfs_nonblank(mock_generate_datamatrix, mock_generate_id_grid,
datadir, tmpdir):
exam_pdf = os.path.join(datadir, 'exam-2pages.pdf')
pdf_generation_helper.generate_pdfs(exam_pdf, 'ABCDEFGHIJKL', str(tmpdir),
2, 25, 150, 125, 150)
pdf_generation.generate_pdfs(exam_pdf, 'ABCDEFGHIJKL', str(tmpdir), 2, 25, 150, 125, 150)
img_filenames = [os.path.join(datadir, f'generated-{i}.png')
for i in [0, 1]]
......@@ -160,8 +155,7 @@ def test_generate_pdfs_black(mock_generate_datamatrix, mock_generate_id_grid,
datadir, tmpdir):
black_pdf = os.path.join(datadir, 'black-a4-2pages.pdf')
pdf_generation_helper.generate_pdfs(black_pdf, 'ABCDEFGHIJKL', str(tmpdir),
2, 25, 150, 125, 150)
pdf_generation.generate_pdfs(black_pdf, 'ABCDEFGHIJKL', str(tmpdir), 2, 25, 150, 125, 150)
images = [PIL.Image.open(os.path.join(datadir, 'generated-black.png'))] * 2
for pdf_name in ['00000.pdf', '00001.pdf']:
......@@ -171,8 +165,7 @@ def test_generate_pdfs_black(mock_generate_datamatrix, mock_generate_id_grid,
def test_generate_pdfs_exam_is_file(mock_generate_datamatrix, mock_generate_id_grid, datadir, tmpdir):
blank_pdf = open(os.path.join(datadir, f'blank-a4-2pages.pdf'), 'rb')
pdf_generation_helper.generate_pdfs(blank_pdf, 'ABCDEFGHIJKL', str(tmpdir),
2, 25, 150, 125, 150)
pdf_generation.generate_pdfs(blank_pdf, 'ABCDEFGHIJKL', str(tmpdir), 2, 25, 150, 125, 150)
img_filenames = [os.path.join(datadir, 'overlays', f'a4-{i}.png') for i in [0, 1]]
images = [PIL.Image.open(x) for x in img_filenames]
......@@ -188,7 +181,7 @@ def test_join_pdfs(mock_generate_datamatrix, mock_generate_id_grid,
out = os.path.join(tmpdir, 'out.pdf')
num_copies = 2
pdf_generation_helper.join_pdfs(directory, out, num_copies)
pdf_generation.join_pdfs(directory, out, num_copies)
image_filename = os.path.join(datadir, f'blank-{name}.png')
images = [PIL.Image.open(image_filename)] * 4
......
......@@ -6,7 +6,7 @@ import numpy as np
import PIL
import pytest
from zesje.helpers import image_helper
from zesje import scans
# Helper functions
......@@ -36,8 +36,7 @@ def generate_binary_image(name, datadir):
ids=['Same horizontal line', 'Same vertical line', 'Negative angle',
'Positive angle'])
def test_calc_angle(test_input1, test_input2, expected):
assert math.isclose(image_helper.calc_angle(test_input1, test_input2),
expected, abs_tol=0.1)
assert math.isclose(scans.calc_angle(test_input1, test_input2), expected, abs_tol=0.1)
# Tests whether the amount of cornermakers is enough to calculate the angle and
......@@ -49,7 +48,7 @@ def test_calc_angle(test_input1, test_input2, expected):
os.path.join('tests', 'data', 'scanned_pdfs')))
def test_detect_enough_cornermarkers(name, datadir):
bin_im = generate_binary_image(name, datadir)
keypoints = image_helper.find_corner_marker_keypoints(bin_im)
keypoints = scans.find_corner_marker_keypoints(bin_im)
assert(len(keypoints) >= 2 & len(keypoints) <= 4)
......@@ -64,7 +63,7 @@ def test_detect_enough_cornermarkers(name, datadir):
os.path.join('tests', 'data', 'scanned_pdfs')))
def test_detect_valid_cornermarkers(name, datadir):
bin_im = generate_binary_image(name, datadir)
keypoints = image_helper.find_corner_marker_keypoints(bin_im)
keypoints = scans.find_corner_marker_keypoints(bin_im)
h, w, *_ = bin_im.shape
(xmm, ymm) = (210, 297)
......
import pytest
from PIL import Image
import os
from pylibdmtx.pylibdmtx import decode
from zesje.helpers.scan_helper import decode_barcode, ExamMetadata, ExtractedBarcode
from zesje.helpers import image_helper
from zesje.scans import decode_barcode, ExamMetadata, ExtractedBarcode
from zesje import images
# Returns the original image instead of retrieving a box from it
......@@ -11,8 +10,7 @@ from zesje.helpers import image_helper
def mock_get_box_return_original(monkeypatch, datadir):
def mock_return(image, widget, padding):
return image
monkeypatch.setattr(image_helper, 'get_box',
mock_return)
monkeypatch.setattr(images, 'get_box', mock_return)
# Tests whether the output of calc angle is correct
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment