1

I have a simple NDB Model with an instance attribute, which is not an NDB property. I want to test that the property value gets lost when the entity is saved and retrieved from the datastore.

Now when the entity is retrieved it is the same instance as the one that has been put to the datastore.

Is there a way to programatically clear/invalidate the memcache and the NDB in-memory cache?

The example is a pytest test, sorry for that.

from google.appengine.ext import testbed, ndb


class User(ndb.Model):
    name = ndb.TextProperty()
    foo = None


class TestNDBModel(object):

    def setup(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

    def teardown(self):
        self.testbed.deactivate()

    def test_foo(self):
        user = User(name='Andy')
        assert user.name == 'Andy'
        assert user.foo is None
        user.foo = 123
        assert user.foo == 123
        user_key = user.put()

        # Here I need to clear the cache somehow,
        # so that the entity must be retrieved from datastore

        retrieved_user = user_key.get()

        # These two will now fail
        assert retrieved_user is not user
        assert retrieved_user.foo is None
Peter Hudec
  • 2,462
  • 3
  • 22
  • 29

3 Answers3

1

You're apparently using the wrong method to assign a value to retrieved_user. Use the following instead:

retrieved_user = user_key.delete(use_datastore=False)

See the following link for more information:

https://developers.google.com/appengine/docs/python/ndb/functions#context_options

Marc
  • 4,546
  • 2
  • 29
  • 45
Dmytro Sadovnychyi
  • 6,171
  • 5
  • 33
  • 60
  • This doesn't help, the retrieved user becomes `None`, even if it's fetched with `User.get_by_id(user_key.id())`. Nevertheless, the link you provided led me to the actual solution. – Peter Hudec Mar 22 '14 at 08:46
1

The solution is to prevent an entity to be cached when it is being put to the DB with entity.put(use_cache=False, use_memcache=False).

from google.appengine.ext import testbed, ndb


class User(ndb.Model):
    name = ndb.TextProperty()
    foo = None


class TestNDBModel(object):

    def setup(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

    def teardown(self):
        self.testbed.deactivate()

    def test_foo(self):
        user = User(name='Andy')
        assert user.name == 'Andy'
        assert user.foo is None
        user.foo = 123
        assert user.foo == 123

        # This prevents the entity to be cached
        user_key = user.put(use_cache=False, use_memcache=False)

        # The entity is now retrieved from DB
        retrieved_user = user_key.get()

        # These two will now pass
        assert retrieved_user is not user
        assert retrieved_user.foo is None
Peter Hudec
  • 2,462
  • 3
  • 22
  • 29
1

I had the same problem, and was searching for a solution. This does the trick:

ndb.get_context().clear_cache()
new name
  • 15,861
  • 19
  • 68
  • 114
  • This is very helpful since I want caching and memcache when running in the wild, and my `put()` or `put_multi()` methods are called in the resources I'm testing, and not in the test itself. Calling `clear_cache()` from inside the test allows me to leave the real code as is, and just control the testbed. – Zach Young Nov 22 '16 at 21:25