GAE测试验证ndb实体是否** **放入数据存储区

时间:2015-02-02 20:15:17

标签: unit-testing google-app-engine google-cloud-datastore app-engine-ndb

非常简单,我想运行一些代码并验证类型MyEntity(ndb.Model)无实体是否已放入数据存储区。

我已经尝试过MyEntity.query().fetch(keys_only=True),但这似乎并不强烈一致(即它没有显示我刚刚添加的一个实体),因此返回空列表并不是真正的确认。 (我应该补充一点,我正在使用testbed.init_datastore_v3_stub而我已尝试datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)MasterSlaveConsistencyPolicy()但没有成功。)

我认为有一种方法可以为所有类型的密钥执行强烈一致的“获取”,但现在我找不到任何支持它的文档。

我知道如果我有一个Key,我可以做一个强烈一致的获取,但是如何转换为验证实体添加,那么没有关键要获得?< / p>

1 个答案:

答案 0 :(得分:1)

好吧,除了disabling the ndb cache via global context之外,我终于通过How to Clear/Invalidate NDB Cache in Testsnormal unit testing testbeds提示)了解它。

也得到了帮助:

助手Mixin:

class NdbTestbedNoCachingStronglyConsistentMixin(unittest.TestCase):
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        policy = None
        # policy = datastore_stub_util.MasterSlaveConsistencyPolicy()
        # policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1.0)
        self.testbed.init_datastore_v3_stub(consistency_policy=policy)
        # self.testbed.init_memcache_stub()

        # It's not enough (or necessary) to use stubs for datastore and memcache -
        # we also need to change ndb policy to ensure that datastore queries
        # are strongly-consistent
        cxt = ndb.get_context()

        def disallow(*args, **kwargs):
            return False

        cxt.set_cache_policy(disallow)
        cxt.set_memcache_policy(disallow)
        super(NdbTestbedNoCachingStronglyConsistentMixin, self).setUp()

    def tearDown(self):
        super(NdbTestbedNoCachingStronglyConsistentMixin, self).tearDown()
        self.testbed.deactivate()

这样的用法(遗漏了一些不相关的代码):

def _get_entity_keys():
    return MyNdbEntity.query().fetch(keys_only=True)


class TestMyEntity(NdbTestbedNoCachingStronglyConsistentMixin):

    def tearDown(self):
        super(TestMyEntity, self).tearDown()

        # Verify our assumptions that no entities persist between tests
        assert MyNdbEntity.get_by_identifier(self.account, TEST_IDENTIFIER) is None
        assert not _get_entity_keys()

    def test_get_uri_should_retrieve_existing_entity(self):
        provider.get_uri(TEST_IDENTIFIER)  # creates entity
        all_assets_keys = _get_entity_keys()
        created_asset = MyNdbEntity.get_by_identifier(self.account, TEST_IDENTIFIER)

        self.assertIn(created_asset.key, all_assets_keys)

        provider.get_uri(TEST_IDENTIFIER)
        all_assets_keys_after_get = _get_entity_keys()

        self.assertSequenceEqual(all_assets_keys_after_get, all_assets_keys)


    def test_get_uri_should_create_entity_and_prefix_identifier_with_security_id(self):
        expected_identifier = TEST_SECURITY_ID + '-' + TEST_IDENTIFIER
        expected_uri = VIEWER_IMAGE_BASE_URI % expected_identifier

        uri = provider.get_uri(TEST_IDENTIFIER)
        image = _get_entity_keys()[0].get()

        self.assertEqual(image.identifier, expected_identifier)
        self.assertEqual(uri, expected_uri)