Browse Source

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
tags/2014.1.b2
Roman Podoliaka 5 years ago
parent
commit
70ebb197a1
1 changed files with 2 additions and 2 deletions
  1. 2
    2
      openstack/common/db/sqlalchemy/models.py

+ 2
- 2
openstack/common/db/sqlalchemy/models.py View File

@@ -102,8 +102,8 @@ class ModelBase(object):
102 102
 
103 103
 
104 104
 class TimestampMixin(object):
105
-    created_at = Column(DateTime, default=timeutils.utcnow)
106
-    updated_at = Column(DateTime, onupdate=timeutils.utcnow)
105
+    created_at = Column(DateTime, default=lambda: timeutils.utcnow())
106
+    updated_at = Column(DateTime, onupdate=lambda: timeutils.utcnow())
107 107
 
108 108
 
109 109
 class SoftDeleteMixin(object):

Loading…
Cancel
Save