예제 #1
0
    def test_matches_with_any_mode_and_custom_value_kwargs_multiple(self):
        """Testing ConditionSet.matches with "any" mode and multiple custom
        value keyword arguments across multiple choices
        """
        class CustomEqualsChoice1(EqualsTestChoice):
            value_kwarg = 'my_value1'

        class CustomEqualsChoice2(EqualsTestChoice):
            value_kwarg = 'my_value2'

        choice1 = CustomEqualsChoice1()
        choice2 = CustomEqualsChoice2()

        condition_set = ConditionSet(ConditionSet.MODE_ANY, [
            Condition(choice1, choice1.get_operator('equals-test-op'),
                      'abc123'),
            Condition(choice2, choice2.get_operator('equals-test-op'),
                      'def456'),
        ])

        self.assertTrue(condition_set.matches(my_value1='abc123',
                                              my_value2='def456'))
        self.assertTrue(condition_set.matches(my_value1='abc123'))
        self.assertTrue(condition_set.matches(my_value2='def456'))
        self.assertTrue(condition_set.matches(my_value1='abc123',
                                              my_value2='xxx'))
        self.assertFalse(condition_set.matches(my_value1='xxx',
                                               my_value2='xxx'))
예제 #2
0
    def test_serialize(self):
        """Testing ConditionSet.serialize"""
        basic_choice = BasicTestChoice()
        equals_choice = EqualsTestChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ALL, [
            Condition(basic_choice,
                      basic_choice.get_operator('basic-test-op'),
                      'abc123'),
            Condition(equals_choice,
                      equals_choice.get_operator('equals-test-op'),
                      'def123'),
        ])

        result = condition_set.serialize()

        self.assertEqual(
            result,
            {
                'mode': 'all',
                'conditions': [
                    {
                        'choice': 'basic-test-choice',
                        'op': 'basic-test-op',
                        'value': 'abc123',
                    },
                    {
                        'choice': 'equals-test-choice',
                        'op': 'equals-test-op',
                        'value': 'def123',
                    },
                ],
            })
예제 #3
0
    def test_matches_with_all_mode_and_custom_value_kwargs_multiple(self):
        """Testing ConditionSet.matches with "all" mode and multiple custom
        value keyword arguments across multiple choices
        """
        class CustomEqualsChoice1(EqualsTestChoice):
            value_kwarg = 'my_value1'

        class CustomEqualsChoice2(EqualsTestChoice):
            value_kwarg = 'my_value2'

        choice1 = CustomEqualsChoice1()
        choice2 = CustomEqualsChoice2()

        condition_set = ConditionSet(ConditionSet.MODE_ALL, [
            Condition(choice1, choice1.get_operator('equals-test-op'),
                      'abc123'),
            Condition(choice2, choice2.get_operator('equals-test-op'),
                      'def456'),
        ])

        self.assertTrue(condition_set.matches(my_value1='abc123',
                                              my_value2='def456'))
        self.assertFalse(condition_set.matches(my_value1='abc123'))
        self.assertFalse(condition_set.matches(my_value2='def456'))
        self.assertFalse(condition_set.matches(my_value1='abc123',
                                               my_value2='xxx'))
예제 #4
0
    def test_matches_with_all_mode_and_match(self):
        """Testing ConditionSet.matches with "all" mode and match"""
        choice = EqualsTestChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ALL, [
            Condition(choice, choice.get_operator('equals-test-op'), 'abc123'),
            Condition(choice, choice.get_operator('equals-test-op'), 'abc123'),
        ])

        self.assertTrue(condition_set.matches(value='abc123'))
예제 #5
0
    def test_matches_with_any_mode_and_no_match(self):
        """Testing ConditionSet.matches with "any" mode and no match"""
        choice = EqualsTestChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ANY, [
            Condition(choice, choice.get_operator('equals-test-op'), 'abc123'),
            Condition(choice, choice.get_operator('equals-test-op'), 'def123'),
        ])

        self.assertFalse(condition_set.matches(value='foo'))
예제 #6
0
    def test_matches_with_custom_value_kwargs(self):
        """Testing ConditionSet.matches with custom value keyword arguments"""
        class CustomEqualsChoice(EqualsTestChoice):
            value_kwarg = 'my_value'

        choice = CustomEqualsChoice()

        condition_set = ConditionSet(ConditionSet.MODE_ALL, [
            Condition(choice, choice.get_operator('equals-test-op'),
                      'abc123'),
        ])

        self.assertTrue(condition_set.matches(my_value='abc123'))
        self.assertFalse(condition_set.matches(value='abc123'))
예제 #7
0
    def test_deserialize_with_choice_kwargs(self):
        """Testing ConditionSet.deserialize with choice_kwargs"""
        choices = ConditionChoices([BasicTestChoice])

        condition_set = ConditionSet.deserialize(
            choices,
            {
                'mode': 'any',
                'conditions': [
                    {
                        'choice': 'basic-test-choice',
                        'op': 'basic-test-op',
                        'value': 'my-value',
                    },
                ],
            },
            choice_kwargs={
                'abc': 123,
            })

        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, 'basic-test-choice')
        self.assertEqual(choice.extra_state, {'abc': 123})
예제 #8
0
    def test_deserialize_with_invalid_mode(self):
        """Testing ConditionSet.deserialize with invalid mode"""
        choices = ConditionChoices([BasicTestChoice])

        with self.assertRaises(InvalidConditionModeError):
            ConditionSet.deserialize(
                choices,
                {
                    'mode': 'invalid',
                    'conditions': [
                        {
                            'choice': 'basic-test-choice',
                            'op': 'basic-test-op',
                            'value': 'my-value',
                        },
                    ],
                })
예제 #9
0
    def test_prepare_value_with_condition_set(self):
        """Testing ConditionsField.prepare_value with ConditionSet"""
        choices = ConditionChoices([BaseConditionStringChoice])
        field = ConditionsField(choices=choices)

        self.assertEqual(field.prepare_value(ConditionSet()), {
            'mode': 'all',
            'conditions': [],
        })
예제 #10
0
파일: fields.py 프로젝트: davidt/djblets
    def to_python(self, value):
        """Parse and return conditions from the field's data.

        This takes the serialized values provided by the field's widget,
        ensures they're valid, and returns a list of the resulting conditions.

        Args:
            value (dict):
                The raw form data, as provided by the widget.

        Returns:
            djblets.conditions.conditions.ConditionSet:
            The resulting condition set from the form.
        """
        if not value:
            # Let validate() handle this. It will be run by clean() after this
            # method returns.
            return None

        try:
            condition_set = ConditionSet.deserialize(
                self.choices,
                value,
                choice_kwargs=self.choice_kwargs)
        except InvalidConditionModeError as e:
            raise forms.ValidationError(six.text_type(e),
                                        code='invalid_mode')
        except (ConditionChoiceNotFoundError,
                ConditionOperatorNotFoundError,
                InvalidConditionValueError) as e:
            if getattr(e, 'code', None) == 'required':
                self.widget.condition_errors[e.condition_index] = \
                    self.error_messages['value_required']
            else:
                self.widget.condition_errors[e.condition_index] = \
                    six.text_type(e)

            raise forms.ValidationError(
                self.error_messages['condition_errors'],
                code='condition_errors')

        return condition_set
예제 #11
0
    def test_deserialize(self):
        """Testing ConditionSet.deserialize"""
        choices = ConditionChoices([BasicTestChoice])

        condition_set = ConditionSet.deserialize(
            choices,
            {
                'mode': 'any',
                'conditions': [
                    {
                        'choice': 'basic-test-choice',
                        'op': 'basic-test-op',
                        'value': 'my-value',
                    },
                ],
            })

        self.assertEqual(condition_set.mode, ConditionSet.MODE_ANY)
        self.assertEqual(len(condition_set.conditions), 1)
        self.assertEqual(condition_set.conditions[0].choice.choice_id,
                         'basic-test-choice')
예제 #12
0
    def to_python(self, value):
        """Parse and return conditions from the field's data.

        This takes the serialized values provided by the field's widget,
        ensures they're valid, and returns a list of the resulting conditions.

        Args:
            value (dict):
                The raw form data, as provided by the widget.

        Returns:
            djblets.conditions.conditions.ConditionSet:
            The resulting condition set from the form.
        """
        if not value:
            # Let validate() handle this. It will be run by clean() after this
            # method returns.
            return None

        try:
            condition_set = ConditionSet.deserialize(
                self.choices, value, choice_kwargs=self.widget.choice_kwargs)
        except InvalidConditionModeError as e:
            raise forms.ValidationError(six.text_type(e), code='invalid_mode')
        except (ConditionChoiceNotFoundError, ConditionOperatorNotFoundError,
                InvalidConditionValueError) as e:
            if getattr(e, 'code', None) == 'required':
                self.widget.condition_errors[e.condition_index] = \
                    self.error_messages['value_required']
            else:
                self.widget.condition_errors[e.condition_index] = \
                    six.text_type(e)

            raise forms.ValidationError(
                self.error_messages['condition_errors'],
                code='condition_errors')

        return condition_set
예제 #13
0
 def test_matches_with_always_mode(self):
     """Testing ConditionSet.matches with "always" mode"""
     condition_set = ConditionSet(ConditionSet.MODE_ALWAYS, [])
     self.assertTrue(condition_set.matches(value='abc123'))