How to override the timestamp field for unit test purposes?

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

Unit Test setup part:

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 date

Therefore, I cannot emulate an old record for my unit testing. How to override this field without changing the model?

+1
source share
1 answer

From docs

You can override the value for property c auto_now_add=True, but not for one s auto_now=True. An automatic value is not generated until the object is recorded; that is, these parameters do not provide dynamic defaults. (This data is different from the old db API.)

+2
source

All Articles