Testbed stub for Google App Engine 'search'

2019-06-24 06:17发布

I am trying to test Google App Engine's new full text search functionality in Python with the development appserver.

Is there a stub for the search that allows one to test it with the testbed local unit testing?

The following is example code that throws an exception:

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

from google.appengine.api import search

def foo():
    d = search.Document(doc_id='X',
        fields=[search.TextField(name='abc', value='123')])
    s = search.Index(name='one').add(d)

tb = testbed.Testbed()
tb.activate()
# tb.init_search_stub() ## does this exist?

foo()

The exception thrown by foo() is: AssertionError: No api proxy found for service "search". Has an api proxy been written for search?

Thoughts and comments appreciated.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-24 06:49

It seems that since SDK 1.8.4 the search stub can be enabled from Testbed:

from google.appengine.api import search
from google.appengine.ext import testbed

try:
    tb = testbed.Testbed()
    tb.activate()
    tb.init_search_stub()
    index = search.Index(name='test')
    index.put(search.Document())
finally:
    tb.deactivate()
查看更多
一纸荒年 Trace。
3楼-- · 2019-06-24 06:57

UPDATE this was valid in 2012. Things changed in 2013: the stub is officially supported. See @siebz0r answer.

It's not in the list of supported stubs (yet, I assume), but there's a SearchServiceStub in simple_search_stub.py which looks like what you're after.

I haven't tested it myself but you could try do something like this:

testbed = testbed.Testbed()
testbed.activate()

stub = SearchServiceStub()
testbed._register_stub(SEARCH_SERVICE_NAME, stub)

SEARCH_SERVICE_NAME should be "search", and it should also be present in SUPPORTED_SERVICES list, otherwise testbed will raise an exception.

The way you "inject" this new service stub is either modify SDK's testbed/__init__.py or do it from your code. Can't really say which approach is better since it's gonna be a hack in either way, 'till the init_search_stub() will officially appear on the list.

Also, the fact that it's not in the list yet is probably because it's just not ready :) So, use it on your own risk.

查看更多
登录 后发表回答