test function with Google App Engine `files` api

前端 未结 3 1455
借酒劲吻你
借酒劲吻你 2021-02-13 17:13

I have a function that uses the Google Blobstore API, and here\'s a degenerate case:

#!/usr/bin/python
from google.appengine.ext import testbed

def foo():
    f         


        
3条回答
  •  生来不讨喜
    2021-02-13 17:47

    It seems like testbed.init_blobstore_stub() is outdated, because dev_appserver inits blobstore stubs differently. Here is my implementation of init_blobstore_stub that allows you to write to and read from blobstore in your tests.

    from google.appengine.ext import testbed
    from google.appengine.api.blobstore import blobstore_stub, file_blob_storage
    from google.appengine.api.files import file_service_stub
    
    class TestbedWithFiles(testbed.Testbed):
    
        def init_blobstore_stub(self):
            blob_storage = file_blob_storage.FileBlobStorage('/tmp/testbed.blobstore',
                                                    testbed.DEFAULT_APP_ID)
            blob_stub = blobstore_stub.BlobstoreServiceStub(blob_storage)
            file_stub = file_service_stub.FileServiceStub(blob_storage)
            self._register_stub('blobstore', blob_stub)
            self._register_stub('file', file_stub)
    
    # Your code...
    def foo():
        from google.appengine.api import files
        blob_filename = files.blobstore.create(mime_type='text/plain')
        with files.open(blob_filename, 'a') as googfile:
            googfile.write("Test data")
    
        files.finalize(blob_filename)
    
    tb = TestbedWithFiles()
    tb.activate()
    tb.init_blobstore_stub()
    
    foo()
    

提交回复
热议问题