【问题标题】:How to write a unit test to see if a property is set to indexed=False?如何编写单元测试以查看属性是否设置为 indexed=False?
【发布时间】:2015-01-17 13:01:04
【问题描述】:

如果我有这样的模型:

class Person(ndb.Model):
  name = ndb.StringProperty()
  age = ndb.IntegerProperty(indexed=False)

如何测试age 的索引是否设置为 False?

【问题讨论】:

  • 在 TDD 中应该捕获任何期望。关闭不必要的索引,提高性能并降低 GAE 的写入成本。
  • 简单的self.assertFalse(Person.age._indexed) 怎么样——有什么不适合你的理由吗?
  • @AlexMartelli 效果很好。请添加它作为答案。
  • 完成,现在是答案。

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


【解决方案1】:

你只需要:

self.assertFalse(Person.age._indexed)

没有任何复杂性。

【讨论】:

    【解决方案2】:
    properties = Person._properties
    assertEqual(properties["age"], GenericProperty('age', indexed=False))
    

    根据您的代码发生的其他情况,您的断言语句中可能需要不同的属性。

    使用Person._properties 查看您应该期待什么。或者,您可以仅解析 indexed=False,或仅解析 _properties 输出中的布尔值,并使用更简单的断言来提供更具体的测试。

    【讨论】:

    • 感谢您的建议。它失败了BadFilterError: invalid filter: Cannot query for unindexed property age.
    • 捕获该错误并将其视为成功可能吗?因为在某种程度上,您的错误向您表明“年龄”未编入索引。你的目标是什么,不是吗?
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多