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)
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 USE_DATASTORE_EMULATOR: self._disable_stub(DATASTORE_SERVICE_NAME) testserver_util.TESTSERVER_UTIL.reset_emulator() testserver_util.remote_api_stub.ConfigureRemoteApi( os.environ['APPLICATION_ID'], '/', lambda: ('', ''), 'localhost:%d' % testserver_util.TESTSERVER_UTIL.api_port, services=[DATASTORE_SERVICE_NAME], apiproxy=self._test_stub_map, use_remote_datastore=False) 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)
def init_datastore_v3_stub(self, enable=True, datastore_file=None, use_sqlite=False, auto_id_policy=AUTO_ID_POLICY_SEQUENTIAL, **stub_kw_args): """Enable the datastore stub. The 'datastore_file' argument can be the path to 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 'datastore_file' defined, changes 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 that you can only access those entities of the datastore file which have the same application ID associated with them 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, False if real service should be disabled. datastore_file: Filename of a dev_appserver datastore file. use_sqlite: True to use the Sqlite stub, False (default) for file stub. auto_id_policy: How datastore stub assigns auto IDs. Either AUTO_ID_POLICY_SEQUENTIAL or AUTO_ID_POLICY_SCATTERED. stub_kw_args: Keyword arguments passed on to the service stub. """ 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)