Merge "Added create and update hooks to version mixin"
This commit is contained in:
commit
b1e6055c7d
|
@ -233,8 +233,8 @@ director objects better we could define a common class:
|
|||
super(AccessMixin, self).on_create_pre()
|
||||
self.created_at = datetime.datetime.now()
|
||||
|
||||
def on_update_pre(self):
|
||||
super(AccessMixin, self).on_update_pre()
|
||||
def on_update_pre(self, orig):
|
||||
super(AccessMixin, self).on_update_pre(orig)
|
||||
self.updated_at = datetime.datetime.now()
|
||||
|
||||
The above code updates the relevant fields on create/update operations, so if
|
||||
|
|
|
@ -189,6 +189,7 @@ operations performed in NbApi, e.g.
|
|||
|
||||
* `on_update_pre` - Called on the object each time it is updated, can be used
|
||||
for example to generate a new version or trigger updates on other objects.
|
||||
Gets the original object that exists in the database as a parameter.
|
||||
|
||||
Since those hooks are methods on objects themselves, we can use super() to
|
||||
chain all needed hooks in parents and mixins, according to Python's native MRO.
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import copy
|
||||
import time
|
||||
import traceback
|
||||
|
||||
|
@ -201,6 +202,7 @@ class NbApi(object):
|
|||
"""
|
||||
model = type(obj)
|
||||
full_obj = self.get(obj)
|
||||
db_obj = copy.copy(full_obj)
|
||||
|
||||
if full_obj is None:
|
||||
raise df_exceptions.DBKeyNotFound(key=obj.id)
|
||||
|
@ -210,7 +212,7 @@ class NbApi(object):
|
|||
if not changed_fields:
|
||||
return
|
||||
|
||||
full_obj.on_update_pre()
|
||||
full_obj.on_update_pre(db_obj)
|
||||
serialized_obj = full_obj.to_json()
|
||||
topic = _get_topic(full_obj)
|
||||
|
||||
|
|
|
@ -156,7 +156,7 @@ class _CommonBase(models.Base):
|
|||
'''
|
||||
pass
|
||||
|
||||
def on_update_pre(self):
|
||||
def on_update_pre(self, orig):
|
||||
'''Hook function called before object is updated in the NB database.
|
||||
'''
|
||||
pass
|
||||
|
|
|
@ -65,6 +65,6 @@ class Listener(mf.ModelBase):
|
|||
super(Listener, self).on_create_pre()
|
||||
self.update_timestamp()
|
||||
|
||||
def on_update_pre(self):
|
||||
super(Listener, self).on_update_pre()
|
||||
def on_update_pre(self, orig):
|
||||
super(Listener, self).on_update_pre(orig)
|
||||
self.update_timestamp()
|
||||
|
|
|
@ -36,6 +36,16 @@ class BasicEvents(mf.MixinBase):
|
|||
class Version(mf.MixinBase):
|
||||
version = fields.IntField()
|
||||
|
||||
def on_create_pre(self):
|
||||
super(Version, self).on_create_pre()
|
||||
if self.version is None:
|
||||
self.version = 1
|
||||
|
||||
def on_update_pre(self, orig):
|
||||
super(Version, self).on_update_pre(orig)
|
||||
if self.version == orig.version:
|
||||
self.version += 1
|
||||
|
||||
def is_newer_than(self, other):
|
||||
return other is None or self.version > other.version
|
||||
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
from jsonmodels import fields
|
||||
import mock
|
||||
import testtools
|
||||
|
||||
from dragonflow.db import api_nb
|
||||
import dragonflow.db.model_framework as mf
|
||||
from dragonflow.db.models import mixins
|
||||
from dragonflow.tests import base as tests_base
|
||||
from dragonflow.tests.common import utils
|
||||
|
||||
|
||||
@mf.register_model
|
||||
@mf.construct_nb_db_model
|
||||
class FieldTestModel(mf.ModelBase, mixins.Version):
|
||||
table_name = 'test_mixins'
|
||||
field = fields.IntField()
|
||||
|
||||
|
||||
class TestMixinVersions(tests_base.BaseTestCase):
|
||||
def setUp(self):
|
||||
super(TestMixinVersions, self).setUp()
|
||||
self.api_nb = api_nb.NbApi(db_driver=mock.Mock())
|
||||
|
||||
def test_on_create(self):
|
||||
instance = FieldTestModel(id='11111111')
|
||||
self.api_nb.create(instance, True)
|
||||
self.assertEqual(1, instance.version)
|
||||
|
||||
@testtools.skip('review/480194')
|
||||
@utils.with_nb_objects(
|
||||
FieldTestModel(id='11111111', version=1, field=1)
|
||||
)
|
||||
def test_on_update(self):
|
||||
instance = FieldTestModel(id='11111111', field=2)
|
||||
self.api_nb.update(instance, True)
|
||||
db_inst = self.api_nb.get(FieldTestModel(id='11111111'))
|
||||
self.assertEqual(2, db_inst.version)
|
Loading…
Reference in New Issue