def test_api_get_last_traded_prices(self): prices = self.ev_loop.run_until_complete( KucoinAPIOrderBookDataSource.get_last_traded_prices(["BTC-USDT", "LTC-USDT"])) for key, value in prices.items(): print(f"{key} last_trade_price: {value}") self.assertGreater(prices["BTC-USDT"], 1000) self.assertLess(prices["LTC-USDT"], 1000)
def test_get_last_traded_prices(self, mock_api): url = CONSTANTS.BASE_PATH_URL + CONSTANTS.TICKER_PRICE_CHANGE_PATH_URL resp = { "data": { "ticker": [{ "symbol": self.trading_pair, "last": 100.0, }] } } mock_api.get(url, body=json.dumps(resp)) ret = self.async_run_with_timeout( coroutine=KucoinAPIOrderBookDataSource.get_last_traded_prices( [self.trading_pair])) self.assertEqual(ret[self.trading_pair], 100)
def test_get_last_traded_prices(self, mock_api): KucoinAPIOrderBookDataSource._trading_pair_symbol_map[ CONSTANTS.DEFAULT_DOMAIN]["TKN1-TKN2"] = "TKN1-TKN2" url1 = web_utils.rest_url( path_url=CONSTANTS.TICKER_PRICE_CHANGE_PATH_URL, domain=CONSTANTS.DEFAULT_DOMAIN) url1 = f"{url1}?symbol={self.trading_pair}" regex_url = re.compile(f"^{url1}".replace(".", r"\.").replace("?", r"\?")) resp = { "code": "200000", "data": { "sequence": "1550467636704", "bestAsk": "0.03715004", "size": "0.17", "price": "100", "bestBidSize": "3.803", "bestBid": "0.03710768", "bestAskSize": "1.788", "time": 1550653727731 } } mock_api.get(regex_url, body=json.dumps(resp)) url2 = web_utils.rest_url( path_url=CONSTANTS.TICKER_PRICE_CHANGE_PATH_URL, domain=CONSTANTS.DEFAULT_DOMAIN) url2 = f"{url2}?symbol=TKN1-TKN2" regex_url = re.compile(f"^{url2}".replace(".", r"\.").replace("?", r"\?")) resp = { "code": "200000", "data": { "sequence": "1550467636704", "bestAsk": "0.03715004", "size": "0.17", "price": "200", "bestBidSize": "3.803", "bestBid": "0.03710768", "bestAskSize": "1.788", "time": 1550653727731 } } mock_api.get(regex_url, body=json.dumps(resp)) ret = self.async_run_with_timeout( coroutine=KucoinAPIOrderBookDataSource.get_last_traded_prices( [self.trading_pair, "TKN1-TKN2"])) ticker_requests = [(key, value) for key, value in mock_api.requests.items() if key[1].human_repr().startswith(url1) or key[1].human_repr().startswith(url2)] request_params = ticker_requests[0][1][0].kwargs["params"] self.assertEqual(f"{self.base_asset}-{self.quote_asset}", request_params["symbol"]) request_params = ticker_requests[1][1][0].kwargs["params"] self.assertEqual("TKN1-TKN2", request_params["symbol"]) self.assertEqual(ret[self.trading_pair], 100) self.assertEqual(ret["TKN1-TKN2"], 200)