Ejemplo n.º 1
0
class BaseListenerTest(unittest.TestCase):

    def setUp(self):
        self.base_listener = BaseListener()

    def test_init(self):
        assert self.base_listener.streams == {}

    def test_register_stream(self):
        self.base_listener.register_stream(1, 'heartbeat')

    def test_on_data(self):
        self.base_listener.on_data({}, 2)
Ejemplo n.º 2
0
 def setUp(self):
     self.base_listener = BaseListener()
Ejemplo n.º 3
0
class BaseListenerTest(unittest.TestCase):
    def setUp(self):
        self.base_listener = BaseListener()

    def test_init(self):
        assert self.base_listener.connection_id is None
        assert self.base_listener.status is None
        assert self.base_listener.stream is None
        assert self.base_listener.stream_unique_id is None
        assert self.base_listener.stream_type is None
        assert self.base_listener.max_latency == 0.5
        assert self.base_listener.connections_available is None

    @mock.patch(
        "betfairlightweight.streaming.listener.BaseListener._add_stream",
        return_value=123,
    )
    def test_register_stream(self, mock_add_stream):
        self.base_listener.register_stream(1, "authentication")

        self.base_listener.register_stream(2, "marketSubscription")
        mock_add_stream.assert_called_with(2, "marketSubscription")
        assert self.base_listener.stream == 123

        self.base_listener.market_stream = "test"
        self.base_listener.register_stream(2, "marketSubscription")
        mock_add_stream.assert_called_with(2, "marketSubscription")
        assert self.base_listener.stream == 123

        self.base_listener.register_stream(3, "orderSubscription")
        mock_add_stream.assert_called_with(3, "orderSubscription")
        assert self.base_listener.stream == 123

        self.base_listener.order_stream = "test"
        self.base_listener.register_stream(3, "orderSubscription")
        mock_add_stream.assert_called_with(3, "orderSubscription")
        assert self.base_listener.stream == 123

        self.base_listener.register_stream(2, "raceSubscription")
        mock_add_stream.assert_called_with(2, "raceSubscription")
        assert self.base_listener.stream == 123

        self.base_listener.register_stream(2, "raceSubscription")
        mock_add_stream.assert_called_with(2, "raceSubscription")
        assert self.base_listener.stream == 123

        self.base_listener.register_stream(2, "cricketSubscription")
        mock_add_stream.assert_called_with(2, "cricketSubscription")
        assert self.base_listener.stream == 123

        self.base_listener.register_stream(2, "cricketSubscription")
        mock_add_stream.assert_called_with(2, "cricketSubscription")
        assert self.base_listener.stream == 123

    def test_update_unique_id(self):
        self.base_listener.stream = mock.Mock()
        self.base_listener.update_unique_id(987)
        self.assertEqual(self.base_listener.stream_unique_id, 987)
        self.assertEqual(self.base_listener.stream.unique_id, 987)

    def test_on_data(self):
        self.base_listener.on_data("{}")

    @mock.patch("betfairlightweight.streaming.listener.CricketStream", return_value=321)
    @mock.patch("betfairlightweight.streaming.listener.RaceStream", return_value=789)
    @mock.patch("betfairlightweight.streaming.listener.OrderStream", return_value=456)
    @mock.patch("betfairlightweight.streaming.listener.MarketStream", return_value=123)
    def test_add_stream(
        self,
        mock_market_stream,
        mock_order_stream,
        mock_race_stream,
        mock_cricket_stream,
    ):
        new_stream = self.base_listener._add_stream(1, "marketSubscription")
        assert new_stream == 123
        mock_market_stream.assert_called_with(self.base_listener, 1)

        new_stream = self.base_listener._add_stream(1, "orderSubscription")
        assert new_stream == 456
        mock_order_stream.assert_called_with(self.base_listener, 1)

        new_stream = self.base_listener._add_stream(1, "raceSubscription")
        assert new_stream == 789
        mock_race_stream.assert_called_with(self.base_listener, 1)

        new_stream = self.base_listener._add_stream(1, "cricketSubscription")
        assert new_stream == 321
        mock_race_stream.assert_called_with(self.base_listener, 1)

    def test_snap(self):
        mock_stream = mock.Mock()
        self.base_listener.stream = mock_stream
        self.base_listener.stream_type = None
        assert self.base_listener.snap() == []

        self.base_listener.stream_type = "test"
        assert self.base_listener.snap() == mock_stream.snap(None)

    def test_props(self):
        assert self.base_listener.initial_clk is None
        assert self.base_listener.clk is None
        stream = mock.Mock()
        stream._initial_clk = 2
        stream._clk = 1
        self.base_listener.stream = stream
        assert self.base_listener.initial_clk == 2
        assert self.base_listener.clk == 1

    def test_updates_processed(self):
        assert self.base_listener.updates_processed is None

        mock_stream = mock.Mock()
        mock_stream._updates_processed = 100
        self.base_listener.stream = mock_stream
        assert self.base_listener.updates_processed == 100

    def test_str(self):
        assert str(self.base_listener) == "BaseListener"

    def test_repr(self):
        assert repr(self.base_listener) == "<BaseListener>"
Ejemplo n.º 4
0
 def setUp(self):
     self.base_listener = BaseListener()
Ejemplo n.º 5
0
class BaseListenerTest(unittest.TestCase):

    def setUp(self):
        self.base_listener = BaseListener()

    def test_init(self):
        assert self.base_listener.connection_id is None
        assert self.base_listener.stream is None
        assert self.base_listener.stream_unique_id is None
        assert self.base_listener.stream_type is None
        assert self.base_listener.max_latency == 0.5

    @mock.patch('betfairlightweight.streaming.listener.BaseListener._add_stream', return_value=123)
    def test_register_stream(self, mock_add_stream):
        self.base_listener.register_stream(1, 'authentication')

        self.base_listener.register_stream(2, 'marketSubscription')
        mock_add_stream.assert_called_with(2, 'marketSubscription')
        assert self.base_listener.stream == 123

        self.base_listener.market_stream = 'test'
        self.base_listener.register_stream(2, 'marketSubscription')
        mock_add_stream.assert_called_with(2, 'marketSubscription')
        assert self.base_listener.stream == 123

        self.base_listener.register_stream(3, 'orderSubscription')
        mock_add_stream.assert_called_with(3, 'orderSubscription')
        assert self.base_listener.stream == 123

        self.base_listener.order_stream = 'test'
        self.base_listener.register_stream(3, 'orderSubscription')
        mock_add_stream.assert_called_with(3, 'orderSubscription')
        assert self.base_listener.stream == 123

    def test_on_data(self):
        self.base_listener.on_data({})

    def test_add_stream(self):
        self.base_listener._add_stream(1, 'operation')

    def test_snap(self):
        mock_stream = mock.Mock()
        self.base_listener.stream = None
        assert self.base_listener.snap() == []

        self.base_listener.stream = mock_stream
        assert self.base_listener.snap() == mock_stream.snap(None)

    def test_props(self):
        assert self.base_listener.initial_clk is None
        assert self.base_listener.clk is None
        stream = mock.Mock()
        stream._initial_clk = 2
        stream._clk = 1
        self.base_listener.stream = stream
        assert self.base_listener.initial_clk == 2
        assert self.base_listener.clk == 1

    def test_str(self):
        assert str(self.base_listener) == 'BaseListener'

    def test_repr(self):
        assert repr(self.base_listener) == '<BaseListener>'
Ejemplo n.º 6
0
class BaseListenerTest(unittest.TestCase):

    def setUp(self):
        self.base_listener = BaseListener()

    def test_init(self):
        assert self.base_listener.market_stream is None
        assert self.base_listener.order_stream is None
        assert self.base_listener.max_latency == 0.5

    @mock.patch('betfairlightweight.streaming.listener.BaseListener._add_stream', return_value=123)
    @mock.patch('sys.stdout')
    def test_register_stream(self, mock_print, mock_add_stream):
        self.base_listener.register_stream(1, 'authentication')

        self.base_listener.register_stream(2, 'marketSubscription')
        mock_add_stream.assert_called_with(2, 'marketSubscription')
        assert self.base_listener.market_stream == 123

        self.base_listener.market_stream = 'test'
        self.base_listener.register_stream(2, 'marketSubscription')
        mock_add_stream.assert_called_with(2, 'marketSubscription')
        assert self.base_listener.market_stream == 123

        self.base_listener.register_stream(3, 'orderSubscription')
        mock_add_stream.assert_called_with(3, 'orderSubscription')
        assert self.base_listener.order_stream == 123

        self.base_listener.order_stream = 'test'
        self.base_listener.register_stream(3, 'orderSubscription')
        mock_add_stream.assert_called_with(3, 'orderSubscription')
        assert self.base_listener.order_stream == 123

    @mock.patch('sys.stdout')
    def test_on_data(self, mock_print):
        self.base_listener.on_data({})

    @mock.patch('sys.stdout')
    def test_add_stream(self, mock_print):
        self.base_listener._add_stream(1, 'operation')

    def test_str(self):
        assert str(self.base_listener) == '<BaseListener>'

    def test_repr(self):
        assert repr(self.base_listener) == '<BaseListener>'