Repository navigation
Expand file tree
/
Copy pathtest_aio.py
More file actions
138 lines (109 loc) · 4.47 KB
/
Copy pathtest_aio.py
File metadata and controls
138 lines (109 loc) · 4.47 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Tests for the async helpers in ``python_utils.aio``."""
import asyncio
import pytest
from python_utils import aio, types
@pytest.mark.asyncio
async def test_acount(monkeypatch: pytest.MonkeyPatch) -> None:
"""Count with a delay between yields until reaching ``stop``."""
sleeps: types.List[float] = []
async def mock_sleep(delay: float) -> None:
"""Record each requested delay instead of sleeping."""
sleeps.append(delay)
monkeypatch.setattr(asyncio, 'sleep', mock_sleep)
async for _i in aio.acount(delay=1, stop=3.5):
pass
assert len(sleeps) == 4
assert sum(sleeps) == 4
@pytest.mark.asyncio
@pytest.mark.parametrize(
'start,step,stop,expected',
[
(10, -2, 0, [10, 8, 6, 4, 2]),
(0, -1, -3, [0, -1, -2]),
(1.5, -0.5, 0, [1.5, 1.0, 0.5]),
# Nothing to count when `start` is already past `stop`, in either
# direction. This is what `range` does as well.
(0, -1, 5, []),
(5, 1, 3, []),
(0, 2, 5, [0, 2, 4]),
],
)
async def test_acount_stop_follows_step_direction(
start: float, step: float, stop: float, expected: types.List[float]
) -> None:
"""Count down to a lower ``stop`` when the step is negative."""
limit: int = len(expected) + 5
items: types.List[float] = []
item: float
# `acount` is annotated as a plain iterator, closing takes a generator.
counter: types.AsyncGenerator[float, None] = types.cast(
types.AsyncGenerator[float, None],
aio.acount(start=start, step=step, stop=stop),
)
async for item in counter:
items.append(item)
# A counter that misses its `stop` never ends by itself.
if len(items) == limit:
break
await counter.aclose()
assert items == expected
@pytest.mark.asyncio
async def test_acontainer() -> None:
"""Collect an async iterable into the requested container."""
async def async_gen() -> types.AsyncIterable[int]:
"""Yield 1, 2, 3 asynchronously."""
yield 1
yield 2
yield 3
async def empty_gen() -> types.AsyncIterable[int]:
"""Yield nothing as an async generator."""
if False:
yield 1
assert await aio.acontainer(async_gen) == [1, 2, 3]
assert await aio.acontainer(async_gen()) == [1, 2, 3]
assert await aio.acontainer(async_gen, set) == {1, 2, 3}
assert await aio.acontainer(async_gen(), set) == {1, 2, 3}
assert await aio.acontainer(async_gen, list) == [1, 2, 3]
assert await aio.acontainer(async_gen(), list) == [1, 2, 3]
assert await aio.acontainer(async_gen, tuple) == (1, 2, 3)
assert await aio.acontainer(async_gen(), tuple) == (1, 2, 3)
assert await aio.acontainer(empty_gen) == []
assert await aio.acontainer(empty_gen()) == []
assert await aio.acontainer(empty_gen, set) == set()
assert await aio.acontainer(empty_gen(), set) == set()
assert await aio.acontainer(empty_gen, list) == list()
assert await aio.acontainer(empty_gen(), list) == list()
assert await aio.acontainer(empty_gen, tuple) == tuple()
assert await aio.acontainer(empty_gen(), tuple) == tuple()
@pytest.mark.asyncio
async def test_adict() -> None:
"""Build a dict from an async iterable of key/value pairs."""
async def async_gen() -> types.AsyncIterable[types.Tuple[int, int]]:
"""Yield key/value pairs asynchronously."""
yield 1, 2
yield 3, 4
yield 5, 6
async def empty_gen() -> types.AsyncIterable[types.Tuple[int, int]]:
"""Yield no pairs as an async generator."""
if False:
yield 1, 2
assert await aio.adict(async_gen) == {1: 2, 3: 4, 5: 6}
assert await aio.adict(async_gen()) == {1: 2, 3: 4, 5: 6}
assert await aio.adict(empty_gen) == {}
assert await aio.adict(empty_gen()) == {}
@pytest.mark.asyncio
async def test_acontainer_custom_container_types() -> None:
"""Accept custom container factories without losing their return type."""
from collections.abc import AsyncIterator, Iterable
from typing_extensions import assert_type
async def numbers() -> AsyncIterator[int]:
yield 1
yield 2
def summarize(items: Iterable[int]) -> dict[str, int]:
return {'total': sum(items)}
frozen = await aio.acontainer(numbers(), frozenset)
assert_type(frozen, frozenset[int])
assert frozen == frozenset({1, 2})
summary = await aio.acontainer(numbers, summarize)
assert_type(summary, dict[str, int])
assert summary == {'total': 3}