如何覆盖单元测试目的的时间戳字段? [英] How to override a timestamp field for unit test purposes?

查看:399
本文介绍了如何覆盖单元测试目的的时间戳字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

部分单元测试设置:

Part of Unit Test setup:

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

因此,我无法模拟旧记录为我的单元测试。

Hence I can't emulate an old record for my unit testing. How do I override that field without having to change the model?

推荐答案

$ b

From the docs


可以使用 auto_now_add = True 来覆盖某个属性的值,但不能为 auto_now = True 。直到写入实体才会生成自动值;也就是说,这些选项不提供动态默认值。 (这些细节与旧的db API不同。)

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.)

这篇关于如何覆盖单元测试目的的时间戳字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆