Unit testing Google's Cloud Storage API

前端 未结 2 1801
迷失自我
迷失自我 2021-02-20 16:38

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.g

2条回答
  •  离开以前
    2021-02-20 16:56

    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.

提交回复
热议问题