def create_market_cache(self, market_books, publish_time): output_market_book = [] for market_book in market_books: market_id = market_book['id'] market_book_cache = self._market_caches.get(market_id) if market_book.get('img') or market_book_cache is None: # historic data does not contain img market_book_cache = MarketBookCache(publish_time=publish_time, **market_book) self._market_caches[market_id] = market_book_cache market_book_cache.update_cache(market_book, publish_time) output_market_book.append( market_book_cache.create_resource(123, market_book, True) ) return output_market_book
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)
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)
class TestMarketBookCache(unittest.TestCase): def setUp(self): self.market_book_cache = MarketBookCache("1.2345", 12345, True, False, False) def test_init(self): self.assertTrue(self.market_book_cache.active) self.assertEqual(self.market_book_cache.market_id, "1.2345") self.assertEqual(self.market_book_cache.publish_time, 12345) self.assertTrue(self.market_book_cache.lightweight) self.assertFalse(self.market_book_cache.calculate_market_tv) self.assertFalse(self.market_book_cache.cumulative_runner_tv) self.assertEqual(self.market_book_cache.total_matched, 0) self.assertEqual(self.market_book_cache.market_definition, {}) self.assertIsNone(self.market_book_cache._market_definition_resource) self.assertIsNone(self.market_book_cache._definition_bet_delay) self.assertIsNone(self.market_book_cache._definition_version) self.assertIsNone(self.market_book_cache._definition_complete) self.assertIsNone(self.market_book_cache._definition_runners_voidable) self.assertIsNone(self.market_book_cache._definition_status) self.assertIsNone(self.market_book_cache._definition_bsp_reconciled) self.assertIsNone(self.market_book_cache._definition_cross_matching) self.assertIsNone(self.market_book_cache._definition_in_play) self.assertIsNone(self.market_book_cache._definition_number_of_winners) self.assertIsNone( self.market_book_cache._definition_number_of_active_runners) self.assertIsNone( self.market_book_cache._definition_price_ladder_definition) self.assertIsNone( self.market_book_cache._definition_key_line_description) self.assertIsNone(self.market_book_cache.streaming_update) self.assertEqual(self.market_book_cache.runners, []) self.assertEqual(self.market_book_cache.runner_dict, {}) self.assertEqual(self.market_book_cache._number_of_runners, 0) @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, True) self.assertTrue(self.market_book_cache.active) 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, True) self.assertTrue(self.market_book_cache.active) assert self.market_book_cache.total_matched == book.get("tv") self.assertEqual(self.market_book_cache.streaming_update, book) def test_update_multiple_rc(self): # update data with multiple rc entries for the same selection data = { "rc": [ { "atb": [[1.01, 200]], "id": 13536143 }, { "atl": [[1000.0, 200]], "id": 13536143 }, ] } market_book_cache = MarketBookCache("1.123", 123, True, False, False) market_book_cache.update_cache(data, 123, True) self.assertTrue(market_book_cache.active) assert len(market_book_cache.runners) == len( market_book_cache.runner_dict) # @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') def test_refresh_cache(self): mock_runner = mock.Mock() self.market_book_cache.runners = [mock_runner] self.market_book_cache.refresh_cache() mock_runner.traded.refresh.assert_called() mock_runner.available_to_back.refresh.assert_called() mock_runner.available_to_lay.refresh.assert_called() mock_runner.best_available_to_back.refresh.assert_called() mock_runner.best_available_to_lay.refresh.assert_called() mock_runner.best_display_available_to_back.refresh.assert_called() mock_runner.best_display_available_to_lay.refresh.assert_called() mock_runner.starting_price_back.refresh.assert_called() mock_runner.starting_price_lay.refresh.assert_called() mock_runner.serialise.assert_called() @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, snap=True) assert market_book == { "streaming_unique_id": 1234, "streaming_snap": True, } assert market_book == mock_serialise() # not lightweight self.market_book_cache.lightweight = False market_book = self.market_book_cache.create_resource(1234, snap=True) 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) assert market_book == { "streaming_unique_id": 1234, "streaming_snap": True, } @mock.patch("betfairlightweight.streaming.cache.MarketBook") def test_create_resource_resource(self, mock_market_book): self.market_book_cache.lightweight = False self.assertEqual(self.market_book_cache.create_resource(1, False), mock_market_book()) # todo @mock.patch("betfairlightweight.streaming.cache.MarketDefinition") @mock.patch( "betfairlightweight.streaming.cache.MarketBookCache._add_new_runner") def test__process_market_definition(self, mock__add_new_runner, mock_market_definition_cls): self.market_book_cache.lightweight = False mock_market_definition = {"runners": [{"id": 12}]} self.market_book_cache._process_market_definition( mock_market_definition) self.assertEqual(self.market_book_cache.market_definition, mock_market_definition) self.assertEqual(self.market_book_cache.runner_dict, {}) mock__add_new_runner.assert_called_with(id=12, hc=0, definition={"id": 12}) mock__add_new_runner().serialise.assert_called() mock_market_definition = {"runners": [{"id": 34, "hc": 1}]} self.market_book_cache._process_market_definition( mock_market_definition) self.assertEqual(self.market_book_cache.market_definition, mock_market_definition) self.assertEqual(self.market_book_cache.runner_dict, {}) mock__add_new_runner.assert_called_with(id=34, hc=1, definition={ "id": 34, "hc": 1 }) mock__add_new_runner().serialise.assert_called() mock_market_definition_cls.assert_called_with(**mock_market_definition) self.assertEqual( self.market_book_cache._market_definition_resource, mock_market_definition_cls(), ) @mock.patch("betfairlightweight.streaming.cache.MarketDefinition") def test__process_market_definition_caches(self, mock_market_definition_cls): mock_market_definition = { "betDelay": 1, "version": 234, "complete": True, "runnersVoidable": False, "status": "ACTIVE", "bspReconciled": True, "crossMatching": False, "inPlay": True, "numberOfWinners": 5, "numberOfActiveRunners": 6, "priceLadderDefinition": "", } self.market_book_cache._process_market_definition( mock_market_definition) self.assertEqual(self.market_book_cache._definition_bet_delay, 1) self.assertEqual(self.market_book_cache._definition_version, 234) self.assertEqual(self.market_book_cache._definition_complete, True) self.assertEqual(self.market_book_cache._definition_runners_voidable, False) self.assertEqual(self.market_book_cache._definition_status, "ACTIVE") self.assertEqual(self.market_book_cache._definition_bsp_reconciled, True) self.assertEqual(self.market_book_cache._definition_cross_matching, False) self.assertEqual(self.market_book_cache._definition_in_play, True) self.assertEqual(self.market_book_cache._definition_number_of_winners, 5) self.assertEqual( self.market_book_cache._definition_number_of_active_runners, 6) self.assertEqual( self.market_book_cache._definition_price_ladder_definition, "") self.assertEqual( self.market_book_cache._definition_key_line_description, None) @mock.patch("betfairlightweight.streaming.cache.RunnerBookCache") def test__add_new_runner(self, mock_runner_book_cache): self.assertEqual(self.market_book_cache.runner_dict, {}) self.market_book_cache._add_new_runner(id=1, hc=2, definition={1: 2}) mock_runner_book_cache.assert_called_with(lightweight=True, id=1, hc=2, definition={1: 2}) self.assertEqual( self.market_book_cache.runner_dict, { ( mock_runner_book_cache().selection_id, mock_runner_book_cache().handicap, ): mock_runner_book_cache() }, ) self.assertEqual(self.market_book_cache.runners, [mock_runner_book_cache()]) self.assertEqual(self.market_book_cache._number_of_runners, 1) def test_closed(self): self.assertFalse(self.market_book_cache.closed) self.market_book_cache._definition_status = "CLOSED" self.assertTrue(self.market_book_cache.closed)
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}