-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from francipvb/features/reduce-operator
Implemented the `reduce` and `reduce_async` operators
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import asyncio | ||
|
||
import pytest | ||
from expression import pipe | ||
|
||
import aioreactive as rx | ||
from aioreactive.notification import OnCompleted, OnNext | ||
from aioreactive.testing import AsyncTestObserver, VirtualTimeEventLoop | ||
|
||
|
||
class MyException(Exception): | ||
pass | ||
|
||
|
||
@pytest.fixture() | ||
def event_loop(): | ||
loop = VirtualTimeEventLoop() | ||
try: | ||
yield loop | ||
finally: | ||
loop.close() | ||
|
||
|
||
def sync_sum(a: int, b: int) -> int: | ||
return a + b | ||
|
||
|
||
async def async_sum(a: int, b: int) -> int: | ||
await asyncio.sleep(0.2) | ||
return a + b | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_reduce(): | ||
xs = rx.from_iterable([1, 2, 3, 4]) | ||
observer = AsyncTestObserver() | ||
ys = pipe(xs, rx.reduce(sync_sum, 0)) | ||
await rx.run(ys, observer) | ||
|
||
values = list(map(lambda t: t[1], observer.values)) | ||
assert values == [ | ||
OnNext(10), | ||
OnCompleted, | ||
] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_reduce_async(): | ||
xs = rx.from_iterable([1, 2, 3, 4]) | ||
observer = AsyncTestObserver() | ||
ys = pipe(xs, rx.reduce_async(async_sum, 0)) | ||
|
||
await rx.run(ys, observer) | ||
|
||
values = list(map(lambda t: t[1], observer.values)) | ||
assert values == [ | ||
OnNext(10), | ||
OnCompleted, | ||
] |