Esempio n. 1
0
class TestMarketBookCache(unittest.TestCase):
    def setUp(self):
        self.market_book_cache = MarketBookCache(
            **{'marketDefinition': {
                'runners': {}
            }})

    def test_error(self):
        with self.assertRaises(CacheError):
            self.market_book_cache = MarketBookCache()

    @mock.patch(
        'betfairlightweight.streaming.cache.MarketBookCache.strip_datetime')
    def test_update_cache_md(self, mock_strip_datetime):
        publish_time = mock.Mock()
        market_change = create_mock_json(
            'tests/resources/streaming_mcm_UPDATE_md.json')
        book_data = market_change.json().get('mc')

        for book in book_data:
            self.market_book_cache.update_cache(book, publish_time)
            mock_strip_datetime.assert_called_with(publish_time)
            assert self.market_book_cache.market_definition == book.get(
                'marketDefinition')

    @mock.patch(
        'betfairlightweight.streaming.cache.MarketBookCache.strip_datetime')
    def test_update_cache_tv(self, mock_strip_datetime):
        publish_time = mock.Mock()
        market_change = create_mock_json(
            'tests/resources/streaming_mcm_UPDATE_tv.json')
        book_data = market_change.json().get('mc')

        for book in book_data:
            self.market_book_cache.update_cache(book, publish_time)
            mock_strip_datetime.assert_called_with(publish_time)
            assert self.market_book_cache.total_matched == book.get('tv')

    # @mock.patch('betfairlightweight.resources.streamingresources.MarketBookCache.strip_datetime')
    # def test_update_cache_rc(self, mock_strip_datetime):
    #     publish_time = mock.Mock()
    #     market_change = create_mock_json('tests/resources/streaming_mcm_UPDATE.json')
    #     book_data = market_change.json().get('mc')
    #
    #     for book in book_data:
    #         self.market_book_cache.update_cache(book, publish_time)
    #         mock_strip_datetime.assert_called_with(publish_time)
    #
    #         assert self.market_book_cache.total_matched == book.get('tv')

    @mock.patch('betfairlightweight.streaming.cache.MarketBookCache.serialise',
                new_callable=mock.PropertyMock,
                return_value={})
    @mock.patch('betfairlightweight.streaming.cache.MarketDefinition')
    @mock.patch('betfairlightweight.streaming.cache.MarketBook')
    def test_create_resource(self, mock_market_book, mock_market_definition,
                             mock_serialise):
        # lightweight
        market_book = self.market_book_cache.create_resource(
            1234, {"test"}, True)
        assert market_book == {
            'streaming_update': {"test"},
            'streaming_unique_id': 1234
        }
        assert market_book == mock_serialise()
        # not lightweight
        market_book = self.market_book_cache.create_resource(1234, {}, False)
        assert market_book == mock_market_book()

    def test_update_runner_dict(self):
        assert self.market_book_cache.runner_dict == {}

        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', 1.25), Runner(456, 'b', -0.25))
        self.market_book_cache.runners = [a, b]
        self.market_book_cache._update_runner_dict()
        assert self.market_book_cache.runner_dict == {
            (123, 1.25): a,
            (456, -0.25): b
        }

    def test_init_multiple_rc(self):
        # Initialize data with multiple rc entries for the same selection
        data = {'marketDefinition': {'runners': {}}}
        data['rc'] = [{
            'atb': [[1.01, 200]],
            'id': 13536143
        }, {
            'atl': [[1000.0, 200]],
            'id': 13536143
        }]

        market_book_cache = MarketBookCache(**data)

        assert len(market_book_cache.runners) == len(
            market_book_cache.runner_dict)
Esempio n. 2
0
class TestMarketBookCache(unittest.TestCase):
    def setUp(self):
        self.market_book_cache = MarketBookCache(
            **{"marketDefinition": {"runners": {}}}
        )

    def test_error(self):
        with self.assertRaises(CacheError):
            self.market_book_cache = MarketBookCache()

    @mock.patch("betfairlightweight.streaming.cache.MarketBookCache.strip_datetime")
    def test_update_cache_md(self, mock_strip_datetime):
        publish_time = mock.Mock()
        market_change = create_mock_json("tests/resources/streaming_mcm_UPDATE_md.json")
        book_data = market_change.json().get("mc")

        for book in book_data:
            self.market_book_cache.update_cache(book, publish_time)
            mock_strip_datetime.assert_called_with(publish_time)
            assert self.market_book_cache.market_definition == book.get(
                "marketDefinition"
            )
            self.assertEqual(self.market_book_cache.streaming_update, book)

    @mock.patch("betfairlightweight.streaming.cache.MarketBookCache.strip_datetime")
    def test_update_cache_tv(self, mock_strip_datetime):
        publish_time = mock.Mock()
        market_change = create_mock_json("tests/resources/streaming_mcm_UPDATE_tv.json")
        book_data = market_change.json().get("mc")

        for book in book_data:
            self.market_book_cache.update_cache(book, publish_time)
            mock_strip_datetime.assert_called_with(publish_time)
            assert self.market_book_cache.total_matched == book.get("tv")
            self.assertEqual(self.market_book_cache.streaming_update, book)

    # @mock.patch('betfairlightweight.resources.streamingresources.MarketBookCache.strip_datetime')
    # def test_update_cache_rc(self, mock_strip_datetime):
    #     publish_time = mock.Mock()
    #     market_change = create_mock_json('tests/resources/streaming_mcm_UPDATE.json')
    #     book_data = market_change.json().get('mc')
    #
    #     for book in book_data:
    #         self.market_book_cache.update_cache(book, publish_time)
    #         mock_strip_datetime.assert_called_with(publish_time)
    #
    #         assert self.market_book_cache.total_matched == book.get('tv')

    @mock.patch(
        "betfairlightweight.streaming.cache.MarketBookCache.serialise",
        new_callable=mock.PropertyMock,
        return_value={},
    )
    @mock.patch("betfairlightweight.streaming.cache.MarketDefinition")
    @mock.patch("betfairlightweight.streaming.cache.MarketBook")
    def test_create_resource(
        self, mock_market_book, mock_market_definition, mock_serialise
    ):
        # lightweight
        market_book = self.market_book_cache.create_resource(1234, True)
        assert market_book == {
            "streaming_update": self.market_book_cache.streaming_update,
            "streaming_unique_id": 1234,
            "streaming_snap": False,
        }
        assert market_book == mock_serialise()
        # not lightweight
        market_book = self.market_book_cache.create_resource(1234, False)
        assert market_book == mock_market_book()

    @mock.patch(
        "betfairlightweight.streaming.cache.MarketBookCache.serialise",
        new_callable=mock.PropertyMock,
        return_value={},
    )
    @mock.patch("betfairlightweight.streaming.cache.MarketDefinition")
    @mock.patch("betfairlightweight.streaming.cache.MarketBook")
    def test_create_resource_snap(self, *_):
        market_book = self.market_book_cache.create_resource(1234, True, True)
        assert market_book == {
            "streaming_update": self.market_book_cache.streaming_update,
            "streaming_unique_id": 1234,
            "streaming_snap": True,
        }

    def test_update_runner_dict(self):
        assert self.market_book_cache.runner_dict == {}

        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", 1.25), Runner(456, "b", -0.25))
        self.market_book_cache.runners = [a, b]
        self.market_book_cache._update_runner_dict()
        assert self.market_book_cache.runner_dict == {(123, 1.25): a, (456, -0.25): b}

    def test_init_multiple_rc(self):
        # Initialize data with multiple rc entries for the same selection
        data = {"marketDefinition": {"runners": {}}}
        data["rc"] = [
            {"atb": [[1.01, 200]], "id": 13536143},
            {"atl": [[1000.0, 200]], "id": 13536143},
        ]

        market_book_cache = MarketBookCache(**data)

        assert len(market_book_cache.runners) == len(market_book_cache.runner_dict)

    def test_closed(self):
        self.assertFalse(self.market_book_cache.closed)
        self.market_book_cache.market_definition = {"status": "CLOSED"}
        self.assertTrue(self.market_book_cache.closed)
Esempio n. 3
0
class TestMarketBookCache(unittest.TestCase):

    def setUp(self):
        self.market_book_cache = MarketBookCache(**{'marketDefinition': {'runners': {}}})

    def test_error(self):
        with self.assertRaises(CacheError):
            self.market_book_cache = MarketBookCache()

    @mock.patch('betfairlightweight.streaming.cache.MarketBookCache.strip_datetime')
    def test_update_cache_md(self, mock_strip_datetime):
        publish_time = mock.Mock()
        market_change = create_mock_json('tests/resources/streaming_mcm_UPDATE_md.json')
        book_data = market_change.json().get('mc')

        for book in book_data:
            self.market_book_cache.update_cache(book, publish_time)
            mock_strip_datetime.assert_called_with(publish_time)
            assert self.market_book_cache.market_definition == book.get('marketDefinition')

    @mock.patch('betfairlightweight.streaming.cache.MarketBookCache.strip_datetime')
    def test_update_cache_tv(self, mock_strip_datetime):
        publish_time = mock.Mock()
        market_change = create_mock_json('tests/resources/streaming_mcm_UPDATE_tv.json')
        book_data = market_change.json().get('mc')

        for book in book_data:
            self.market_book_cache.update_cache(book, publish_time)
            mock_strip_datetime.assert_called_with(publish_time)
            assert self.market_book_cache.total_matched == book.get('tv')

    # @mock.patch('betfairlightweight.resources.streamingresources.MarketBookCache.strip_datetime')
    # def test_update_cache_rc(self, mock_strip_datetime):
    #     publish_time = mock.Mock()
    #     market_change = create_mock_json('tests/resources/streaming_mcm_UPDATE.json')
    #     book_data = market_change.json().get('mc')
    #
    #     for book in book_data:
    #         self.market_book_cache.update_cache(book, publish_time)
    #         mock_strip_datetime.assert_called_with(publish_time)
    #
    #         assert self.market_book_cache.total_matched == book.get('tv')

    @mock.patch('betfairlightweight.streaming.cache.MarketBookCache.serialise')
    @mock.patch('betfairlightweight.streaming.cache.MarketDefinition')
    @mock.patch('betfairlightweight.streaming.cache.MarketBook')
    def test_create_resource(self, mock_market_book, mock_market_definition, mock_serialise):
        # lightweight
        market_book = self.market_book_cache.create_resource(1234, {}, True)
        assert market_book == mock_serialise
        # not lightweight
        market_book = self.market_book_cache.create_resource(1234, {}, False)
        assert market_book == mock_market_book()

    def test_update_runner_dict(self):
        assert self.market_book_cache.runner_dict == {}

        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', 1.25), Runner(456, 'b', -0.25))
        self.market_book_cache.runners = [a, b]
        self.market_book_cache._update_runner_dict()
        assert self.market_book_cache.runner_dict == {(123, 1.25): a, (456, -0.25): b}