def test_w_transaction(datastore_pb2, in_context): class Mutation: def __init__(self, delete=None): self.delete = delete def __eq__(self, other): return self.delete == other.delete eventloop = mock.Mock(spec=("add_idle", "run")) with in_context.new(eventloop=eventloop, transaction=b"tx123").use() as context: datastore_pb2.Mutation = Mutation key1 = key_module.Key("SomeKind", 1)._key key2 = key_module.Key("SomeKind", 2)._key key3 = key_module.Key("SomeKind", 3)._key future1 = _api.delete(key1) future2 = _api.delete(key2) future3 = _api.delete(key3) batch = context.commit_batches[b"tx123"] assert batch.mutations == [ Mutation(delete=key1.to_protobuf()), Mutation(delete=key2.to_protobuf()), Mutation(delete=key3.to_protobuf()), ] assert batch.futures == [future1, future2, future3]
def test_cache_enabled(Batch, global_cache): key = key_module.Key("SomeKind", 1) cache_key = _cache.global_cache_key(key._key) batch = Batch.return_value batch.delete.return_value = future_result(None) future = _api.delete(key._key, _options.Options()) assert future.result() is None assert global_cache.get([cache_key]) == [None]
def test_without_datastore(Batch, global_cache): key = key_module.Key("SomeKind", 1) cache_key = _cache.global_cache_key(key._key) global_cache.set({cache_key: b"foo"}) batch = Batch.return_value batch.delete.side_effect = Exception("Shouldn't use Datastore") future = _api.delete(key._key, _options.Options(use_datastore=False)) assert future.result() is None assert global_cache.get([cache_key]) == [None]
def delete_async(self, **options): """Schedule deletion of the entity for this key. The result of the returned future becomes available once the deletion is complete. In all cases the future's result is :data:`None` (i.e. there is no way to tell whether the entity existed or not). Args: options (Dict[str, Any]): The context options for the request. For example, ``{"deadline": 5}``. """ return _datastore_api.delete(self._key, **options)
def test_w_transaction(datastore_pb2, in_context): class Mutation: def __init__(self, delete=None): self.delete = delete def __eq__(self, other): return self.delete == other.delete with in_context.new(transaction=b"tx123").use() as context: datastore_pb2.Mutation = Mutation key1 = key_module.Key("SomeKind", 1)._key key2 = key_module.Key("SomeKind", 2)._key key3 = key_module.Key("SomeKind", 3)._key assert _api.delete(key1, _options.Options()).result() is None assert _api.delete(key2, _options.Options()).result() is None assert _api.delete(key3, _options.Options()).result() is None batch = context.commit_batches[b"tx123"] assert batch.mutations == [ Mutation(delete=key1.to_protobuf()), Mutation(delete=key2.to_protobuf()), Mutation(delete=key3.to_protobuf()), ]
def test_no_transaction(datastore_pb2, in_context): class Mutation: def __init__(self, delete=None): self.delete = delete def __eq__(self, other): return self.delete == other.delete datastore_pb2.Mutation = Mutation key1 = key_module.Key("SomeKind", 1)._key key2 = key_module.Key("SomeKind", 2)._key key3 = key_module.Key("SomeKind", 3)._key _api.delete(key1, _options.Options()) _api.delete(key2, _options.Options()) _api.delete(key3, _options.Options()) batch = in_context.batches[_api._NonTransactionalCommitBatch][()] assert batch.mutations == [ Mutation(delete=key1.to_protobuf()), Mutation(delete=key2.to_protobuf()), Mutation(delete=key3.to_protobuf()), ]