refactoring: splitting conftest logic into submodules

This commit is contained in:
Matthias Bisping 2022-04-14 18:49:18 +02:00
parent 3a3ab81223
commit e0b9a3f32e
5 changed files with 78 additions and 72 deletions

View File

@ -9,7 +9,7 @@ from operator import itemgetter
import fpdf
import numpy as np
import pytest
from funcy import rcompose, merge
from funcy import rcompose
from image_prediction.exceptions import (
UnknownLabelFormat,
@ -24,16 +24,16 @@ from test.utils.generation.pdf import add_image, pdf_stream
from test.utils.label import map_labels
pytest_plugins = [
'test.fixtures.model',
'test.fixtures.model_store',
'test.fixtures.image',
'test.fixtures.input',
'test.fixtures.parameters',
'test.fixtures.label',
"test.fixtures.image",
"test.fixtures.input",
"test.fixtures.label",
"test.fixtures.metadata",
"test.fixtures.model",
"test.fixtures.model_store",
"test.fixtures.parameters",
]
@pytest.fixture(autouse=True)
def mute_logger():
logger = get_logger()
@ -124,53 +124,11 @@ def expected_predictions_mapped_and_formatted(expected_predictions_mapped):
return [{k.value: v for k, v in epm.items()} for epm in expected_predictions_mapped]
@pytest.fixture
def metadata(images, info_label_map):
page_idx = 0
def current_page_idx():
nonlocal page_idx
page_idx += random.randint(0, 3)
return min(page_idx, len(images) - 1)
def build_image_metadata(image):
width, height = image.size
page_width = 595
page_height = 842
x1 = random.randint(0, page_width - width)
x2 = x1 + width
y1 = random.randint(0, page_height - height)
y2 = y1 + height
metadata = {
info_label_map.PAGE_WIDTH: page_width,
info_label_map.PAGE_HEIGHT: page_height,
info_label_map.PAGE_IDX: current_page_idx(),
info_label_map.WIDTH: width,
info_label_map.HEIGHT: height,
info_label_map.X1: x1,
info_label_map.X2: x2,
info_label_map.Y1: y1,
info_label_map.Y2: y2,
info_label_map.ALPHA: image.mode == "RGBA",
}
return metadata
return list(map(build_image_metadata, images))
@pytest.fixture
def info_label_map():
return Info
@pytest.fixture
def metadata_formatted(metadata):
def format_metadata(metadata):
return {key.value: val for key, val in metadata.items()}
return list(map(format_metadata, metadata))
@pytest.fixture
def image_metadata_pairs(images, metadata):
return list(starmap(ImageMetadataPair, zip(images, metadata)))
@ -202,22 +160,3 @@ def real_expected_service_response():
def pipeline():
pipeline = load_pipeline(verbose=False)
return pipeline
def get_base_position_metadata(width, height, page_width, page_height):
return {
Info.WIDTH: width,
Info.HEIGHT: height,
Info.PAGE_IDX: 0,
Info.PAGE_WIDTH: page_width,
Info.PAGE_HEIGHT: page_height,
}
@pytest.fixture
def base_patch_metadata(width, height, page_width, page_height):
metadata = get_base_position_metadata(width, height, page_width, page_height)
metadata = merge(metadata, {Info.X1: 0, Info.Y1: 0, Info.X2: width, Info.Y2: height})
return metadata

56
test/fixtures/metadata.py vendored Normal file
View File

@ -0,0 +1,56 @@
import random
import pytest
from funcy import merge
from image_prediction.info import Info
from test.utils.metadata import get_base_position_metadata
@pytest.fixture
def metadata(images, info_label_map):
page_idx = 0
def current_page_idx():
nonlocal page_idx
page_idx += random.randint(0, 3)
return min(page_idx, len(images) - 1)
def build_image_metadata(image):
width, height = image.size
page_width = 595
page_height = 842
x1 = random.randint(0, page_width - width)
x2 = x1 + width
y1 = random.randint(0, page_height - height)
y2 = y1 + height
metadata = {
info_label_map.PAGE_WIDTH: page_width,
info_label_map.PAGE_HEIGHT: page_height,
info_label_map.PAGE_IDX: current_page_idx(),
info_label_map.WIDTH: width,
info_label_map.HEIGHT: height,
info_label_map.X1: x1,
info_label_map.X2: x2,
info_label_map.Y1: y1,
info_label_map.Y2: y2,
info_label_map.ALPHA: image.mode == "RGBA",
}
return metadata
return list(map(build_image_metadata, images))
@pytest.fixture
def metadata_formatted(metadata):
def format_metadata(metadata):
return {key.value: val for key, val in metadata.items()}
return list(map(format_metadata, metadata))
@pytest.fixture
def base_patch_metadata(width, height, page_width, page_height):
metadata = get_base_position_metadata(width, height, page_width, page_height)
metadata = merge(metadata, {Info.X1: 0, Info.Y1: 0, Info.X2: width, Info.Y2: height})
return metadata

View File

@ -12,7 +12,7 @@ from image_prediction.info import Info
from image_prediction.transformer.transformers.coordinate.fitz import FitzCoordinateTransformer
from image_prediction.transformer.transformers.coordinate.fpdf import FPDFCoordinateTransformer
from image_prediction.transformer.transformers.coordinate.pdfnet import PDFNetCoordinateTransformer
from test.conftest import get_base_position_metadata
from test.utils.metadata import get_base_position_metadata
from test.utils.generation.image import array_to_image
from test.utils.generation.pdf import add_image
from test.utils.comparison import transform_equal

View File

@ -30,9 +30,9 @@ from image_prediction.stitching.utils import (
make_coord_getter,
make_length_getter,
)
from test.utils.generation.pdf import add_image
from test.utils.generation.image import random_single_color_image_from_metadata, gray_image_from_metadata
from test.utils.comparison import images_equal
from test.utils.generation.image import random_single_color_image_from_metadata, gray_image_from_metadata
from test.utils.generation.pdf import add_image
from test.utils.stitching import BoxSplitter
x1_getter, y1_getter, x2_getter, y2_getter = map(make_coord_getter, ("x1", "y1", "x2", "y2"))

11
test/utils/metadata.py Normal file
View File

@ -0,0 +1,11 @@
from image_prediction.info import Info
def get_base_position_metadata(width, height, page_width, page_height):
return {
Info.WIDTH: width,
Info.HEIGHT: height,
Info.PAGE_IDX: 0,
Info.PAGE_WIDTH: page_width,
Info.PAGE_HEIGHT: page_height,
}