def test_to_task_with_payload(self): """Ensure to_task with a payload produces the right task object.""" import datetime import time from google.appengine.ext import testbed from furious.batcher import Message testbed = testbed.Testbed() testbed.activate() # This just drops the microseconds. It is a total mess, but is needed # to handle all the rounding crap. eta = datetime.datetime.now() + datetime.timedelta(minutes=43) eta_posix = time.mktime(eta.timetuple()) task_args = {'eta': eta_posix, 'payload': [1, 2, 3]} options = {'task_args': task_args} task = Message.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = { 'Host': 'testbed.example.com', 'X-AppEngine-Current-Namespace': '' } self.assertEqual(eta_posix, task.eta_posix) self.assertEqual(full_headers, task.headers) self.assertEqual(task_args['payload'], json.loads(task.payload))
def SetUpTestbedTestCase(case): """Set up appengine testbed enviroment.""" case.testbed = testbed.Testbed() case.testbed.activate() # The oauth_aware decorator will 302 to login unless there is either # a current user _or_ a valid oauth header; this is easier to stub. case.testbed.setup_env(user_email='*****@*****.**', user_id='1234', overwrite=True) case.testbed.init_all_stubs() policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) case.testbed.init_datastore_v3_stub(consistency_policy=policy) case.testbed.init_taskqueue_stub(_all_queues_valid=True) os.environ['AUTH_DOMAIN'] = 'example.com' # Lazily stub out key-fetching RPC dependency. def Stub(data, **_): return data case.patches = [ mock.patch.object(crypto, 'Decrypt', side_effect=Stub), mock.patch.object(crypto, 'Encrypt', side_effect=Stub), ] for m in case.patches: m.start()
def bootstrap(sdk_path): sys.path.insert(0, sdk_path) # Force the google module to reload, for some reason on windows this can sometimes point to the one in site-packages. import google reload(google) import dev_appserver dev_appserver.fix_sys_path() sys.path = [re.sub('webob_0_9', 'webob_1_1_1', x) for x in sys.path] import fix_imports (fix_imports) # Activate a testbed so that httplib2 always knows that it's in app engine from google.appengine.ext import testbed testbed = testbed.Testbed() testbed.activate() testbed.init_urlfetch_stub() # Remove agressive logging rootLogger = logging.getLogger() for handler in rootLogger.handlers: if isinstance(handler, logging.StreamHandler): rootLogger.removeHandler(handler)
def _test(**kwargs): """ This stub uses the testbed to initialize the bare minimum to use the Datastore connector. Tests themselves should setup/tear down their own stubs by using DjangaeDiscoverRunner or the nose plugin. The stubs here are just for bootstrapping the tests. Obviously any data inserted between here, and the tests themselves will be wiped out when the tests begin! """ from google.appengine.ext import testbed from google.appengine.datastore import datastore_stub_util MINIMAL_STUBS = { "init_memcache_stub": {}, "init_datastore_v3_stub": { "use_sqlite": True, "auto_id_policy": testbed.AUTO_ID_POLICY_SCATTERED, "consistency_policy": datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) } } testbed = testbed.Testbed() testbed.activate() for init_name, stub_kwargs in MINIMAL_STUBS.items(): getattr(testbed, init_name)(**stub_kwargs) yield if testbed: testbed.deactivate()
def handle(self, **options): from google.appengine.ext import testbed testbed = testbed.Testbed() testbed.activate() testbed.init_all_stubs() from auto_api import js js.model_js()
def test_to_task(self): """Ensure to_task produces the right task object.""" import datetime import time from google.appengine.ext import testbed from furious. async import Async from furious. async import ASYNC_ENDPOINT testbed = testbed.Testbed() testbed.activate() # This just drops the microseconds. It is a total mess, but is needed # to handle all the rounding crap. eta = datetime.datetime.now() + datetime.timedelta(minutes=43) eta_posix = time.mktime(eta.timetuple()) headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) expected_url = "%s/%s" % (ASYNC_ENDPOINT, 'test') task_args = {'eta': eta_posix} options = { 'job': job, 'headers': headers, 'task_args': task_args, 'id': 'ident', 'context_id': 'contextid', 'parent_id': 'parentid' } task = Async.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = { 'Host': 'testbed.example.com', 'X-AppEngine-Current-Namespace': '' } full_headers.update(headers) self.assertEqual(eta_posix, task.eta_posix) self.assertEqual(expected_url, task.url) self.assertEqual(full_headers, task.headers) options['task_args']['eta'] = datetime.datetime.fromtimestamp( eta_posix) options['_recursion'] = {'current': 1, 'max': 100} options['_type'] = 'furious.async.Async' self.assertEqual( options, Async.from_dict(json.loads(task.payload)).get_options())
def setUp(self): from google.appengine.ext.testbed import TASKQUEUE_SERVICE_NAME, Testbed testbed = Testbed() testbed.activate() self.addCleanup(testbed.deactivate) self.addCleanup(TestAsync_decorate_job.foo_values.clear) testbed.init_taskqueue_stub() self._taskq_service = testbed.get_stub(TASKQUEUE_SERVICE_NAME)
def setUp(self): from google.appengine.ext.testbed import ( TASKQUEUE_SERVICE_NAME, Testbed, ) testbed = Testbed() testbed.activate() self.addCleanup(testbed.deactivate) self.addCleanup(TestAsync_decorate_job.foo_values.clear) testbed.init_taskqueue_stub() self._taskq_service = testbed.get_stub(TASKQUEUE_SERVICE_NAME)
def test_to_task(self): """Ensure to_task produces the right task object.""" import datetime import time from google.appengine.ext import testbed from furious.async import Async from furious.async import ASYNC_ENDPOINT testbed = testbed.Testbed() testbed.activate() # This just drops the microseconds. It is a total mess, but is needed # to handle all the rounding crap. eta = datetime.datetime.now() + datetime.timedelta(minutes=43) eta_posix = time.mktime(eta.timetuple()) headers = {"some": "thing", "fun": 1} job = ("test", None, None) expected_url = "%s/%s" % (ASYNC_ENDPOINT, "test") task_args = {"eta": eta_posix} options = { "job": job, "headers": headers, "task_args": task_args, "id": "ident", "context_id": "contextid", "parent_id": "parentid", } task = Async.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = {"Host": "testbed.example.com", "X-AppEngine-Current-Namespace": ""} full_headers.update(headers) self.assertEqual(eta_posix, task.eta_posix) self.assertEqual(expected_url, task.url) self.assertEqual(full_headers, task.headers) options["task_args"]["eta"] = datetime.datetime.fromtimestamp(eta_posix) options["_recursion"] = {"current": 1, "max": 100} options["_type"] = "furious.async.Async" self.assertEqual(options, Async.from_dict(json.loads(task.payload)).get_options())
def _test(**kwargs): """ This stub uses the testbed to initialize the bare minimum to use the Datastore connector. Tests themselves should setup/tear down their own stubs by using DjangaeDiscoverRunner or the nose plugin. The stubs here are just for bootstrapping the tests. Obviously any data inserted between here, and the tests themselves will be wiped out when the tests begin! """ from google.appengine.ext import testbed from google.appengine.datastore import datastore_stub_util MINIMAL_STUBS = { "init_app_identity_stub": {}, "init_memcache_stub": {}, "init_datastore_v3_stub": { "use_sqlite": True, "auto_id_policy": testbed.AUTO_ID_POLICY_SCATTERED, "consistency_policy": datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) } } from .blobstore_service import start_blobstore_service, stop_blobstore_service # Dummy values for testing, based on the defaults for dev_appserver # (differentiating between the default runserver port of 8000 can also be useful # for picking up hard-coding issues etc.) os.environ["HTTP_HOST"] = "localhost:8080" os.environ['SERVER_NAME'] = "localhost" os.environ['SERVER_PORT'] = "8080" os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % ( os.environ['SERVER_NAME'], os.environ['SERVER_PORT']) testbed = testbed.Testbed() testbed.activate() for init_name, stub_kwargs in MINIMAL_STUBS.items(): getattr(testbed, init_name)(**stub_kwargs) start_blobstore_service() try: yield finally: stop_blobstore_service() if testbed: testbed.deactivate()
def test_to_task(self): """Ensure to_task produces the right task object.""" import datetime import time from google.appengine.ext import testbed from furious.async import Async from furious.async import ASYNC_ENDPOINT testbed = testbed.Testbed() testbed.activate() # This just drops the microseconds. It is a total mess, but is needed # to handle all the rounding crap. eta = datetime.datetime.now() + datetime.timedelta(minutes=43) eta_posix = time.mktime(eta.timetuple()) headers = {'some': 'thing', 'fun': 1} job = ('test', None, None) expected_url = "%s/%s" % (ASYNC_ENDPOINT, 'test') task_args = {'eta': eta_posix} options = {'job': job, 'headers': headers, 'task_args': task_args, 'id': 'ident', 'context_id': 'contextid', 'parent_id': 'parentid'} task = Async.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = { 'Host': 'testbed.example.com', 'X-AppEngine-Current-Namespace': '' } full_headers.update(headers) self.assertEqual(eta_posix, task.eta_posix) self.assertEqual(expected_url, task.url) self.assertEqual(full_headers, task.headers) options['task_args']['eta'] = datetime.datetime.fromtimestamp( eta_posix) options['_recursion'] = {'current': 1, 'max': 100} options['_type'] = 'furious.async.Async' self.assertEqual( options, Async.from_dict(json.loads(task.payload)).get_options())
def _test(**kwargs): """ This stub uses the testbed to initialize the bare minimum to use the Datastore connector. Tests themselves should setup/tear down their own stubs by using DjangaeDiscoverRunner or the nose plugin. The stubs here are just for bootstrapping the tests. Obviously any data inserted between here, and the tests themselves will be wiped out when the tests begin! """ from google.appengine.ext import testbed from google.appengine.datastore import datastore_stub_util MINIMAL_STUBS = { "init_app_identity_stub": {}, "init_memcache_stub": {}, "init_datastore_v3_stub": { "use_sqlite": True, "auto_id_policy": testbed.AUTO_ID_POLICY_SCATTERED, "consistency_policy": datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) } } from .blobstore_service import start_blobstore_service, stop_blobstore_service # Dummy values for testing, based on the defaults for dev_appserver # (differentiating between the default runserver port of 8000 can also be useful # for picking up hard-coding issues etc.) os.environ["HTTP_HOST"] = "localhost:8080" os.environ['SERVER_NAME'] = "localhost" os.environ['SERVER_PORT'] = "8080" os.environ['DEFAULT_VERSION_HOSTNAME'] = '%s:%s' % ( os.environ['SERVER_NAME'], os.environ['SERVER_PORT'] ) testbed = testbed.Testbed() testbed.activate() for init_name, stub_kwargs in MINIMAL_STUBS.items(): getattr(testbed, init_name)(**stub_kwargs) start_blobstore_service() try: yield finally: stop_blobstore_service() if testbed: testbed.deactivate()
def init_datastore(): from google.appengine.ext import testbed from google.appengine.ext import ndb # First, create an instance of the Testbed class. testbed = testbed.Testbed() # Then activate the testbed, which prepares the service stubs for use. testbed.activate() # Next, declare which service stubs you want to use. testbed.init_app_identity_stub() testbed.init_datastore_v3_stub() testbed.init_memcache_stub() testbed.init_blobstore_stub() testbed.init_urlfetch_stub() # Clear ndb's in-context cache between tests. # This prevents data from leaking between tests. # Alternatively, you could disable caching by # using ndb.get_context().set_cache_policy(False) ndb.get_context().clear_cache()
def main(sdk_path, test_path): # If the sdk path points to a google cloud sdk installation # then we should alter it to point to the GAE platform location. if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')): sys.path.insert(0, os.path.join(sdk_path, 'platform/google_appengine')) else: sys.path.insert(0, sdk_path) # Ensure that the google.appengine.* packages are available # in tests as well as all bundled third-party packages. import dev_appserver dev_appserver.fix_sys_path() # Loading appengine_config from the current project ensures that any # changes to configuration there are available to all tests (e.g. # sys.path modifications, namespaces, etc.) try: import appengine_config (appengine_config) except ImportError: print "Note: unable to import appengine_config." from google.appengine.ext import testbed testbed = testbed.Testbed() testbed.activate() testbed.init_app_identity_stub() testbed.init_datastore_v3_stub() testbed.init_user_stub() testbed.init_memcache_stub() # Discover and run tests. suite = unittest.loader.TestLoader().discover(test_path) for root, dirs, files in os.walk(test_path): for name in dirs: # print(os.path.join(root,name)) suite.addTests(unittest.loader.TestLoader().discover( os.path.join(root, name))) unittest.TextTestRunner(verbosity=2).run(suite)
def SetUpTestbedTestCase(case): """Set up appengine testbed enviroment.""" case.testbed = testbed.Testbed() case.testbed.activate() # The oauth_aware decorator will 302 to login unless there is either # a current user _or_ a valid oauth header; this is easier to stub. case.testbed.setup_env( user_email='*****@*****.**', user_id='1234', overwrite=True) case.testbed.init_all_stubs() policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1) case.testbed.init_datastore_v3_stub(consistency_policy=policy) case.testbed.init_taskqueue_stub(_all_queues_valid=True) os.environ['AUTH_DOMAIN'] = 'example.com' # Lazily stub out key-fetching RPC dependency. crypto.Decrypt = lambda x: x crypto.Encrypt = lambda x: x
def test_to_task_without_payload(self): """Ensure to_task without a payload produces the right task object.""" from google.appengine.ext import testbed from furious.batcher import Message testbed = testbed.Testbed() testbed.activate() options = {'task_args': {}} task = Message.from_dict(options).to_task() # App Engine sets these headers by default. full_headers = { 'Host': 'testbed.example.com', 'X-AppEngine-Current-Namespace': '' } self.assertEqual(full_headers, task.headers) self.assertIsNone(json.loads(task.payload))
def main(sdk_path, test_path): # If the sdk path points to a google cloud sdk installation # then we should alter it to point to the GAE platform location. if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')): sys.path.insert(0, os.path.join(sdk_path, 'platform/google_appengine')) else: sys.path.insert(0, sdk_path) # Ensure that the google.appengine.* packages are available # in tests as well as all bundled third-party packages. import dev_appserver dev_appserver.fix_sys_path() # Loading appengine_config from the current project ensures that any # changes to configuration there are available to all tests (e.g. # sys.path modifications, namespaces, etc.) try: import appengine_config (appengine_config) except ImportError: print "Note: unable to import appengine_config." from google.appengine.ext import testbed testbed = testbed.Testbed() testbed.activate() testbed.init_app_identity_stub() testbed.init_datastore_v3_stub() testbed.init_user_stub() testbed.init_memcache_stub() # Discover and run tests. suite = unittest.loader.TestLoader().discover(test_path) for root, dirs, files in os.walk(test_path): for name in dirs: # print(os.path.join(root,name)) suite.addTests(unittest.loader.TestLoader().discover(os.path.join(root,name))) unittest.TextTestRunner(verbosity=2).run(suite)
import os import logging sys.path[:0] = [os.path.dirname(__file__) + "/../vendor"] from flask import Flask app = Flask(__name__) if "APPLICATION_ID" not in os.environ: os.environ["APPLICATION_ID"] = "dev~test" # Hide all the App Engine cruft. logging.disable(logging.INFO) from google.appengine.ext import testbed, ndb testbed = testbed.Testbed() testbed.activate() testbed.init_all_stubs() ndb.get_context().set_cache_policy(False) app.testing = True # Ensure that the Recaptcha field is small. app.config["RECAPTCHA_DATA_ATTRS"] = {"size": "compact"} # Enable global CSRF protection. from flask_wtf.csrf import CsrfProtect CsrfProtect(app) # Imported for side effects: import caravel.storage.config import caravel.controllers.listings import caravel.controllers.api
def init_testbed(): from google.appengine.ext import testbed testbed = testbed.Testbed() testbed.activate() testbed.init_all_stubs()
sys.path.insert(0, '/home/jonparrott/.bin/google_appengine') sys.path.insert(0, '/home/jonparrott/bin/google_appengine') # # for OSX default # if os.uname() == 'Darwin': sys.path.insert(0, '/usr/local/google_appengine') import google reload(google) # Remove wrong module import dev_appserver dev_appserver.fix_sys_path() from google.appengine.ext import testbed testbed = testbed.Testbed() testbed.activate() testbed.init_memcache_stub() # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = ['sphinx.ext.autodoc', 'sphinx.ext.coverage', 'sphinx.ext.viewcode'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames.