def test_pipe_filter_stream(self): stream = Stream( source(), record_class=TestRecord).pipe_filter(lambda x: x.value > 5) self.assertEqual(len(stream.ops), 1) res = asyncio.run(run_tasks([stream.sink(), consumer(stream)]))[1] self.assertEqual(res, [6, 7, 8, 9])
def test_pipe_stream(self): stream = Stream( source(), record_class=TestRecord).pipe(lambda x: TestRecord(x.value + 1)) self.assertEqual(len(stream.ops), 1) res = asyncio.run(run_tasks([stream.sink(), consumer(stream)]))[1] self.assertEqual(res, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
def test_filterby_stream(self): stream = Stream(source(), record_class=TestRecord) res = asyncio.run( run_tasks([ stream.sink(), consumer_filterby(stream, lambda x: x.value % 2 == 0), ]))[1] self.assertEqual(res, [0, 2, 4, 6, 8])
def test_flatten_stream(self): stream = Stream(source_lists(), record_class=TestListRecord) res = asyncio.run(run_tasks([stream.sink(), consumer_flatten(stream)]))[1] self.assertEqual(len(res), 5) self.assertEqual( set(res), set(map(lambda x: json.dumps({"value": x}), {0, 1, 2})), )
def test_merge_stream(self): streamA = Stream(source(), record_class=TestRecord) streamB = Stream(sourceB(), record_class=TestRecord) res = asyncio.run( run_tasks([ streamA.sink(), streamB.sink(), consumer_merge(streamA, streamB), ]))[2] self.assertEqual(len(res), 21) self.assertEqual( set(res), { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 100, 11, 12, 13, 14, 15, 16, 17, 18, 19, }, )
def test_enumerate_stream(self): stream = Stream(source(), record_class=TestRecord) res = asyncio.run( run_tasks([stream.sink(), consumer_enumerate(stream)]))[1] self.assertEqual( res, [ (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), ], )
def test_window_stream(self): stream = Stream(source(), record_class=TestRecord) res = asyncio.run( run_tasks([stream.sink(), consumer_window(stream, 4)]))[1] self.assertEqual(res, tuple(map(TestRecord, [6, 7, 8, 9])))
def test_distinct_stream(self): stream = Stream(source_repeated(), record_class=TestRecord) res = asyncio.run(run_tasks([stream.sink(), consumer_distinct(stream)]))[1] self.assertEqual(len(res), 6) self.assertEqual(set(res), {0, 1, 2, 3, 4, 5})
def test_take_stream(self): stream = Stream(source(), record_class=TestRecord) res = asyncio.run(run_tasks([stream.sink(), consumer_take(stream, 3)]))[1] self.assertEqual(res, [0, 1, 2])