def test_no_output(): source = Stream() assert source.emit(1) is None
def test_map_errors_raises(): a = Stream() b = a.map(lambda x: 1 / x) # noqa: F841 with pytest.raises(ZeroDivisionError): a.emit(0)
def test_share_common_ioloop(clean): # noqa: F811 a = Stream() b = Stream() aa = a.timed_window(0.01) bb = b.timed_window(0.01) assert aa.loop is bb.loop
def test_disconnect(): source = Stream() upstream = Stream() L = upstream.sink_to_list() source.emit(1) assert L == [] source.connect(upstream) source.emit(2) source.emit(3) assert L == [2, 3] source.disconnect(upstream) source.emit(4) assert L == [2, 3]
def test_docstrings(): for s in [Stream, Stream()]: assert 'every element' in s.map.__doc__ assert s.map.__name__ == 'map' assert 'predicate' in s.filter.__doc__ assert s.filter.__name__ == 'filter'
def test_zip_latest_reverse(): a = Stream() b = Stream() c = a.zip_latest(b) L = c.sink_to_list() b.emit('a') a.emit(1) a.emit(2) a.emit(3) b.emit('b') a.emit(4) assert L == [(1, 'a'), (2, 'a'), (3, 'a'), (4, 'b')]
def test_zip_latest_ref_counts(): a = Stream() b = Stream() _ = a.zip_latest(b) ref1 = RefCounter() a.emit(1, metadata=[{'ref': ref1}]) assert ref1.count == 1 # Retained until stream b has a value # The lossless stream is never retained if all upstreams have a value ref2 = RefCounter() b.emit(2, metadata=[{'ref': ref2}]) assert ref1.count == 0 assert ref2.count == 1 # Kick out the stream b value and verify it has zero references ref3 = RefCounter() b.emit(3, metadata=[{'ref': ref3}]) assert ref2.count == 0 assert ref3.count == 1 # Verify the lossless value is not retained, but the lossy value is ref4 = RefCounter() a.emit(3, metadata=[{'ref': ref4}]) assert ref3.count == 1 assert ref4.count == 0
def test_combine_latest_ref_counts(): a = Stream() b = Stream() _ = a.combine_latest(b) ref1 = RefCounter() a.emit(1, metadata=[{'ref': ref1}]) assert ref1.count == 1 # The new value kicks out the old value ref2 = RefCounter() a.emit(2, metadata=[{'ref': ref2}]) assert ref1.count == 0 assert ref2.count == 1 # The value on stream a is still retained and the value on stream b is new ref3 = RefCounter() b.emit(3, metadata=[{'ref': ref3}]) assert ref2.count == 1 assert ref3.count == 1
def test_combine_latest_metadata(): a = Stream() b = Stream() L = metadata(a.combine_latest(b)).sink_to_list() a.emit(1, metadata=[{'v': 1}]) b.emit(2, metadata=[{'v': 2}]) b.emit(3) b.emit(4, metadata=[{'v': 4}]) assert L == [ [{ 'v': 1 }, { 'v': 2 }], # first emit when 2 is introduced [{ 'v': 1 }], # 3 has no metadata but it replaces the value on 'b' [{ 'v': 1 }, { 'v': 4 }] # 4 replaces the value without metadata on 'b' ]
def test_zip_literals(): a = Stream() b = Stream() c = sz.zip(a, 123, b) L = c.sink_to_list() a.emit(1) b.emit(2) assert L == [(1, 123, 2)] a.emit(4) b.emit(5) assert L == [(1, 123, 2), (4, 123, 5)]
def test_combine_latest_emit_on_stream(): a = Stream() b = Stream() c = a.combine_latest(b, emit_on=0) L = c.sink_to_list() a.emit(1) b.emit('a') a.emit(2) a.emit(3) b.emit('b') a.emit(4) assert L == [(2, 'a'), (3, 'a'), (4, 'b')]
def test_zip(): a = Stream() b = Stream() c = sz.zip(a, b) L = c.sink_to_list() a.emit(1) b.emit('a') a.emit(2) b.emit('b') assert L == [(1, 'a'), (2, 'b')] d = Stream() # test zip from the object itself # zip 3 streams together e = a.zip(b, d) L2 = e.sink_to_list() a.emit(1) b.emit(2) d.emit(3) assert L2 == [(1, 2, 3)]
def test_timed_window_timedelta(clean): # noqa: F811 pytest.importorskip('pandas') source = Stream(asynchronous=True) a = source.timed_window('10ms') assert a.interval == 0.010
def test_mixed_async(): s1 = Stream(asynchronous=False) with pytest.raises(ValueError): Stream(asynchronous=True, upstream=s1)
def test_stream_name_str(): source = Stream(stream_name='this is not a stream') assert str(source) == '<this is not a stream; Stream>'
def test_zip_ref_counts(): a = Stream() b = Stream() _ = a.zip(b) # The first value in a becomes buffered ref1 = RefCounter() a.emit(1, metadata=[{'ref': ref1}]) assert ref1.count == 1 # The second value in a also becomes buffered ref2 = RefCounter() a.emit(2, metadata=[{'ref': ref2}]) assert ref1.count == 1 assert ref2.count == 1 # All emitted values are removed from the buffer ref3 = RefCounter() b.emit(3, metadata=[{'ref': ref3}]) assert ref1.count == 0 assert ref2.count == 1 # still in the buffer assert ref3.count == 0
def test_zip_latest(): a = Stream() b = Stream() c = a.zip_latest(b) d = a.combine_latest(b, emit_on=a) L = c.sink_to_list() L2 = d.sink_to_list() a.emit(1) a.emit(2) b.emit('a') b.emit('b') a.emit(3) assert L == [(1, 'a'), (2, 'a'), (3, 'b')] assert L2 == [(3, 'b')]
def test_unique_history(): source = Stream() s = source.unique(maxsize=2) s2 = source.unique(maxsize=2, hashable=False) L = s.sink_to_list() L2 = s2.sink_to_list() source.emit(1) source.emit(2) source.emit(1) source.emit(2) source.emit(1) source.emit(2) assert L == [1, 2] assert L == L2 source.emit(3) source.emit(2) assert L == [1, 2, 3] assert L == L2 source.emit(1) assert L == [1, 2, 3, 1] assert L == L2 # update 2 position source.emit(2) # knock out 1 source.emit(3) # update 2 position source.emit(2) assert L == [1, 2, 3, 1, 3] assert L == L2
def test_triple_zip_latest(): from streamz.core import Stream s1 = Stream() s2 = Stream() s3 = Stream() s_simple = s1.zip_latest(s2, s3) L_simple = s_simple.sink_to_list() s1.emit(1) s2.emit('I') s2.emit("II") s1.emit(2) s2.emit("III") s3.emit('a') s3.emit('b') s1.emit(3) assert L_simple == [(1, 'III', 'a'), (2, 'III', 'a'), (3, 'III', 'b')]
def test_unique_history_dict(): source = Stream() s = source.unique(maxsize=2, hashable=False) L = s.sink_to_list() a = {'hi': 'world'} b = {'hi': 'bar'} c = {'foo': 'bar'} source.emit(a) source.emit(b) source.emit(a) source.emit(b) source.emit(a) source.emit(b) assert L == [a, b] source.emit(c) source.emit(b) assert L == [a, b, c] source.emit(a) assert L == [a, b, c, a]
def test_zip_latest_metadata(): a = Stream() b = Stream() L = metadata(a.zip_latest(b)).sink_to_list() a.emit(1, metadata=[{'v': 1}]) b.emit(2, metadata=[{'v': 2}]) a.emit(3) b.emit(4, metadata=[{'v': 4}]) assert L == [ [{ 'v': 1 }, { 'v': 2 }], # the first emit when 2 is introduced [{ 'v': 2 }] # 3 has no metadata ]
def test_union(): a = Stream() b = Stream() c = Stream() L = a.union(b, c).sink_to_list() a.emit(1) assert L == [1] b.emit(2) assert L == [1, 2] a.emit(3) assert L == [1, 2, 3] c.emit(4) assert L == [1, 2, 3, 4]
def test_map_args(): source = Stream() L = source.map(operator.add, 10).sink_to_list() source.emit(1) assert L == [11]
def test_collect(): source1 = Stream() source2 = Stream() collector = source1.collect() L = collector.sink_to_list() source2.sink(collector.flush) source1.emit(1) source1.emit(2) assert L == [] source2.emit('anything') # flushes collector assert L == [(1, 2)] source2.emit('anything') assert L == [(1, 2), ()] source1.emit(3) assert L == [(1, 2), ()] source2.emit('anything') assert L == [(1, 2), (), (3, )]
def test_percolate_loop_information(clean): # noqa: F811 source = Stream() assert not source.loop s = source.timed_window(0.5) assert source.loop is s.loop
def test_timed_window_str(clean): # noqa: F811 source = Stream() s = source.timed_window(.05) assert str(s) == '<timed_window: 0.05>'
def test_accumulate_errors_raises(): a = Stream() b = a.accumulate(lambda x, y: x / y, with_state=True) # noqa: F841 with pytest.raises(ZeroDivisionError): a.emit(1) a.emit(0)
def test_partition_str(): source = Stream() s = source.partition(2) assert str(s) == '<partition: 2>'
def test_slice_err(): a = Stream() with pytest.raises(ValueError): a.slice(end=-1)
def test_timed_window_unique(): tests = [ (0.05, sz.identity, "first", [1, 2, 1, 3, 1, 3, 3, 2], [(1, 2, 3)]), (0.05, sz.identity, "last", [1, 2, 1, 3, 1, 3, 3, 2], [(1, 3, 2)]), ( 0.05, len, "last", ["f", "fo", "f", "foo", "f", "foo", "foo", "fo"], [("f", "foo", "fo")], ), ( 0.05, "id", "first", [{ "id": 0, "foo": "bar" }, { "id": 1, "foo": "bat" }, { "id": 0, "foo": "baz" }], [({ "id": 0, "foo": "bar" }, { "id": 1, "foo": "bat" })], ), ( 0.05, "id", "last", [{ "id": 0, "foo": "bar" }, { "id": 1, "foo": "bat" }, { "id": 0, "foo": "baz" }], [({ "id": 1, "foo": "bat" }, { "id": 0, "foo": "baz" })], ), ] for interval, key, keep, elements, exp_result in tests: source = Stream(asynchronous=True) a = source.timed_window_unique(interval, key, keep) assert a.loop is IOLoop.current() L = a.sink_to_list() for ele in elements: yield source.emit(ele) yield gen.sleep(a.interval) assert L assert all(wi in elements for window in L for wi in window) assert sum(1 for window in L for _ in window) <= len(elements) assert L == exp_result yield gen.sleep(a.interval) assert not L[-1]