11

I have an API endpoint I'm trying to write unit tests for and I can't seem to figure out how to unit test the Python Google Cloud Storage client library calls (https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/). I was hoping to find a stub somewhere in the library and have it be as simple as unit testing the mail API would be (https://cloud.google.com/appengine/docs/python/tools/localunittesting?hl=en), but haven't found anything yet. Any idea how to go about this?

Ismail
  • 1,068
  • 1
  • 6
  • 11
Brandon
  • 2,886
  • 3
  • 29
  • 44
  • I have not found a way in the current test stubs that the SDK provides. You will probably be stuck using [mock](https://pypi.python.org/pypi/mock) to stub out the cloud storage api calls. – Josh J Sep 16 '15 at 18:04

2 Answers2

1

The list of available unit test does not list GCS. You can file a feature request on their GitHub to add that functionality.

In the mean time using the setUp for your tests to create files is probably your best bet.

Ryan
  • 2,512
  • 1
  • 13
  • 20
  • Good idea. I created an issue: https://github.com/GoogleCloudPlatform/appengine-gcs-client/issues/21 – Brandon Sep 22 '15 at 06:00
0

Since we have been bitten by severalGoogle API transistions so far (blobstore, blobstore with gs:/, cloudstorage, google-clound-storage) we have long created our own thin wrapper around all GCS access. This also includes stubbing for tests, like this:


def open(path, mode='w', bucket=None, content_type=None):
    if not bucket:
        bucket = app_identity.get_default_gcs_bucket_name()
    jsonpath = '/{}'.format(os.path.join(bucket, path))
    jsonpath = jsonpath.replace('*', str(datetime.date.today()))

    if os.environ.get(b'GAETK2_UNITTEST'):
        LOGGER.info('running unittest, GCS disabled')
        return StringIO()

    return cloudstorage.open(jsonpath, mode, content_type=content_type)

Still a lot of work if you want to retrofit that on a big application. But might be worth it - the next Google API depreciation will come.

max
  • 29,122
  • 12
  • 52
  • 79