示例#1
0
    def test_cache_hit(_LookupBatch, 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)
        entity_pb = model._entity_to_protobuf(entity)
        cache_value = entity_pb.SerializeToString()

        global_cache.set({cache_key: cache_value})

        batch = _LookupBatch.return_value
        batch.add.side_effect = Exception("Shouldn't get called.")

        future = _api.lookup(key._key, _options.ReadOptions())
        assert future.result() == entity_pb
示例#2
0
    def test_cache_miss(_LookupBatch, 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)
        entity_pb = model._entity_to_protobuf(entity)
        cache_value = entity_pb.SerializeToString()

        batch = _LookupBatch.return_value
        batch.add.return_value = future_result(entity_pb)

        future = _api.lookup(key._key, _options.ReadOptions())
        assert future.result() == entity_pb

        assert global_cache.get([cache_key]) == [cache_value]
示例#3
0
    def test_cache_locked(_LookupBatch, 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)
        entity_pb = model._entity_to_protobuf(entity)

        global_cache.set({cache_key: _cache._LOCKED})

        batch = _LookupBatch.return_value
        batch.add.return_value = future_result(entity_pb)

        future = _api.lookup(key._key, _options.ReadOptions())
        assert future.result() == entity_pb

        assert global_cache.get([cache_key]) == [_cache._LOCKED]
示例#4
0
    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]