Skip to content

Commit

Permalink
REF: Move ServicePatcher into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
cortadocodes committed Jul 16, 2024
1 parent 7364285 commit ebc3726
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 27 deletions.
20 changes: 0 additions & 20 deletions octue/cloud/emulators/child.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import json
import logging
import warnings
from unittest.mock import patch

from octue.cloud.emulators._pub_sub import MockSubscriber, MockSubscription, MockTopic
from octue.cloud.events.replayer import EventReplayer
from octue.utils.patches import MultiPatcher


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -109,20 +106,3 @@ def ask(
event_replayer = EventReplayer(handle_monitor_message=handle_monitor_message, record_events=record_events)
result = event_replayer.handle_events(self.events)
return (result, self.events[0].get("attributes", {}).get("question_uuid"))


class ServicePatcher(MultiPatcher):
"""A multi-patcher that provides the patches needed to run mock services.
:return None:
"""

def __init__(self):
super().__init__(
patches=[
patch("octue.cloud.pub_sub.service.Topic", new=MockTopic),
patch("octue.cloud.pub_sub.service.Subscription", new=MockSubscription),
patch("octue.cloud.pub_sub.events.SubscriberClient", new=MockSubscriber),
patch("google.cloud.pubsub_v1.SubscriberClient", new=MockSubscriber),
]
)
21 changes: 21 additions & 0 deletions octue/cloud/emulators/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from unittest.mock import patch

from octue.cloud.emulators._pub_sub import MockSubscriber, MockSubscription, MockTopic
from octue.utils.patches import MultiPatcher


class ServicePatcher(MultiPatcher):
"""A multi-patcher that provides the patches needed to run mock services.
:return None:
"""

def __init__(self):
super().__init__(
patches=[
patch("octue.cloud.pub_sub.service.Topic", new=MockTopic),
patch("octue.cloud.pub_sub.service.Subscription", new=MockSubscription),
patch("octue.cloud.pub_sub.events.SubscriberClient", new=MockSubscriber),
patch("google.cloud.pubsub_v1.SubscriberClient", new=MockSubscriber),
]
)
2 changes: 1 addition & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from octue.cloud import storage
from octue.cloud.emulators._pub_sub import MockTopic
from octue.cloud.emulators.child import ServicePatcher
from octue.cloud.emulators.cloud_storage import GoogleCloudStorageEmulatorTestResultModifier
from octue.cloud.emulators.service import ServicePatcher
from octue.cloud.events import OCTUE_SERVICES_PREFIX
from octue.cloud.storage import GoogleCloudStorageClient
from octue.resources import Datafile, Dataset, Manifest
Expand Down
3 changes: 2 additions & 1 deletion tests/cloud/emulators/test_child_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import os

from octue.cloud.emulators._pub_sub import MockService
from octue.cloud.emulators.child import ChildEmulator, ServicePatcher
from octue.cloud.emulators.child import ChildEmulator
from octue.cloud.emulators.service import ServicePatcher
from octue.resources.service_backends import GCPPubSubBackend
from tests import TESTS_DIR
from tests.base import BaseTestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/cloud/pub_sub/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest.mock import patch

from octue.cloud.emulators._pub_sub import MESSAGES, MockMessage, MockService, MockSubscription
from octue.cloud.emulators.child import ServicePatcher
from octue.cloud.emulators.service import ServicePatcher
from octue.cloud.pub_sub.events import GoogleCloudPubSubEventHandler
from octue.resources.service_backends import GCPPubSubBackend
from tests import TEST_PROJECT_NAME
Expand Down
2 changes: 1 addition & 1 deletion tests/cloud/pub_sub/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from unittest.mock import patch

from octue.cloud.emulators._pub_sub import MESSAGES, MockService
from octue.cloud.emulators.child import ServicePatcher
from octue.cloud.emulators.service import ServicePatcher
from octue.cloud.pub_sub.logging import GoogleCloudPubSubHandler
from octue.resources.service_backends import GCPPubSubBackend
from tests.base import BaseTestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/cloud/pub_sub/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
MockSubscription,
MockTopic,
)
from octue.cloud.emulators.child import ServicePatcher
from octue.cloud.emulators.cloud_storage import mock_generate_signed_url
from octue.cloud.emulators.service import ServicePatcher
from octue.cloud.pub_sub.service import Service
from octue.exceptions import InvalidMonitorMessage
from octue.resources import Analysis, Datafile, Dataset, Manifest
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/test_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from google.auth.exceptions import DefaultCredentialsError

from octue.cloud.emulators._pub_sub import MockAnalysis, MockService
from octue.cloud.emulators.child import ServicePatcher
from octue.cloud.emulators.service import ServicePatcher
from octue.resources.child import Child
from octue.resources.service_backends import GCPPubSubBackend
from tests import MOCK_SERVICE_REVISION_TAG
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from octue.cli import octue_cli
from octue.cloud import storage
from octue.cloud.emulators._pub_sub import MockService, MockSubscription, MockTopic
from octue.cloud.emulators.child import ServicePatcher
from octue.cloud.emulators.service import ServicePatcher
from octue.cloud.events import OCTUE_SERVICES_PREFIX
from octue.cloud.pub_sub import Topic
from octue.configuration import AppConfiguration, ServiceConfiguration
Expand Down

0 comments on commit ebc3726

Please sign in to comment.