1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#
# Copyright (C) 2019 Red Hat, Inc. All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
#
import unittest
from unittest.mock import Mock
from dasbus.server.container import DBusContainer, DBusContainerError
from dasbus.server.interface import dbus_interface
from dasbus.server.publishable import Publishable
from dasbus.server.template import BasicInterfaceTemplate
from dasbus.typing import Str, ObjPath
@dbus_interface("org.Project.Object")
class MyInterface(BasicInterfaceTemplate):
def HelloWorld(self) -> Str:
return self.implementation.hello_world()
class MyObject(Publishable):
def for_publication(self):
return MyInterface(self)
def hello_world(self):
return "Hello World!"
class MyUnpublishable(object):
pass
class DBusContainerTestCase(unittest.TestCase):
"""Test DBus containers."""
def setUp(self):
self.message_bus = Mock()
self.container = DBusContainer(
namespace=("org", "Project"),
basename="Object",
message_bus=self.message_bus
)
def test_set_namespace(self):
"""Test set_namespace."""
self.container.set_namespace(("org", "Another", "Project"))
path = self.container.to_object_path(MyObject())
self.assertEqual(path, "/org/Another/Project/Object/1")
path = self.container.to_object_path(MyObject())
self.assertEqual(path, "/org/Another/Project/Object/2")
def test_to_object_path_failed(self):
"""Test failed to_object_path."""
with self.assertRaises(TypeError) as cm:
self.container.to_object_path(MyUnpublishable())
self.assertEqual(
"Type 'MyUnpublishable' is not publishable.",
str(cm.exception)
)
with self.assertRaises(DBusContainerError) as cm:
self.container._find_object_path(MyObject())
self.assertEqual(
"No object path found.",
str(cm.exception)
)
def test_to_object_path(self):
"""Test to_object_path."""
obj = MyObject()
path = self.container.to_object_path(obj)
self.message_bus.publish_object.assert_called_once()
published_path, published_obj = \
self.message_bus.publish_object.call_args[0]
self.assertEqual(path, "/org/Project/Object/1")
self.assertEqual(path, published_path)
self.assertIsInstance(published_obj, MyInterface)
self.assertEqual(obj, published_obj.implementation)
self.message_bus.reset_mock()
self.assertEqual(self.container.to_object_path(obj), path)
self.message_bus.publish_object.assert_not_called()
self.assertEqual(self.container.to_object_path(obj), path)
self.message_bus.publish_object.assert_not_called()
def test_to_object_path_list(self):
"""Test to_object_path_list."""
objects = [MyObject(), MyObject(), MyObject()]
paths = self.container.to_object_path_list(objects)
self.assertEqual(self.message_bus.publish_object.call_count, 3)
self.assertEqual(paths, [
"/org/Project/Object/1",
"/org/Project/Object/2",
"/org/Project/Object/3"
])
self.message_bus.reset_mock()
self.assertEqual(paths, self.container.to_object_path_list(objects))
self.message_bus.publish_object.assert_not_called()
self.assertEqual(paths, self.container.to_object_path_list(objects))
self.message_bus.publish_object.assert_not_called()
def test_from_object_path_failed(self):
"""Test failures."""
with self.assertRaises(DBusContainerError) as cm:
self.container.from_object_path(ObjPath("/org/Project/Object/1"))
self.assertEqual(
"Unknown object path '/org/Project/Object/1'.",
str(cm.exception)
)
def test_from_object_path(self):
"""Test from_object_path."""
obj = MyObject()
path = self.container.to_object_path(obj)
self.assertEqual(obj, self.container.from_object_path(path))
self.assertEqual(path, self.container.to_object_path(obj))
self.assertEqual(obj, self.container.from_object_path(path))
self.assertEqual(path, self.container.to_object_path(obj))
def test_from_object_path_list(self):
"""Test from_object_path_list."""
objects = [MyObject(), MyObject(), MyObject()]
paths = self.container.to_object_path_list(objects)
self.assertEqual(objects, self.container.from_object_path_list(paths))
self.assertEqual(paths, self.container.to_object_path_list(objects))
self.assertEqual(objects, self.container.from_object_path_list(paths))
self.assertEqual(paths, self.container.to_object_path_list(objects))
def test_multiple_objects(self):
"""Test multiple objects."""
obj = MyObject()
path = self.container.to_object_path(obj)
self.assertEqual(path, "/org/Project/Object/1")
self.assertEqual(obj, self.container.from_object_path(path))
self.message_bus.publish_object.assert_called_once()
self.message_bus.reset_mock()
obj = MyObject()
path = self.container.to_object_path(obj)
self.assertEqual(path, "/org/Project/Object/2")
self.assertEqual(obj, self.container.from_object_path(path))
self.message_bus.publish_object.assert_called_once()
self.message_bus.reset_mock()
obj = MyObject()
path = self.container.to_object_path(obj)
self.assertEqual(path, "/org/Project/Object/3")
self.assertEqual(obj, self.container.from_object_path(path))
self.message_bus.publish_object.assert_called_once()
self.message_bus.reset_mock()
|