-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50196fe
commit eaadbbc
Showing
3 changed files
with
38 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
with pytest.raises(RuntimeError): | ||
async_instance.test_fn(sync=True) |