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

1from typing_extensions import final, override 

2 

3from pytest_mock import MockerFixture 

4 

5from prisma._proxy import LazyProxy 

6 

7 

8class MyObject: 

9 @override 

10 def __repr__(self) -> str: 

11 return '<MyObject foo!>' 

12 

13 @override 

14 def __str__(self) -> str: 

15 return 'foo!' 

16 

17 @property 

18 def foo(self) -> str: 

19 return 'FOO' 

20 

21 

22@final 

23class MyProxy(LazyProxy[MyObject]): 

24 @override 

25 def __load__(self) -> MyObject: 

26 return MyObject() 

27 

28 

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!>' 

34 

35 props = [p for p in dir(obj) if not p.startswith('_')] 

36 assert props == ['foo'] 

37 

38 

39def test_proxied_properties() -> None: 

40 """Ensure properties can be accessed""" 

41 obj = MyProxy().__as_proxied__() 

42 assert obj.foo == 'FOO' 

43 

44 

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__') 

49 

50 assert obj.foo == 'FOO' 

51 assert obj.foo == 'FOO' 

52 

53 assert spy.call_count == 1