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, )),
    ]
Exemplo n.º 2
0
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, )),
    ]
Exemplo n.º 3
0
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)),
    ]
Exemplo n.º 4
0
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)),
    ]
Exemplo n.º 5
0
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)),
    ]
Exemplo n.º 6
0
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, )),
    ]
Exemplo n.º 7
0
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)),
    ]
Exemplo n.º 8
0
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, )),
    ]
Exemplo n.º 9
0
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)),
    ]
Exemplo n.º 10
0
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)),
    ]
Exemplo n.º 11
0
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, )),
    ]
Exemplo n.º 12
0
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, )),
    ]
Exemplo n.º 13
0
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, )),
    ]
Exemplo n.º 14
0
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)),
    ]
Exemplo n.º 15
0
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)),
    ]
Exemplo n.º 16
0
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)),
    ]
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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)
Exemplo n.º 19
0
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
Exemplo n.º 20
0
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
Exemplo n.º 21
0
def test_multiplex():
    source = [1, 2, 3, 4]
    actual_error = []
    actual_completed = []
    actual_result = []
    mux_actual_result = []

    def on_completed():
        actual_completed.append(True)

    rx.from_(source).pipe(
        rs.ops.multiplex(rx.pipe(ops.do_action(
            mux_actual_result.append), ), ), ).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 == source
    assert mux_actual_result == [
        rs.OnCreateMux((0, )),
        rs.OnNextMux((0, ), 1),
        rs.OnNextMux((0, ), 2),
        rs.OnNextMux((0, ), 3),
        rs.OnNextMux((0, ), 4),
        rs.OnCompletedMux((0, )),
    ]
Exemplo n.º 22
0
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, )),
    ]
Exemplo n.º 23
0
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),
    ]
Exemplo n.º 24
0
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
Exemplo n.º 25
0
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)),
    ]
Exemplo n.º 26
0
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, )),
    ]
Exemplo n.º 27
0
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, )),
    ]
Exemplo n.º 28
0
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)),
    ]
Exemplo n.º 29
0
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)),
    ]
Exemplo n.º 30
0
def test_group_by():
    source = [1, 2, 2, 1]
    actual_error = []
    actual_completed = []
    actual_result = []
    mux_actual_result = []

    def on_completed():
        actual_completed.append(True)

    store = rs.state.StoreManager(store_factory=rs.state.MemoryStore)
    rx.from_(source).pipe(
        rs.state.with_store(
            store,
            rx.pipe(
                rs.ops.group_by(
                    lambda i: i,
                    rx.pipe(ops.do_action(mux_actual_result.append), ),
                ))), ).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 == source

    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.OnCreateMux((1, (0, )), store),
        rs.OnNextMux((1, (0, )), 2, store),
        rs.OnNextMux((1, (0, )), 2, store),
        rs.OnNextMux((0, (0, )), 1, store),
        rs.OnCompletedMux((0, (0, )), store),
        rs.OnCompletedMux((1, (0, )), store),
    ]