def test_no_datastore_or_global_cache(): def MockEntity(*path): key = ds_key_module.Key(*path, project="testing") return entity.Entity(key=key) mock_entity = MockEntity("what", "ever") with pytest.raises(TypeError): _api.put(mock_entity, _options.Options(use_datastore=False)).result()
def test_w_transaction(datastore_pb2, in_context): class Mutation: def __init__(self, upsert=None): self.upsert = upsert def __eq__(self, other): return self.upsert is other.upsert class PathElement: id = None def __init__(self, name): self.name = name def MockEntity(*path): path = [PathElement(name) for name in path] return mock.Mock(key=mock.Mock(path=path)) eventloop = mock.Mock(spec=("add_idle", "run")) context = in_context.new(eventloop=eventloop, transaction=b"123") with context.use() as context: datastore_pb2.Mutation = Mutation entity1 = MockEntity("a", "1") future1 = _api.put(entity1, _options.Options()) entity2 = MockEntity("a", None) future2 = _api.put(entity2, _options.Options()) entity3 = MockEntity() future3 = _api.put(entity3, _options.Options()) batch = context.commit_batches[b"123"] assert batch.mutations == [ Mutation(upsert=entity1), Mutation(upsert=entity2), Mutation(upsert=entity3), ] assert batch.futures == [future1, future2, future3] assert batch.transaction == b"123" assert batch.incomplete_mutations == [ Mutation(upsert=entity2), Mutation(upsert=entity3), ] assert batch.incomplete_futures == [future2, future3]
def test_no_datastore_incomplete_key(Batch, global_cache): class SomeKind(model.Model): pass key = key_module.Key("SomeKind", None) entity = SomeKind(key=key) future = _api.put( model._entity_to_ds_entity(entity), _options.Options(use_datastore=False), ) with pytest.raises(TypeError): future.result()
def test_w_transaction(datastore_pb2, in_context): class Mutation: def __init__(self, upsert=None): self.upsert = upsert def __eq__(self, other): return self.upsert == other.upsert def MockEntity(*path): key = ds_key_module.Key(*path, project="testing") return entity.Entity(key=key) with in_context.new(transaction=b"123").use() as context: datastore_pb2.Mutation = Mutation entity1 = MockEntity("a", "1") _api.put(entity1, _options.Options()) entity2 = MockEntity("a") _api.put(entity2, _options.Options()) entity3 = MockEntity("b") _api.put(entity3, _options.Options()) batch = context.commit_batches[b"123"] assert batch.mutations == [ Mutation(upsert=helpers.entity_to_protobuf(entity1)), Mutation(upsert=helpers.entity_to_protobuf(entity2)), Mutation(upsert=helpers.entity_to_protobuf(entity3)), ] assert batch.transaction == b"123" assert batch.incomplete_mutations == [ Mutation(upsert=helpers.entity_to_protobuf(entity2)), Mutation(upsert=helpers.entity_to_protobuf(entity3)), ]
def test_no_transaction(datastore_pb2, in_context): class Mutation: def __init__(self, upsert=None): self.upsert = upsert def __eq__(self, other): return self.upsert == other.upsert def MockEntity(*path): key = ds_key_module.Key(*path, project="testing") return entity.Entity(key=key) datastore_pb2.Mutation = Mutation entity1 = MockEntity("a", "1") _api.put(entity1, _options.Options()) entity2 = MockEntity("a") _api.put(entity2, _options.Options()) entity3 = MockEntity("b") _api.put(entity3, _options.Options()) batch = in_context.batches[_api._NonTransactionalCommitBatch][()] assert batch.mutations == [ Mutation(upsert=helpers.entity_to_protobuf(entity1)), Mutation(upsert=helpers.entity_to_protobuf(entity2)), Mutation(upsert=helpers.entity_to_protobuf(entity3)), ]
def test_put(datastore_pb2, runstate): class Mutation: def __init__(self, upsert=None): self.upsert = upsert def __eq__(self, other): return self.upsert is other.upsert runstate.eventloop = mock.Mock(spec=("add_idle", "run")) datastore_pb2.Mutation = Mutation entity1, entity2, entity3 = object(), object(), object() future1 = _api.put(entity1) future2 = _api.put(entity2) future3 = _api.put(entity3) batch = runstate.batches[_api._CommitBatch][()] assert batch.mutations == [ Mutation(upsert=entity1), Mutation(upsert=entity2), Mutation(upsert=entity3), ] assert batch.futures == [future1, future2, future3]
def test_no_key_returned(Batch, global_cache): class SomeKind(model.Model): pass key = key_module.Key("SomeKind", 1) cache_key = _cache.global_cache_key(key._key) entity = SomeKind(key=key) batch = Batch.return_value batch.put.return_value = future_result(None) future = _api.put(model._entity_to_ds_entity(entity), _options.Options()) assert future.result() is None assert global_cache.get([cache_key]) == [None]
def test_no_transaction(datastore_pb2, in_context): class Mutation: def __init__(self, upsert=None): self.upsert = upsert def __eq__(self, other): return self.upsert is other.upsert eventloop = mock.Mock(spec=("add_idle", "run")) with in_context.new(eventloop=eventloop).use() as context: datastore_pb2.Mutation = Mutation entity1, entity2, entity3 = object(), object(), object() future1 = _api.put(entity1) future2 = _api.put(entity2) future3 = _api.put(entity3) batch = context.batches[_api._NonTransactionalCommitBatch][()] assert batch.mutations == [ Mutation(upsert=entity1), Mutation(upsert=entity2), Mutation(upsert=entity3), ] assert batch.futures == [future1, future2, future3]
def test_no_datastore(Batch, global_cache): class SomeKind(model.Model): pass key = key_module.Key("SomeKind", 1) cache_key = _cache.global_cache_key(key._key) entity = SomeKind(key=key) cache_value = model._entity_to_protobuf(entity).SerializeToString() batch = Batch.return_value batch.put.return_value = future_result(None) future = _api.put( model._entity_to_ds_entity(entity), _options.Options(use_datastore=False), ) assert future.result() is None assert global_cache.get([cache_key]) == [cache_value]