How to override a timestamp field for unit test purposes?

此生再无相见时 提交于 2020-01-05 05:43:09

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/19379133/how-to-override-a-timestamp-field-for-unit-test-purposes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!