From 0c75a0590a0e683bfbaaba55488fb780e33579b5 Mon Sep 17 00:00:00 2001 From: hyades Date: Wed, 24 Jul 2013 00:10:50 +0530 Subject: [PATCH] Added properties - controller.py --- python-api/gstswitch/controller.py | 82 +++++++++++++++++++++++-- python-api/gstswitch/test_controller.py | 77 +++++++++++++++++++++++ 2 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 python-api/gstswitch/test_controller.py diff --git a/python-api/gstswitch/controller.py b/python-api/gstswitch/controller.py index 5217609..d8d0321 100644 --- a/python-api/gstswitch/controller.py +++ b/python-api/gstswitch/controller.py @@ -22,6 +22,80 @@ def __init__( self.object_path = object_path self.default_interface = default_interface + @property + def address(self): + return self._address + + @property + def bus_name(self): + if self._bus_name is None: + return None + return self._bus_name + + @property + def object_path(self): + return self._object_path + + @property + def default_interface(self): + return self._default_interface + + @address.setter + def address(self, address): + """Set the Address + http://dbus.freedesktop.org/doc/dbus-specification.html#addresses + """ + if not address: + raise ValueError("Address '{0}' cannot be blank") + else: + a = str(address) + if a.find(':') > 0: + self._address = a + else: + raise ValueError("""Address must follow specifications mentioned at + http://dbus.freedesktop.org/doc/dbus-specification.html#addresses""") + + @bus_name.setter + def bus_name(self, bus_name): + """Set the Bus Name + http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus + """ + if bus_name is None: + self._bus_name = None + return + a = str(bus_name) + self._bus_name = a + + @object_path.setter + def object_path(self, object_path): + """Set the object_path + http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path + """ + if not object_path: + raise ValueError("object_path '{0} cannot be blank'") + else: + a = str(object_path) + if a[0] == '/': + self._object_path = a + else: + raise ValueError("""object_path must follow specifications mentioned at + http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path""") + + @default_interface.setter + def default_interface(self, default_interface): + """Set the default_interface + http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface + """ + if not default_interface: + raise ValueError("default_interface '{0} cannot be blank'") + else: + a = str(default_interface) + if a.count('.') > 1: + self._default_interface = a + else: + raise ValueError("""default_interface must follow specifications mentioned at + http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface""") + def establish_connection(self): """Establishes a fresh connection to the dbus Connection stored as self.connection @@ -30,10 +104,10 @@ def establish_connection(self): :returns: None """ self.connection = Connection( - self.address, - self.bus_name, - self.object_path, - self.default_interface) + address=self.address, + bus_name=self.bus_name, + object_path=self.object_path, + default_interface=self.default_interface) self.connection.connect_dbus() diff --git a/python-api/gstswitch/test_controller.py b/python-api/gstswitch/test_controller.py new file mode 100644 index 0000000..8e935e1 --- /dev/null +++ b/python-api/gstswitch/test_controller.py @@ -0,0 +1,77 @@ +from controller import Controller +from exception import * +import pytest +from mock import Mock, patch + + +class TestAddress(object): + + def test_address_null(self): + address = ['', None, [], {}] + for x in address: + with pytest.raises(ValueError): + Controller(address=x) + + def test_address_colon(self): + address = 'abcdefghijk' + with pytest.raises(ValueError): + Controller(address=address) + + def test_address_normal(self): + address = ['unix:abstract=gstswitch', 'unix:temp=/tmp/abcd/xyz'] + for x in address: + conn = Controller(address=x) + assert conn.address == x + + +class TestBusName(object): + + def test_normal(self): + names = ['', 'abcd', 12345] + for bus in names: + conn = Controller(bus_name=bus) + assert conn.bus_name == str(bus) + + def test_normal_none(self): + name = None + conn = Controller(bus_name=name) + assert conn.bus_name == name + + +class TestObjectPath(object): + + def test_object_path_blank(self): + paths = [None, '', {}, []] + for object_path in paths: + with pytest.raises(ValueError): + Controller(object_path=object_path) + + def test_object_path_slash(self): + object_path = 'a/////////' + with pytest.raises(ValueError): + Controller(object_path=object_path) + + def test_object_path_normal(self): + object_path = "/info/duzy/gst/switch/SwitchController" + conn = Controller(object_path=object_path) + assert conn.object_path == object_path + + +class TestInterface(object): + + def test_interface_none(self): + default_interface = [None, '', [], {}] + for x in default_interface: + with pytest.raises(ValueError): + Controller(default_interface=x) + + def test_interface_dot(self): + default_interface = ['.', 'info.', 'info'] + for x in default_interface: + with pytest.raises(ValueError): + Controller(default_interface=x) + + def test_interface_normal(self): + default_interface = "info.duzy.gst.switch.SwitchControllerInterface" + conn = Controller(default_interface=default_interface) + assert default_interface == conn.default_interface \ No newline at end of file