test function with Google App Engine `files` api

前端 未结 3 1456
借酒劲吻你
借酒劲吻你 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:29

    Any chance that you are trying to do this using the gaeunit.py test runner? I see the same error while using that, since it does it's own code to replace the api proxy.

    The error disappears when I added 'file' to the "as-is" list of proxies in the _run_test_suite function of gaeunit.py.

    Honestly, I'm not sure that the gaeunit.py proxy replacement code is needed at all since I'm also using the more recently recommended testbed code in the test cases as per http://code.google.com/appengine/docs/python/tools/localunittesting.html. So, at this point I've commented it all out of gaeunit.py, which also seems to be working.

    Note that I'm doing all this on a dev server only, in highly experimental mode on python27 in GAE with Python 2.7.

    Hope this helps.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2021-02-13 17:53

    I don't know if it was added later to the SDK, but using Testbed.init_files_stub should fix it:

    tb = testbed.Testbed()
    tb.activate()
    tb.init_blobstore_stub()
    tb.init_files_stub()
    
    0 讨论(0)
提交回复
热议问题