def test_model_to_view(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() list_instance_factory = TestListInstanceFactory() controller = Controller.ListController('test', root, value_interface, None, list_instance_factory, 0) with self.assertRaises(AssertionError): controller.model_to_view({}) controller = Controller.ListController('test', root, value_interface, optional_interface, list_instance_factory, 0) controller.model_to_view({}) self.assertFalse(optional_interface.val) controller.model_to_view({'test': []}) self.assertEqual(value_interface.val, '0') self.assertEqual(len(list_instance_factory.instances), 0) self.assertTrue(optional_interface.val) controller.model_to_view({'test': ['1', '2', '3']}) self.assertEqual(value_interface.val, '3') self.assertEqual(len(list_instance_factory.instances), 3) self.assertTrue(optional_interface.val) self.assertEqual(list_instance_factory.values[0].val, '1') self.assertEqual(list_instance_factory.values[1].val, '2') self.assertEqual(list_instance_factory.values[2].val, '3')
def test_model_to_view(self): root = Controller.RootController('root') value_interface = TestBitstringInterface() optional_interface = TestOptionalInterface() controller = Controller.BitstringController('test', root, value_interface, None, 5) with self.assertRaises(AssertionError): controller.model_to_view({}) controller = Controller.BitstringController('test', root, value_interface, optional_interface, 6) controller.model_to_view({}) self.assertFalse(optional_interface.val) with self.assertRaises(AssertionError): controller.model_to_view({'test': (b'\x00', 7)}) controller.model_to_view({'test': (b'\x33', 6)}) self.assertListEqual(value_interface.values, [0, 1, 4, 5]) controller = Controller.BitstringController('test', root, value_interface, optional_interface, 22) controller.model_to_view({'test': (b'\x82\x81\x81', 22)}) self.assertListEqual(value_interface.values, [1, 7, 8, 15, 16])
def test_add_controller(self): root = Controller.RootController('root') controller = Controller.ContainerController('test', root, None) self.assertEqual(len(controller._controllers), 0) controller.add_controller('sub', controller) self.assertEqual(len(controller._controllers), 1)
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): root = Controller.RootController('root') value_interface = TestValueInterface() controller = Controller.ChoiceController('test', root, value_interface, None, TestChoiceInstanceFactory(), 'choice') controller.add_controller('sub', controller) self.assertEqual(controller._controller, controller)
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_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 test_nullController(self): root = Controller.RootController('root') controller = Controller.NullController('Test', root, None) with self.assertRaises(Exception): controller.add_controller('Test', root)
def test_add_controller(self): with self.assertRaises(Exception): root = Controller.RootController('root') controller = Controller.BoolController('test', root, TestValueInterface(), None, True) controller.add_controller('test', controller)
def test_init(self): root = Controller.RootController('root') value_interface = TestValueInterface() Controller.BoolController('test', root, value_interface, None, True) self.assertTrue(value_interface.val) Controller.BoolController('test', root, value_interface, None, False) self.assertFalse(value_interface.val)
def test_add_controller(self): root = Controller.RootController('root') value_interface = TestValueInterface() controller = Controller.ListController('test', root, value_interface, None, TestListInstanceFactory(), 0) self.assertEqual(len(controller._controllers), 0) controller.add_controller('sub', controller) self.assertEqual(len(controller._controllers), 1)
def test_event_handlers(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() choice_instance_factory = TestChoiceInstanceFactory() controller = Controller.ChoiceController('test', root, value_interface, optional_interface, choice_instance_factory, 'choice') value_interface.val = 'choice2' controller.event_handler() self.assertEqual(choice_instance_factory.member, 'choice2')
def test_view_to_model(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() controller = Controller.BoolController('test', root, value_interface, optional_interface, False) value_interface.val = 'True' optional_interface.val = True self.assertTrue(controller.view_to_model()) optional_interface.val = False self.assertIsNone(controller.view_to_model())
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_init(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() Controller.ListController('test', root, value_interface, None, TestListInstanceFactory(), 1) self.assertEqual(value_interface.val, '1') value_interface.val = 42 Controller.ListController('test', root, value_interface, optional_interface, TestListInstanceFactory(), 0) self.assertEqual(value_interface.val, 42)
def create_choice_controller( self, type_: oer.Type, value_interface: ValueInterface, optional_interface: Optional[OptionalInterface], choice_instance_factory): def has_no_recursive_member(m): if isinstance(m, oer.Recursive): return False elif type(m) in [oer.Set, oer.Sequence]: return not any( isinstance(m, oer.Recursive) for m in m.root_members) elif type(m) in [oer.SequenceOf, oer.SetOf]: return not isinstance(m.element_type, oer.Recursive) return True if isinstance(type_, oer.Choice): if type_.default is None: # Do not use recursive types as default candidates = list( filter(has_no_recursive_member, type_.members)) default = sorted([member.name for member in candidates])[0] else: default = type_.default controller = Controller.ChoiceController(type_.name, self._parent, value_interface, optional_interface, choice_instance_factory, default) self.__register_events(controller, value_interface, optional_interface) else: raise Exception(f"Unknown type for ControllerFactory: {type_}")
def test_view_to_model(self): root = Controller.RootController('root') value_interface = TestBitstringInterface() optional_interface = TestOptionalInterface() controller = Controller.BitstringController('test', root, value_interface, optional_interface, 22) optional_interface.val = False self.assertIsNone(controller.view_to_model()) optional_interface.val = True self.assertEqual(controller.view_to_model(), (b'\x00\x00\x00', 22)) value_interface.values = [1, 7, 8, 15, 16] self.assertEqual(controller.view_to_model(), (b'\x82\x81\x01', 22))
def test_controller(self): root = Controller.RootController('root') controller = Controller.Controller('Test', root, None) with self.assertRaises(NotImplementedError): controller.add_controller('Error', root) with self.assertRaises(NotImplementedError): controller.model_to_view({}) with self.assertRaises(NotImplementedError): controller.view_to_model() controller.optional_handler() self.assertEqual(str(controller), 'Test')
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() choice_instance_factory = TestChoiceInstanceFactory() controller = Controller.ChoiceController('test', root, value_interface, optional_interface, choice_instance_factory, 'choice') optional_interface.val = False self.assertIsNone(controller.view_to_model()) optional_interface.val = True choice_instance_factory.value.val = '1' self.assertEqual(controller.view_to_model(), ('choice', '1')) optional_interface.val = True value_interface.val = 'choice2' choice_instance_factory.value.val = '2' self.assertEqual(controller.view_to_model(), ('choice2', '2'))
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_model_to_view(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() choice_instance_factory = TestChoiceInstanceFactory() controller = Controller.ChoiceController('test', root, value_interface, None, choice_instance_factory, 'choice') with self.assertRaises(AssertionError): controller.model_to_view({}) controller = Controller.ChoiceController('test', root, value_interface, optional_interface, choice_instance_factory, 'choice') controller.model_to_view({}) self.assertFalse(optional_interface.val) controller.model_to_view({'test': ('choice2', '2')}) self.assertEqual(value_interface.val, 'choice2') self.assertEqual(choice_instance_factory.member, 'choice2') self.assertTrue(optional_interface.val) self.assertEqual(choice_instance_factory.value.val, '2')
def create_bitstring_controller( self, type_: oer.Type, bitstring_interface: BitstringInterface, optional_interface: Optional[OptionalInterface]): if isinstance(type_, oer.BitString): controller = Controller.BitstringController( type_.name, self._parent, bitstring_interface, optional_interface, type_.number_of_bits) if optional_interface is not None: optional_interface.register_optional_event( controller.optional_handler) else: raise Exception(f"Unknown type for ControllerFactory: {type_}")
def test_view_to_model(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() list_instance_factory = TestListInstanceFactory() controller = Controller.ListController('test', root, value_interface, optional_interface, list_instance_factory, 0) optional_interface.val = False self.assertIsNone(controller.view_to_model()) optional_interface.val = True self.assertEqual(controller.view_to_model(), []) value_interface.val = 2 controller.event_handler() list_instance_factory.values[0].val = 1 list_instance_factory.values[1].val = 2 self.assertEqual(controller.view_to_model(), [1, 2])
def create_bool_controller( self, type_: oer.Type, value_interface: ValueInterface, optional_interface: Optional[OptionalInterface]): if isinstance(type_, oer.Boolean): controller = Controller.BoolController(type_.name, self._parent, value_interface, optional_interface, type_.default) self.__register_events(controller, value_interface, optional_interface) else: raise Exception(f"Unknown type for ControllerFactory: {type_}")
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')
def create_container_controller( self, type_: oer.Type, optional_interface: Optional[OptionalInterface] ) -> Controller.ContainerController: if isinstance(type_, oer.Sequence) or isinstance(type_, oer.Set): controller = Controller.ContainerController( type_.name, self._parent, optional_interface) if optional_interface is not None: optional_interface.register_optional_event( controller.optional_handler) return controller else: raise Exception(f"Unknown type for ControllerFactory: {type_}")
def create_list_controller(self, type_: oer.Type, value_interface: ValueInterface, optional_interface: Optional[OptionalInterface], list_instance_factory, minimum_elements: int): if isinstance(type_, oer.SequenceOf) or isinstance(type_, oer.SetOf): controller = Controller.ListController(type_.name, self._parent, value_interface, optional_interface, list_instance_factory, minimum_elements) self.__register_events(controller, value_interface, optional_interface) else: raise Exception(f"Unknown type for ControllerFactory: {type_}")
def test_model_to_view(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() controller = Controller.BoolController('test', root, value_interface, None, True) controller.model_to_view({'test': 'false'}) self.assertEqual(value_interface.val, 'false') controller.model_to_view('True') self.assertEqual(value_interface.val, 'True') with self.assertRaises(AssertionError): controller.model_to_view({}) controller = Controller.BoolController('test', root, value_interface, optional_interface, True) controller.model_to_view({'test': 'false'}) self.assertEqual(value_interface.val, 'false') self.assertTrue(optional_interface.val) controller.model_to_view({}) self.assertFalse(optional_interface.val)
def test_event_handlers(self): root = Controller.RootController('root') value_interface = TestValueInterface() optional_interface = TestOptionalInterface() list_instance_factory = TestListInstanceFactory() controller = Controller.ListController('test', root, value_interface, optional_interface, list_instance_factory, 0) self.assertEqual(len(list_instance_factory.instances), 0) value_interface.val = 2 controller.event_handler() self.assertEqual(len(list_instance_factory.instances), 2) optional_interface.val = False controller.optional_handler() self.assertEqual(len(list_instance_factory.instances), 0) self.assertEqual(value_interface.val, 2) optional_interface.val = True controller.optional_handler() self.assertEqual(len(list_instance_factory.instances), 2) self.assertEqual(value_interface.val, 2)