Example #1
0
    def test_strip_datetime(self):
        base_resource = BaseResource()
        for string in [
                "2100-06-01T10:10:00.000Z",
                "2100-06-01T10:10:00.00Z",
                "2100-06-01T10:10:00.0Z",
        ]:
            stripped = base_resource.strip_datetime(string)
            assert type(stripped) == datetime.datetime

        stripped = base_resource.strip_datetime(None)
        assert not stripped

        stripped = base_resource.strip_datetime("45")
        assert not stripped

        integer = 1465631675000
        stripped = base_resource.strip_datetime(integer)
        assert type(stripped) == datetime.datetime

        stripped = base_resource.strip_datetime(None)
        assert not stripped

        stripped = base_resource.strip_datetime("45")
        assert not stripped
Example #2
0
 def snap(self, market_ids: list = None) -> list:
     market_books = []
     for cache in list(self._caches.values()):
         if market_ids and cache.market_id not in market_ids:
             continue
         # if market has closed send regardless
         if cache.market_definition["status"] != "CLOSED":
             if self._listener.inplay:
                 if not cache.market_definition["inPlay"]:
                     continue
             elif self._listener.seconds_to_start:
                 _now = datetime.datetime.utcfromtimestamp(cache.publish_time / 1e3)
                 _market_time = BaseResource.strip_datetime(
                     cache.market_definition["marketTime"]
                 )
                 seconds_to_start = (_market_time - _now).total_seconds()
                 if seconds_to_start > self._listener.seconds_to_start:
                     continue
             if self._listener.inplay is False:
                 if cache.market_definition["inPlay"]:
                     continue
         market_books.append(
             cache.create_resource(self.unique_id, None, self._lightweight)
         )
     return market_books
 def test_init(self):
     base_resource = BaseResource()
     assert base_resource._datetime_created is not None
     assert base_resource._datetime_updated is not None
     assert base_resource.elapsed_time is None
     assert base_resource._response is None
     assert base_resource._data == {}
Example #4
0
 def test_init(self):
     assert self.unmatched_order.bet_id == 1
     assert self.unmatched_order.price == 2
     assert self.unmatched_order.size == 3
     assert self.unmatched_order.side == "L"
     assert self.unmatched_order.status == "E"
     assert self.unmatched_order.persistence_type == "L"
     assert self.unmatched_order.order_type == "L"
     assert self.unmatched_order.placed_date == BaseResource.strip_datetime(8)
     assert self.unmatched_order.size_matched == 9
     assert self.unmatched_order.size_remaining == 10
     assert self.unmatched_order.size_lapsed == 11
     assert self.unmatched_order.size_cancelled == 12
     assert self.unmatched_order.size_voided == 13
     assert self.unmatched_order.reference_order == 14
     assert self.unmatched_order.reference_strategy == 15
     assert self.unmatched_order.lapsed_date == BaseResource.strip_datetime(16)
     assert self.unmatched_order.lapse_status_reason_code == 17
Example #5
0
    def test_data(self):
        mock_response = create_mock_json('tests/resources/base_resource.json')
        base_resource = BaseResource(date_time_sent=datetime.datetime(2003, 8, 4, 12, 30, 45),
                                     **mock_response.json())

        assert base_resource._datetime_sent == datetime.datetime(2003, 8, 4, 12, 30, 45)
        assert base_resource.id == 12345
        assert base_resource.elapsed_time > 0
        assert base_resource._data == mock_response.json()
Example #6
0
 def test_init(self):
     base_resource = BaseResource()
     assert base_resource.Meta.identifier == 'id'
     assert base_resource.Meta.attributes == {'id': 'id'}
     assert base_resource.Meta.sub_resources == {}
     assert base_resource.Meta.datetime_attributes == ()
     assert base_resource._datetime_sent is None
     assert base_resource.datetime_created is not None
     assert base_resource._datetime_updated is not None
     assert base_resource.elapsed_time is None
     assert base_resource.id is None
     assert base_resource._data == {}
     with self.assertRaises(AttributeError):
         assert base_resource.not_in
Example #7
0
    def test_strip_datetime(self):
        base_resource = BaseResource()
        for string in ['2100-06-01T10:10:00.000Z', '2100-06-01T10:10:00.00Z', '2100-06-01T10:10:00.0Z']:
            stripped = base_resource.strip_datetime(string)
            assert type(stripped) == datetime.datetime

        stripped = base_resource.strip_datetime(None)
        assert not stripped

        stripped = base_resource.strip_datetime('45')
        assert not stripped

        integer = 1465631675000
        stripped = base_resource.strip_datetime(integer)
        assert type(stripped) == datetime.datetime

        stripped = base_resource.strip_datetime(None)
        assert not stripped

        stripped = base_resource.strip_datetime('45')
        assert not stripped

        stripped = base_resource.strip_datetime(-1230000000345446)
        assert not stripped
    def test_strip_datetime(self):
        base_resource = BaseResource()
        for string in ['2100-06-01T10:10:00.000Z', '2100-06-01T10:10:00.00Z', '2100-06-01T10:10:00.0Z']:
            stripped = base_resource.strip_datetime(string)
            assert type(stripped) == datetime.datetime

        stripped = base_resource.strip_datetime(None)
        assert not stripped

        stripped = base_resource.strip_datetime('45')
        assert not stripped

        integer = 1465631675000
        stripped = base_resource.strip_datetime(integer)
        assert type(stripped) == datetime.datetime

        stripped = base_resource.strip_datetime(None)
        assert not stripped

        stripped = base_resource.strip_datetime('45')
        assert not stripped
Example #9
0
 def snap(self, market_ids: list = None) -> list:
     market_books = []
     for cache in list(self._caches.values()):
         if market_ids and cache.market_id not in market_ids:
             continue
         # if market is not open (closed/suspended) send regardless
         if cache._definition_status == "OPEN":
             if self._listener.inplay:
                 if not cache._definition_in_play:
                     continue
             elif self._listener.seconds_to_start:
                 _now = datetime.datetime.utcfromtimestamp(
                     cache.publish_time / 1e3)
                 _market_time = BaseResource.strip_datetime(
                     cache.market_definition["marketTime"])
                 seconds_to_start = (_market_time - _now).total_seconds()
                 if seconds_to_start > self._listener.seconds_to_start:
                     continue
             if self._listener.inplay is False:
                 if cache._definition_in_play:
                     continue
         market_books.append(
             cache.create_resource(self.unique_id, snap=True))
     return market_books
 def test_data_json(self):
     mock_response = create_mock_json('tests/resources/base_resource.json')
     base_resource = BaseResource(date_time_sent=datetime.datetime(2003, 8, 4, 12, 30, 45),
                                  **mock_response.json())
     assert base_resource.json() == json.dumps(mock_response.json())
Example #11
0
 def test_matched_date_string(self):
     now = BaseResource.strip_datetime(4)
     assert self.unmatched_order.matched_date_string == now.strftime(
         "%Y-%m-%dT%H:%M:%S.%fZ"
     )
Example #12
0
    def _process(self, data: list, publish_time: int) -> bool:
        active = False
        for market_book in data:
            if "id" not in market_book:
                continue
            market_id = market_book["id"]
            full_image = market_book.get("img", False)
            market_book_cache = self._caches.get(market_id)

            if (full_image or market_book_cache is
                    None):  # historic data does not contain img
                if "marketDefinition" not in market_book:
                    logger.warning(
                        "[%s: %s]: Missing marketDefinition on market %s resulting "
                        "in potential missing data in the MarketBook (make sure "
                        "EX_MARKET_DEF is requested)" %
                        (self, self.unique_id, market_id))
                market_book_cache = MarketBookCache(
                    market_id,
                    publish_time,
                    self._lightweight,
                    self._calculate_market_tv,
                    self._cumulative_runner_tv,
                )
                self._caches[market_id] = market_book_cache
                logger.info(
                    "[%s: %s]: %s added, %s markets in cache" %
                    (self, self.unique_id, market_id, len(self._caches)))

            # listener_kwargs filtering
            active = True
            if "marketDefinition" in market_book:
                _definition_status = market_book["marketDefinition"].get(
                    "status")
                _definition_in_play = market_book["marketDefinition"].get(
                    "inPlay")
                _definition_market_time = market_book["marketDefinition"].get(
                    "marketTime")
            else:
                _definition_status = market_book_cache._definition_status
                _definition_in_play = market_book_cache._definition_in_play
                _definition_market_time = market_book_cache.market_definition[
                    "marketTime"]

            # if market is not open (closed/suspended) process regardless
            if _definition_status == "OPEN":
                if self._listener.inplay:
                    if not _definition_in_play:
                        active = False
                elif self._listener.seconds_to_start:
                    _now = datetime.datetime.utcfromtimestamp(publish_time /
                                                              1e3)
                    _market_time = BaseResource.strip_datetime(
                        _definition_market_time)
                    seconds_to_start = (_market_time - _now).total_seconds()
                    if seconds_to_start > self._listener.seconds_to_start:
                        active = False
                if self._listener.inplay is False:
                    if _definition_in_play:
                        active = False
            # check if refresh required
            if active and not market_book_cache.active:
                market_book_cache.refresh_cache()

            market_book_cache.update_cache(market_book,
                                           publish_time,
                                           active=active)
            self._updates_processed += 1
        return active
Example #13
0
 def test_placed_date_string(self):
     now = BaseResource.strip_datetime(8)
     assert self.unmatched_order.placed_date_string == now.strftime(
         '%Y-%m-%dT%H:%M:%S.%fZ')
Example #14
0
 def test_str_repr(self):
     base_resource = BaseResource()
     assert str(base_resource) == "BaseResource"
     assert repr(base_resource) == "<BaseResource>"
Example #15
0
 def test_data_json(self):
     mock_response = create_mock_json("tests/resources/base_resource.json")
     base_resource = BaseResource(elapsed_time=1.2, **mock_response.json())
     assert base_resource.json() == json.dumps(mock_response.json())
Example #16
0
    def test_data(self):
        mock_response = create_mock_json("tests/resources/base_resource.json")
        base_resource = BaseResource(elapsed_time=1.2, **mock_response.json())

        assert base_resource.elapsed_time == 1.2
        assert base_resource._data == mock_response.json()
Example #17
0
 def test_response(self):
     mock_response = mock.Mock()
     base_resource = BaseResource(_response=mock_response)
     assert base_resource._response == mock_response
Example #18
0
 def test_data_json(self):
     mock_response = create_mock_json('tests/resources/base_resource.json')
     base_resource = BaseResource(date_time_sent=datetime.datetime(2003, 8, 4, 12, 30, 45),
                                  **mock_response.json())
     assert base_resource.json() == json.dumps(mock_response.json())
Example #19
0
 def test_str_repr(self):
     base_resource = BaseResource()
     assert str(base_resource) == 'BaseResource'
     assert repr(base_resource) == '<BaseResource>'