Google App Engine,使用ndb的Python 2.7
当我运行以下测试时 - 它会在最后一个断言上抛出错误 - >
self.assertEqual(models.Log.query().count(), 1)
AssertionError: 0 != 1
Log是一个基本的ndb.Model类。运行这些测试感谢帮助。
import unittest2
from google.appengine.ext import ndb
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_util
import rm.base.models as models
class TestModels(unittest2.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)
def tearDown(self):
self.testbed.deactivate()
def testModelsLog(self):
l = models.Log(comment='hello kitty')
l.put()
self.assertEqual(l.comment, 'hello kitty')
self.assertTrue(l.user is None)
self.assertEqual(models.Log.query().count(), 1)
答案 0 :(得分:3)
你的问题是什么?
AssertionError是预期的。
您没有进行强烈一致的查询,
你读过了吗? https://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency正如它所说:“要获得强一致的查询结果,您需要使用祖先查询将结果限制为单个实体组。”你不是在做什么。
答案 1 :(得分:1)
请注意,您正在设置PseudoRandomHRConsistencyPolicy,概率为0.这意味着0是计数查询的预期结果。
这并不反映“正常”行为,而是数据存储如何在极端条件下运行。
要测试正常操作,请不要将PseudoRandomHRConsistencyPolicy策略添加到测试中,除非这是您真正需要测试的内容。