1

ndb を使用した Google App Engine、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)
4

2 に答える 2

1

確率 0 で PseudoRandomHRConsistencyPolicy を設定していることに注意してください。これは、カウント クエリの予想される結果が 0 であることを意味します。

これは「通常の」動作を反映したものではなく、極端な条件でデータストアがどのように動作するかを反映しています。

通常の操作をテストするには、本当にテストする必要がある場合を除き、PseudoRandomHRConsistencyPolicy ポリシーをテストに追加しないでください。

https://developers.google.com/appengine/docs/python/tools/localunittesting#Writing_HRD_Datastore_Tests

于 2013-05-11T06:47:49.403 に答える