Fix mocking of utcnow() for model datetime cols

Now when we are going to deprecate timeutils.set_time_override()
and use mock instead, it easy to hit the following gotcha: when
testr does tests discovering, it imports all modules within a given
directory and it might happen that common.db.sqlalchemy.models
is imported before timeutils.utcnow() is mocked in a test case.
In this case the SQLAlchemy model will save the reference to the
original timeutils.utcnow() function, which will be called later
when the model will be saved to a DB, if created_at or updated_at
value aren't provided.

In order to overcome this we can defer resolution of timeutils.utcnow
reference by passing lambda functions to SA Column __init__().

Related-Bug: #1266962

Change-Id: I7adce90eacb4a3f334d77da7b4a176c31ff818ed
This commit is contained in:
Roman Podoliaka 2014-01-17 16:26:24 +02:00
parent fc1843ba48
commit 70ebb197a1
1 changed files with 2 additions and 2 deletions

View File

@ -102,8 +102,8 @@ class ModelBase(object):
class TimestampMixin(object):
created_at = Column(DateTime, default=timeutils.utcnow)
updated_at = Column(DateTime, onupdate=timeutils.utcnow)
created_at = Column(DateTime, default=lambda: timeutils.utcnow())
updated_at = Column(DateTime, onupdate=lambda: timeutils.utcnow())
class SoftDeleteMixin(object):