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
|
import asyncio
from typing import Iterator
from typing import List
from typing import Mapping
import jsonpath
class MockLazyMapping(Mapping[str, object]):
def __init__(self, val: object):
self.key = "bar"
self.val = val
self.call_count = 0
self.await_count = 0
def __len__(self) -> int: # pragma: no cover
return 1
def __iter__(self) -> Iterator[str]: # pragma: no cover
return iter([self.key])
def __getitem__(self, k: str) -> object: # pragma: no cover
self.call_count += 1
if k == self.key:
return self.val
raise KeyError(k)
async def __getitem_async__(self, k: str) -> object:
self.await_count += 1
if k == self.key:
return self.val
raise KeyError(k)
def test_async_getitem() -> None:
lazy_mapping = MockLazyMapping("thing")
data = {"foo": lazy_mapping}
async def coro() -> List[object]:
return await jsonpath.findall_async("$.foo.bar | $.foo.nosuchthing", data)
matches = asyncio.run(coro())
assert len(matches) == 1
assert matches[0] == "thing"
assert lazy_mapping.call_count == 0
assert lazy_mapping.await_count == 2 # noqa: PLR2004
|