octue/octue-sdk-python

Make octue services easy to test and patch

nvn-nil opened this issue · 1 comments

Feature request

Use Case

Create Google Cloud clients only as required to make it easy to test applications using octue. I recommend reviewing other uses of Google clients too.

Or maybe provide an escape hatch by providing an offline/local mode for tests.

Thanks in advance!

Current state

The clients for different google services are created in the init function. This makes patching very difficult for tests.

I'm monkey-patching this particular one in my tests. This was easier than patching many other functions that use the client when I'm only doing local unit tests.

# Monkey patch the unnecessary credential check in init
def diagnostics_init(self, cloud_path):
    self.cloud_path = cloud_path
    self.analysis_id = None
    self.configuration_values = None
    self.configuration_manifest = None
    self.input_values = None
    self.input_manifest = None
    self.questions = []


@cached_property
def diagnostic_storage_client_property(self):
    return GoogleCloudStorageClient()


Diagnostics.__init__ = diagnostics_init
Diagnostics._storage_client = diagnostic_storage_client_property

This is obviously not ideal for a variety of reasons but was easier in my case.

Thanks for the idea @nvn-nil! I've applied it to:

  • Diagnostics
  • Topic
  • Subscription
  • GoogleCloudPubSubEventHandler

I'd already implemented the same thing in another way in Service (I think at your request a while ago).