Beispiel #1
0
 def setUp(self):
     self.order_book_cache = OrderBookCache(**{})
     self.runner = mock.Mock()
     self.runner.selection_id = 10895629
     self.runner.handicap = 0
     self.runner.serialise_orders = mock.Mock(return_value=[])
     self.runner.unmatched_orders = [1]
     self.order_book_cache.runners = {(10895629, 0): self.runner}
Beispiel #2
0
class TestOrderBookCache(unittest.TestCase):
    def setUp(self):
        self.order_book_cache = OrderBookCache(**{})
        self.runner = mock.Mock()
        self.runner.selection_id = 10895629
        self.runner.serialise_orders = mock.Mock(return_value=[])
        self.order_book_cache.runners = [self.runner]

    def test_update_cache(self):
        mock_response = create_mock_json(
            'tests/resources/streaming_ocm_UPDATE.json')
        for order_book in mock_response.json().get('oc'):
            self.order_book_cache.update_cache(order_book, 1234)

            for order_changes in order_book.get('orc'):
                # self.runner.matched_lays.update.assert_called_with(order_changes.get('ml', []))
                # self.runner.matched_backs.update.assert_called_with(order_book.get('mb', []))
                self.runner.update_unmatched.assert_called_with(
                    order_changes.get('uo', []))

    @mock.patch('betfairlightweight.streaming.cache.OrderBookRunner')
    def test_update_cache_new(self, mock_order_book_runner):
        self.runner.selection_id = 108956
        mock_response = create_mock_json(
            'tests/resources/streaming_ocm_UPDATE.json')
        for order_book in mock_response.json().get('oc'):
            self.order_book_cache.update_cache(order_book, 1234)

            for order_changes in order_book.get('orc'):
                mock_order_book_runner.assert_called_with(**order_changes)

    @mock.patch('betfairlightweight.streaming.cache.OrderBookCache.serialise')
    @mock.patch('betfairlightweight.streaming.cache.CurrentOrders')
    def test_create_resource(self, mock_current_orders, mock_serialise):
        current_orders = self.order_book_cache.create_resource(123, {}, False)

        assert current_orders == mock_current_orders()

    def test_runner_dict(self):
        class Runner:
            def __init__(self, selection_id, name):
                self.selection_id = selection_id
                self.name = name

        (a, b) = (Runner(123, 'a'), Runner(456, 'b'))
        self.order_book_cache.runners = [a, b]
        assert self.order_book_cache.runner_dict == {123: a, 456: b}

    def test_serialise(self):
        serialised = self.order_book_cache.serialise

        assert serialised == {'currentOrders': [], 'moreAvailable': False}
Beispiel #3
0
    def create_order_cache(self, order_books, publish_time):
        output_order_book = []
        for order_book in order_books:
            market_id = order_book['id']
            order_book_cache = self._order_caches.get(market_id)

            if order_book_cache is None:
                order_book_cache = OrderBookCache(publish_time=publish_time, **order_book)
                self._order_caches[market_id] = order_book_cache

            order_book_cache.update_cache(order_book, publish_time)

            output_order_book.append(
                order_book_cache.create_resource(54, order_book, True)
            )
        return output_order_book
Beispiel #4
0
class TestOrderBookCache(unittest.TestCase):
    def setUp(self):
        self.order_book_cache = OrderBookCache(**{})
        self.runner = mock.Mock()
        self.runner.selection_id = 10895629
        self.runner.handicap = 0
        self.runner.serialise_orders = mock.Mock(return_value=[])
        self.runner.unmatched_orders = [1]
        self.order_book_cache.runners = {(10895629, 0): self.runner}

    def test_full_image(self):
        self.order_book_cache.runners = {}
        mock_response = create_mock_json(
            "tests/resources/streaming_ocm_FULL_IMAGE.json"
        )
        for order_book in mock_response.json().get("oc"):
            self.order_book_cache.update_cache(order_book, 1234)
            self.assertEqual(self.order_book_cache.streaming_update, order_book)

        self.assertEqual(len(self.order_book_cache.runners), 5)
        for k, v in self.order_book_cache.runners.items():
            self.assertEqual(len(v.unmatched_orders), 1)

    def test_update_cache(self):
        mock_response = create_mock_json("tests/resources/streaming_ocm_UPDATE.json")
        for order_book in mock_response.json().get("oc"):
            self.order_book_cache.update_cache(order_book, 1234)
            self.assertEqual(self.order_book_cache.streaming_update, order_book)

            for order_changes in order_book.get("orc"):
                # self.runner.matched_lays.update.assert_called_with(order_changes.get('ml', []))
                # self.runner.matched_backs.update.assert_called_with(order_book.get('mb', []))
                self.runner.update_unmatched.assert_called_with(
                    order_changes.get("uo", [])
                )

    @mock.patch("betfairlightweight.streaming.cache.OrderBookRunner")
    def test_update_cache_new(self, mock_order_book_runner):
        self.order_book_cache.runners = {(108956, 0): self.runner}
        mock_response = create_mock_json("tests/resources/streaming_ocm_UPDATE.json")
        for order_book in mock_response.json().get("oc"):
            self.order_book_cache.update_cache(order_book, 1234)
            self.assertEqual(self.order_book_cache.streaming_update, order_book)

            for order_changes in order_book.get("orc"):
                mock_order_book_runner.assert_called_with(**order_changes)

    def test_update_cache_closed(self):
        mock_response = create_mock_json("tests/resources/streaming_ocm_SUB_IMAGE.json")
        for order_book in mock_response.json().get("oc"):
            self.order_book_cache.update_cache(order_book, 1234)
            self.assertEqual(self.order_book_cache.streaming_update, order_book)
        self.assertTrue(self.order_book_cache.closed)

    @mock.patch(
        "betfairlightweight.streaming.cache.OrderBookCache.serialise",
        new_callable=mock.PropertyMock,
        return_value={},
    )
    @mock.patch("betfairlightweight.streaming.cache.CurrentOrders")
    def test_create_resource(self, mock_current_orders, mock_serialise):
        # lightweight
        current_orders = self.order_book_cache.create_resource(123, True)
        assert current_orders == mock_serialise()
        assert current_orders == {
            "streaming_update": self.order_book_cache.streaming_update,
            "streaming_unique_id": 123,
            "streaming_snap": False,
        }
        # not lightweight
        current_orders = self.order_book_cache.create_resource(123, False)
        assert current_orders == mock_current_orders()

    def test_serialise(self):
        mock_runner_one = mock.Mock()
        mock_runner_one.serialise_orders.return_value = [1]
        mock_runner_two = mock.Mock()
        mock_runner_two.serialise_orders.return_value = [2, 3]
        self.order_book_cache.runners = {
            (123, 0): mock_runner_one,
            (123, 1): mock_runner_two,
        }
        serialised = self.order_book_cache.serialise
        assert serialised == {"currentOrders": [1, 2, 3], "moreAvailable": False}
class TestOrderBookCache(unittest.TestCase):
    def setUp(self):
        self.order_book_cache = OrderBookCache(**{})
        self.runner = mock.Mock()
        self.runner.selection_id = 10895629
        self.runner.handicap = 0
        self.runner.serialise_orders = mock.Mock(return_value=[])
        self.runner.unmatched_orders = [1]
        self.order_book_cache.runners = [self.runner]

    def test_full_image(self):
        mock_response = create_mock_json(
            'tests/resources/streaming_ocm_FULL_IMAGE.json')
        for order_book in mock_response.json().get('oc'):
            self.order_book_cache.update_cache(order_book, 1234)

        self.assertEqual(len(self.order_book_cache.runners), 5)
        self.assertEqual(len(self.order_book_cache.runner_dict), 5)
        for k, v in self.order_book_cache.runner_dict.items():
            if k == (7017905, 0):
                self.assertEqual(len(v.unmatched_orders), 2)
            else:
                self.assertEqual(len(v.unmatched_orders), 1)

    def test_update_cache(self):
        mock_response = create_mock_json(
            'tests/resources/streaming_ocm_UPDATE.json')
        for order_book in mock_response.json().get('oc'):
            self.order_book_cache.update_cache(order_book, 1234)

            for order_changes in order_book.get('orc'):
                # self.runner.matched_lays.update.assert_called_with(order_changes.get('ml', []))
                # self.runner.matched_backs.update.assert_called_with(order_book.get('mb', []))
                self.runner.update_unmatched.assert_called_with(
                    order_changes.get('uo', []))

    @mock.patch('betfairlightweight.streaming.cache.OrderBookRunner')
    def test_update_cache_new(self, mock_order_book_runner):
        self.runner.selection_id = 108956
        mock_response = create_mock_json(
            'tests/resources/streaming_ocm_UPDATE.json')
        for order_book in mock_response.json().get('oc'):
            self.order_book_cache.update_cache(order_book, 1234)

            for order_changes in order_book.get('orc'):
                mock_order_book_runner.assert_called_with(**order_changes)

    def test_update_cache_closed(self):
        mock_response = create_mock_json(
            'tests/resources/streaming_ocm_SUB_IMAGE.json')
        for order_book in mock_response.json().get('oc'):
            self.order_book_cache.update_cache(order_book, 1234)
        self.assertTrue(self.order_book_cache.closed)

    @mock.patch('betfairlightweight.streaming.cache.OrderBookCache.serialise',
                new_callable=mock.PropertyMock,
                return_value={})
    @mock.patch('betfairlightweight.streaming.cache.CurrentOrders')
    def test_create_resource(self, mock_current_orders, mock_serialise):
        # lightweight
        current_orders = self.order_book_cache.create_resource(
            123, {"test"}, True)
        assert current_orders == mock_serialise()
        assert current_orders == {
            'streaming_update': {"test"},
            'streaming_unique_id': 123
        }
        # not lightweight
        current_orders = self.order_book_cache.create_resource(123, {}, False)
        assert current_orders == mock_current_orders()

    def test_runner_dict(self):
        class Runner:
            def __init__(self, selection_id, name, handicap):
                self.selection_id = selection_id
                self.name = name
                self.handicap = handicap

        (a, b) = (Runner(123, 'a', 0), Runner(456, 'b', 1))
        self.order_book_cache.runners = [a, b]
        assert self.order_book_cache.runner_dict == {(123, 0): a, (456, 1): b}

    def test_serialise(self):
        serialised = self.order_book_cache.serialise

        assert serialised == {'currentOrders': [], 'moreAvailable': False}
Beispiel #6
0
 def setUp(self):
     self.order_book_cache = OrderBookCache(**{})
     self.runner = mock.Mock()
     self.runner.selection_id = 10895629
     self.runner.serialise_orders = mock.Mock(return_value=[])
     self.order_book_cache.runners = [self.runner]