测试Datatstore服务并从属性过滤器查询中获取null

时间:2014-01-29 21:11:22

标签: google-app-engine google-cloud-datastore

我正在通过电子邮件在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())查询,则会获得用户。也许它与索引有关?你觉得怎么样?

2 个答案:

答案 0 :(得分:1)

您已在probability=0上设置PseudoRandomHRConsistencyPolicy,这意味着您的查询永远不会对查询可见。您可能希望将其设置为1,除非您专门测试最终一致性下的行为。 (我从来没有觉得有必要这样做,但是ymmv。)

答案 1 :(得分:0)

根据docs

  

自动创建简单查询的索引,例如单个属性上的查询。必须在名为index.yaml的配置文件中定义复杂查询的索引。

点击链接获取示例。