Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test function with Google App Engine `files` api

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.

like image 523
Brian M. Hunt Avatar asked Nov 15 '11 00:11

Brian M. Hunt


People also ask

Is Google App Engine an API?

Use this RESTful API with any programming language to manage your App Engine applications.


2 Answers

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()
like image 121
2 revs Avatar answered Sep 20 '22 01:09

2 revs


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()
like image 43
Dinoboff Avatar answered Sep 24 '22 01:09

Dinoboff