def SetAutoIdPolicy(self, auto_id_policy):
        """Set the auto id policy of cloud datastore emulator.

    Args:
      auto_id_policy: A string indicating how the emulator assigns auto IDs,
        should be either datastore_stub_util.SCATTERED or
        datastore_stub_util.SEQUENTIAL.
    """
        datastore_stub_util.UpdateEmulatorConfig(port=self._emulator_port,
                                                 auto_id_policy=auto_id_policy)
    def SetConsistencyPolicy(self, consistency_policy):
        """Set the job consistency policy of cloud datastore emulator.

    Args:
      consistency_policy: An instance of
        datastore_stub_util.PseudoRandomHRConsistencyPolicy or
        datastore_stub_util.MasterSlaveConsistencyPolicy.
    """
        datastore_stub_util.UpdateEmulatorConfig(
            port=self._emulator_port, consistency_policy=consistency_policy)
        if isinstance(consistency_policy,
                      datastore_stub_util.PseudoRandomHRConsistencyPolicy):
            consistency_policy.is_using_cloud_datastore_emulator = True
            consistency_policy.emulator_port = self._emulator_port
Example #3
0
    def init_datastore_v3_stub(self,
                               enable=True,
                               datastore_file=None,
                               use_sqlite=False,
                               auto_id_policy=AUTO_ID_POLICY_SEQUENTIAL,
                               **stub_kw_args):
        """Enables the datastore stub.

    The `datastore_file` argument can be set to the path of an existing
    datastore file, or `None` (default) to use an in-memory datastore that is
    initially empty. If you use the sqlite stub and have defined
    `datastore_file`, the changes that you apply in a test will be written to
    the file. If you use the default datastore stub, changes are *not* saved to
    disk unless you set `save_changes=True`.

    Note:
        You can only access those entities of the datastore file that use the
        same application ID as the test run. You can change the application ID
        for a test with `setup_env()`.

    Args:
      enable: `True` if the fake service should be enabled, or `False` if the
          real service should be disabled.
      datastore_file: File name of a dev_appserver datastore file.
      use_sqlite: `True` to use the Sqlite stub, or `False` (default) to use
          the file stub.
      auto_id_policy: How datastore stub assigns auto IDs. This value can be
          either `AUTO_ID_POLICY_SEQUENTIAL` or `AUTO_ID_POLICY_SCATTERED`.
      **stub_kw_args: Keyword arguments passed on to the service stub.

    Raises:
      StubNotSupportedError: If datastore_sqlite_stub is None.
    """
        if self._use_datastore_emulator:

            self._disable_stub(DATASTORE_SERVICE_NAME)

            delegate_stub = (remote_api_stub.DatastoreStubTestbedDelegate(
                self.rpc_server,
                '/',
                stub_kw_args.get('max_request_size',
                                 apiproxy_stub.MAX_REQUEST_SIZE),
                emulator_port=self._emulator_port))
            delegate_stub.Clear()
            self._test_stub_map.RegisterStub(DATASTORE_SERVICE_NAME,
                                             delegate_stub)
            consistency_policy = stub_kw_args.get(
                'consistency_policy',
                datastore_stub_util.PseudoRandomHRConsistencyPolicy(
                    probability=1.0))
            datastore_stub_util.UpdateEmulatorConfig(self._emulator_port,
                                                     auto_id_policy,
                                                     consistency_policy)
            if isinstance(consistency_policy,
                          datastore_stub_util.PseudoRandomHRConsistencyPolicy):
                consistency_policy.is_using_cloud_datastore_emulator = True
                consistency_policy.emulator_port = self._emulator_port

            self._enabled_stubs[DATASTORE_SERVICE_NAME] = None
            return
        if not enable:
            self._disable_stub(DATASTORE_SERVICE_NAME)
            self._disable_stub(datastore_v4_stub.SERVICE_NAME)
            self._disable_stub(cloud_datastore_v1_stub.SERVICE_NAME)
            return
        if use_sqlite:
            if datastore_sqlite_stub is None:
                raise StubNotSupportedError(
                    'The sqlite stub is not supported in production.')
            stub = datastore_sqlite_stub.DatastoreSqliteStub(
                os.environ['APPLICATION_ID'],
                datastore_file,
                use_atexit=False,
                auto_id_policy=auto_id_policy,
                **stub_kw_args)
        else:
            stub_kw_args.setdefault('save_changes', False)
            stub = datastore_file_stub.DatastoreFileStub(
                os.environ['APPLICATION_ID'],
                datastore_file,
                use_atexit=False,
                auto_id_policy=auto_id_policy,
                **stub_kw_args)
        self._register_stub(DATASTORE_SERVICE_NAME, stub,
                            self._deactivate_datastore_v3_stub)
        v4_stub = datastore_v4_stub.DatastoreV4Stub(
            os.environ['APPLICATION_ID'])
        self._register_stub(datastore_v4_stub.SERVICE_NAME, v4_stub)
        if datastore_pbs._CLOUD_DATASTORE_ENABLED:
            helper = datastore_pbs.googledatastore.helper
            credential_env = helper._DATASTORE_USE_STUB_CREDENTIAL_FOR_TEST_ENV
            os.environ[credential_env] = 'True'
            cloud_stub = cloud_datastore_v1_stub.CloudDatastoreV1Stub(
                os.environ['APPLICATION_ID'])
            self._register_stub(cloud_datastore_v1_stub.SERVICE_NAME,
                                cloud_stub)