refactoring: skipping invalid parameter combinations
This commit is contained in:
parent
a1c292a485
commit
7adbdefb4e
3
test/fixtures/input.py
vendored
3
test/fixtures/input.py
vendored
@ -5,6 +5,7 @@ import pytest
|
||||
from PIL import Image
|
||||
from funcy import lmap, compose, flatten
|
||||
|
||||
from pyinfra.server.dispatcher.dispatcher import Nothing
|
||||
from pyinfra.server.utils import pack, unpack, normalize_item
|
||||
from pyinfra.utils.func import star, lift, lstarlift
|
||||
from test.utils.image import image_to_bytes
|
||||
@ -32,6 +33,8 @@ def input_data_items(item_type, n_items, pdf):
|
||||
|
||||
@pytest.fixture
|
||||
def targets(input_data_items, item_type, operation, metadata):
|
||||
if operation is Nothing:
|
||||
return Nothing
|
||||
op = compose(lift(star(pack)), normalize_item, operation)
|
||||
expected = lmap(unpack, flatten(starmap(op, zip(input_data_items, metadata))))
|
||||
return expected
|
||||
|
||||
23
test/fixtures/server.py
vendored
23
test/fixtures/server.py
vendored
@ -7,10 +7,11 @@ import fitz
|
||||
import pytest
|
||||
import requests
|
||||
from PIL import Image
|
||||
from funcy import retry, first, compose
|
||||
from funcy import retry, first, compose, identity
|
||||
from waitress import serve
|
||||
|
||||
from pyinfra.server.bufferizer.lazy_bufferizer import FlatStreamBuffer
|
||||
from pyinfra.server.dispatcher.dispatcher import Nothing
|
||||
from pyinfra.server.server import (
|
||||
set_up_processing_server,
|
||||
make_streamable_and_wrap_in_packing_logic,
|
||||
@ -66,9 +67,15 @@ def operation(core_operation):
|
||||
return zip(result, metadata)
|
||||
else:
|
||||
return result, metadata
|
||||
if core_operation is Nothing:
|
||||
return Nothing
|
||||
return op
|
||||
|
||||
|
||||
class InvalidParameterCombination(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def core_operation(item_type, one_to_many):
|
||||
def upper(string: bytes):
|
||||
@ -88,9 +95,19 @@ def core_operation(item_type, one_to_many):
|
||||
yield f"page_{i}".encode()
|
||||
|
||||
try:
|
||||
return {"string": duplicate if one_to_many else upper, "image": rotate, "pdf": stream_pages}[item_type]
|
||||
d = {
|
||||
False: {
|
||||
"string": upper,
|
||||
"image": rotate,
|
||||
},
|
||||
True: {
|
||||
"string": duplicate,
|
||||
"pdf": stream_pages,
|
||||
},
|
||||
}
|
||||
return d[one_to_many][item_type]
|
||||
except KeyError:
|
||||
raise ValueError(f"No operation specified for item type {item_type}")
|
||||
return Nothing
|
||||
|
||||
|
||||
@pytest.fixture(params=["pdf", "string", "image"])
|
||||
|
||||
@ -2,6 +2,7 @@ import pytest
|
||||
from funcy import rcompose, lmap
|
||||
|
||||
from pyinfra.server.client_pipeline import ClientPipeline
|
||||
from pyinfra.server.dispatcher.dispatcher import Nothing
|
||||
from pyinfra.server.dispatcher.dispatchers.forwarding import ForwardingDispatcher
|
||||
from pyinfra.server.dispatcher.dispatchers.rest import RestDispatcher
|
||||
from pyinfra.server.interpreter.interpreters.identity import IdentityInterpreter
|
||||
@ -11,6 +12,7 @@ from pyinfra.server.receiver.receivers.identity import IdentityReceiver
|
||||
from pyinfra.server.receiver.receivers.rest import RestReceiver
|
||||
from pyinfra.server.utils import unpack
|
||||
from pyinfra.utils.func import lift
|
||||
from test.fixtures.server import InvalidParameterCombination
|
||||
|
||||
|
||||
def test_mock_pipeline():
|
||||
@ -24,6 +26,17 @@ def test_mock_pipeline():
|
||||
assert list(pipeline(data)) == list(rcompose(f, g, h, u)(data))
|
||||
|
||||
|
||||
def pass_invalid_combination(fn):
|
||||
def inner(*args, **kwargs):
|
||||
try:
|
||||
return fn(*args, **kwargs)
|
||||
except InvalidParameterCombination:
|
||||
pass
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
# @pass_invalid_combination
|
||||
@pytest.mark.parametrize(
|
||||
"client_pipeline_type",
|
||||
[
|
||||
@ -31,7 +44,9 @@ def test_mock_pipeline():
|
||||
# "basic",
|
||||
],
|
||||
)
|
||||
def test_pipeline(client_pipeline, input_data_items, metadata, targets):
|
||||
def test_pipeline(core_operation, client_pipeline, input_data_items, metadata, targets, item_type, one_to_many):
|
||||
if targets is Nothing:
|
||||
pytest.skip(f"invalid parameter combination: {item_type=}, {one_to_many=}")
|
||||
output = client_pipeline(input_data_items, metadata)
|
||||
output = lmap(unpack, output)
|
||||
assert output == targets
|
||||
@ -75,7 +90,9 @@ def rest_client_pipeline(server_process, endpoint, rest_interpreter):
|
||||
|
||||
@pytest.fixture
|
||||
def basic_client_pipeline(endpoint, rest_interpreter, server_stream_function):
|
||||
return ClientPipeline(RestPacker(), ForwardingDispatcher(server_stream_function), IdentityReceiver(), IdentityInterpreter())
|
||||
return ClientPipeline(
|
||||
RestPacker(), ForwardingDispatcher(server_stream_function), IdentityReceiver(), IdentityInterpreter()
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user