Skip to content

Commit

Permalink
parameterize resolve fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
arahlin committed Sep 11, 2024
1 parent 3d4ca88 commit 244c3f0
Showing 1 changed file with 46 additions and 74 deletions.
120 changes: 46 additions & 74 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,29 @@ def test_cpp_enum_orjson_serialize():
# tuber.py tests
#

ACCEPT_TYPES = [
[
ACCEPT_TYPES = {
"json": [
"application/json",
],
[
"cbor": [
"application/cbor",
],
[
"both": [
"application/json",
"application/cbor",
],
]
}


@pytest.fixture(scope="module", params=list(ACCEPT_TYPES))
def accept_types(request):
return ACCEPT_TYPES[request.param]

@pytest.fixture(scope="module")
def resolve(tuberd_host):
async def resolver(objname=None, accept_types=None, simple=None):
if simple:

@pytest.fixture(scope="module", params=["simple", "async"])
def resolve(request, tuberd_host, accept_types):
async def resolver(objname=None):
if request.param == "simple":
return tuber.resolve_simple(tuberd_host, objname, accept_types)
else:
return await tuber.resolve(tuberd_host, objname, accept_types)
Expand Down Expand Up @@ -351,56 +356,46 @@ async def tuber_result(res):
return res


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_hello(accept_types, simple, resolve):
s = await resolve("Wrapper", accept_types, simple)
async def test_tuberpy_hello(resolve):
s = await resolve("Wrapper")
x = await tuber_result(s.increment([1, 2, 3, 4, 5]))
assert x == [2, 3, 4, 5, 6]


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_dir(accept_types, simple, resolve):
async def test_tuberpy_dir(resolve):
"""Ensure embedded methods end up in dir() of objects.
This is a crude proxy for the ability to tab-complete."""
s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")
assert "increment" in dir(s)


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_module_docstrings(accept_types, simple, resolve):
async def test_tuberpy_module_docstrings(resolve):
"""Ensure docstrings in C++ methods end up in the TuberObject's __doc__ dunder."""

s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")
assert s.__doc__.strip() == tm.Wrapper.__doc__.strip()


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_method_docstrings(accept_types, simple, resolve):
async def test_tuberpy_method_docstrings(resolve):
"""Ensure docstrings in C++ methods end up in the TuberObject's __doc__ dunder."""

s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")
assert s.increment.__doc__.strip() == tm.Wrapper.increment.__doc__.split("\n", 1)[-1].strip()

# check signature
sig = inspect.signature(s.increment)
assert "x" in sig.parameters


@pytest.mark.parametrize("simple", [False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_session_cache(accept_types, simple, resolve):
async def test_tuberpy_session_cache(accept_types, tuberd_host):
"""Ensure we don't create a new ClientSession with every call."""
s = await resolve("Wrapper", accept_types, simple)
s = await tuber.resolve(tuberd_host, "Wrapper", accept_types)
await s.increment([1, 2, 3])
aiohttp.ClientSession = None # break ClientSession instantiation
await s.increment([4, 5, 6])
Expand All @@ -409,12 +404,10 @@ async def test_tuberpy_session_cache(accept_types, simple, resolve):
assert aiohttp.ClientSession # type: ignore[truthy-function]


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_async_context(accept_types, simple, resolve):
async def test_tuberpy_async_context(resolve):
"""Ensure we can use tuber_contexts to batch calls."""
s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")

async with tuber_context(s) as ctx:
r1 = ctx.increment([1, 2, 3])
Expand All @@ -426,12 +419,10 @@ async def test_tuberpy_async_context(accept_types, simple, resolve):
assert r2 == [3, 4, 5]


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_async_context_with_kwargs(accept_types, simple, resolve):
async def test_tuberpy_async_context_with_kwargs(resolve):
"""Ensure we can use tuber_contexts to batch calls."""
s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")

async with tuber_context(s, x=[1, 2, 3]) as ctx:
r1 = ctx.increment()
Expand All @@ -443,12 +434,10 @@ async def test_tuberpy_async_context_with_kwargs(accept_types, simple, resolve):
assert r2 == [2, 3, 4]


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_async_context_with_exception(accept_types, simple, resolve):
async def test_tuberpy_async_context_with_exception(resolve):
"""Ensure exceptions in a sequence of calls show up as expected."""
s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")

with pytest.raises(tuber.TuberRemoteError):
async with tuber_context(s) as ctx:
Expand All @@ -471,22 +460,18 @@ async def test_tuberpy_async_context_with_exception(accept_types, simple, resolv
await tuber_result(r3)


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_unserializable(accept_types, simple, resolve):
async def test_tuberpy_unserializable(resolve):
"""Ensure unserializable objects return an error."""
s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")
with pytest.raises(tuber.TuberRemoteError):
r = await tuber_result(s.unserializable())


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_serialize_enum_class(accept_types, simple, resolve):
async def test_tuberpy_serialize_enum_class(resolve):
"""Return an enum class, which must be converted in pybind11 to something serializable."""
s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")

# Retrieve a Kind::X value
r = await tuber_result(s.return_x())
Expand All @@ -501,12 +486,10 @@ async def test_tuberpy_serialize_enum_class(accept_types, simple, resolve):


@pytest.mark.xfail
@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_async_context_with_unserializable(accept_types, simple, resolve):
async def test_tuberpy_async_context_with_unserializable(resolve):
"""Ensure exceptions in a sequence of calls show up as expected."""
s = await resolve("Wrapper", accept_types, simple)
s = await resolve("Wrapper")

async with tuber_context(s) as ctx:
r1 = ctx.increment([1, 2, 3]) # fine
Expand All @@ -522,12 +505,10 @@ async def test_tuberpy_async_context_with_unserializable(accept_types, simple, r
await tuber_result(r3)


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_warnings(accept_types, simple, resolve):
async def test_tuberpy_warnings(resolve):
"""Ensure warnings are captured"""
s = await resolve("Warnings", accept_types, simple)
s = await resolve("Warnings")

# Single, simple warning
with pytest.warns(match="This is a warning"):
Expand All @@ -543,24 +524,20 @@ async def test_tuberpy_warnings(accept_types, simple, resolve):
r = await tuber_result(s.single_warning("This is a warning", error=True))


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_resolve_all(accept_types, simple, resolve):
async def test_tuberpy_resolve_all(resolve):
"""Ensure resolve finds all registry entries"""
s = await resolve(accept_types=accept_types, simple=simple)
s = await resolve()

assert set(dir(s)) >= set(registry)
assert set(dir(s.Types)) >= set(dir(registry["Types"]))


@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_registry_context(accept_types, simple, resolve):
async def test_tuberpy_registry_context(resolve):
"""Ensure registry entries are accessible from top level context"""

s = await resolve(accept_types=accept_types, simple=simple)
s = await resolve()

async with tuber_context(s) as ctx:
ctx.Wrapper.increment(x=[1, 2, 3])
Expand All @@ -576,13 +553,10 @@ async def test_tuberpy_registry_context(accept_types, simple, resolve):
assert r2 == Types.INTEGER


@pytest.mark.parametrize("simple", [True])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_fake_async(accept_types, simple, resolve):
def test_tuberpy_fake_async(accept_types, tuberd_host):
"""Ensure async execution works with simple context"""

s = await resolve(accept_types=accept_types, simple=simple)
s = tuber.resolve_simple(tuberd_host, accept_types=accept_types)

with s.tuber_context() as ctx:
ctx.Wrapper.increment(x=[1, 2, 3])
Expand All @@ -597,12 +571,10 @@ async def test_tuberpy_fake_async(accept_types, simple, resolve):


@pytest.mark.parametrize("continue_on_error", [True, False])
@pytest.mark.parametrize("simple", [True, False])
@pytest.mark.parametrize("accept_types", ACCEPT_TYPES)
@pytest.mark.asyncio
async def test_tuberpy_continue_errors(accept_types, simple, continue_on_error, resolve):
async def test_tuberpy_continue_errors(continue_on_error, resolve):
"""Ensure errors are turned into warnings"""
s = await resolve(accept_types=accept_types, simple=simple)
s = await resolve()

with pytest.warns(match="This is a warning"):
async with tuber_context(s) as ctx:
Expand Down

0 comments on commit 244c3f0

Please sign in to comment.