我正在通过电子邮件在appengine数据存储上为用户过滤器编写测试,这里是代码:
class TestUser(unittest.TestCase):
def setUp(self):
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for use.
self.testbed.activate()
# Create a consistency policy that will simulate the High Replication consistency model.
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0)
# Initialize the datastore stub with this policy.
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
self.testbed.init_memcache_stub()
def tearDown(self):
self.testbed.deactivate()
def testQueryByEmail(self):
user = User(name='jon', last_name='doe', email='jdoe@some.com', password='pass')
user.put()
fetched = User.query().filter(User.email == user.email).get()
self.assertEqual(user.email,fetched.email)
我得到fetched = None
所以测试失败了。
如果我按ID fetched = User.get_by_id(key.id())
查询,则会获得用户。也许它与索引有关?你觉得怎么样?
答案 0 :(得分:1)
您已在probability=0
上设置PseudoRandomHRConsistencyPolicy
,这意味着您的查询永远不会对查询可见。您可能希望将其设置为1,除非您专门测试最终一致性下的行为。 (我从来没有觉得有必要这样做,但是ymmv。)
答案 1 :(得分:0)