def test_normalize(sub: Subscription): assert isinstance(sub._normalize([]), tuple) assert sub._normalize(['b', 'c', 'a']) == ('A', 'B', 'C') assert sub._normalize(['b', MarketEvents.TRADES, 'a']) == ('A', 'B', MarketEvents.TRADES.value) assert sub._normalize([MarketEvents.ORDERS, MarketEvents.CANCELS ]) == (MarketEvents.CANCELS.value, MarketEvents.ORDERS.value)
def test_check_events(sub: Subscription): evts = [MarketEvents.ORDERS, MarketEvents.ORDERS] assert sub._check_events(evts, MarketEvents) == evts with pytest.raises(ValueError): sub._check_events([AccountEvents.ORDERS, AccountEvents.TRADES], MarketEvents) assert sub._check_events( [AccountEvents.TRADES.value, AccountEvents.CANCELS.value], AccountEvents) == [ AccountEvents.TRADES.value, AccountEvents.CANCELS.value, ]
async def test_subscribe_warning(sm: SubscriptionManager): sm._logger.warning = Mock() send_mock = CoroutineMock() send_mock.return_value = 'some return value' sm._ds.send_message = send_mock sm._sub_payload = Mock() sub_payload_value = 'some value' sm._sub_payload.return_value = sub_payload_value sub = Subscription(category=Category.MARKET, events=[MarketEvents.CANCELS, MarketEvents.ORDERS], topics=['ETH_AURA', 'ETH_ZRX']) sm.subscriptions[sub.category] = {} result = await sm.subscribe(sub) sm._logger.warning.assert_called_once() sm._sub_payload.assert_called_once_with(Action.SUBSCRIBE, topics=sub.topics, events=sub.events) send_mock.assert_awaited_once_with(sub.category.value, sub_payload_value, None) assert result == 'some return value'
def _process_subscribe_action_response(self, category: Category, payload: Dict): self.subscriptions[category] = Subscription( category, payload['events'], payload['topics'] ) self._logger.info('Successfully subbed to category %s: %s', category, payload)
def test_sub_init(): category = Category.MARKET events = [MarketEvents.ORDERS, MarketEvents.CANCELS] topics = ['ETH_ZRX', 'ETH_AURA'] with patch('aioidex.types.subscriptions.Subscription._normalize') as m: s = Subscription(category=category, events=events, topics=topics) m.assert_has_calls([call(events), call(topics)]) assert isinstance(s.category, Category) assert s.category == category
async def test_resubscribe(sm: SubscriptionManager): sub = Subscription(category=Category.MARKET, events=[MarketEvents.CANCELS, MarketEvents.ORDERS], topics=['ETH_AURA', 'ETH_ZRX']) sm.subscriptions = {Category.MARKET: sub} sm._init_subscriptions = Mock() sm.subscribe = CoroutineMock() await sm.resubscribe() sm._init_subscriptions.assert_called_once() sm.subscribe.assert_awaited_once_with(sub)
def sub(): s = Subscription(category=Category.MARKET, events=[MarketEvents.ORDERS, MarketEvents.CANCELS], topics=['ETH_ZRX', 'ETH_AURA']) yield s