File: test_loader.py

package info (click to toggle)
python-async-property 0.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: python: 765; makefile: 87; sh: 6
file content (57 lines) | stat: -rw-r--r-- 1,216 bytes parent folder | download
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
52
53
54
55
56
57
import pytest
from async_property import async_cached_property, AwaitLoader

pytestmark = pytest.mark.asyncio


class MyModel(AwaitLoader):
    @async_cached_property
    async def foo(self):
        return 'bar'


async def test_empty_instance():
    assert await AwaitLoader()


async def test_loader():
    instance = await MyModel()
    assert 'foo' in instance.__async_property__.cache
    assert instance.foo == 'bar'


async def test_delayed_field():
    instance = MyModel()
    coro = instance.foo
    await instance
    assert instance.foo == 'bar'
    assert await coro == 'bar'


async def test_delayed_field_with_setter():
    instance = MyModel()
    coro = instance.foo
    await instance
    assert instance.foo == 'bar'
    instance.foo = 'abc'
    assert await coro == 'abc'


async def test_empty_instance_loaders():
    assert AwaitLoader._async_property_loaders == ()


async def test_instance_loaders():
    assert MyModel._async_property_loaders == (
        ('foo', MyModel.foo.get_loader),
    )


class MyModelWithLoad(AwaitLoader):
    async def load(self):
        self.loaded = True


async def test_call_load():
    instance = await MyModelWithLoad()
    assert instance.loaded == True