예제 #1
0
    def test_get_choice_with_kwargs(self):
        """Testing ConditionChoices.get_choice with kwargs for extra state"""
        class MyChoice1(BaseConditionChoice):
            choice_id = 'my-choice-1'

        choices = ConditionChoices([MyChoice1])
        choice = choices.get_choice('my-choice-1', choice_kwargs={'abc': 123})
        self.assertEqual(choice.extra_state, {'abc': 123})
예제 #2
0
    def test_get_choice(self):
        """Testing ConditionChoices.get_choice"""
        class MyChoice1(BaseConditionChoice):
            choice_id = 'my-choice-1'

        choices = ConditionChoices([MyChoice1])
        self.assertEqual(choices.get_choice('my-choice-1').__class__,
                         MyChoice1)
예제 #3
0
    def test_get_choices(self):
        """Testing ConditionChoices.get_choices"""
        class MyChoice1(BaseConditionChoice):
            choice_id = 'my-choice-1'

        class MyChoice2(BaseConditionChoice):
            choice_id = 'my-choice-2'

        choices = ConditionChoices([MyChoice1, MyChoice2])

        choices = list(choices.get_choices())
        self.assertEqual(len(choices), 2)
        self.assertEqual(choices[0].__class__, MyChoice1)
        self.assertEqual(choices[1].__class__, MyChoice2)
예제 #4
0
    def test_get_choices_with_kwargs(self):
        """Testing ConditionChoices.get_choices with kwargs"""
        class MyChoice1(BaseConditionChoice):
            choice_id = 'my-choice-1'

        class MyChoice2(BaseConditionChoice):
            choice_id = 'my-choice-2'

        choices = ConditionChoices([MyChoice1, MyChoice2])

        choices = list(choices.get_choices(choice_kwargs={'abc': 123}))
        self.assertEqual(len(choices), 2)
        self.assertEqual(choices[0].extra_state, {'abc': 123})
        self.assertEqual(choices[1].extra_state, {'abc': 123})
예제 #5
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})
예제 #6
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".',
        })
예제 #7
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.',
        })
예제 #8
0
    def test_value_from_datadict_with_missing_choice_rows(self):
        """Testing ConditionsWidget.value_from_datadict with missing choice
        rows
        """
        class MyChoice(BaseConditionIntegerChoice):
            choice_id = 'my-choice'

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

        data = MultiValueDict('')
        data.update({
            'my_conditions_mode': 'any',
            'my_conditions_last_id': '5',
            'my_conditions_choice[5]': 'my-choice',
            'my_conditions_operator[5]': 'is-not',
            'my_conditions_value[5]': 'my-value',
        })

        self.assertEqual(
            field.widget.value_from_datadict(data, MultiValueDict(''),
                                             'my_conditions'),
            {
                'mode': 'any',
                'conditions': [
                    {
                        'choice': 'my-choice',
                        'op': 'is-not',
                        'value': 'my-value',
                    },
                ],
            })
예제 #9
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')
예제 #10
0
    def test_get_context_with_condition_errors(self):
        """Testing ConditionsWidget.get_context with condition errors"""
        class MyOperator1(BaseConditionOperator):
            operator_id = 'my-op-1'
            name = 'My Op 1'
            value_field = ConditionValueIntegerField()

        class MyChoice1(BaseConditionChoice):
            choice_id = 'my-choice'
            name = 'My Choice'
            operators = ConditionOperators([MyOperator1])

        choices = ConditionChoices([MyChoice1])
        field = ConditionsField(choices=choices)
        field.widget.condition_errors[0] = 'This is an error.'

        result = field.widget.get_context(
            'my_conditions', {
                'mode':
                'any',
                'conditions': [
                    {
                        'choice': 'my-choice',
                        'op': 'my-op-1',
                        'value': 'my-value-1',
                    },
                ],
            }, {
                'id': 'my-conditions',
            })

        rendered_rows = result['rendered_rows']
        self.assertEqual(rendered_rows, [{
            'choice': ('<select id="my-conditions_choice_0"'
                       ' name="my_conditions_choice[0]">\n'
                       '<option value="my-choice" selected="selected">'
                       'My Choice</option>\n'
                       '</select>'),
            'operator': ('<select id="my-conditions_operator_0"'
                         ' name="my_conditions_operator[0]">\n'
                         '<option value="my-op-1" selected="selected">'
                         'My Op 1</option>\n'
                         '</select>'),
            'error':
            'This is an error.',
        }])

        serialized_choices = result['serialized_choices']
        self.assertEqual(len(serialized_choices), 1)
        self.assertEqual(serialized_choices[0]['id'], 'my-choice')

        serialized_rows = result['serialized_rows']
        self.assertEqual(serialized_rows, [{
            'choiceID': 'my-choice',
            'operatorID': 'my-op-1',
            'valid': True,
            'value': 'my-value-1',
            'error': 'This is an error.',
        }])
예제 #11
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': [],
        })
예제 #12
0
    def test_prepare_value_with_serialized_data(self):
        """Testing ConditionsField.prepare_value with serialized data"""
        choices = ConditionChoices([BaseConditionStringChoice])
        field = ConditionsField(choices=choices)

        data = {
            'mode': 'all',
            'conditions': [],
        }

        self.assertEqual(field.prepare_value(data), data)
예제 #13
0
    def test_init_with_missing_operators(self):
        """Testing ConditionsField initialization with choices missing
        operators
        """
        class MyChoice(BaseConditionChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        msg = 'MyChoice must define a non-empty "operators" attribute.'

        with self.assertRaisesMessage(ValueError, msg):
            ConditionsField(choices=choices)
예제 #14
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')
예제 #15
0
    def test_deserialize_with_missing_value(self):
        """Testing Condition.deserialize with missing value in data"""
        choices = ConditionChoices([BasicTestChoice])

        with self.assertRaises(InvalidConditionValueError) as cm:
            Condition.deserialize(
                choices,
                {
                    'choice': 'basic-test-choice',
                    'op': 'basic-test-op',
                },
                condition_index=1)

        e = cm.exception
        self.assertEqual(six.text_type(e), 'A value is required.')
        self.assertEqual(e.condition_index, 1)
예제 #16
0
    def test_deserialize_with_missing_operator(self):
        """Testing Condition.deserialize with missing operator in data"""
        choices = ConditionChoices()

        with self.assertRaises(ConditionOperatorNotFoundError) as cm:
            Condition.deserialize(
                choices,
                {
                    'choice': 'my-choice',
                    'value': 'my-value',
                },
                condition_index=1)

        e = cm.exception
        self.assertEqual(six.text_type(e), 'An operator is required.')
        self.assertEqual(e.condition_index, 1)
예제 #17
0
    def test_deserialize(self):
        """Testing Condition.deserialize"""
        choices = ConditionChoices([BasicTestChoice])

        condition = Condition.deserialize(
            choices,
            {
                'choice': 'basic-test-choice',
                'op': 'basic-test-op',
                'value': 'my-value',
            })

        self.assertEqual(condition.choice.__class__, BasicTestChoice)
        self.assertEqual(condition.operator.__class__, BasicTestOperator)
        self.assertEqual(condition.value, 'my-value')
        self.assertEqual(condition.raw_value, 'my-value')
예제 #18
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',
                        },
                    ],
                })
예제 #19
0
    def test_value_from_datadict_with_missing_data(self):
        """Testing ConditionsWidget.value_from_datadict with missing data"""
        class MyChoice(BaseConditionIntegerChoice):
            choice_id = 'my-choice'

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

        data = MultiValueDict('')

        self.assertEqual(
            field.widget.value_from_datadict(data, MultiValueDict(''),
                                             'my_conditions'),
            {
                'mode': None,
                'conditions': [],
            })
예제 #20
0
    def test_render(self):
        """Testing ConditionsWidget.render"""
        class MyOperator(BaseConditionOperator):
            operator_id = 'my-op'
            name = 'My Op'
            value_field = ConditionValueIntegerField()

        class MyChoice(BaseConditionChoice):
            choice_id = 'my-choice'
            name = 'My Choice'
            operators = ConditionOperators([MyOperator])

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

        rendered = field.widget.render(
            'my_conditions',
            {
                'mode': 'any',
                'conditions': [
                    {
                        'choice': 'my-choice',
                        'op': 'my-op',
                        'value': 'my-value-1',
                    },
                ],
            },
            {
                'id': 'my-conditions',
            })

        self.assertIn('<div class="conditions-field" id="my-conditions">',
                      rendered)
        self.assertIn('<input type="hidden" name="my_conditions_last_id"'
                      ' value="1">',
                      rendered)
        self.assertRegexpMatches(
            rendered,
            '<option value="my-choice" selected(="selected")?>My Choice'
            '</option>')
        self.assertRegexpMatches(
            rendered,
            '<option value="my-op" selected(="selected")?>My Op</option>')
        self.assertIn('<span class="conditions-field-value"></span>',
                      rendered)
예제 #21
0
    def test_deserialize_with_choice_kwargs(self):
        """Testing Condition.deserialize with choice_kwargs"""
        choices = ConditionChoices([BasicTestChoice])

        condition = Condition.deserialize(
            choices,
            {
                'choice': 'basic-test-choice',
                'op': 'basic-test-op',
                'value': 'my-value',
            },
            choice_kwargs={
                'abc': 123,
            })

        self.assertEqual(condition.choice.__class__, BasicTestChoice)
        self.assertEqual(condition.choice.extra_state, {'abc': 123})
        self.assertEqual(condition.operator.__class__, BasicTestOperator)
        self.assertEqual(condition.value, 'my-value')
        self.assertEqual(condition.raw_value, 'my-value')
예제 #22
0
    def test_deserialize_with_invalid_choice(self):
        """Testing Condition.deserialize with invalid choice in data"""
        choices = ConditionChoices()

        with self.assertRaises(ConditionChoiceNotFoundError) as cm:
            Condition.deserialize(
                choices,
                {
                    'choice': 'invalid-choice',
                    'op': 'my-op',
                    'value': 'my-value',
                },
                condition_index=1)

        e = cm.exception
        self.assertEqual(six.text_type(e),
                         'No condition choice was found matching '
                         '"invalid-choice".')
        self.assertEqual(e.choice_id, 'invalid-choice')
        self.assertEqual(e.condition_index, 1)
예제 #23
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')
예제 #24
0
    def test_deserialize_with_op_value_field(self):
        """Testing Condition.deserialize with operator's value_field"""
        class MyChoice(BaseConditionChoice):
            choice_id = 'my-choice'
            operators = ConditionOperators([BooleanTestOperator])
            default_value_field = ConditionValueFormField(forms.CharField())

        choices = ConditionChoices([MyChoice])

        condition = Condition.deserialize(
            choices,
            {
                'choice': 'my-choice',
                'op': 'boolean-test-op',
                'value': True,
            })

        self.assertEqual(condition.choice.__class__, MyChoice)
        self.assertEqual(condition.operator.__class__, BooleanTestOperator)
        self.assertEqual(condition.value, True)
        self.assertEqual(condition.raw_value, True)
예제 #25
0
    def test_deepcopy(self):
        """Testing ConditionsWidget.__deepcopy__"""
        class MyChoice(BaseConditionIntegerChoice):
            choice_id = 'my-choice'

        choices = ConditionChoices([MyChoice])
        field = ConditionsField(choices=choices)
        widget1 = field.widget
        widget2 = copy.deepcopy(widget1)

        widget1.mode_widget.attrs['foo'] = True
        widget1.choice_widget.attrs['foo'] = True
        widget1.operator_widget.attrs['foo'] = True
        widget1.condition_errors[0] = 'This is a test.'

        self.assertEqual(widget2.mode_widget.attrs, {})
        self.assertEqual(widget2.choice_widget.attrs, {})
        self.assertEqual(widget2.operator_widget.attrs, {})
        self.assertEqual(widget2.condition_errors, {})

        # Choices won't be modified between copies, and is a shared object,
        # so both should have the same instance.
        self.assertIs(widget1.choices, widget2.choices)
예제 #26
0
    def test_deserialize_with_invalid_value(self):
        """Testing Condition.deserialize with invalid value in data"""
        class MyChoice(BaseConditionChoice):
            choice_id = 'my-choice'
            operators = ConditionOperators([BasicTestOperator])
            default_value_field = ConditionValueFormField(forms.IntegerField())

        choices = ConditionChoices([MyChoice])

        with self.assertRaises(InvalidConditionValueError) as cm:
            Condition.deserialize(
                choices,
                {
                    'choice': 'my-choice',
                    'op': 'basic-test-op',
                    'value': 'invalid-value'
                },
                condition_index=1)

        e = cm.exception
        self.assertEqual(six.text_type(e), 'Enter a whole number.')
        self.assertEqual(e.code, 'invalid')
        self.assertEqual(e.condition_index, 1)
예제 #27
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".',
            })
예제 #28
0
    def test_deserialize_with_invalid_operator(self):
        """Testing Condition.deserialize with invalid operator in data"""
        class MyChoice(BaseConditionChoice):
            choice_id = 'my-choice'
            operators = ConditionOperators()

        choices = ConditionChoices([MyChoice])

        with self.assertRaises(ConditionOperatorNotFoundError) as cm:
            Condition.deserialize(
                choices,
                {
                    'choice': 'my-choice',
                    'op': 'invalid-op',
                    'value': 'my-value',
                },
                condition_index=1)

        e = cm.exception
        self.assertEqual(six.text_type(e),
                         'No operator was found matching '
                         '"invalid-op".')
        self.assertEqual(e.operator_id, 'invalid-op')
        self.assertEqual(e.condition_index, 1)
예제 #29
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.',
        })
예제 #30
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})
예제 #31
0
    def test_get_choice_with_invalid_id(self):
        """Testing ConditionChoices.get_choice with invalid ID"""
        choices = ConditionChoices()

        with self.assertRaises(ConditionChoiceNotFoundError):
            choices.get_choice('invalid')