changed operation signature to return iterables for 1 -> n compatibility

This commit is contained in:
Matthias Bisping 2022-04-29 13:34:27 +02:00
parent 2550a0eff2
commit 23070f3480
2 changed files with 15 additions and 8 deletions

View File

@ -1,8 +1,10 @@
from itertools import chain
from operator import itemgetter
from funcy import compose
from funcy import compose, first, rcompose
from more_itertools import flatten
from pyinfra.utils.func import star, lift, lstarlift
from pyinfra.utils.func import star, lift, lstarlift, starlift
from test.utils.server import bytes_to_string, string_to_bytes
@ -22,8 +24,13 @@ def bundle(data: bytes, metadata: dict):
def unpack_op_pack(operation):
return compose(star(pack), star(operation), unpack)
return compose(first, lstarlift(pack), star(operation), unpack)
def unpack_batchop_pack(operation):
return compose(lstarlift(pack), operation, lift(unpack))
return rcompose(
lift(unpack), # unpack the buffer items
operation, # apply operation on unpacked items
flatten, # operations may be 1 -> 1, 1 -> n or n -> 1, hence flatten
lstarlift(pack),
)

View File

@ -6,13 +6,13 @@ from typing import Iterable
import pytest
import requests
from funcy import rcompose, compose, rpartial, identity, lmap, ilen
from funcy import rcompose, compose, rpartial, identity, lmap, ilen, first
from pyinfra.rest import pack, unpack, bundle
from pyinfra.utils.func import lift, starlift, parallel_map, star
from pyinfra.utils.func import lift, starlift, parallel_map, star, lstarlift
logger = logging.getLogger("PIL.PngImagePlugin")
logger.setLevel(logging.INFO)
logger.setLevel(logging.WARNING)
def dispatch_methods(input_data):
@ -44,7 +44,7 @@ def post_partial(url, input_data: Iterable[bytes], metadata):
@pytest.mark.parametrize("item_type", ["string", "image"])
def test_sending_partial_request(url, data_items, metadata, operation, server_process):
expected = lmap(compose(star(bundle), partial(operation, metadata=metadata)), data_items)
expected = lmap(compose(first, lstarlift(bundle), partial(operation, metadata=metadata)), data_items)
output = list(post_partial(f"{url}/process", data_items, metadata))
assert output == expected