Skip to content

Commit

Permalink
[Community] update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Oct 4, 2024
1 parent 6841e3e commit 6f7bb8d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
23 changes: 9 additions & 14 deletions octobot/community/feeds/community_mqtt_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ async def stop(self):
self._reset()
self.logger.debug("Stopped")

async def restart(self):
try:
if not self.should_stop:
await self.stop()
await self.start()
except Exception as err:
self.logger.exception(err, True, f"{err}")

def _reset(self):
self._connected_at_least_once = False
self._stop_on_cfg_action = None
Expand All @@ -128,12 +120,15 @@ def _get_default_subscription_topics(self) -> set:
return set(self._default_callbacks_by_subscription_topic)

def _build_default_callbacks_by_subscription_topic(self) -> dict:
return {
self._build_topic(
commons_enums.CommunityChannelTypes.CONFIGURATION,
self.authenticator.get_saved_mqtt_device_uuid()
): [self._config_feed_callback, ]
}
try:
return {
self._build_topic(
commons_enums.CommunityChannelTypes.CONFIGURATION,
self.authenticator.get_saved_mqtt_device_uuid()
): [self._config_feed_callback, ]
}
except errors.NoBotDeviceError:
return {}

async def _config_feed_callback(self, data: dict):
"""
Expand Down
4 changes: 2 additions & 2 deletions octobot/community/feeds/community_supabase_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import octobot.community.supabase_backend.enums as enums
import octobot.community.feeds.abstract_feed as abstract_feed
import octobot.constants as constants
import octobot.enums as enums
import octobot.enums


class CommunitySupabaseFeed(abstract_feed.AbstractFeed):
Expand Down Expand Up @@ -78,7 +78,7 @@ async def _process_message(self, table: str, message: dict):
except Exception as err:
self.logger.exception(err, True, f"Unexpected error when processing message: {err}")

async def start(self, stop_on_cfg_action: typing.Optional[enums.CommunityConfigurationActions]):
async def start(self, stop_on_cfg_action: typing.Optional[octobot.enums.CommunityConfigurationActions]):
# handled in supabase client directly, just ensure no subscriptions are pending
for table in self.feed_callbacks:
await self._subscribe_to_table_if_necessary(table)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/community/test_community_mqtt_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ async def connected_community_feed(authenticator):
mock.patch.object(gmqtt.Client, "connect", mock.AsyncMock()) as _connect_mock:
await feed.register_feed_callback(commons_enums.CommunityChannelTypes.SIGNAL, mock.AsyncMock())
_subscribe_mock.assert_called_once_with((f"{commons_enums.CommunityChannelTypes.SIGNAL.value}/None", ))
await feed.start()
get_selected_bot_device_uuid_mock.assert_called_once()
await feed.start(None)
assert get_selected_bot_device_uuid_mock.call_count == 2
_connect_mock.assert_called_once_with(FEED_URL, feed.mqtt_broker_port, version=feed.MQTT_VERSION)
yield feed
finally:
Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/community/test_community_supabase_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ async def test_start_and_connect(authenticated_feed):
# without feed_callbacks
with mock.patch.object(authenticated_feed.authenticator.supabase_client, "get_subscribed_channel_tables",
mock.Mock(return_value=[])) as get_subscribed_channel_tables_mock:
await authenticated_feed.start()
await authenticated_feed.start(None)
is_logged_in_mock.assert_not_called()
_subscribe_to_table_mock.assert_not_called()
get_subscribed_channel_tables_mock.assert_not_called()
# with feed_callbacks
authenticated_feed.feed_callbacks = {"signals": None, "plopplop":None}
await authenticated_feed.start()
await authenticated_feed.start(None)
assert is_logged_in_mock.call_count == 2
# no sub channel: call _subscribe_to_table
assert _subscribe_to_table_mock.call_count == 2
Expand All @@ -106,7 +106,7 @@ async def test_start_and_connect(authenticated_feed):
with mock.patch.object(authenticated_feed.authenticator.supabase_client, "get_subscribed_channel_tables",
mock.Mock(return_value=["signals"])) as get_subscribed_channel_tables_mock:
# sub channel on signals: call _subscribe_to_table just for plopplop
await authenticated_feed.start()
await authenticated_feed.start(None)
is_logged_in_mock.assert_called_once()
# no sub channel: call _subscribe_to_table
_subscribe_to_table_mock.assert_called_with("plopplop")
Expand All @@ -120,7 +120,7 @@ async def test_start_and_connect(authenticated_feed):
with mock.patch.object(authenticated_feed.authenticator.supabase_client, "get_subscribed_channel_tables",
mock.Mock(return_value=["plop"])), \
mock.patch.object(authenticated_feed.authenticator, "is_logged_in", mock.Mock(return_value=False)):
await authenticated_feed.start()
await authenticated_feed.start(None)

async def test_connection(authenticated_feed):
with mock.patch.object(authenticated_feed.authenticator, "is_logged_in", mock.Mock(return_value=True)) \
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/community/test_community_ws_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async def connected_community_feed(authenticator):
as _fetch_stream_identifier_mock:
await feed.register_feed_callback(commons_enums.CommunityChannelTypes.SIGNAL, mock.AsyncMock())
_fetch_stream_identifier_mock.assert_called_once_with(None)
await feed.start()
await feed.start(None)
yield feed
finally:
if feed is not None:
Expand Down Expand Up @@ -217,7 +217,7 @@ async def test_reconnect(authenticator):
client = community.CommunityWSFeed(f"ws://{HOST}:{PORT}", authenticator)
client.RECONNECT_DELAY = 0
await client.register_feed_callback(commons_enums.CommunityChannelTypes.SIGNAL, client_handler)
await client.start()
await client.start(None)

# 1. ensure client is both receiving and sending messages
client_handler.assert_not_called()
Expand Down

0 comments on commit 6f7bb8d

Please sign in to comment.