refactoring: splitting source data from encoded data in data fixture
This commit is contained in:
parent
7b998cdaf6
commit
c944cdb1a7
53
test/fixtures/input.py
vendored
53
test/fixtures/input.py
vendored
@ -10,6 +10,7 @@ from pyinfra.server.normalization import normalize_item
|
||||
from pyinfra.server.packing import pack, unpack
|
||||
from pyinfra.utils.func import star, lift, lstarlift
|
||||
from test.utils.image import image_to_bytes
|
||||
from test.utils.pdf import pdf_stream
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -21,17 +22,43 @@ def data(data_type, pdf):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def input_data_items(item_type, n_items, pdf):
|
||||
def input_data_items(unencoded_input_data, input_data_encoder):
|
||||
return input_data_encoder(unencoded_input_data)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def unencoded_input_data(item_type, unencoded_strings, unencoded_images, unencoded_pdfs):
|
||||
if item_type == "string":
|
||||
return [bytes(f"content{i}", encoding="utf8") for i in range(n_items)]
|
||||
return unencoded_strings
|
||||
elif item_type == "image":
|
||||
return images(n_items)
|
||||
return unencoded_images
|
||||
elif item_type == "pdf":
|
||||
return [pdf] * n_items
|
||||
return unencoded_pdfs
|
||||
else:
|
||||
raise ValueError(f"Unknown item type {item_type}")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def input_data_encoder(item_type):
|
||||
if item_type == "string":
|
||||
return strings_to_bytes
|
||||
elif item_type == "image":
|
||||
return images_to_bytes
|
||||
elif item_type == "pdf":
|
||||
return pdfs_to_bytes
|
||||
else:
|
||||
raise ValueError(f"Unknown item type {item_type}")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def unencoded_pdfs(n_items, unencoded_pdf):
|
||||
return [unencoded_pdf] * n_items
|
||||
|
||||
|
||||
def pdfs_to_bytes(unencoded_pdfs):
|
||||
return [pdf_stream(pdf) for pdf in unencoded_pdfs]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def target_data_items(input_data_items, core_operation):
|
||||
|
||||
@ -43,6 +70,15 @@ def target_data_items(input_data_items, core_operation):
|
||||
return expected
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def unencoded_strings(n_items):
|
||||
return [f"content{i}" for i in range(n_items)]
|
||||
|
||||
|
||||
def strings_to_bytes(strings):
|
||||
return [bytes(s, encoding="utf8") for s in strings]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def targets(input_data_items, operation, metadata):
|
||||
|
||||
@ -87,8 +123,13 @@ def input_batch(n_items):
|
||||
return np.random.random_sample(size=(n_items, 3, 30, 30))
|
||||
|
||||
|
||||
def images(n_items):
|
||||
return lmap(compose(image_to_bytes, array_to_image), input_batch(n_items))
|
||||
@pytest.fixture
|
||||
def unencoded_images(n_items):
|
||||
return lmap(array_to_image, input_batch(n_items))
|
||||
|
||||
|
||||
def images_to_bytes(images):
|
||||
return lmap(image_to_bytes, images)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
6
test/fixtures/pdf.py
vendored
6
test/fixtures/pdf.py
vendored
@ -1,13 +1,11 @@
|
||||
import fpdf
|
||||
import pytest
|
||||
|
||||
from test.utils.pdf import pdf_stream
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pdf(n_pages):
|
||||
def unencoded_pdf(n_pages):
|
||||
pdf = fpdf.FPDF(unit="pt")
|
||||
for _ in range(n_pages):
|
||||
pdf.add_page()
|
||||
|
||||
return pdf_stream(pdf)
|
||||
return pdf
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user