Merge "Squash revert to breaking changes"

This commit is contained in:
Jenkins 2016-11-24 16:53:17 +00:00 committed by Gerrit Code Review
commit c028ceb6f9
4 changed files with 90 additions and 6 deletions

35
neutron/db/model_base.py Normal file
View File

@ -0,0 +1,35 @@
# Copyright (c) 2012 OpenStack Foundation.
#
# 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 neutron_lib.db import model_base as lib_mb
import sqlalchemy as sa
from neutron.common import _deprecate
_deprecate._moved_global('HasTenant', new_module=lib_mb, new_name='HasProject')
def get_unique_keys(model):
try:
constraints = model.__table__.constraints
except AttributeError:
constraints = []
return [[c.name for c in constraint.columns]
for constraint in constraints
if isinstance(constraint, sa.UniqueConstraint)]
# This shim is used to deprecate the old contents.
_deprecate._MovedGlobals(lib_mb)

View File

@ -18,7 +18,6 @@ import itertools
from neutron_lib import exceptions as n_exc
from oslo_db import exception as obj_exc
from oslo_db.sqlalchemy import utils as db_utils
from oslo_serialization import jsonutils
from oslo_versionedobjects import base as obj_base
from oslo_versionedobjects import fields as obj_fields
@ -27,6 +26,7 @@ import six
from neutron._i18n import _
from neutron.api.v2 import attributes
from neutron.db import api as db_api
from neutron.db import model_base
from neutron.db import standard_attr
from neutron.objects.db import api as obj_db_api
from neutron.objects import exceptions as o_exc
@ -240,8 +240,7 @@ class DeclarativeObject(abc.ABCMeta):
model_to_obj_translation = {
v: k for (k, v) in cls.fields_need_translation.items()}
keys = db_utils.get_unique_keys(model) or []
for model_unique_key in keys:
for model_unique_key in model_base.get_unique_keys(model):
obj_unique_key = [model_to_obj_translation.get(key, key)
for key in model_unique_key]
if obj_field_names.issuperset(obj_unique_key):

View File

@ -0,0 +1,50 @@
# Copyright (c) 2016 Mirantis, Inc.
# All Rights Reserved.
#
# 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.
import mock
import sqlalchemy as sa
from neutron.db import model_base
from neutron.tests import base as test_base
class GetUniqueKeysTestCase(test_base.BaseTestCase):
def test_with_unique_constraints(self):
model = mock.Mock()
metadata = sa.MetaData()
model.__table__ = sa.Table(
"test_table", metadata,
sa.Column("a", sa.Integer, unique=True),
sa.Column("b", sa.Integer),
sa.Column("c", sa.Integer),
sa.Column("d", sa.Integer),
sa.UniqueConstraint("c", "d"))
expected = {("a",), ("c", "d")}
observed = {tuple(sorted(key)) for key in
model_base.get_unique_keys(model)}
self.assertEqual(expected, observed)
def test_without_unique_constraints(self):
model = mock.Mock()
metadata = sa.MetaData()
model.__table__ = sa.Table(
"test_table", metadata,
sa.Column("a", sa.Integer),
sa.Column("b", sa.Integer))
self.assertEqual([], model_base.get_unique_keys(model))
def test_not_a_model(self):
self.assertEqual([], model_base.get_unique_keys(None))

View File

@ -20,7 +20,6 @@ import mock
import netaddr
from neutron_lib import exceptions as n_exc
from oslo_db import exception as obj_exc
from oslo_db.sqlalchemy import utils as db_utils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from oslo_versionedobjects import base as obj_base
@ -32,6 +31,7 @@ from neutron.common import constants
from neutron.common import utils
from neutron import context
from neutron.db import db_base_plugin_v2
from neutron.db import model_base
from neutron.db.models import external_net as ext_net_model
from neutron.db.models import l3 as l3_model
from neutron import objects
@ -624,7 +624,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase):
self.assertTrue(self._is_test_class(obj))
self._check_equal(self.objs[0], obj)
get_object_mock.assert_called_once_with(
mock.ANY, self._test_class.db_model,
self.context, self._test_class.db_model,
**self._test_class.modify_fields_to_db(obj_keys))
def _get_synthetic_fields_get_objects_calls(self, db_objs):
@ -982,7 +982,7 @@ class BaseDbObjectUniqueKeysTestCase(BaseObjectIfaceTestCase):
class UniqueKeysTestCase(test_base.BaseTestCase):
def test_class_creation(self):
m_get_unique_keys = mock.patch.object(db_utils, 'get_unique_keys')
m_get_unique_keys = mock.patch.object(model_base, 'get_unique_keys')
with m_get_unique_keys as get_unique_keys:
get_unique_keys.return_value = [['field1'],
['field2', 'db_field3']]