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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
import asyncio
import pytest
from aiostream import stream, pipe
from aiostream.test_utils import assert_run, event_loop, add_resource
# Pytest fixtures
assert_run, event_loop
@pytest.mark.asyncio
async def test_take(assert_run, event_loop):
with event_loop.assert_cleanup():
xs = stream.count() | add_resource.pipe(1) | pipe.take(3)
await assert_run(xs, [0, 1, 2])
with event_loop.assert_cleanup():
xs = stream.count() | add_resource.pipe(1) | pipe.take(0)
await assert_run(xs, [])
@pytest.mark.asyncio
async def test_takelast(assert_run, event_loop):
xs = stream.range(10) | add_resource.pipe(1) | pipe.takelast(3)
await assert_run(xs, [7, 8, 9])
@pytest.mark.asyncio
async def test_skip(assert_run, event_loop):
xs = stream.range(10) | add_resource.pipe(1) | pipe.skip(8)
await assert_run(xs, [8, 9])
@pytest.mark.asyncio
async def test_skiplast(assert_run, event_loop):
with event_loop.assert_cleanup():
xs = stream.range(10) | add_resource.pipe(1) | pipe.skiplast(8)
await assert_run(xs, [0, 1])
with event_loop.assert_cleanup():
xs = stream.range(10) | add_resource.pipe(1) | pipe.skiplast(0)
await assert_run(xs, list(range(10)))
@pytest.mark.asyncio
async def test_filterindex(assert_run, event_loop):
filterindex = stream.select.filterindex
xs = (
stream.range(10)
| add_resource.pipe(1)
| filterindex.pipe(lambda x: x in [4, 7, 8])
)
await assert_run(xs, [4, 7, 8])
@pytest.mark.asyncio
async def test_slice(assert_run, event_loop):
slice = stream.select.slice
with event_loop.assert_cleanup():
xs = stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(2)
await assert_run(xs, [10, 11])
with event_loop.assert_cleanup():
xs = stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(8, None)
await assert_run(xs, [18, 19])
with event_loop.assert_cleanup():
xs = stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(-3, -1)
await assert_run(xs, [17, 18])
with event_loop.assert_cleanup():
xs = stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(-5, -1, 2)
await assert_run(xs, [15, 17])
with pytest.raises(ValueError):
xs = stream.range(10, 20) | slice.pipe(5, 1, -1)
with pytest.raises(ValueError):
xs = stream.range(10, 20) | slice.pipe(-8, 8)
@pytest.mark.asyncio
async def test_item(assert_run, event_loop):
item = stream.select.item
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1) | item.pipe(2)
await assert_run(xs, [2])
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1) | item.pipe(-2)
await assert_run(xs, [3])
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1) | item.pipe(10)
exception = IndexError(
"Index out of range",
)
await assert_run(xs, [], exception)
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1) | item.pipe(-10)
exception = IndexError(
"Index out of range",
)
await assert_run(xs, [], exception)
@pytest.mark.asyncio
async def test_getitem(assert_run, event_loop):
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1) | pipe.getitem(2)
await assert_run(xs, [2])
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1)
await assert_run(xs[2], [2])
with event_loop.assert_cleanup():
s = slice(1, 3)
xs = stream.range(5) | add_resource.pipe(1) | pipe.getitem(s)
await assert_run(xs, [1, 2])
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1)
await assert_run(xs[1:3], [1, 2])
with event_loop.assert_cleanup():
s = slice(1, 5, 2)
xs = stream.range(5) | add_resource.pipe(1) | pipe.getitem(s)
await assert_run(xs, [1, 3])
with event_loop.assert_cleanup():
xs = stream.range(5) | add_resource.pipe(1)
await assert_run(xs[1:5:2], [1, 3])
with pytest.raises(TypeError):
xs = stream.range(5)[None]
@pytest.mark.asyncio
async def test_filter(assert_run, event_loop):
with event_loop.assert_cleanup():
xs = (
stream.range(1, 10)
| add_resource.pipe(1)
| pipe.filter(lambda x: x in [4, 7, 8])
)
await assert_run(xs, [4, 7, 8])
async def afunc(x):
await asyncio.sleep(1)
return x in [3, 6, 9]
with event_loop.assert_cleanup():
xs = stream.range(1, 10) | add_resource.pipe(1) | pipe.filter(afunc)
await assert_run(xs, [3, 6, 9])
assert event_loop.steps == [1] * 10
@pytest.mark.asyncio
async def test_until(assert_run, event_loop):
with event_loop.assert_cleanup():
xs = stream.range(1, 10) | add_resource.pipe(1) | pipe.until(lambda x: x == 3)
await assert_run(xs, [1, 2, 3])
async def afunc(x):
await asyncio.sleep(1)
return x == 3
with event_loop.assert_cleanup():
xs = stream.range(1, 10) | add_resource.pipe(1) | pipe.until(afunc)
await assert_run(xs, [1, 2, 3])
assert event_loop.steps == [1] * 4
@pytest.mark.asyncio
async def test_takewhile(assert_run, event_loop):
with event_loop.assert_cleanup():
xs = (
stream.range(1, 10) | add_resource.pipe(1) | pipe.takewhile(lambda x: x < 4)
)
await assert_run(xs, [1, 2, 3])
async def afunc(x):
await asyncio.sleep(1)
return x < 4
with event_loop.assert_cleanup():
xs = stream.range(1, 10) | add_resource.pipe(1) | pipe.takewhile(afunc)
await assert_run(xs, [1, 2, 3])
assert event_loop.steps == [1] * 5
@pytest.mark.asyncio
async def test_dropwhile(assert_run, event_loop):
with event_loop.assert_cleanup():
xs = (
stream.range(1, 10) | add_resource.pipe(1) | pipe.dropwhile(lambda x: x < 7)
)
await assert_run(xs, [7, 8, 9])
async def afunc(x):
await asyncio.sleep(1)
return x < 7
with event_loop.assert_cleanup():
xs = stream.range(1, 10) | add_resource.pipe(1) | pipe.dropwhile(afunc)
await assert_run(xs, [7, 8, 9])
assert event_loop.steps == [1] * 8
|