1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
import pytest
from async_property import async_property
from async_property.base import AsyncPropertyDescriptor
from async_property.proxy import AwaitableOnly
pytestmark = pytest.mark.asyncio
class MyModel:
@async_property
async def foo(self) -> str:
return 'bar'
async def test_descriptor():
assert isinstance(MyModel.foo, AsyncPropertyDescriptor)
assert MyModel.foo.__name__ == 'foo'
assert MyModel.foo.__annotations__['return'] == str
async def test_property():
instance = MyModel()
assert isinstance(instance.foo, AwaitableOnly)
assert await instance.foo == 'bar'
async def test_multiple_calls():
instance = MyModel()
assert await instance.foo == 'bar'
assert await instance.foo == 'bar'
async def test_setter():
instance = MyModel()
with pytest.raises(ValueError):
instance.foo = 'abc'
async def test_deleter():
instance = MyModel()
with pytest.raises(ValueError):
del instance.foo
async def test_sync_error():
with pytest.raises(AssertionError):
class BadModel:
@async_property
def foo(self):
return 'bar'
|