4

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.

Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343

2 Answers2

10

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.

alex
  • 2,450
  • 16
  • 22
  • The answer by @siebz0r is the better one for readers of this question, going forward, so I marked it as correct. This answer was great in the interim, though. Cheers. – Brian M. Hunt Nov 29 '13 at 18:55
5

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()
siebz0r
  • 18,867
  • 14
  • 64
  • 107