def test_subscribe_all(subscribe_all):
    p_select = StatefulPublisher('a')
    p1 = StatefulPublisher(0)
    p2 = StatefulPublisher(1)
    dut = Switch({'a': p1, 'b': p2, 'c': 2}, subscribe_all=subscribe_all)

    assert not p_select.subscriptions
    assert not p1.subscriptions
    assert not p2.subscriptions
    assert not dut.subscriptions

    mock_cb = mock.Mock()
    disposable = p_select | dut | Sink(mock_cb)

    assert len(dut.subscriptions) == 1
    assert len(p_select.subscriptions) == 1
    assert len(p1.subscriptions) == 1
    assert len(p2.subscriptions) == (1 if subscribe_all else 0)

    p_select.notify('b')

    assert len(dut.subscriptions) == 1
    assert len(p_select.subscriptions) == 1
    assert len(p1.subscriptions) == (1 if subscribe_all else 0)
    assert len(p2.subscriptions) == 1

    disposable.dispose()

    assert not p_select.subscriptions
    assert not p1.subscriptions
    assert not p2.subscriptions
    assert not dut.subscriptions
def test_getattr_attribute():
    p = StatefulPublisher()

    class Foo:
        a = None

        def __init__(self, a=5):
            self.a = a

    p.inherit_type(Foo)

    dut = p.a
    m = mock.Mock()
    dut | op.Sink(m)

    with pytest.raises(ValueError):
        dut.get()

    m.assert_not_called()

    p.notify(Foo(3))

    assert dut.get() == 3

    m.assert_called_once_with(3)

    with pytest.raises(ValueError):
        dut.emit_op(0, who=Publisher())

    with pytest.raises(AttributeError):
        dut.assnign(5)
Esempio n. 3
0
def test_stateful_publisher():
    p = StatefulPublisher(1)
    future = p | OnEmitFuture(timeout=1, loop=asyncio.get_event_loop())

    assert future.result() == 1

    p.notify(2)
    assert future.result() == 1
def test_inherit_with_operators():
    p = StatefulPublisher()
    p.inherit_type(str)

    dut = op.Len(('abc' + p + 'ghi').upper())
    m = mock.Mock()
    dut | op.Sink(m)

    p.notify('def')
    m.assert_called_once_with(9)
def test_inherit_getattr():
    p = StatefulPublisher()
    p.inherit_type(str)

    dut = p.lower().split(' ')
    m = mock.Mock()
    dut | op.Sink(m)

    p.notify('This is a TEST')
    m.assert_called_once_with(['this', 'is', 'a', 'test'])
Esempio n. 6
0
def test_allow_stateless_extensive():
    source1 = StatefulPublisher(0)
    source2 = Publisher()
    source3 = StatefulPublisher(0)
    source4 = Publisher()

    dut = CombineLatest(source1,
                        source2,
                        source3,
                        source4,
                        allow_stateless=True,
                        emit_on=(source2, source3))

    with pytest.raises(ValueError):
        dut | Sink()  # source4 is stateless but not in emit_on list

    def reverse(s1, s2, s3, s4):
        return (s4, s3, s2, s1)

    dut = CombineLatest(source1,
                        source2,
                        source3,
                        source4,
                        map_=reverse,
                        allow_stateless=True,
                        emit_on=(source2, source3, source4))

    with pytest.raises(ValueError):
        dut.get()

    collector = Collector()
    dut.subscribe(collector)

    with pytest.raises(ValueError):
        dut.get()
    assert collector.result_vector == ()

    collector.reset()
    source1.notify(1)
    source2.notify(2)
    with pytest.raises(ValueError):
        dut.get()
    assert collector.result_vector == ((NONE, 0, 2, 1), )

    collector.reset()
    source3.notify(3)
    source4.notify(4)
    with pytest.raises(ValueError):
        dut.get()
    assert collector.result_vector == (
        (NONE, 3, NONE, 1),
        (4, 3, NONE, 1),
    )
Esempio n. 7
0
def test_allow_stateless_with_stateful_publishers():
    source1 = StatefulPublisher(0)
    source2 = StatefulPublisher(0)

    dut = CombineLatest(source1, source2, allow_stateless=True)

    collector = Collector()
    dut.subscribe(collector)

    source1.notify(1)
    source2.notify(1)

    assert collector.result_vector == ((0, 0), (1, 0), (1, 1))
Esempio n. 8
0
def test_uninitialised_with_stateful():
    source = StatefulPublisher(1)
    dut = source | Cache()
    cb = mock.Mock()
    dut | Sink(cb)

    cb.assert_called_once_with(1)
    source.notify(1)
    cb.assert_called_once_with(1)

    cb.reset_mock()
    source.notify(2)

    cb.assert_called_once_with(2)
Esempio n. 9
0
def test_stateless_map():
    source1 = StatefulPublisher(0)
    source2 = Publisher()

    dut = CombineLatest(source1,
                        source2,
                        map_=lambda a, b: a + (0 if b is NONE else b),
                        allow_stateless=True)

    collector = Collector()
    dut.subscribe(collector)

    with pytest.raises(ValueError):
        dut.get()
    assert collector.result_vector == ()

    collector.reset()
    source1.notify(1)
    assert collector.result_vector == (1, )

    source1.notify(1)
    assert collector.result_vector == (1, )

    source1.notify(1.0)
    assert collector.result_vector == (1, )

    source2.notify(0)
    assert collector.result_vector == (1, 1)

    source2.notify(1)
    assert collector.result_vector == (1, 1, 2)

    source2.notify(1)
    assert collector.result_vector == (1, 1, 2, 2)
def test_getattr_method():
    p = StatefulPublisher()
    p.inherit_type(str)

    dut1 = p.split()
    dut2 = p.split(',')
    dut3 = p.split(sep='!')

    mock1 = mock.Mock()
    mock2 = mock.Mock()
    mock3 = mock.Mock()

    dut1 | op.Sink(mock1)
    dut2 | op.Sink(mock2)
    dut3 | op.Sink(mock3)

    with pytest.raises(ValueError):
        dut1.get()

    with pytest.raises(ValueError):
        dut2.get()

    with pytest.raises(ValueError):
        dut3.get()

    mock1.assert_not_called()
    mock2.assert_not_called()
    mock3.assert_not_called()

    p.notify('This is just a test, honestly!')

    assert dut1.get() == ['This', 'is', 'just', 'a', 'test,', 'honestly!']
    assert dut2.get() == ['This is just a test', ' honestly!']
    assert dut3.get() == ['This is just a test, honestly', '']

    mock1.assert_called_once_with(
        ['This', 'is', 'just', 'a', 'test,', 'honestly!'])
    mock2.assert_called_once_with(['This is just a test', ' honestly!'])
    mock3.assert_called_once_with(['This is just a test, honestly', ''])
def test_operator_concat():
    DUT = OperatorConcat(Map(lambda v: v / 2),
                         Reduce(lambda s, v: s + v, init=0))
    mock_cb = mock.Mock()

    p = StatefulPublisher(0)

    p | DUT
    assert DUT.get() == 0

    DUT | Sink(mock_cb)

    for v in range(5):
        p.notify(v)

    mock_cb.assert_has_calls([
        mock.call(0),
        mock.call(0.5),
        mock.call(1.5),
        mock.call(3),
        mock.call(5)
    ])

    assert DUT.get() == 5
Esempio n. 12
0
def test_allow_stateless():
    source1 = StatefulPublisher(0)
    source2 = Publisher()

    dut = CombineLatest(source1, source2, allow_stateless=True)
    with pytest.raises(ValueError):
        dut.get()

    collector = Collector()
    disposable = dut.subscribe(collector)

    with pytest.raises(ValueError):
        dut.get()
    assert collector.result_vector == ()

    source1.notify(1)
    source2.notify(True)
    source1.notify(2)
    source2.notify(False)

    assert collector.result_vector == ((1, NONE), (1, True), (2, NONE),
                                       (2, False))

    # combine latest should also emit when stateless publisher is emitting
    collector.reset()

    source2.notify(False)
    assert collector.result_vector == ((2, False), )

    source2.notify(False)
    assert collector.result_vector == ((2, False), (2, False))

    source1.notify(0)
    assert collector.result_vector == ((2, False), (2, False), (0, NONE))

    disposable.dispose()
    assert len(dut.subscriptions) == 0

    source3 = StatefulPublisher(0)
    dut2 = CombineLatest(dut, source3)

    with pytest.raises(ValueError):
        dut2.get()
Esempio n. 13
0
def test_stateful_publisher():
    p = StatefulPublisher()
    v = Value(0)

    with pytest.raises(ValueError):
        p.get()

    mock_sink = mock.Mock()

    disposable = p | v
    v | op.Sink(mock_sink)

    mock_sink.assert_called_once_with(0)
    mock_sink.reset_mock()

    p.notify(1)

    mock_sink.assert_called_once_with(1)
    assert p.get() == 1
    mock_sink.reset_mock()

    p.notify(1)
    assert p.get() == 1
    mock_sink.assert_not_called()

    p.notify(2)
    assert p.get() == 2
    mock_sink.assert_called_once_with(2)

    p.reset_state()

    with pytest.raises(ValueError):
        p.get()

    p.reset_state(3)
    assert p.get() == 3