Merge "Add a TimestampedObject mixin"

This commit is contained in:
Jenkins 2017-09-25 23:47:29 +00:00 committed by Gerrit Code Review
commit a32faa0913
2 changed files with 52 additions and 0 deletions

View File

@ -715,6 +715,18 @@ class ComparableVersionedObject(object):
return NotImplemented
class TimestampedObject(object):
"""Mixin class for db backed objects with timestamp fields.
Sqlalchemy models that inherit from the oslo_db TimestampMixin will include
these fields and the corresponding objects will benefit from this mixin.
"""
fields = {
'created_at': obj_fields.DateTimeField(nullable=True),
'updated_at': obj_fields.DateTimeField(nullable=True),
}
class VersionedObjectDictCompat(object):
"""Mix-in to provide dictionary key access compatibility

View File

@ -23,6 +23,7 @@ import mock
from oslo_context import context
from oslo_serialization import jsonutils
from oslo_utils import timeutils
import testtools
from testtools import matchers
from oslo_versionedobjects import base
@ -2441,3 +2442,42 @@ class TestListObjectConcat(test.TestCase):
return [] + obj
self.assertRaises(TypeError, add, list1)
class TestTimestampedObject(test.TestCase):
"""Test TimestampedObject mixin.
Do this by creating an object that uses the mixin and confirm that the
added fields are there and in fact behaves as the DateTimeFields we desire.
"""
def setUp(self):
super(TestTimestampedObject, self).setUp()
@base.VersionedObjectRegistry.register_if(False)
class MyTimestampedObject(base.VersionedObject,
base.TimestampedObject):
fields = {
'field1': fields.Field(fields.String()),
}
self.myclass = MyTimestampedObject
self.my_object = self.myclass(field1='field1')
def test_timestamped_has_fields(self):
self.assertEqual('field1', self.my_object.field1)
self.assertIn('updated_at', self.my_object.fields)
self.assertIn('created_at', self.my_object.fields)
def test_timestamped_holds_timestamps(self):
now = timeutils.utcnow(with_timezone=True)
self.my_object.updated_at = now
self.my_object.created_at = now
self.assertEqual(now, self.my_object.updated_at)
self.assertEqual(now, self.my_object.created_at)
def test_timestamped_rejects_not_timestamps(self):
with testtools.ExpectedException(ValueError, '.*parse date.*'):
self.my_object.updated_at = 'a string'
with testtools.ExpectedException(ValueError, '.*parse date.*'):
self.my_object.created_at = 'a string'