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(): 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? Commented Nov 15, 2011 at 4:16
  • @eduardocereto: The unit tests are run locally, and it is the latest SDK (1.6.0). Commented Nov 15, 2011 at 14:33
  • Here is a related public issue: code.google.com/p/googleappengine/issues/detail?id=5301 Commented Dec 1, 2011 at 11:52

3 Answers 3

15

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() 
Sign up to request clarification or add additional context in comments.

3 Comments

this did the trick for me after I manually put a DEFAULT_APP_ID on testbed.
This solution is fantastic! Thank you for sharing.
I am getting an exception in my test when creating one inside a ndb transaction (AttributeError: 'Entity' object has no attribute '_to_pb')
3

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

Comments

2

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.

7 Comments

Thanks Nathan. I'm using the local unit testing (i.e. not gaeunit). I'm also using the Python 2.7 runtime. What's "highly experimental mode"?
With "highly experimental mode" I was just referring to the use of Python 2.7, nothing more than that. Given your use of the local unit testing, I'm surprised you're seeing the issue as it's not occurring for me in that context with very similar code to yours. Good luck!
I cannot reproduce the results you suggest. If you take the example in the question and put it into a .py file and execute it, do you not get the same result as above?
Your code above includes a bad reference to pdf.read. I replaced that with 'Test data' and then the code successfully runs without error for me in my environment. Have you tried running the exact code above outside of any test runner etc? I can't find any modifications to the modules used in this code that I've made.
The error set out in the question is reproducible. Are you running the code on the command line in Mac OS X with GAE 1.6.0?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.