def test__clear_global_cache_nothing_to_do(self): context = self._make_one(global_cache=global_cache._InProcessGlobalCache()) with context.use(): context.global_cache.cache["anotherkey"] = "otherdata" context._clear_global_cache().result() assert context.global_cache.cache == {"anotherkey": "otherdata"}
def test_retrieve_entity_with_global_cache(ds_entity, client_context): entity_id = test_utils.system.unique_resource_id() ds_entity(KIND, entity_id, foo=42, bar="none", baz=b"night") class SomeKind(ndb.Model): foo = ndb.IntegerProperty() bar = ndb.StringProperty() baz = ndb.StringProperty() global_cache = global_cache_module._InProcessGlobalCache() cache_dict = global_cache_module._InProcessGlobalCache.cache with client_context.new(global_cache=global_cache).use() as context: context.set_global_cache_policy(None) # Use default key = ndb.Key(KIND, entity_id) entity = key.get() assert isinstance(entity, SomeKind) assert entity.foo == 42 assert entity.bar == "none" assert entity.baz == "night" cache_key = _cache.global_cache_key(key._key) assert cache_key in cache_dict patch = mock.patch("google.cloud.ndb._datastore_api._LookupBatch.add") patch.side_effect = Exception("Shouldn't call this") with patch: entity = key.get() assert isinstance(entity, SomeKind) assert entity.foo == 42 assert entity.bar == "none" assert entity.baz == "night"
def test_insert_entity_with_global_cache(dispose_of, client_context): class SomeKind(ndb.Model): foo = ndb.IntegerProperty() bar = ndb.StringProperty() global_cache = global_cache_module._InProcessGlobalCache() cache_dict = global_cache_module._InProcessGlobalCache.cache with client_context.new(global_cache=global_cache).use() as context: context.set_global_cache_policy(None) # Use default entity = SomeKind(foo=42, bar="none") key = entity.put() dispose_of(key._key) cache_key = _cache.global_cache_key(key._key) assert not cache_dict retrieved = key.get() assert retrieved.foo == 42 assert retrieved.bar == "none" assert cache_key in cache_dict entity.foo = 43 entity.put() # This is py27 behavior. I can see a case being made for caching the # entity on write rather than waiting for a subsequent lookup. assert cache_key not in cache_dict
def test_watch_compare_and_swap_with_expires(time): time.time.return_value = 0 cache = global_cache._InProcessGlobalCache() cache.cache[b"one"] = (b"food", None) cache.cache[b"two"] = (b"bard", None) cache.cache[b"three"] = (b"bazz", None) result = cache.watch({ b"one": b"food", b"two": b"bard", b"three": b"bazd" }) assert result is None cache.cache[b"two"] = (b"hamburgers", None) result = cache.compare_and_swap( { b"one": b"foo", b"two": b"bar", b"three": b"baz" }, expires=5) assert result == {b"one": True, b"two": False, b"three": False} result = cache.get([b"one", b"two", b"three"]) assert result == [b"foo", b"hamburgers", b"bazz"] time.time.return_value = 10 result = cache.get([b"one", b"two", b"three"]) assert result == [None, b"hamburgers", b"bazz"]
def test_set_get_delete(): cache = global_cache._InProcessGlobalCache() result = cache.set({b"one": b"foo", b"two": b"bar", b"three": b"baz"}) assert result is None result = cache.get([b"two", b"three", b"one"]) assert result == [b"bar", b"baz", b"foo"] cache = global_cache._InProcessGlobalCache() result = cache.get([b"two", b"three", b"one"]) assert result == [b"bar", b"baz", b"foo"] result = cache.delete([b"one", b"two", b"three"]) assert result is None result = cache.get([b"two", b"three", b"one"]) assert result == [None, None, None]
def test_watch_unwatch(): cache = global_cache._InProcessGlobalCache() result = cache.watch([b"one", b"two", b"three"]) assert result is None result = cache.unwatch([b"one", b"two", b"three"]) assert result is None assert cache._watch_keys == {}
def global_cache(context): assert not context_module._state.context cache = global_cache_module._InProcessGlobalCache() with context.new(global_cache=cache).use(): yield cache assert not context_module._state.context
def test_set_if_not_exists(): cache = global_cache._InProcessGlobalCache() result = cache.set_if_not_exists({b"one": b"foo", b"two": b"bar"}) assert result == {b"one": True, b"two": True} result = cache.set_if_not_exists({b"two": b"bar", b"three": b"baz"}) assert result == {b"two": False, b"three": True} result = cache.get([b"two", b"three", b"one"]) assert result == [b"bar", b"baz", b"foo"]
def test__clear_global_cache(self): context = self._make_one(global_cache=global_cache._InProcessGlobalCache()) with context.use(): key = key_module.Key("SomeKind", 1) cache_key = _cache.global_cache_key(key._key) context.cache[key] = "testdata" context.global_cache.cache[cache_key] = "testdata" context.global_cache.cache["anotherkey"] = "otherdata" context._clear_global_cache().result() assert context.global_cache.cache == {"anotherkey": "otherdata"}
def test_watch_compare_and_swap(): cache = global_cache._InProcessGlobalCache() result = cache.watch([b"one", b"two", b"three"]) assert result is None cache.cache[b"two"] = (b"hamburgers", None) result = cache.compare_and_swap( {b"one": b"foo", b"two": b"bar", b"three": b"baz"} ) assert result is None result = cache.get([b"one", b"two", b"three"]) assert result == [b"foo", b"hamburgers", b"baz"]
def test_set_get_delete_w_expires(time): time.time.return_value = 0 cache = global_cache._InProcessGlobalCache() result = cache.set( {b"one": b"foo", b"two": b"bar", b"three": b"baz"}, expires=5 ) assert result is None result = cache.get([b"two", b"three", b"one"]) assert result == [b"bar", b"baz", b"foo"] time.time.return_value = 10 result = cache.get([b"two", b"three", b"one"]) assert result == [None, None, None]
def test_delete_entity_in_transaction_with_global_cache( client_context, ds_entity): """Regression test for #426 https://github.com/googleapis/python-ndb/issues/426 """ class SomeKind(ndb.Model): foo = ndb.IntegerProperty() entity_id = test_utils.system.unique_resource_id() ds_entity(KIND, entity_id, foo=42) global_cache = global_cache_module._InProcessGlobalCache() with client_context.new(global_cache=global_cache).use(): key = ndb.Key(KIND, entity_id) assert key.get().foo == 42 ndb.transaction(key.delete) assert key.get() is None
def test_delete_entity_with_global_cache(ds_entity, client_context): entity_id = test_utils.system.unique_resource_id() ds_entity(KIND, entity_id, foo=42) class SomeKind(ndb.Model): foo = ndb.IntegerProperty() key = ndb.Key(KIND, entity_id) cache_key = _cache.global_cache_key(key._key) global_cache = global_cache_module._InProcessGlobalCache() cache_dict = global_cache_module._InProcessGlobalCache.cache with client_context.new(global_cache=global_cache).use(): assert key.get().foo == 42 assert cache_key in cache_dict assert key.delete() is None assert cache_key not in cache_dict # This is py27 behavior. Not entirely sold on leaving _LOCKED value for # Datastore misses. assert key.get() is None assert cache_dict[cache_key][0] == b"0"
def test_crud_without_datastore(ds_entity, client_context): entity_id = test_utils.system.unique_resource_id() class SomeKind(ndb.Model): foo = ndb.IntegerProperty() bar = ndb.StringProperty() baz = ndb.StringProperty() global_cache = global_cache_module._InProcessGlobalCache() with client_context.new(global_cache=global_cache).use() as context: context.set_global_cache_policy(None) # Use default context.set_datastore_policy(False) # Don't use Datastore key = ndb.Key(KIND, entity_id) SomeKind(foo=42, bar="none", baz="night", _key=key).put() entity = key.get() assert isinstance(entity, SomeKind) assert entity.foo == 42 assert entity.bar == "none" assert entity.baz == "night" key.delete() assert key.get() is None
def test_set_if_not_exists_w_expires(time): time.time.return_value = 0 cache = global_cache._InProcessGlobalCache() result = cache.set_if_not_exists({ b"one": b"foo", b"two": b"bar" }, expires=5) assert result == {b"one": True, b"two": True} result = cache.set_if_not_exists({ b"two": b"bar", b"three": b"baz" }, expires=5) assert result == {b"two": False, b"three": True} result = cache.get([b"two", b"three", b"one"]) assert result == [b"bar", b"baz", b"foo"] time.time.return_value = 10 result = cache.get([b"two", b"three", b"one"]) assert result == [None, None, None]
def test_clear(): cache = global_cache._InProcessGlobalCache() cache.cache["foo"] = "bar" cache.clear() assert cache.cache == {}