def test_operator_with_publishers(): v1 = Value(0) v2 = Value(0) o = v1 + v2 assert isinstance(o, Publisher) assert not isinstance(o, Subscriber) assert o.get() == 0 v1.emit(1) assert o.get() == 1 assert len(o.subscriptions) == 0 mock_sink = mock.Mock() o | op.Sink(mock_sink) assert len(o.subscriptions) == 1 mock_sink.assert_called_once_with(1) mock_sink.reset_mock() v2.emit(3) mock_sink.assert_called_once_with(4) with pytest.raises(ValueError): o.emit_op(0, who=Publisher()) with pytest.raises(AttributeError): Value(1) | o
def test_sink(operator_cls): cb = mock.Mock() s = Value() sink_instance = s.subscribe(operator_cls(cb)) assert isinstance(sink_instance, Disposable) assert not cb.called assert len(s.subscriptions) == 1 # test various emits on source with pytest.raises(TypeError): s.emit() s.emit(None) cb.assert_called_with(None) s.emit(1) cb.assert_called_with(1) s.emit((1, 2)) cb.assert_called_with((1, 2)) # testing dispose()s cb.reset_mock() sink_instance.dispose() assert len(s.subscriptions) == 0 s.emit(1) assert not cb.called
def test_sink_without_function(operator_cls): s = Value() sink_instance = s.subscribe(operator_cls()) assert isinstance(sink_instance, Disposable) assert len(s.subscriptions) == 1 s.emit(1)
def test_operator_with_constant_r(): v1 = 1 v2 = Value(0) o = v1 - v2 assert isinstance(o, Publisher) assert o.get() == 1 v2.emit(1) assert o.get() == 0 assert len(o.subscriptions) == 0 mock_sink = mock.Mock() o | op.Sink(mock_sink) assert len(o.subscriptions) == 1 mock_sink.assert_called_once_with(0) mock_sink.reset_mock() v2.emit(3) mock_sink.assert_called_once_with(-2) with pytest.raises(ValueError): Value(1) | o with pytest.raises(ValueError): o.emit_op(0, who=Publisher())
def test_operator_with_constant(): v1 = Value(0) v2 = 1 o = v1 + v2 assert isinstance(o, Publisher) assert o.get() == 1 v1.emit(1) assert o.get() == 2 assert len(o.subscriptions) == 0 mock_sink = mock.Mock() o.subscribe(Sink(mock_sink)) assert len(o.subscriptions) == 1 mock_sink.assert_called_once_with(2) mock_sink.reset_mock() v1.emit(3) mock_sink.assert_called_once_with(4) with pytest.raises(TypeError): Value(1) | o with pytest.raises(ValueError): o.emit(0, who=Publisher())
def test_getitem(): v1 = Value(('a', 'b', 'c')) v2 = Value(2) o = (v1[v2]) assert isinstance(o, Publisher) assert o.get() == 'c' v2.emit(1) assert o.get() == 'b'
def test_concat(): v1 = StatefulPublisher((1, 2)) v2 = Value((0, 0)) o = v1 + v2 assert isinstance(o, Publisher) assert o.get() == (1, 2, 0, 0) v2.emit((1, 3)) assert o.get() == (1, 2, 1, 3)
def test_mod_str(): v1 = StatefulPublisher('%.2f %d') v2 = Value((0, 0)) o = v1 % v2 assert isinstance(o, Publisher) assert o.get() == '0.00 0' v2.emit((1, 3)) assert o.get() == '1.00 3'
def test_sink_partial(operator_cls): cb = mock.Mock() s = Value() sink_instance = s.subscribe(operator_cls(cb, 1, 2, 3, a=1)) assert isinstance(sink_instance, Disposable) assert not cb.called assert len(s.subscriptions) == 1 # test various emits on source s.emit(None) cb.assert_called_with(1, 2, 3, None, a=1) s.emit(1) cb.assert_called_with(1, 2, 3, 1, a=1) s.emit((1, 2)) cb.assert_called_with(1, 2, 3, (1, 2), a=1) # testing dispose() cb.reset_mock() sink_instance.dispose() assert len(s.subscriptions) == 0 s.emit(1) assert not cb.called
def test_sink_on_subscription(operator): cb = mock.Mock() s = Value(0) sink_instance = s.subscribe(operator(cb)) assert isinstance(sink_instance, Disposable) cb.assert_called_with(0) assert len(s.subscriptions) == 1 s.emit(1) cb.assert_called_with(1) # testing dispose() cb.reset_mock() sink_instance.dispose() assert len(s.subscriptions) == 0 s.emit(1) assert not cb.called
def test_in_operator(): pi = Value(1) ci = 1 pc = Value((1, 2, 3)) cc = (1, 2, 3) dut1 = op.In(pi, pc) dut2 = op.In(ci, pc) dut3 = op.In(pi, cc) with pytest.raises(TypeError): op.In(ci, cc) assert dut1.get() == True assert dut2.get() == True assert dut3.get() == True pi.emit('a') pc.emit((2.3, 'b')) assert dut1.get() == False assert dut2.get() == False assert dut3.get() == False
def test_sink2(operator_cls): cb = mock.Mock() s = Value() sink_instance = s.subscribe(operator_cls(cb, unpack=True)) assert isinstance(sink_instance, Disposable) # test various emits on source with pytest.raises(TypeError): s.emit() with pytest.raises(TypeError): s.emit(1) cb.assert_not_called() s.emit((1, 2)) cb.assert_called_with(1, 2)