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 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_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_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 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_init(self): root = Controller.RootController('root') value_interface = TestValueInterface() Controller.ChoiceController('test', root, value_interface, None, TestChoiceInstanceFactory(), 'choice') self.assertEqual(value_interface.val, 'choice')