From eaadbbcf48ce254bac493072719feec2f912252b Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 26 Feb 2023 03:13:42 -0500 Subject: [PATCH] fix: ASyncBase._should_await (#8) --- a_sync/base.py | 1 + tests/fixtures.py | 16 ++++++++++------ tests/test_base.py | 42 +++++++++++++++++++++++++++--------------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/a_sync/base.py b/a_sync/base.py index 66d61e0b..a9f968ff 100644 --- a/a_sync/base.py +++ b/a_sync/base.py @@ -19,6 +19,7 @@ def _should_await(self, kwargs: dict) -> bool: if flag == 'asynchronous': # must invert return not sync + return sync # No flag found in kwargs, check for a flag attribute. for flag in flags: diff --git a/tests/fixtures.py b/tests/fixtures.py index 0adf7020..ec16e655 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,18 +1,22 @@ -from a_sync import ASyncBase -from async_property import async_property, async_cached_property + +import asyncio + +from a_sync import ASyncBase, async_property, async_cached_property class TestClass(ASyncBase): - def __init__(self, sync: bool): + def __init__(self, v: int, sync: bool): + self.v = v self.sync = sync async def test_fn(self) -> int: - return 2 + return self.v @async_property async def test_property(self) -> int: - return 4 + return self.v * 2 @async_cached_property async def test_cached_property(self) -> int: - return 6 + await asyncio.sleep(2) + return self.v * 3 \ No newline at end of file diff --git a/tests/test_base.py b/tests/test_base.py index e3570adb..8abb77c9 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -1,31 +1,43 @@ import asyncio -import inspect +import pytest +import time from tests.fixtures import TestClass from a_sync._meta import ASyncMeta -def _await(coro): - return asyncio.get_event_loop().run_until_complete(coro) -def test_base_sync(): - sync_instance = TestClass(True) +@pytest.mark.parametrize('i', range(10)) +def test_base_sync(i: int): + sync_instance = TestClass(i, True) assert isinstance(sync_instance.__class__, ASyncMeta) - assert sync_instance.test_fn() == 2 - assert sync_instance.test_property == 4 - assert sync_instance.test_cached_property == 6 + assert sync_instance.test_fn() == i + assert sync_instance.test_property == i * 2 + start = time.time() + assert sync_instance.test_cached_property == i * 3 + assert isinstance(sync_instance.test_cached_property, int) + duration = time.time() - start + assert duration < 3, "There is a 2 second sleep in 'test_cached_property' but it should only run once." # Can we override with kwargs? - assert inspect.isawaitable(sync_instance.test_fn(sync=False)) + val = asyncio.get_event_loop().run_until_complete(sync_instance.test_fn(sync=False)) + assert isinstance(val, int) -def test_base_async(): - async_instance = TestClass(False) +@pytest.mark.asyncio +@pytest.mark.parametrize('i', list(range(10))) +async def test_base_async(i: int): + async_instance = TestClass(i, False) assert isinstance(async_instance.__class__, ASyncMeta) - assert _await(async_instance.test_fn()) == 2 - assert _await(async_instance.test_property) == 4 - assert _await(async_instance.test_cached_property) == 6 + assert await async_instance.test_fn() == i + assert await async_instance.test_property == i * 2 + start = time.time() + assert await async_instance.test_cached_property == i * 3 + assert isinstance(await async_instance.test_cached_property, int) + duration = time.time() - start + assert duration < 3, "There is a 2 second sleep in 'test_cached_property' but it should only run once." # Can we override with kwargs? - assert isinstance(async_instance.test_fn(sync=True), int) \ No newline at end of file + with pytest.raises(RuntimeError): + async_instance.test_fn(sync=True)