Python google.appengine.ext.testbed.Testbed() Examples

The following are 30 code examples of google.appengine.ext.testbed.Testbed(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.appengine.ext.testbed , or try the search function .
Example #1
Source File: datastore_test.py    From python-docs-samples with Apache License 2.0 6 votes vote down vote up
def setUp(self):
        # First, create an instance of the Testbed class.
        self.testbed = testbed.Testbed()
        # Then activate the testbed, which prepares the service stubs for use.
        self.testbed.activate()
        # Next, declare which service stubs you want to use.
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_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()

# [END datastore_example_test]

    # [START datastore_example_teardown] 
Example #2
Source File: gettaskletrace.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def cause_problem():
  tb = testbed.Testbed()
  tb.activate()
  tb.init_datastore_v3_stub()
  tb.init_memcache_stub()
  ctx = tasklets.make_default_context()
  tasklets.set_context(ctx)
  ctx.set_datastore_policy(True)
  ctx.set_cache_policy(False)
  ctx.set_memcache_policy(True)

  @tasklets.tasklet
  def problem_tasklet():
    class Foo(model.Model):
      pass
    key = yield ctx.put(Foo())
    yield ctx.get(key)  # Trigger get_tasklet that does not complete...
    yield ctx.delete(key)  # ... by the time this delete_tasklet starts.
    a = yield ctx.get(key)
    assert a is None, '%r is not None' % a

  problem_tasklet().check_success()
  print 'No problem yet...'
  tb.deactivate() 
Example #3
Source File: kobench.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def main():
  utils.tweak_logging()  # Interpret -v and -q flags.

  tb = testbed.Testbed()
  tb.activate()
  tb.init_datastore_v3_stub()
  tb.init_memcache_stub()

  n = 1000
  for arg in sys.argv[1:]:
    try:
      n = int(arg)
      break
    except Exception:
      pass

  populate(n)

  profiler(bench, n) 
Example #4
Source File: dbench.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def main():
  utils.tweak_logging()  # Interpret -v and -q flags.

  tb = testbed.Testbed()
  tb.activate()
  tb.init_datastore_v3_stub()
  tb.init_memcache_stub()

  n = 1000
  for arg in sys.argv[1:]:
    try:
      n = int(arg)
      break
    except Exception:
      pass

  populate(n)

  profiler(bench, n) 
Example #5
Source File: __init__.py    From sndlatr with Apache License 2.0 6 votes vote down vote up
def setUp(self):
        self.testbed = testbed.Testbed()

        self.testbed.activate()
        self.testbed.init_user_stub()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_urlfetch_stub()
        self.testbed.init_mail_stub()
        self.testbed.init_taskqueue_stub(
            root_path=os.path.join(os.path.dirname(__file__), '..'))
        self.addCleanup(self.testbed.deactivate)

        self.taskqueue_stub = self.testbed.get_stub(
            testbed.TASKQUEUE_SERVICE_NAME)
        self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)

        urlfetch = self.testbed.get_stub('urlfetch')
        urlfetch._RetrieveURL = self.retrieve_mock
        self._response_queue = []
        self.patch_xsrf() 
Example #6
Source File: base_test.py    From graphene-gae with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setUp(self):
        super(BaseTest, self).setUp()

        root_path = '.'
        application_id = 'graphene-gae-test'

        # First, create an instance of the Testbed class.
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.setup_env(app_id=application_id, overwrite=True)
        policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=self.datastore_probability)
        self.testbed.init_datastore_v3_stub(root_path=root_path, consistency_policy=policy, require_indexes=True)
        self.testbed.init_app_identity_stub()
        self.testbed.init_blobstore_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_taskqueue_stub(root_path=root_path)
        self.testbed.init_urlfetch_stub()
        self.storage = cloudstorage_stub.CloudStorageStub(self.testbed.get_stub('blobstore').storage)
        self.testbed.init_mail_stub()
        self.testbed.init_user_stub()
        self.taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

        ndb.get_context().clear_cache()
        ndb.get_context().set_cache_policy(lambda x: True) 
Example #7
Source File: app_handler_test_base.py    From googleapps-message-recall with Apache License 2.0 6 votes vote down vote up
def setUp(self):
    SetupLogging()
    self._testbed = testbed.Testbed()
    self._testbed.activate()
    self._testbed.setup_env(overwrite=True, USER_EMAIL=_ADMIN_USER_EMAIL,
                            USER_ID='1', USER_IS_ADMIN='1')
    self._testbed.init_user_stub()
    self._testbed.init_memcache_stub()

    # Fake the admin checks.
    frontend_views._CacheUserEmailBillingEnabled(_ADMIN_USER_EMAIL)
    frontend_views._CacheUserEmailAsAdmin(_ADMIN_USER_EMAIL)

    # Derived classes need to set this as follows for tests:
    #
    #   self._testapp = webtest.TestApp(webapp2.WSGIApplication([
    #       (r'/about', frontend_views.AboutPageHandler),
    #       ], debug=True))
    self._testapp = None 
Example #8
Source File: test_Model.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    # First, create an instance of the Testbed class.
    self.testbed = testbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for use.
    self.testbed.activate()
    # Next, declare which service stubs you want to use.
    self.testbed.init_datastore_v3_stub() 
Example #9
Source File: stress.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def main():
  global cache_policy, memcache_policy, datastore_policy

  # Test every single policy choice
  for cache_policy in (True, False):
    for memcache_policy in (True, False):
      for datastore_policy in (True, False):
        if not (cache_policy or memcache_policy or datastore_policy):
          continue
        logger.info('c: %i mc: %i ds: %i', cache_policy, memcache_policy,
                    datastore_policy)

        tb = testbed.Testbed()
        tb.activate()
        tb.init_datastore_v3_stub()
        tb.init_memcache_stub()
        tb.init_taskqueue_stub()
        datastore_stub = apiproxy_stub_map.apiproxy.GetStub('datastore_v3')
        datastore_stub.SetConsistencyPolicy(
          datastore_stub_util.BaseHighReplicationConsistencyPolicy())

        threads = []
        for _ in range(INSTANCES):
          stress_thread = Stress()
          stress_thread.start()
          threads.append(stress_thread)

        for t in threads:
          t.join()

        tb.deactivate() 
Example #10
Source File: _endpointscfg_impl.py    From endpoints-python with Apache License 2.0 5 votes vote down vote up
def _SetupStubs():
  tb = testbed.Testbed()
  tb.setup_env(CURRENT_VERSION_ID='1.0')
  tb.activate()
  for k, v in testbed.INIT_STUB_METHOD_NAMES.iteritems():
    # The old stub initialization code didn't support the image service at all
    # so we just ignore it here.
    if k != 'images':
      getattr(tb, v)() 
Example #11
Source File: test_Model.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    # First, create an instance of the Testbed class.
    self.testbed = testbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for use.
    self.testbed.activate()
    # Next, declare which service stubs you want to use.
    self.testbed.init_datastore_v3_stub() 
Example #12
Source File: entity_state_updater_test.py    From googleapps-message-recall with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    SetupLogging()
    self._testbed = testbed.Testbed()
    self._testbed.activate()
    self._testbed.init_datastore_v3_stub()
    self._testbed.init_memcache_stub()

    self._task_key = _CreateRecallTaskEntity(_OWNER_EMAIL, _MESSAGE_CRITERIA)
    self._user_key = _CreateDomainUserEntity(self._task_key.id(),
                                             user_email=_OWNER_EMAIL) 
Example #13
Source File: test_Model.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    # First, create an instance of the Testbed class.
    self.testbed = testbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for use.
    self.testbed.activate()
    # Next, declare which service stubs you want to use.
    self.testbed.init_datastore_v3_stub() 
Example #14
Source File: test_Model.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    # First, create an instance of the Testbed class.
    self.testbed = testbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for use.
    self.testbed.activate()
    # Next, declare which service stubs you want to use.
    self.testbed.init_datastore_v3_stub() 
Example #15
Source File: handlers_test.py    From gae-secure-scaffold-python with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_datastore_v3_stub()
    self.testbed.init_memcache_stub()
    self.app = webapp2.WSGIApplication([('/', DummyHandler),
                                        ('/ajax', DummyAjaxHandler),
                                        ('/cron', DummyCronHandler),
                                        ('/task', DummyTaskHandler)]) 
Example #16
Source File: gae_test.py    From ray with MIT License 5 votes vote down vote up
def setUp(self):
        # fix enviroment
        app_id = 'myapp'
        os.environ['APPLICATION_ID'] = app_id
        apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
        stub = datastore_file_stub.DatastoreFileStub(app_id, '/dev/null', '/')
        apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)

        # activate mock services
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_user_stub()
        self.testbed.init_memcache_stub() 
Example #17
Source File: bigquery_handler_test.py    From gcp-census with Apache License 2.0 5 votes vote down vote up
def init_webtest(self):
        self.under_test = webtest.TestApp(routes.app)
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_memcache_stub()

        path = os.path.join(os.path.dirname(__file__), '../config')
        logging.debug("queue.yaml path: %s", path)
        self.testbed.init_taskqueue_stub(root_path=path)
        self.taskqueue_stub = self.testbed.get_stub(
            testbed.TASKQUEUE_SERVICE_NAME)
        self.testbed.init_app_identity_stub() 
Example #18
Source File: models_test.py    From gae-secure-scaffold-python with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_datastore_v3_stub() 
Example #19
Source File: race.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def main():
  tb = testbed.Testbed()
  tb.activate()
  tb.init_datastore_v3_stub()
  tb.init_memcache_stub()
  subverting_aries_fix()
  tb.deactivate() 
Example #20
Source File: loanertest.py    From loaner with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    """Set up the environment for testing."""
    super(TestCase, self).setUp()
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_datastore_v3_stub()
    self.testbed.init_memcache_stub()
    self.testbed.init_user_stub()
    self.testbed.init_search_stub()
    self.testbed.init_taskqueue_stub()
    self.login_user()

    taskqueue_patcher = mock.patch.object(taskqueue, 'add')
    self.addCleanup(taskqueue_patcher.stop)
    self.taskqueue_add = taskqueue_patcher.start()
    self.taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)

    # The events.raise_event method raises an exception if there are no events
    # in datastore. It's called often in the model methods, many of which are
    # used in testing. When you want to test raise_event specifically, first run
    # stop() on this patcher; be sure to run start() again before end of test.
    def side_effect(event_name, device=None, shelf=None):
      """Side effect for raise_event that returns the model."""
      del event_name  # Unused.
      if device:
        return device
      else:
        return shelf

    self.testbed.mock_raiseevent = mock.Mock(side_effect=side_effect)
    self.testbed.raise_event_patcher = mock.patch.object(
        events, 'raise_event', self.testbed.mock_raiseevent)
    self.addCleanup(self.testbed.raise_event_patcher.stop)
    self.testbed.raise_event_patcher.start() 
Example #21
Source File: appengine.py    From python-repo-tools with Apache License 2.0 5 votes vote down vote up
def setup_testbed():
    """Sets up the GAE testbed and enables common stubs."""
    from google.appengine.datastore import datastore_stub_util
    from google.appengine.ext import testbed as gaetestbed

    # Setup the datastore and memcache stub.
    # First, create an instance of the Testbed class.
    tb = gaetestbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for
    # use.
    tb.activate()
    # Create a consistency policy that will simulate the High
    # Replication consistency model.
    policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
        probability=1.0)
    # Initialize the datastore stub with this policy.
    tb.init_datastore_v3_stub(
        datastore_file=tempfile.mkstemp()[1],
        consistency_policy=policy)
    tb.init_memcache_stub()

    # Setup remaining stubs.
    tb.init_urlfetch_stub()
    tb.init_app_identity_stub()
    tb.init_blobstore_stub()
    tb.init_user_stub()
    tb.init_logservice_stub()
    # tb.init_taskqueue_stub(root_path='tests/resources')
    tb.init_taskqueue_stub()
    tb.taskqueue_stub = tb.get_stub(gaetestbed.TASKQUEUE_SERVICE_NAME)

    return tb 
Example #22
Source File: test_cachefactory.py    From budou with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    # First, create an instance of the Testbed class.
    self.testbed = testbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for use.
    self.testbed.activate()
    # Next, declare which service stubs you want to use.
    self.testbed.init_datastore_v3_stub()
    self.testbed.init_memcache_stub()
    self.cache = budou.load_cache() 
Example #23
Source File: appengine_helper.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def setup_testbed():
    """Sets up the GAE testbed and enables common stubs."""
    from google.appengine.datastore import datastore_stub_util
    from google.appengine.ext import testbed as gaetestbed

    # Setup the datastore and memcache stub.
    # First, create an instance of the Testbed class.
    tb = gaetestbed.Testbed()
    # Then activate the testbed, which prepares the service stubs for
    # use.
    tb.activate()
    # Create a consistency policy that will simulate the High
    # Replication consistency model.
    policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
        probability=1.0)
    # Initialize the datastore stub with this policy.
    tb.init_datastore_v3_stub(
        datastore_file=tempfile.mkstemp()[1],
        consistency_policy=policy)
    tb.init_memcache_stub()

    # Setup remaining stubs.
    tb.init_urlfetch_stub()
    tb.init_app_identity_stub()
    tb.init_blobstore_stub()
    tb.init_user_stub()
    tb.init_logservice_stub()
    # tb.init_taskqueue_stub(root_path='tests/resources')
    tb.init_taskqueue_stub()
    tb.taskqueue_stub = tb.get_stub(gaetestbed.TASKQUEUE_SERVICE_NAME)

    return tb 
Example #24
Source File: datastore_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        # First, create an instance of the Testbed class.
        self.testbed = testbed.Testbed()
        # Then activate the testbed, which prepares the service stubs for use.
        self.testbed.activate()
        # Create a consistency policy that will simulate the High Replication
        # consistency model.
        self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(
            probability=0)
        # Initialize the datastore stub with this policy.
        self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
        # Initialize memcache stub too, since ndb also uses memcache
        self.testbed.init_memcache_stub()
        # Clear in-context cache before each test.
        ndb.get_context().clear_cache() 
Example #25
Source File: mail_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_mail_stub()
        self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME) 
Example #26
Source File: login_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_user_stub() 
Example #27
Source File: env_vars_test.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.setup_env(
            app_id='your-app-id',
            my_config_setting='example',
            overwrite=True) 
Example #28
Source File: test_storage.py    From limits with MIT License 5 votes vote down vote up
def setUp(self):
        pymemcache.client.Client(('localhost', 22122)).flush_all()
        redis.from_url('unix:///tmp/limits.redis.sock').flushall()
        redis.from_url("redis://localhost:7379").flushall()
        redis.from_url("redis://:sekret@localhost:7389").flushall()
        redis.sentinel.Sentinel([
            ("localhost", 26379)
        ]).master_for("localhost-redis-sentinel").flushall()
        rediscluster.RedisCluster("localhost", 7000).flushall()
        if RUN_GAE:
            from google.appengine.ext import testbed
            tb = testbed.Testbed()
            tb.activate()
            tb.init_memcache_stub() 
Example #29
Source File: test_gae_memcached.py    From limits with MIT License 5 votes vote down vote up
def setUp(self):
        from google.appengine.ext import testbed
        tb = testbed.Testbed()
        tb.activate()
        tb.init_memcache_stub() 
Example #30
Source File: conftest.py    From beans with MIT License 5 votes vote down vote up
def minimal_database():
    my_testbed = testbed.Testbed()
    my_testbed.activate()
    my_testbed.init_datastore_v3_stub()
    my_testbed.init_memcache_stub()
    # Clear ndb's in-context cache between tests.
    ndb.get_context().clear_cache()