def test_buildFormMinimumNeeds(self):
        """Test that we can build a form by passing it a function and params.
        """
        myFunctionId = 'Flood Evacuation Function Vector Hazard'
        myFunctionList = get_plugins(myFunctionId)
        assert len(myFunctionList) == 1
        assert myFunctionList[0].keys()[0] == myFunctionId

        myDialog = FunctionOptionsDialog(None)
        myParameters = {
            'thresholds': [1.0],
            'postprocessors': {
                'Gender': {'on': True},
                'Age': {
                    'on': True,
                    'params': {
                        'youth_ratio': 0.263,
                        'elder_ratio': 0.078,
                        'adult_ratio': 0.659}}}}

        myDialog.buildForm(myParameters)

        assert myDialog.tabWidget.count() == 2

        myChildren = myDialog.tabWidget.findChildren(QLineEdit)
        assert len(myChildren) == 4
    def test_build_form(self):
        """Test that we can build a form by passing it a function and params.
        """
        # noinspection PyUnresolvedReferences
        # pylint: disable=W0612
        from safe.engine.impact_functions_for_testing import \
            itb_fatality_model_configurable
        # pylint: enable=W0612
        function_id = 'I T B Fatality Function Configurable'
        function_list = get_plugins(function_id)
        assert len(function_list) == 1
        assert function_list[0].keys()[0] == function_id

        dialog = FunctionOptionsDialog(None)
        parameter = {
            'thresholds': [1.0],
            'postprocessors': {
                'Gender': {'on': True},
                'Age': {
                    'on': True,
                    'params': {
                        'youth_ratio': 0.263,
                        'elderly_ratio': 0.078,
                        'adult_ratio': 0.659}}}}

        dialog.build_form(parameter)

        assert dialog.tabWidget.count() == 2

        children = dialog.tabWidget.findChildren(QLineEdit)
        assert len(children) == 4
    def test_parseInput(self):
        myInput = {
            'thresholds': lambda: [1.0],
            'postprocessors': {
                'Gender': {'on': lambda: True},
                'Age': {
                    'on': lambda: True,
                    'params': {
                        'youth_ratio': lambda: 0.263,
                        'elder_ratio': lambda: 0.078,
                        'adult_ratio': lambda: 0.659}}}}

        myDialog = FunctionOptionsDialog(None)
        myResult = myDialog.parseInput(myInput)
        print myResult
        assert myResult == OrderedDict([
            ('thresholds', [1.0]),
            ('postprocessors', OrderedDict([
                ('Gender', OrderedDict([('on', True)])),
                ('Age', OrderedDict([
                    ('on', True),
                    ('params', OrderedDict([
                        ('youth_ratio', 0.263),
                        ('elder_ratio', 0.078),
                        ('adult_ratio', 0.659)]))]))]))])
    def test_build_form_minimum_needs(self):
        """Test that we can build a form by passing it a function and params.
        """
        function_id = 'Flood Evacuation Function Vector Hazard'
        function_list = get_plugins(function_id)
        assert len(function_list) == 1
        assert function_list[0].keys()[0] == function_id

        dialog = FunctionOptionsDialog(None)
        parameters = {
            'thresholds': [1.0],
            'postprocessors': {
                'Gender': {'on': True},
                'Age': {
                    'on': True,
                    'params': {
                        'youth_ratio': 0.263,
                        'elderly_ratio': 0.078,
                        'adult_ratio': 0.659}}}}

        dialog.build_form(parameters)

        assert dialog.tabWidget.count() == 2

        children = dialog.tabWidget.findChildren(QLineEdit)
        assert len(children) == 4
    def test_parse_input(self):
        function_input = {
            'thresholds': lambda: [1.0],
            'postprocessors': {
                'Gender': {'on': lambda: True},
                'Age': {
                    'on': lambda: True,
                    'params': {
                        'youth_ratio': lambda: 0.263,
                        'elderly_ratio': lambda: 0.078,
                        'adult_ratio': lambda: 0.659}}}}

        dialog = FunctionOptionsDialog(None)
        result = dialog.parse_input(function_input)
        print result
        expected = OrderedDict([
            ('thresholds', [1.0]),
            ('postprocessors', OrderedDict([
                ('Gender', OrderedDict([('on', True)])),
                ('Age', OrderedDict([
                    ('on', True),
                    ('params', OrderedDict([
                        ('elderly_ratio', 0.078),
                        ('youth_ratio', 0.263),
                        ('adult_ratio', 0.659)]))]))]))])
        # noinspection PyPep8Naming
        self.maxDiff = None
        self.assertDictEqual(result, expected)
    def test_buildFormMinimumNeeds(self):
        """Test that we can build a form by passing it a function and params.
        """
        myFunctionId = 'Flood Evacuation Function Vector Hazard'
        myFunctionList = get_plugins(myFunctionId)
        assert len(myFunctionList) == 1
        assert myFunctionList[0].keys()[0] == myFunctionId

        myDialog = FunctionOptionsDialog(None)
        myParameters = {
            'thresholds': [1.0],
            'postprocessors': {
                'Gender': {
                    'on': True
                },
                'Age': {
                    'on': True,
                    'params': {
                        'youth_ratio': 0.263,
                        'elder_ratio': 0.078,
                        'adult_ratio': 0.659
                    }
                }
            }
        }

        myDialog.buildForm(myParameters)

        assert myDialog.tabWidget.count() == 2

        myChildren = myDialog.tabWidget.findChildren(QLineEdit)
        assert len(myChildren) == 4
    def test_parseInput(self):
        myInput = {
            'thresholds': lambda: [1.0],
            'postprocessors': {
                'Gender': {
                    'on': lambda: True
                },
                'Age': {
                    'on': lambda: True,
                    'params': {
                        'youth_ratio': lambda: 0.263,
                        'elder_ratio': lambda: 0.078,
                        'adult_ratio': lambda: 0.659
                    }
                }
            }
        }

        myDialog = FunctionOptionsDialog(None)
        myResult = myDialog.parseInput(myInput)
        print myResult
        assert myResult == OrderedDict([
            ('thresholds', [1.0]),
            ('postprocessors',
             OrderedDict([('Gender', OrderedDict([('on', True)])),
                          ('Age',
                           OrderedDict([('on', True),
                                        ('params',
                                         OrderedDict([('youth_ratio', 0.263),
                                                      ('elder_ratio', 0.078),
                                                      ('adult_ratio', 0.659)]))
                                        ]))]))
        ])
    def test_buildForm(self):
        """Test that we can build a form by passing it a function and params.
        """
        # noinspection PyUnresolvedReferences
        # pylint: disable=W0612
        from safe.engine.impact_functions_for_testing import \
            itb_fatality_model_configurable
        # pylint: enable=W0612
        myFunctionId = 'I T B Fatality Function Configurable'
        myFunctionList = get_plugins(myFunctionId)
        assert len(myFunctionList) == 1
        assert myFunctionList[0].keys()[0] == myFunctionId

        myDialog = FunctionOptionsDialog(None)
        myParameters = {
            'thresholds': [1.0],
            'postprocessors': {
                'Gender': {'on': True},
                'Age': {
                    'on': True,
                    'params': {
                        'youth_ratio': 0.263,
                        'elder_ratio': 0.078,
                        'adult_ratio': 0.659}}}}

        myDialog.build_form(myParameters)

        assert myDialog.tabWidget.count() == 2

        myChildren = myDialog.tabWidget.findChildren(QLineEdit)
        assert len(myChildren) == 4
    def test_build_form(self):
        """Test that we can build a form by passing it a function and params.
        """
        # noinspection PyUnresolvedReferences
        # pylint: disable=W0612
        from safe.engine.impact_functions_for_testing import \
            itb_fatality_model_configurable
        # pylint: enable=W0612
        function_id = 'I T B Fatality Function Configurable'
        function_list = get_plugins(function_id)
        assert len(function_list) == 1
        assert function_list[0].keys()[0] == function_id

        dialog = FunctionOptionsDialog(None)

        # Define rice for minimum needs
        rice = ResourceParameter()
        rice.value = 2.8
        rice.frequency = 'weekly'
        rice.minimum_allowed_value = 1.4
        rice.maximum_allowed_value = 5.6
        rice.name = 'Rice'
        rice.unit.abbreviation = 'kg'
        rice.unit.name = 'kilogram'
        rice.unit.plural = 'kilograms'

        parameter = {
            'thresholds': [1.0],
            'postprocessors': {
                'Gender': {'on': True},
                'Age': {
                    'on': True,
                    'params': {
                        'youth_ratio': 0.263,
                        'elderly_ratio': 0.078,
                        'adult_ratio': 0.659
                    }
                }
            },
            'minimum needs': [rice]
        }

        dialog.build_form(parameter)

        message = 'There should be %s tabwidget but got %s' % (
            3, dialog.tabWidget.count())
        self.assertEqual(dialog.tabWidget.count(), 3, message)

        children = dialog.tabWidget.findChildren(QLineEdit)
        message = 'There should be %s QLineEdit but got %s' % (
            5, len(children))
        self.assertEqual(len(children), 5, message)
    def test_buildWidget(self):
        myDialog = FunctionOptionsDialog(None)
        myValue = myDialog.buildWidget(myDialog.configLayout, 'foo', [2.3])
        myWidget = myDialog.findChild(QLineEdit)

        # initial value must be same with default
        assert myValue() == [2.3]

        # change to 5.9
        myWidget.setText('5.9')
        assert myValue() == [5.9]

        myWidget.setText('5.9, 70')
        assert myValue() == [5.9, 70]

        myWidget.setText('bar')
        try:
            myValue()
        except ValueError:
            ## expected to raises this exception
            pass
        else:
            raise Exception("Fail: must be raise an exception")
    def test_buildWidget(self):
        myDialog = FunctionOptionsDialog(None)
        myValue = myDialog.buildWidget(myDialog.configLayout, 'foo', [2.3])
        myWidget = myDialog.findChild(QLineEdit)

        # initial value must be same with default
        assert myValue() == [2.3]

        # change to 5.9
        myWidget.setText('5.9')
        assert myValue() == [5.9]

        myWidget.setText('5.9, 70')
        assert myValue() == [5.9, 70]

        myWidget.setText('bar')
        try:
            myValue()
        except ValueError:
            ## expected to raises this exception
            pass
        else:
            raise Exception("Fail: must be raise an exception")
    def test_build_widget(self):
        dialog = FunctionOptionsDialog(None)
        value = dialog.build_widget(dialog.configLayout, 'foo', [2.3])
        widget = dialog.findChild(QLineEdit)

        # initial value must be same with default
        expected_value = [2.3]
        real_value = value()
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        # change to 5.9
        widget.setText('5.9')
        expected_value = [5.9]
        real_value = value()
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget.setText('5.9, 70')
        expected_value = [5.9, 70]
        real_value = value()
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget.setText('bar')
        try:
            value()
        except ValueError:
            # expected to raises this exception
            pass
        else:
            raise Exception("Fail: must be raise an exception")

        dialog = FunctionOptionsDialog(None)
        value = dialog.build_widget(dialog.configLayout, 'foo', True)
        widget = dialog.findChild(QCheckBox)

        # initial value must be same with default
        expected_value = True
        real_value = value()
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget.setChecked(False)
        expected_value = False
        real_value = value()
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        original_value = {'a': 1, 'b': 2}
        dialog = FunctionOptionsDialog(None)
        value = dialog.build_widget(dialog.configLayout, 'foo', original_value)
        widget = dialog.findChild(QLineEdit)

        # initial value must be same with default
        expected_value = original_value
        real_value = value()
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = {'a': 2, 'b': 1}
        widget.setText(str(expected_value))
        real_value = value()
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)