13
votes

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():
    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 = testbed.Testbed()
tb.activate()
tb.init_blobstore_stub()

foo() # in reality, I'm a function called from a 'faux client'
      # in a unittest testcase.

The error this generates is:

Traceback (most recent call last):
  File "e.py", line 18, in 
    foo() # in reality, I'm a function called from a 'faux client'
  File "e.py", line 8, in foo
    blob_filename = files.blobstore.create(mime_type='text/plain')
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/blobstore.py", line 68, in create
    return files._create(_BLOBSTORE_FILESYSTEM, params=params)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 491, in _create
    _make_call('Create', request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 230, in _make_call
    rpc = _create_rpc(deadline=deadline)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/files/file.py", line 213, in _create_rpc
    return apiproxy_stub_map.UserRPC('file', deadline)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 393, in __init__
    self.__rpc = CreateRPC(service, stubmap)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 67, in CreateRPC
    assert stub, 'No api proxy found for service "%s"' % service
AssertionError: No api proxy found for service "file"

I don't want to have to modify foo in order to be able to test it. Is there a way to make foo work as expected (i.e. create the given file) in Google App Engine's unit tests?

I would expect to be able to do this with Google's API Proxy, but I don't understand it well enough to figure it out on my own.

I'd be grateful for your thoughts and suggestions.

Thanks for reading.

3
Are you having these errors when pushing live or running locally? Do you have te latest version of the SDK?Eduardo
@eduardocereto: The unit tests are run locally, and it is the latest SDK (1.6.0).Brian M. Hunt

3 Answers

15
votes

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()
3
votes

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()
2
votes

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.