如何从数据存储中刷新NDB实体? [英] How do I refresh an NDB entity from the datastore?

查看:79
本文介绍了如何从数据存储中刷新NDB实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在测试中断言我的代码被修改的实体称为 Model.put()。不幸的是,似乎有一些缓存正在进行,例如:

  from google.appengine.ext import ndb 

class MyModel(ndb.Model):
name = StringProperty(indexed = True)
text = StringProperty()
$ b $ def update_entity(id,text) :
entity = MyModel.get_by_id(id)
entity.text = text
#这是entity.put()应该发生的地方,但不是



$ b

通过此测试:

  def test_updates_entity_in_datastore (unittest.TestCase):
name ='Beartato'
entity = MyModel(id = 12345L,name = name,text = None)
text ='foo bar baz'
update_entity(entity.key.id(),text)
new_entity = entity.key.get()#不做任何事情,显然
#new_entity = entity.query(MyModel.name == name ).fetch()[0]#Same
assert new_entity.text == text

当我真的宁愿它没有t,因为在现实世界中, update_entity 实际上不会改变数据存储区中的任何内容。使用Nose, datastore_v3_stub和memcache_stub。

解决方案

您可以绕过这样的缓存:

$ b

这些选项来自于ndb的 上下文选项。它们可以应用于 Model.get_by_id() Model.query()。fetch() Model.query().get() too


I'd like to be able to assert in tests that my code called Model.put() for the entities that were modified. Unfortunately, there seems to be some caching going on, such that this code:

from google.appengine.ext import ndb

class MyModel(ndb.Model):
    name = StringProperty(indexed=True)
    text = StringProperty()

def update_entity(id, text):
    entity = MyModel.get_by_id(id)
    entity.text = text
    # This is where entity.put() should happen but doesn't

Passes this test:

def test_updates_entity_in_datastore(unittest.TestCase):
    name = 'Beartato'
    entity = MyModel(id=12345L, name=name, text=None)
    text = 'foo bar baz'
    update_entity(entity.key.id(), text)
    new_entity = entity.key.get()  # Doesn't do anything, apparently
    # new_entity = entity.query(MyModel.name == name).fetch()[0]  # Same
    assert new_entity.text == text

When I would really rather it didn't, since in the real world, update_entity won't actually change anything in the datastore.

Using Nose, datastore_v3_stub, and memcache_stub.

解决方案

You can bypass the caching like this:

entity = key.get(use_cache=False, use_memcache=False)

These options are from ndb's context options. They can be applied to Model.get_by_id(), Model.query().fetch() and Model.query().get() too

这篇关于如何从数据存储中刷新NDB实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆