I'm trying to test a filter for the DateTimeProperty with App Engine's NDB but I have it set to auto_now?
Is there a way to get around this for unit testing?
Example:
class MyModel(ndb.Model)
timestamp = ndb.DateTimeProperty(auto_now)
name = ndb.StringProperty()
def testMyModelFilter(self):
test1 = MyModel()
test1.timestamp = datetime.datetime.now() - datetime.timedelta(hours=2)
test1.put()
test2 = MyModel()
test2.timestamp = datetime.datetime.now() - datetime.timedelta(hours=1)
test2.put()
hour_ago = datetime.datetime.now() - datetime.timedelta(hours=1)
fetched = MyModel.query().filter(MyModel.timestamp < hour_ago).fetch(
None, keys_only=True)
Unfortunately, when I commit it to the datastore with test.put(), it uses the time when it was put().
_pre_put_hook()
or setMyModel.timestamp
to a normalndb.DateTimeProperty
if you're in a test. – Leanoraleanttest.put()
totest_key
, and then get the entity using the key? – Coleen