From fbee41f5dd768833f0bd6cdc4627970fc8a6fa74 Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Fri, 7 Mar 2025 18:25:09 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(backend)=20avoid=20repeating?= =?UTF-8?q?=20'service'=20in=20python=20modules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These modules are already stored under the 'service' folder, it was redundant. Renamed these files based on @lunika feedbacks. --- src/backend/core/api/viewsets.py | 4 +- src/backend/core/authentication/backends.py | 2 +- ...it_events_service.py => livekit_events.py} | 2 +- .../services/{lobby_service.py => lobby.py} | 0 .../{marketing_service.py => marketing.py} | 0 .../tests/authentication/test_backends.py | 4 +- .../core/tests/rooms/test_api_rooms_lobby.py | 2 +- .../tests/rooms/test_api_rooms_webhook.py | 2 +- ...ents_service.py => test_livekit_events.py} | 4 +- .../{test_lobby_service.py => test_lobby.py} | 50 +++++++++---------- ...marketing_service.py => test_marketing.py} | 12 ++--- src/backend/meet/settings.py | 2 +- 12 files changed, 40 insertions(+), 44 deletions(-) rename src/backend/core/services/{livekit_events_service.py => livekit_events.py} (98%) rename src/backend/core/services/{lobby_service.py => lobby.py} (100%) rename src/backend/core/services/{marketing_service.py => marketing.py} (100%) rename src/backend/core/tests/services/{test_livekit_events_service.py => test_livekit_events.py} (97%) rename src/backend/core/tests/services/{test_lobby_service.py => test_lobby.py} (95%) rename src/backend/core/tests/services/{test_marketing_service.py => test_marketing.py} (94%) diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 92222621..d38994b1 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -39,11 +39,11 @@ from core.recording.worker.factories import ( from core.recording.worker.mediator import ( WorkerServiceMediator, ) -from core.services.livekit_events_service import ( +from core.services.livekit_events import ( LiveKitEventsService, LiveKitWebhookError, ) -from core.services.lobby_service import ( +from core.services.lobby import ( LobbyParticipantNotFound, LobbyService, ) diff --git a/src/backend/core/authentication/backends.py b/src/backend/core/authentication/backends.py index 46fa366f..71c6d21a 100644 --- a/src/backend/core/authentication/backends.py +++ b/src/backend/core/authentication/backends.py @@ -10,7 +10,7 @@ from mozilla_django_oidc.auth import ( ) from core.models import User -from core.services.marketing_service import ( +from core.services.marketing import ( ContactCreationError, ContactData, get_marketing_service, diff --git a/src/backend/core/services/livekit_events_service.py b/src/backend/core/services/livekit_events.py similarity index 98% rename from src/backend/core/services/livekit_events_service.py rename to src/backend/core/services/livekit_events.py index d40a42ed..b4cf9866 100644 --- a/src/backend/core/services/livekit_events_service.py +++ b/src/backend/core/services/livekit_events.py @@ -7,7 +7,7 @@ from django.conf import settings from livekit import api -from .lobby_service import LobbyService +from .lobby import LobbyService class LiveKitWebhookError(Exception): diff --git a/src/backend/core/services/lobby_service.py b/src/backend/core/services/lobby.py similarity index 100% rename from src/backend/core/services/lobby_service.py rename to src/backend/core/services/lobby.py diff --git a/src/backend/core/services/marketing_service.py b/src/backend/core/services/marketing.py similarity index 100% rename from src/backend/core/services/marketing_service.py rename to src/backend/core/services/marketing.py diff --git a/src/backend/core/tests/authentication/test_backends.py b/src/backend/core/tests/authentication/test_backends.py index 750b46c0..576f9c0d 100644 --- a/src/backend/core/tests/authentication/test_backends.py +++ b/src/backend/core/tests/authentication/test_backends.py @@ -9,7 +9,7 @@ import pytest from core import models from core.authentication.backends import OIDCAuthenticationBackend from core.factories import UserFactory -from core.services import marketing_service +from core.services import marketing pytestmark = pytest.mark.django_db @@ -535,7 +535,7 @@ def test_marketing_signup_handles_service_initialization_errors( @pytest.mark.parametrize( "error", [ - marketing_service.ContactCreationError, + marketing.ContactCreationError, ImproperlyConfigured, ImportError, ], diff --git a/src/backend/core/tests/rooms/test_api_rooms_lobby.py b/src/backend/core/tests/rooms/test_api_rooms_lobby.py index d7442734..25034a4b 100644 --- a/src/backend/core/tests/rooms/test_api_rooms_lobby.py +++ b/src/backend/core/tests/rooms/test_api_rooms_lobby.py @@ -14,7 +14,7 @@ from rest_framework.test import APIClient from ... import utils from ...factories import RoomFactory, UserFactory from ...models import RoomAccessLevel -from ...services.lobby_service import ( +from ...services.lobby import ( LobbyService, ) diff --git a/src/backend/core/tests/rooms/test_api_rooms_webhook.py b/src/backend/core/tests/rooms/test_api_rooms_webhook.py index 7b0cb7f1..bbac4328 100644 --- a/src/backend/core/tests/rooms/test_api_rooms_webhook.py +++ b/src/backend/core/tests/rooms/test_api_rooms_webhook.py @@ -12,7 +12,7 @@ from unittest import mock import pytest from livekit import api -from ...services.livekit_events_service import ActionFailedError, LiveKitEventsService +from ...services.livekit_events import ActionFailedError, LiveKitEventsService @pytest.fixture diff --git a/src/backend/core/tests/services/test_livekit_events_service.py b/src/backend/core/tests/services/test_livekit_events.py similarity index 97% rename from src/backend/core/tests/services/test_livekit_events_service.py rename to src/backend/core/tests/services/test_livekit_events.py index c55762b1..0a78d0a7 100644 --- a/src/backend/core/tests/services/test_livekit_events_service.py +++ b/src/backend/core/tests/services/test_livekit_events.py @@ -8,7 +8,7 @@ from unittest import mock import pytest -from core.services.livekit_events_service import ( +from core.services.livekit_events import ( ActionFailedError, AuthenticationError, InvalidPayloadError, @@ -16,7 +16,7 @@ from core.services.livekit_events_service import ( UnsupportedEventTypeError, api, ) -from core.services.lobby_service import LobbyService +from core.services.lobby import LobbyService pytestmark = pytest.mark.django_db diff --git a/src/backend/core/tests/services/test_lobby_service.py b/src/backend/core/tests/services/test_lobby.py similarity index 95% rename from src/backend/core/tests/services/test_lobby_service.py rename to src/backend/core/tests/services/test_lobby.py index 6a553853..1382aa05 100644 --- a/src/backend/core/tests/services/test_lobby_service.py +++ b/src/backend/core/tests/services/test_lobby.py @@ -18,7 +18,7 @@ from livekit.api import TwirpError from core.factories import RoomFactory from core.models import RoomAccessLevel -from core.services.lobby_service import ( +from core.services.lobby import ( LobbyNotificationError, LobbyParticipant, LobbyParticipantNotFound, @@ -309,7 +309,7 @@ def test_request_entry_trusted_room( lobby_service._get_participant.assert_called_once_with(room.id, participant_id) -@mock.patch("core.services.lobby_service.LobbyService.enter") +@mock.patch("core.services.lobby.LobbyService.enter") def test_request_entry_new_participant( mock_enter, lobby_service, participant_id, username ): @@ -338,7 +338,7 @@ def test_request_entry_new_participant( lobby_service._get_participant.assert_called_once_with(room.id, participant_id) -@mock.patch("core.services.lobby_service.LobbyService.refresh_waiting_status") +@mock.patch("core.services.lobby.LobbyService.refresh_waiting_status") def test_request_entry_waiting_participant( mock_refresh, lobby_service, participant_id, username ): @@ -400,7 +400,7 @@ def test_request_entry_accepted_participant( lobby_service._get_participant.assert_called_once_with(room.id, participant_id) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_refresh_waiting_status(mock_cache, lobby_service, participant_id): """Test refreshing waiting status for a participant.""" lobby_service._get_cache_key = mock.Mock(return_value="mocked_cache_key") @@ -412,9 +412,9 @@ def test_refresh_waiting_status(mock_cache, lobby_service, participant_id): # pylint: disable=R0917 -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") @mock.patch("core.utils.generate_color") -@mock.patch("core.services.lobby_service.LobbyService.notify_participants") +@mock.patch("core.services.lobby.LobbyService.notify_participants") def test_enter_success( mock_notify, mock_generate_color, @@ -447,9 +447,9 @@ def test_enter_success( # pylint: disable=R0917 -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") @mock.patch("core.utils.generate_color") -@mock.patch("core.services.lobby_service.LobbyService.notify_participants") +@mock.patch("core.services.lobby.LobbyService.notify_participants") def test_enter_with_notification_error( mock_notify, mock_generate_color, @@ -479,7 +479,7 @@ def test_enter_with_notification_error( ) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_get_participant_not_found(mock_cache, lobby_service, participant_id): """Test getting a participant that doesn't exist.""" mock_cache.get.return_value = None @@ -494,8 +494,8 @@ def test_get_participant_not_found(mock_cache, lobby_service, participant_id): mock_cache.get.assert_called_once_with("mocked_cache_key") -@mock.patch("core.services.lobby_service.cache") -@mock.patch("core.services.lobby_service.LobbyParticipant.from_dict") +@mock.patch("core.services.lobby.cache") +@mock.patch("core.services.lobby.LobbyParticipant.from_dict") def test_get_participant_parsing_error( mock_from_dict, mock_cache, lobby_service, participant_id ): @@ -512,7 +512,7 @@ def test_get_participant_parsing_error( mock_cache.delete.assert_called_once_with("mocked_cache_key") -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_list_waiting_participants_empty(mock_cache, lobby_service): """Test listing waiting participants when none exist.""" mock_cache.keys.return_value = [] @@ -526,7 +526,7 @@ def test_list_waiting_participants_empty(mock_cache, lobby_service): mock_cache.get_many.assert_not_called() -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_list_waiting_participants(mock_cache, lobby_service, participant_dict): """Test listing waiting participants with valid data.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -544,7 +544,7 @@ def test_list_waiting_participants(mock_cache, lobby_service, participant_dict): mock_cache.get_many.assert_called_once_with([cache_key]) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_list_waiting_participants_multiple(mock_cache, lobby_service): """Test listing multiple waiting participants with valid data.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -587,7 +587,7 @@ def test_list_waiting_participants_multiple(mock_cache, lobby_service): mock_cache.get_many.assert_called_once_with([cache_key1, cache_key2]) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_list_waiting_participants_corrupted_data(mock_cache, lobby_service): """Test listing waiting participants with corrupted data.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -601,7 +601,7 @@ def test_list_waiting_participants_corrupted_data(mock_cache, lobby_service): mock_cache.delete.assert_called_once_with(cache_key) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_list_waiting_participants_partially_corrupted(mock_cache, lobby_service): """Test listing waiting participants with one valid and one corrupted entry.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -640,7 +640,7 @@ def test_list_waiting_participants_partially_corrupted(mock_cache, lobby_service mock_cache.get_many.assert_called_once_with([cache_key1, cache_key2]) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_list_waiting_participants_non_waiting(mock_cache, lobby_service): """Test listing only waiting participants (not accepted/denied).""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -673,7 +673,7 @@ def test_list_waiting_participants_non_waiting(mock_cache, lobby_service): assert result[0]["status"] == "waiting" -@mock.patch("core.services.lobby_service.LobbyService._update_participant_status") +@mock.patch("core.services.lobby.LobbyService._update_participant_status") def test_handle_participant_entry_allow(mock_update, lobby_service, participant_id): """Test handling allowed participant entry.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -687,7 +687,7 @@ def test_handle_participant_entry_allow(mock_update, lobby_service, participant_ ) -@mock.patch("core.services.lobby_service.LobbyService._update_participant_status") +@mock.patch("core.services.lobby.LobbyService._update_participant_status") def test_handle_participant_entry_deny(mock_update, lobby_service, participant_id): """Test handling denied participant entry.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -701,7 +701,7 @@ def test_handle_participant_entry_deny(mock_update, lobby_service, participant_i ) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_update_participant_status_not_found(mock_cache, lobby_service, participant_id): """Test updating status for non-existent participant.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -720,8 +720,8 @@ def test_update_participant_status_not_found(mock_cache, lobby_service, particip mock_cache.get.assert_called_once_with("mocked_cache_key") -@mock.patch("core.services.lobby_service.cache") -@mock.patch("core.services.lobby_service.LobbyParticipant.from_dict") +@mock.patch("core.services.lobby.cache") +@mock.patch("core.services.lobby.LobbyParticipant.from_dict") def test_update_participant_status_corrupted_data( mock_from_dict, mock_cache, lobby_service, participant_id ): @@ -743,7 +743,7 @@ def test_update_participant_status_corrupted_data( lobby_service._get_cache_key.assert_called_once_with(room.id, participant_id) -@mock.patch("core.services.lobby_service.cache") +@mock.patch("core.services.lobby.cache") def test_update_participant_status_success(mock_cache, lobby_service, participant_id): """Test successful participant status update.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -776,7 +776,7 @@ def test_update_participant_status_success(mock_cache, lobby_service, participan lobby_service._get_cache_key.assert_called_once_with(room.id, participant_id) -@mock.patch("core.services.lobby_service.LiveKitAPI") +@mock.patch("core.services.lobby.LiveKitAPI") def test_notify_participants_success(mock_livekit_api, lobby_service): """Test successful participant notification.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) @@ -807,7 +807,7 @@ def test_notify_participants_success(mock_livekit_api, lobby_service): mock_api_instance.aclose.assert_called_once() -@mock.patch("core.services.lobby_service.LiveKitAPI") +@mock.patch("core.services.lobby.LiveKitAPI") def test_notify_participants_error(mock_livekit_api, lobby_service): """Test participant notification with API error.""" room = RoomFactory(access_level=RoomAccessLevel.RESTRICTED) diff --git a/src/backend/core/tests/services/test_marketing_service.py b/src/backend/core/tests/services/test_marketing.py similarity index 94% rename from src/backend/core/tests/services/test_marketing_service.py rename to src/backend/core/tests/services/test_marketing.py index 910fbae5..6a19c70d 100644 --- a/src/backend/core/tests/services/test_marketing_service.py +++ b/src/backend/core/tests/services/test_marketing.py @@ -12,7 +12,7 @@ from django.core.exceptions import ImproperlyConfigured import brevo_python import pytest -from core.services.marketing_service import ( +from core.services.marketing import ( BrevoMarketingService, ContactCreationError, ContactData, @@ -144,9 +144,7 @@ def clear_marketing_cache(): def test_get_marketing_service_caching(clear_marketing_cache): """Test marketing service caching behavior.""" settings.BREVO_API_KEY = "test-api-key" - settings.MARKETING_SERVICE_CLASS = ( - "core.services.marketing_service.BrevoMarketingService" - ) + settings.MARKETING_SERVICE_CLASS = "core.services.marketing.BrevoMarketingService" service1 = get_marketing_service() service2 = get_marketing_service() @@ -163,14 +161,12 @@ def test_get_marketing_service_invalid_class(clear_marketing_cache): get_marketing_service() -@mock.patch("core.services.marketing_service.import_string") +@mock.patch("core.services.marketing.import_string") def test_service_instantiation_called_once(mock_import_string, clear_marketing_cache): """Test service class is instantiated only once.""" settings.BREVO_API_KEY = "test-api-key" - settings.MARKETING_SERVICE_CLASS = ( - "core.services.marketing_service.BrevoMarketingService" - ) + settings.MARKETING_SERVICE_CLASS = "core.services.marketing.BrevoMarketingService" get_marketing_service.cache_clear() mock_service_cls = mock.Mock() diff --git a/src/backend/meet/settings.py b/src/backend/meet/settings.py index f25cf0a9..10f44c38 100755 --- a/src/backend/meet/settings.py +++ b/src/backend/meet/settings.py @@ -484,7 +484,7 @@ class Base(Configuration): environ_prefix=None, ) MARKETING_SERVICE_CLASS = values.Value( - "core.services.marketing_service.BrevoMarketingService", + "core.services.marketing.BrevoMarketingService", environ_name="MARKETING_SERVICE_CLASS", environ_prefix=None, )