Ejemplo n.º 1
0
    def test_to_python_with_invalid_value_error(self):
        """Testing ConditionsField.to_python with invalid value error"""
        class MyChoice(BaseConditionIntegerChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode':
                'any',
                'conditions': [{
                    'choice': 'my-choice',
                    'op': 'is',
                    'value': 'invalid-value',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(field.widget.condition_errors, {
            0: 'Enter a whole number.',
        })
Ejemplo n.º 2
0
    def test_to_python_with_value_required_error(self):
        """Testing ConditionsField.to_python with value required error"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode': 'any',
                'conditions': [{
                    'choice': 'my-choice',
                    'op': 'is',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(
            field.widget.condition_errors,
            {
                0: 'A value is required.',
            })
Ejemplo n.º 3
0
    def test_to_python_with_operator_not_found_error(self):
        """Testing ConditionsField.to_python with operator not found error"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode':
                'any',
                'conditions': [{
                    'choice': 'my-choice',
                    'op': 'invalid-op',
                    'value': 'my-value',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(field.widget.condition_errors, {
            0: 'No operator was found matching "invalid-op".',
        })
Ejemplo n.º 4
0
    def test_to_python_with_invalid_value_error(self):
        """Testing ConditionsField.to_python with invalid value error"""
        class MyChoice(BaseConditionIntegerChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode': 'any',
                'conditions': [{
                    'choice': 'my-choice',
                    'op': 'is',
                    'value': 'invalid-value',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(
            field.widget.condition_errors,
            {
                0: 'Enter a whole number.',
            })
Ejemplo n.º 5
0
    def test_to_python_with_operator_not_found_error(self):
        """Testing ConditionsField.to_python with operator not found error"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode': 'any',
                'conditions': [{
                    'choice': 'my-choice',
                    'op': 'invalid-op',
                    'value': 'my-value',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(
            field.widget.condition_errors,
            {
                0: 'No operator was found matching "invalid-op".',
            })
Ejemplo n.º 6
0
    def test_to_python_with_mode_error(self):
        """Testing ConditionsField.to_python with mode error"""
        choices = ConditionChoices()
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode': 'invalid',
                'conditions': [],
            })

        self.assertEqual(cm.exception.messages,
                         ['"invalid" is not a valid condition mode.'])
        self.assertEqual(cm.exception.code, 'invalid_mode')
Ejemplo n.º 7
0
    def test_to_python_with_mode_error(self):
        """Testing ConditionsField.to_python with mode error"""
        choices = ConditionChoices()
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode': 'invalid',
                'conditions': [],
            })

        self.assertEqual(cm.exception.messages,
                         ['"invalid" is not a valid condition mode.'])
        self.assertEqual(cm.exception.code, 'invalid_mode')
Ejemplo n.º 8
0
    def test_to_python(self):
        """Testing ConditionsField.to_python"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        condition_set = field.to_python({
            'mode':
            'any',
            'conditions': [{
                'choice': 'my-choice',
                'op': 'is',
                'value': 'my-value',
            }]
        })

        self.assertEqual(condition_set.mode, ConditionSet.MODE_ANY)
        self.assertEqual(len(condition_set.conditions), 1)

        condition = condition_set.conditions[0]
        self.assertEqual(condition.choice.choice_id, 'my-choice')
        self.assertEqual(condition.operator.operator_id, 'is')
        self.assertEqual(condition.value, 'my-value')
Ejemplo n.º 9
0
    def test_to_python_with_choice_not_found_error(self):
        """Testing ConditionsField.to_python with choice not found error"""
        choices = ConditionChoices()
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode':
                'any',
                'conditions': [{
                    'choice': 'invalid-choice',
                    'op': 'is',
                    'value': 'my-value',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(
            field.widget.condition_errors, {
                0: 'No condition choice was found matching "invalid-choice".',
            })
Ejemplo n.º 10
0
    def test_to_python_with_choice_not_found_error(self):
        """Testing ConditionsField.to_python with choice not found error"""
        choices = ConditionChoices()
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode': 'any',
                'conditions': [{
                    'choice': 'invalid-choice',
                    'op': 'is',
                    'value': 'my-value',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(
            field.widget.condition_errors,
            {
                0: 'No condition choice was found matching "invalid-choice".',
            })
Ejemplo n.º 11
0
    def test_to_python_with_value_required_error(self):
        """Testing ConditionsField.to_python with value required error"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        with self.assertRaises(ValidationError) as cm:
            field.to_python({
                'mode':
                'any',
                'conditions': [{
                    'choice': 'my-choice',
                    'op': 'is',
                }],
            })

        self.assertEqual(cm.exception.messages,
                         ['There was an error with one of your conditions.'])
        self.assertEqual(cm.exception.code, 'condition_errors')
        self.assertEqual(field.widget.condition_errors, {
            0: 'A value is required.',
        })
Ejemplo n.º 12
0
    def test_to_python_with_choice_kwargs(self):
        """Testing ConditionsField.to_python with choice_kwargs set"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices, choice_kwargs={'abc': 123})

        condition_set = field.to_python({
            'mode':
            'any',
            'conditions': [{
                'choice': 'my-choice',
                'op': 'is',
                'value': 'my-value',
            }]
        })

        self.assertEqual(condition_set.mode, ConditionSet.MODE_ANY)
        self.assertEqual(len(condition_set.conditions), 1)

        choice = condition_set.conditions[0].choice
        self.assertEqual(choice.choice_id, 'my-choice')
        self.assertEqual(choice.extra_state, {'abc': 123})
Ejemplo n.º 13
0
    def test_to_python(self):
        """Testing ConditionsField.to_python"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)

        condition_set = field.to_python({
            'mode': 'any',
            'conditions': [{
                'choice': 'my-choice',
                'op': 'is',
                'value': 'my-value',
            }]
        })

        self.assertEqual(condition_set.mode, ConditionSet.MODE_ANY)
        self.assertEqual(len(condition_set.conditions), 1)

        condition = condition_set.conditions[0]
        self.assertEqual(condition.choice.choice_id, 'my-choice')
        self.assertEqual(condition.operator.operator_id, 'is')
        self.assertEqual(condition.value, 'my-value')
Ejemplo n.º 14
0
    def test_to_python_with_choice_kwargs(self):
        """Testing ConditionsField.to_python with choice_kwargs set"""
        class MyChoice(BaseConditionStringChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices,
                                choice_kwargs={'abc': 123})

        condition_set = field.to_python({
            'mode': 'any',
            'conditions': [{
                'choice': 'my-choice',
                'op': 'is',
                'value': 'my-value',
            }]
        })

        self.assertEqual(condition_set.mode, ConditionSet.MODE_ANY)
        self.assertEqual(len(condition_set.conditions), 1)

        choice = condition_set.conditions[0].choice
        self.assertEqual(choice.choice_id, 'my-choice')
        self.assertEqual(choice.extra_state, {'abc': 123})