How to override a timestamp field for unit test pu

2019-07-30 16:22发布

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条回答
ら.Afraid
2楼-- · 2019-07-30 17:02

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

查看更多
登录 后发表回答