def test_describe_mux(): s1 = [random.normalvariate(0.0, 1.0) for _ in range(200)] s2 = [random.normalvariate(1.0, 2.0) for _ in range(200)] source = [ rs.OnCreateMux((1 ,None)), rs.OnCreateMux((2, None)), ] source.extend([rs.OnNextMux((1, None), i) for i in s1]) source.extend([rs.OnNextMux((2, None), i) for i in s2]) source.extend([ rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]) actual_result = [] store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_store( store, rs.math.dist.update(reduce=True), ), rs.math.dist.describe(), ).subscribe(on_next=actual_result.append)
def test_last_mux_empty(): source = [ rs.OnCreateMux((1, )), rs.OnCreateMux((2, )), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.ops.last(), ), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnCreateMux((2, )), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ]
def test_distinct_until_changed_mux(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnCompletedMux((1, )), ] actual_result = [] def on_next(i): actual_result.append(i) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.ops.distinct_until_changed()), ).subscribe(on_next) assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnCompletedMux((1, )), ]
def test_scan_mux_reduce_empty_on_complete(): source = [ rs.OnCreateMux((0, None)), rs.OnCompletedMux((0, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store( rs.ops.scan(lambda acc, i: i + acc, seed=0, reduce=True), )).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((0, None)), rs.OnNextMux((0, None), 0), rs.OnCompletedMux((0, None)), ]
def test_tee_map_mux(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), 2), rs.OnNextMux((2, ), 2), rs.OnNextMux((1, ), 1), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] actual_result = [] actual_error = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.tee_map(lambda d: d.pipe(rs.ops.map(lambda i: i * 2), ), lambda d: d.pipe(rs.ops.map(lambda i: i))), ).subscribe( on_next=actual_result.append, on_error=actual_error.append, ) assert actual_error == [] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), (2, 1)), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), (4, 2)), rs.OnNextMux((2, ), (4, 2)), rs.OnNextMux((1, ), (2, 1)), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ]
def test_assert_mux_ok(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 3), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] rx.from_(source).pipe(rs.cast_as_mux_observable(), rs.ops.assert_(lambda i: i > 0)).subscribe( on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 3), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]
def test_mean_mux_key_mapper(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), ('a', 2)), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), ('A', 3)), rs.OnNextMux((2, None), ('B', 6)), rs.OnNextMux((1, None), ('b', 3)), rs.OnNextMux((1, None), ('c', 10)), rs.OnNextMux((1, None), ('d', 4)), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.mean(lambda i: i[1], reduce=True), ), ).subscribe( on_next=actual_result.append, on_error=lambda e: print(e), ) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), 4.75), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), 4.5), rs.OnCompletedMux((2, None)), ]
def test_variance_mux(): s1 = [random.normalvariate(0.0, 1.0) for _ in range(10)] s2 = [random.normalvariate(1.0, 2.0) for _ in range(10)] source = [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), ] source.extend([rs.OnNextMux((1, None), i) for i in s1]) source.extend([rs.OnNextMux((2, None), i) for i in s2]) source.extend([ rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]) actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.formal.variance(reduce=True), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), approx(np.var(s1))), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), approx(np.var(s2))), rs.OnCompletedMux((2, None)), ]
def test_assert_mux_fail(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), -1), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), -1), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] error = [] rx.from_(source).pipe(rs.cast_as_mux_observable(), rs.ops.assert_(lambda i: i > 0)).subscribe( on_next=actual_result.append, on_error=error.append, ) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), ] assert type(error[0]) == ValueError
def test_pad_start_no_value(): actual_result = [] source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 4), rs.OnNextMux((1, ), 5), rs.OnCompletedMux((1, )), ] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.data.pad_start(size=3), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 4), rs.OnNextMux((1, ), 5), rs.OnCompletedMux((1, )), ]
def test_to_list_mux(): actual_result = [] source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnCreateMux((2, )), rs.OnNextMux((1, ), 3), rs.OnNextMux((2, ), 10), rs.OnNextMux((2, ), 11), rs.OnNextMux((1, ), 4), rs.OnNextMux((2, ), 12), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.data.to_list(), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, )), rs.OnCreateMux((2, )), rs.OnNextMux((1, ), [1, 2, 3, 4]), rs.OnCompletedMux((1, )), rs.OnNextMux((2, ), [10, 11, 12]), rs.OnCompletedMux((2, )), ]
def test_sum_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 2), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 8), rs.OnNextMux((2, None), 6), rs.OnNextMux((1, None), 3), rs.OnNextMux((1, None), 10), rs.OnNextMux((1, None), 4), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_completed = [] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.sum(reduce=True), ), ).subscribe(on_next=actual_result.append, on_completed=lambda: actual_completed.append(True)) assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), 19), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), 14), rs.OnCompletedMux((2, None)), ]
def test_min_mux_reduce(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 8), rs.OnNextMux((2, None), 6), rs.OnNextMux((1, None), 10), rs.OnNextMux((1, None), 3), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.min(reduce=True)), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnCreateMux((2, None)), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnNextMux((2, None), 6), rs.OnCompletedMux((2, None)), ]
def test_clip_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnNextMux((1, None), 4), rs.OnNextMux((1, None), 5), rs.OnNextMux((1, None), 6), rs.OnCompletedMux((1, None)), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.data.clip(lower_bound=2, higher_bound=5)).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnNextMux((1, None), 4), rs.OnNextMux((1, None), 5), rs.OnNextMux((1, None), 5), rs.OnCompletedMux((1, None)), ]
def test_tee_map_mux_combine_latest_empty(): source = [ rs.OnCreateMux((1, )), rs.OnCompletedMux((1, )), rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 10), rs.OnNextMux((1, ), 20), rs.OnCompletedMux((1, )), ] actual_result = [] actual_error = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.tee_map( rs.ops.map(lambda i: i * 2), rs.ops.map(lambda i: i), join='combine_latest', ), ).subscribe( on_next=actual_result.append, on_error=actual_error.append, ) assert actual_error == [] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnCompletedMux((1, )), rs.OnCreateMux((1, )), rs.OnNextMux((1, ), (20, None)), rs.OnNextMux((1, ), (20, 10)), rs.OnNextMux((1, ), (40, 10)), rs.OnNextMux((1, ), (40, 20)), rs.OnCompletedMux((1, )), ]
def test_completion(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), ] actual_result = [] actual_completed = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store( rs.ops.tee_map( rs.ops.first(), rs.ops.last(), ), ), ).subscribe(on_next=actual_result.append, on_completed=lambda: actual_completed.append(True)) #assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), (1, 2)), rs.OnCompletedMux((1, None)), ]
def test_lag_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnNextMux((1, None), 4), rs.OnNextMux((1, None), 5), rs.OnNextMux((1, None), 6), rs.OnCompletedMux((1, None)), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.data.lag(2), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), (1, 1)), rs.OnNextMux((1, None), (1, 2)), rs.OnNextMux((1, None), (1, 3)), rs.OnNextMux((1, None), (2, 4)), rs.OnNextMux((1, None), (3, 5)), rs.OnNextMux((1, None), (4, 6)), rs.OnCompletedMux((1, None)), ]
def test_start_with(): actual_result = [] source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 4), rs.OnNextMux((1, ), 5), rs.OnCompletedMux((1, )), ] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.ops.start_with([-3, -2, -1, 0]), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), -3), rs.OnNextMux((1, ), -2), rs.OnNextMux((1, ), -1), rs.OnNextMux((1, ), 0), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 4), rs.OnNextMux((1, ), 5), rs.OnCompletedMux((1, )), ]
def test_split(): source = ["1a", "2a", "3b", "4b", "5c", "6c", "7c", "8d", "9d"] source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), '1a'), rs.OnNextMux((1, None), '2a'), rs.OnNextMux((1, None), '3b'), rs.OnNextMux((1, None), '4b'), rs.OnNextMux((1, None), '5c'), rs.OnNextMux((1, None), '6c'), rs.OnNextMux((1, None), '7c'), rs.OnNextMux((1, None), '8d'), rs.OnNextMux((1, None), '9d'), rs.OnCompletedMux((1, None)), ] actual_result = [] mux_actual_result = [] expected_result = [ ["1a", "2a"], ["3b", "4b"], ["5c", "6c", "7c"], ["8d", "9d"], ] def on_next(i): actual_result.append(i) store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_store( store, rs.data.split(lambda i: i[-1], rx.pipe(ops.do_action(mux_actual_result.append), )), ), ).subscribe(on_next) assert type(mux_actual_result[0]) is rs.state.ProbeStateTopology assert mux_actual_result[1:] == [ rs.OnCreateMux((1, (1, None)), store), rs.OnNextMux((1, (1, None)), '1a', store), rs.OnNextMux((1, (1, None)), '2a', store), rs.OnCompletedMux((1, (1, None)), store), rs.OnCreateMux((1, (1, None)), store), rs.OnNextMux((1, (1, None)), '3b', store), rs.OnNextMux((1, (1, None)), '4b', store), rs.OnCompletedMux((1, (1, None)), store), rs.OnCreateMux((1, (1, None)), store), rs.OnNextMux((1, (1, None)), '5c', store), rs.OnNextMux((1, (1, None)), '6c', store), rs.OnNextMux((1, (1, None)), '7c', store), rs.OnCompletedMux((1, (1, None)), store), rs.OnCreateMux((1, (1, None)), store), rs.OnNextMux((1, (1, None)), '8d', store), rs.OnNextMux((1, (1, None)), '9d', store), rs.OnCompletedMux((1, (1, None)), store), ] assert actual_result == source
def test_closing_mapper_exclude(): source = [ rs.OnCreateMux((1 ,None)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=1)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=2)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=3)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=4)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=5)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=6)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=10)), rs.OnNextMux((1, None), datetime(2020, 1, 2, second=12)), rs.OnCompletedMux((1, None)), ] actual_result = [] mux_actual_result = [] def on_next(i): actual_result.append(i) store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_store( store, rs.data.time_split( time_mapper=lambda i: i, active_timeout=timedelta(seconds=5), inactive_timeout=timedelta(seconds=3), closing_mapper=lambda i: i == datetime(2020, 1, 2, second=4), include_closing_item=False, pipeline=rx.pipe( ops.do_action(mux_actual_result.append), )), ), ).subscribe(on_next) assert type(mux_actual_result[0]) is rs.state.ProbeStateTopology assert mux_actual_result[1:] == [ rs.OnCreateMux((1, (1, None)), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=1), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=2), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=3), store), rs.OnCompletedMux((1, (1, None)), store), rs.OnCreateMux((1, (1, None)), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=4), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=5), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=6), store), rs.OnCompletedMux((1, (1, None)), store), rs.OnCreateMux((1, (1, None)), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=10), store), rs.OnNextMux((1, (1, None)), datetime(2020, 1, 2, second=12), store), rs.OnCompletedMux((1, (1, None)), store), ] assert actual_result == source
def test_pad_end_empty(): actual_result = [] source = [ rs.OnCreateMux((1, )), rs.OnCompletedMux((1, )), ] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.data.pad_end(size=3, value=0), ), ).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, )), rs.OnCompletedMux((1, )), ]
def test_roll_with_stride(): source = [ rs.OnCreateMux((0, )), rs.OnNextMux((0, ), 1), rs.OnNextMux((0, ), 2), rs.OnNextMux((0, ), 3), rs.OnNextMux((0, ), 4), rs.OnNextMux((0, ), 5), rs.OnNextMux((0, ), 6), rs.OnCompletedMux((0, )), ] actual_result = [] mux_actual_result = [] def on_next(i): actual_result.append(i) store = rs.state.StoreManager(store_factory=rs.state.MemoryStore) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_store( store, rs.data.roll(window=3, stride=2, pipeline=rx.pipe( ops.do_action(mux_actual_result.append), )), ), ).subscribe(on_next) assert type(mux_actual_result[0]) is rs.state.ProbeStateTopology assert mux_actual_result[1:] == [ rs.OnCreateMux((0, (0, )), store), rs.OnNextMux((0, (0, )), 1, store), rs.OnNextMux((0, (0, )), 2, store), rs.OnCreateMux((1, (0, )), store), rs.OnNextMux((0, (0, )), 3, store), rs.OnCompletedMux((0, (0, )), store), rs.OnNextMux((1, (0, )), 3, store), rs.OnNextMux((1, (0, )), 4, store), rs.OnCreateMux((0, (0, )), store), rs.OnNextMux((0, (0, )), 5, store), rs.OnNextMux((1, (0, )), 5, store), rs.OnCompletedMux((1, (0, )), store), rs.OnNextMux((0, (0, )), 6, store), rs.OnCompletedMux((0, (0, )), store), ]
def test_max_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 8), rs.OnNextMux((2, None), 6), rs.OnNextMux((1, None), 10), rs.OnNextMux((1, None), 3), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store(rs.math.max(), ), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] try: assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 8), rs.OnNextMux((2, None), 8), rs.OnNextMux((1, None), 10), rs.OnNextMux((1, None), 10), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] except Exception as e: import traceback traceback.print_tb(e.__traceback__) raise e
def test_take_mux(): source = [ rs.OnCreateMux((1 ,None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 10), rs.OnNextMux((2, None), 11), rs.OnNextMux((1, None), 2), rs.OnNextMux((1, None), 3), rs.OnNextMux((2, None), 12), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.state.with_memory_store( rs.ops.take(2), ), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1 ,None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 10), rs.OnNextMux((2, None), 11), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]
def test_tee_map_mux_combine_latest(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), 2), rs.OnNextMux((2, ), 3), rs.OnNextMux((1, ), 2), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] actual_result = [] actual_error = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.tee_map( lambda d: d.pipe(rs.ops.map(lambda i: i * 2), ), lambda d: d.pipe( rs.ops.map(lambda i: i), rs.ops.filter(lambda i: i % 2 != 0), ), join='combine_latest', ), ).subscribe( on_next=actual_result.append, on_error=actual_error.append, ) assert actual_error == [] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), (2, None)), rs.OnNextMux((1, ), (2, 1)), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), (4, None)), rs.OnNextMux((2, ), (6, None)), rs.OnNextMux((2, ), (6, 3)), rs.OnNextMux((1, ), (4, 1)), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ]
def test_flat_map_mux(): source = [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), [1, 2, 3, 4]), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), [10, 11, 12, 13]), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.flat_map(), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, )), rs.OnNextMux((1, ), 1), rs.OnNextMux((1, ), 2), rs.OnNextMux((1, ), 3), rs.OnNextMux((1, ), 4), rs.OnCreateMux((2, )), rs.OnNextMux((2, ), 10), rs.OnNextMux((2, ), 11), rs.OnNextMux((2, ), 12), rs.OnNextMux((2, ), 13), rs.OnCompletedMux((1, )), rs.OnCompletedMux((2, )), ]
def test_starmap_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), (1, 3)), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), (2, 5)), rs.OnNextMux((2, None), (2, 2)), rs.OnNextMux((1, None), (1, 1)), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.starmap(lambda i, j: i + j), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 4), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 7), rs.OnNextMux((2, None), 4), rs.OnNextMux((1, None), 2), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]
def test_identity_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 2), rs.OnNextMux((1, None), 1), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_error = [] actual_completed = [] actual_result = [] def on_completed(): actual_completed.append(True) rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.ops.identity(), ).subscribe( on_next=actual_result.append, on_completed=on_completed, on_error=actual_error.append, ) assert actual_error == [] assert actual_completed == [True] assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 2), rs.OnNextMux((1, None), 1), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ]
def test_fill_none_mux(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), None), rs.OnNextMux((1, None), 1.2), rs.OnNextMux((1, None), 5.348), rs.OnNextMux((1, None), None), rs.OnCompletedMux((1, None)), ] actual_result = [] rx.from_(source).pipe( rs.cast_as_mux_observable(), rs.data.fill_none(0)).subscribe(on_next=actual_result.append) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 0), rs.OnNextMux((1, None), 1.2), rs.OnNextMux((1, None), 5.348), rs.OnNextMux((1, None), 0), rs.OnCompletedMux((1, None)), ]
def test_assert_mux_predicate_error(): source = [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 3), rs.OnNextMux((1, None), 'raise'), rs.OnNextMux((1, None), 3), rs.OnCompletedMux((1, None)), rs.OnCompletedMux((2, None)), ] actual_result = [] actual_error = [] def _raise(i): if i == 'raise': raise ValueError() return True rx.from_(source).pipe(rs.cast_as_mux_observable(), rs.ops.assert_(_raise)).subscribe( on_next=actual_result.append, on_error=actual_error.append, ) assert actual_result == [ rs.OnCreateMux((1, None)), rs.OnNextMux((1, None), 1), rs.OnCreateMux((2, None)), rs.OnNextMux((2, None), 2), rs.OnNextMux((2, None), 3), ] assert len(actual_error) == 1 assert type(actual_error[0]) == ValueError