Coverage for tests/test_proxy.py: 100%
33 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1from typing_extensions import final, override
3from pytest_mock import MockerFixture
5from prisma._proxy import LazyProxy
8class MyObject:
9 @override
10 def __repr__(self) -> str:
11 return '<MyObject foo!>'
13 @override
14 def __str__(self) -> str:
15 return 'foo!'
17 @property
18 def foo(self) -> str:
19 return 'FOO'
22@final
23class MyProxy(LazyProxy[MyObject]):
24 @override
25 def __load__(self) -> MyObject:
26 return MyObject()
29def test_proxied_data_methods() -> None:
30 """Supported data methods work as expected"""
31 obj = MyProxy().__as_proxied__()
32 assert str(obj) == 'foo!'
33 assert repr(obj) == '<MyObject foo!>'
35 props = [p for p in dir(obj) if not p.startswith('_')]
36 assert props == ['foo']
39def test_proxied_properties() -> None:
40 """Ensure properties can be accessed"""
41 obj = MyProxy().__as_proxied__()
42 assert obj.foo == 'FOO'
45def test_load_once(mocker: MockerFixture) -> None:
46 """The `__load__` function is only called once for multiple proxy attribute accesses"""
47 obj = MyProxy().__as_proxied__()
48 spy = mocker.spy(MyProxy, '__load__')
50 assert obj.foo == 'FOO'
51 assert obj.foo == 'FOO'
53 assert spy.call_count == 1