python - 如何覆盖时间戳字段以进行单元测试?

标签 python google-app-engine unit-testing

class Record(ndb.Model):
    notes = ndb.TextProperty()
    last_updated = ndb.DateTimeProperty(auto_now=True)

部分单元测试设置:

record2 = Record()    
# trying to set the last_updated timestamp to a previous date
record2.last_updated = previous_date

record2.put()
#after saving it, the timestamp is back to today's date

因此我无法模拟单元测试的旧记录。如何在不更改模型的情况下覆盖该字段?

最佳答案

来自 docs

It is possible to override the value for a property with auto_now_add=True, but not for one with auto_now=True. The automatic value is not generated until the entity is written; that is, these options don't provide dynamic defaults. (These details differ from the old db API.)

关于python - 如何覆盖时间戳字段以进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19379133/

相关文章:

wcf - 如何在 WF 工作流中模拟 Web 服务调用?

python - 在 python 的 __str__ 方法中返回或打印

python - parking 位角点坐标通过 Python 3.6 中的 OpenCV 收集

python - 有效比较两个大列表列表中每个列表中的第一项?

google-app-engine - Google AppEngine 的 dev_appserver.py 失败,错误代码为 "invalid command name ' app.yaml'”

google-app-engine - 取消选中时,Google Cloud Storage 公共(public)链接不会失效

visual-studio-2010 - 当我们在解决方案中运行所有测试时,为什么不包含新的单元测试项目?

laravel - 在PhpUnit测试中匹配JsonStructure-Laravel 5.4

python - Matplotlib:让 contourf 颜色标签以对数刻度和多个级别显示

java - 向 Google Admin Directory API 进行身份验证