| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
# you may not use this file except in compliance with the License. |
| 4 |
# You may obtain a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 11 |
# implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import gc |
| 16 |
|
| 17 |
from sqlalchemy import orm |
| 18 |
import testtools |
| 19 |
|
| 20 |
from neutron_lib.db import standard_attr |
| 21 |
from neutron_lib.tests import _base as base |
| 22 |
|
| 23 |
|
| 24 |
class StandardAttrTestCase(base.BaseTestCase): |
| 25 |
def setUp(self): |
| 26 |
super().setUp() |
| 27 |
self.addCleanup(gc.collect) |
| 28 |
|
| 29 |
def _make_decl_base(self): |
| 30 |
# construct a new base so we don't interfere with the main |
| 31 |
# base used in the sql test fixtures |
| 32 |
class BaseV2(orm.DeclarativeBase, |
| 33 |
standard_attr.model_base.NeutronBaseV2): |
| 34 |
pass |
| 35 |
|
| 36 |
return BaseV2 |
| 37 |
|
| 38 |
def test_standard_attr_resource_model_map(self): |
| 39 |
rs_map = standard_attr.get_standard_attr_resource_model_map() |
| 40 |
decl_base = self._make_decl_base() |
| 41 |
|
| 42 |
class MyModel(standard_attr.HasStandardAttributes, |
| 43 |
standard_attr.model_base.HasId, |
| 44 |
decl_base): |
| 45 |
api_collections = ['my_resource', 'my_resource2'] |
| 46 |
api_sub_resources = ['my_subresource'] |
| 47 |
|
| 48 |
rs_map = standard_attr.get_standard_attr_resource_model_map() |
| 49 |
self.assertEqual(MyModel, rs_map['my_resource']) |
| 50 |
self.assertEqual(MyModel, rs_map['my_resource2']) |
| 51 |
self.assertEqual(MyModel, rs_map['my_subresource']) |
| 52 |
|
| 53 |
sub_rs_map = standard_attr.get_standard_attr_resource_model_map( |
| 54 |
include_resources=False, |
| 55 |
include_sub_resources=True) |
| 56 |
self.assertNotIn('my_resource', sub_rs_map) |
| 57 |
self.assertNotIn('my_resource2', sub_rs_map) |
| 58 |
self.assertEqual(MyModel, sub_rs_map['my_subresource']) |
| 59 |
|
| 60 |
nosub_rs_map = standard_attr.get_standard_attr_resource_model_map( |
| 61 |
include_resources=True, |
| 62 |
include_sub_resources=False) |
| 63 |
self.assertEqual(MyModel, nosub_rs_map['my_resource']) |
| 64 |
self.assertEqual(MyModel, nosub_rs_map['my_resource2']) |
| 65 |
self.assertNotIn('my_subresource', nosub_rs_map) |
| 66 |
|
| 67 |
class Dup(standard_attr.HasStandardAttributes, |
| 68 |
standard_attr.model_base.HasId, |
| 69 |
decl_base): |
| 70 |
api_collections = ['my_resource'] |
| 71 |
|
| 72 |
with testtools.ExpectedException(RuntimeError): |
| 73 |
standard_attr.get_standard_attr_resource_model_map() |
| 74 |
|
| 75 |
def test_standard_attr_resource_parent_map(self): |
| 76 |
decl_base = self._make_decl_base() |
| 77 |
|
| 78 |
class TagSupportModel(standard_attr.HasStandardAttributes, |
| 79 |
standard_attr.model_base.HasId, |
| 80 |
decl_base): |
| 81 |
collection_resource_map = {'collection_name': 'member_name'} |
| 82 |
tag_support = True |
| 83 |
|
| 84 |
class TagUnsupportModel(standard_attr.HasStandardAttributes, |
| 85 |
standard_attr.model_base.HasId, |
| 86 |
decl_base): |
| 87 |
collection_resource_map = {'collection_name2': 'member_name2'} |
| 88 |
tag_support = False |
| 89 |
|
| 90 |
class TagUnsupportModel2(standard_attr.HasStandardAttributes, |
| 91 |
standard_attr.model_base.HasId, |
| 92 |
decl_base): |
| 93 |
collection_resource_map = {'collection_name3': 'member_name3'} |
| 94 |
|
| 95 |
parent_map = standard_attr.get_tag_resource_parent_map() |
| 96 |
self.assertEqual('member_name', parent_map['collection_name']) |
| 97 |
self.assertNotIn('collection_name2', parent_map) |
| 98 |
self.assertNotIn('collection_name3', parent_map) |
| 99 |
|
| 100 |
class DupTagSupportModel(standard_attr.HasStandardAttributes, |
| 101 |
standard_attr.model_base.HasId, |
| 102 |
decl_base): |
| 103 |
collection_resource_map = {'collection_name': 'member_name'} |
| 104 |
tag_support = True |
| 105 |
|
| 106 |
with testtools.ExpectedException(RuntimeError): |
| 107 |
standard_attr.get_tag_resource_parent_map() |