def test_model_to_view(self):
        root = Controller.RootController('root')
        value_interface = TestValueInterface()
        optional_interface = TestOptionalInterface()

        controller = Controller.ValueController('test', root, value_interface,
                                                None,
                                                Converter.Str(0, 'default'))
        controller.model_to_view({'test': 'new'})
        self.assertEqual(value_interface.val, 'new')

        controller.model_to_view('new2')
        self.assertEqual(value_interface.val, 'new2')

        with self.assertRaises(AssertionError):
            controller.model_to_view({})

        controller = Controller.ValueController('test', root, value_interface,
                                                optional_interface,
                                                Converter.Str(0, 'default'))
        controller.model_to_view({'test': 'new'})
        self.assertEqual(value_interface.val, 'new')
        self.assertTrue(optional_interface.val)

        controller.model_to_view({})
        self.assertFalse(optional_interface.val)
    def test_model_to_view_value(self):
        root = Controller.RootController('root')
        value_interface = TestValueInterface()
        optional_interface_container = TestOptionalInterface()
        optional_interface_value = TestOptionalInterface()

        container = Controller.ContainerController('test', root, None)
        Controller.ValueController('test', container, value_interface, None,
                                   Converter.Str(0, 'default'))
        container.model_to_view({'test': {'test': 'test'}})
        self.assertEqual(value_interface.val, 'test')

        container = Controller.ContainerController(
            'test', root, optional_interface_container)
        Controller.ValueController('test', container, value_interface,
                                   optional_interface_value,
                                   Converter.Str(0, 'default'))
        container.model_to_view({'test': {'test': 'test'}})
        self.assertTrue(optional_interface_container.val)
        self.assertTrue(optional_interface_value.val)

        container.model_to_view({'test': {}})
        self.assertTrue(optional_interface_container.val)
        self.assertFalse(optional_interface_value.val)

        container.model_to_view({})
        self.assertFalse(optional_interface_container.val)

        container = Controller.ContainerController('test', root, None)
        container2 = Controller.ContainerController('test2', container, None)
        Controller.ValueController('test', container2, value_interface, None,
                                   Converter.Str(0, 'default'))
        container.model_to_view({'test': {'test2': {'test': 'test'}}})
        self.assertEqual(value_interface.val, 'test')
    def test_view_to_model_list(self):
        root = Controller.RootController('root')
        list_value_interface = TestValueInterface()
        value_interface = TestValueInterface()
        list_instance_factory = TestListInstanceFactory()

        container = Controller.ContainerController('test', root, None)
        list_controller = Controller.ListController('test_list', container,
                                                    list_value_interface, None,
                                                    list_instance_factory, 0)

        self.assertEqual(container.view_to_model(), {'test_list': []})

        list_value_interface.val = 1
        list_controller.event_handler()
        list_instance_factory.values[0].val = 1

        self.assertEqual(container.view_to_model(), {'test_list': [1]})

        container.model_to_view({'test': {'test_list': [1]}})
        self.assertEqual(list_value_interface.val, '1')
        self.assertEqual(len(list_instance_factory.instances), 1)
        self.assertEqual(list_instance_factory.values[0].val, 1)

        Controller.ValueController('test_val', container, value_interface,
                                   None, Converter.Str(0, 'default'))
        value_interface.val = 'test'
        self.assertEqual(container.view_to_model(), {
            'test_list': [1],
            'test_val': 'test'
        })
 def test_add_controller(self):
     with self.assertRaises(Exception):
         root = Controller.RootController('root')
         controller = Controller.ValueController('test', root,
                                                 TestValueInterface(), None,
                                                 Converter.Str(0, ''))
         controller.add_controller('test', controller)
    def create_value_controller(
            self,
            type_: oer.Type,
            value_interface: ValueInterface,
            optional_interface: Optional[OptionalInterface],
            minimum: Optional[Union[str, int, float]] = 0):
        if isinstance(type_, oer.Integer):
            controller = Controller.ValueController(
                type_.name, self._parent, value_interface, optional_interface,
                Converter.Int(minimum, type_.default))
        elif isinstance(type_, oer.Real):
            controller = Controller.ValueController(
                type_.name, self._parent, value_interface, optional_interface,
                Converter.Float(minimum, type_.default))
        elif isinstance(type_, oer.Enumerated):
            if type_.default is None:
                default = sorted(type_.value_to_data.values())[0]
            else:
                default = type_.default
            controller = Controller.ValueController(type_.name, self._parent,
                                                    value_interface,
                                                    optional_interface,
                                                    Converter.Str(0, default))
        elif type(type_) in [
                oer.UTF8String, oer.VisibleString, oer.GeneralString,
                oer.ObjectIdentifier, oer.IA5String
        ]:
            controller = Controller.ValueController(
                type_.name, self._parent, value_interface, optional_interface,
                Converter.Str(minimum, type_.default))
        elif isinstance(type_, oer.OctetString) or isinstance(
                type_, oer.BitString):
            controller = Controller.ValueController(
                type_.name, self._parent, value_interface, optional_interface,
                Converter.ByteString(minimum, type_.default))
        elif isinstance(type_, oer.Date):
            controller = Controller.ValueController(
                type_.name, self._parent, value_interface, optional_interface,
                Converter.Any(0, datetime.date.today()))
        elif isinstance(type_, oer.TimeOfDay):
            controller = Controller.ValueController(
                type_.name, self._parent, value_interface, optional_interface,
                Converter.Any(0,
                              datetime.datetime.now().time()))
        elif isinstance(type_, oer.DateTime) or isinstance(
                type_, oer.UTCTime) or isinstance(type_, oer.GeneralizedTime):
            controller = Controller.ValueController(
                type_.name, self._parent, value_interface, optional_interface,
                Converter.Any(0, datetime.datetime.now()))
        else:
            raise Exception(f"Unknown type for ControllerFactory: {type_}")

        self.__register_events(controller, value_interface, optional_interface)
    def test_view_to_model(self):
        root = Controller.RootController('root')
        value_interface = TestValueInterface()
        optional_interface = TestOptionalInterface()

        controller = Controller.ValueController('test', root, value_interface,
                                                optional_interface,
                                                Converter.Str(0, 'default'))
        value_interface.val = 'new'
        optional_interface.val = True
        self.assertEqual('new', controller.view_to_model())

        optional_interface.val = False
        self.assertIsNone(controller.view_to_model())

        controller = Controller.ValueController('test', root,
                                                value_interface, None,
                                                Converter.Float(0.0, 1.0))
        value_interface.val = 'new'
        self.assertEqual(controller.view_to_model(), 0.0)
    def test_view_to_model_value(self):
        root = Controller.RootController('root')
        value_interface = TestValueInterface()
        optional_interface_container = TestOptionalInterface()
        optional_interface_value = TestOptionalInterface()

        container = Controller.ContainerController('test', root, None)
        Controller.ValueController('test', container, value_interface, None,
                                   Converter.Str(0, 'default'))
        value_interface.val = 'test'

        self.assertEqual(container.view_to_model(), {'test': 'test'})

        container = Controller.ContainerController(
            'test', root, optional_interface_container)
        Controller.ValueController('test', container, value_interface,
                                   optional_interface_value,
                                   Converter.Str(0, 'default'))

        optional_interface_value.val = True
        optional_interface_container.val = True
        self.assertEqual(container.view_to_model(), {'test': 'default'})

        optional_interface_value.val = False
        optional_interface_container.val = False
        self.assertIsNone(container.view_to_model())

        optional_interface_value.val = False
        optional_interface_container.val = True
        self.assertEqual(container.view_to_model(), {})

        container = Controller.ContainerController('test', root, None)
        container2 = Controller.ContainerController('test2', container, None)
        Controller.ValueController('test', container2, value_interface, None,
                                   Converter.Str(0, 'default'))

        value_interface.val = 'test'
        self.assertEqual(container.view_to_model(),
                         {'test2': {
                             'test': 'test'
                         }})
    def test_init(self):
        root = Controller.RootController('root')
        value_interface = TestValueInterface()

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Str(0, 'default'))
        self.assertEqual(value_interface.val, 'default')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Int(0, 12))
        self.assertEqual(value_interface.val, '12')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Int(0, None))
        self.assertEqual(value_interface.val, '0')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Int(12, None))
        self.assertEqual(value_interface.val, '12')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Int(None, 12))
        self.assertEqual(value_interface.val, '12')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Float(0.0, 12.1))
        self.assertEqual(value_interface.val, '12.1')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Float(0.0, None))
        self.assertEqual(value_interface.val, '0.0')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Float(12.1, None))
        self.assertEqual(value_interface.val, '12.1')

        Controller.ValueController('test', root, value_interface, None,
                                   Converter.Float(None, 12.1))
        self.assertEqual(value_interface.val, '12.1')
Beispiel #9
0
 def create(self, member: str, p: Controller.ListController):
     self.member = member
     self.value = TestValueInterface()
     self.instance = Controller.ValueController(member, p, self.value, None, Converter.Str(0, None))
Beispiel #10
0
 def create(self, i, p: Controller.ListController):
     self.values[i] = TestValueInterface()
     self.instances[i] = Controller.ValueController(str(i), p,
                                                    self.values[i], None,
                                                    Converter.Str(0, None))