Debian Codegraph @

os_vif/tests/unit/test_base.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import copy
14
15 from unittest import mock
16
17 from oslo_serialization import jsonutils
18 from oslo_versionedobjects import base
19 from oslo_versionedobjects import fields
20
21 from os_vif.objects import base as osv_base
22 from os_vif.tests.unit import base as test_base
23
24
25 class TestVersionedObjectPrintable(test_base.TestCase):
26 @base.VersionedObjectRegistry.register_if(False)
27 class OVOChild1(osv_base.VersionedObject,
28 osv_base.VersionedObjectPrintableMixin):
29 fields = {
30 "child1_field1": fields.ListOfIntegersField()
31 }
32
33 @base.VersionedObjectRegistry.register_if(False)
34 class OVOParent(osv_base.VersionedObject,
35 osv_base.VersionedObjectPrintableMixin,
36 base.ComparableVersionedObject):
37 fields = {
38 "parent_field1": fields.ListOfObjectsField("OVOChild1"),
39 "parent_field2": fields.StringField(),
40 }
41
42 def setUp(self):
43 super(TestVersionedObjectPrintable, self).setUp()
44 child1_1 = self.OVOChild1(child1_field1=[1, 2, 3])
45 child1_2 = self.OVOChild1(child1_field1=[4, 5, 6])
46 self.obj = self.OVOParent(
47 parent_field1=[child1_1, child1_2],
48 parent_field2="test string")
49
50 def test_print_object(self):
51 out = str(self.obj)
52 self.assertIn("'child1_field1': [1, 2, 3]}", out)
53 self.assertIn("'child1_field1': [4, 5, 6]}", out)
54 cmp = str({'parent_field2': "test string"})
55 cmp = cmp.replace('{', '').replace('}', '')
56 self.assertIn(str(cmp), out)
57
58 @mock.patch.object(base.VersionedObject, "obj_class_from_name",
59 side_effect=[OVOParent, OVOChild1, OVOChild1])
60 def test_serialize_object(self, *mock):
61 """Test jsonutils serialization is not affected by this new mixin."""
62 obj_orig = copy.deepcopy(self.obj)
63 obj_orig_primitive = obj_orig.obj_to_primitive()
64 str_orig_primitive = jsonutils.dumps(obj_orig_primitive)
65 obj_new_primitive = jsonutils.loads(str_orig_primitive)
66 obj_new = self.OVOParent.obj_from_primitive(obj_new_primitive)
67 self.assertEqual(obj_orig_primitive, obj_new_primitive)
68 self.assertEqual(obj_orig, obj_new)
69
70 def test_import_non_ovo_class(self):
71 """Test VersionedObjectPrintable could be inherited by non-OVO classes.
72 """
73 class NonOVOClass(osv_base.VersionedObjectPrintableMixin):
74 def __str__(self):
75 return "NonOVOClass __str__ method"
76
77 self.assertEqual("NonOVOClass __str__ method", str(NonOVOClass()))