Ejemplo n.º 1
0
    def test_add_error(self):
        ve = _ExtendedValidationError(None)
        self.assertValidationErrorMatch(ve, None)
        ve.add_error(None, 'foo')
        self.assertValidationErrorMatch(ve, [None, 'foo'])
        ve.add_error(None, ['bar', 'baz'])
        self.assertValidationErrorMatch(ve, [None, 'foo', 'bar', 'baz'])
        ve.add_error('qq', 'rr')
        self.assertValidationErrorMatch(ve, {NON_FIELD_ERRORS: [None, 'foo', 'bar', 'baz'], 'qq': ['rr']})
        ve.add_error('qq', ['ss'])
        self.assertValidationErrorMatch(ve, {NON_FIELD_ERRORS: [None, 'foo', 'bar', 'baz'], 'qq': ['rr', 'ss']})

        ve = _ExtendedValidationError({'qq': 'rr'})
        self.assertValidationErrorMatch(ve, {'qq': ['rr']})
        ve.add_error('qq', 'foo')
        self.assertValidationErrorMatch(ve, {'qq': ['rr', 'foo']})
        ve.add_error(None, {'qq': 'ss'})
        self.assertValidationErrorMatch(ve, {'qq': ['rr', 'foo', 'ss']})
        ve.add_error(None, {'qq': 'tt'})
        self.assertValidationErrorMatch(ve, {'qq': ['rr', 'foo', 'ss', 'tt']})
        ve.add_error(None, 'uu')
        self.assertValidationErrorMatch(ve, {'qq': ['rr', 'foo', 'ss', 'tt'], NON_FIELD_ERRORS: ['uu']})

        ve = _ExtendedValidationError(_NO_VALIDATION_ERROR)
        self.assertValidationErrorMatch(ve, _NO_VALIDATION_ERROR)
        ve.add_error(None, ValidationError('abc'))
        self.assertValidationErrorMatch(ve, 'abc')
        ve.add_error(None, ValidationError(['def']))
        self.assertValidationErrorMatch(ve, ['abc', 'def'])
        ve.add_error(None, ValidationError({'ghi': 'jkl'}))
        self.assertValidationErrorMatch(ve, {NON_FIELD_ERRORS: ['abc', 'def'], 'ghi': ['jkl']})

        ve = _ExtendedValidationError(_NO_VALIDATION_ERROR)
        ve.add_error('abc', 'def')
        self.assertValidationErrorMatch(ve, {'abc': ['def']})
Ejemplo n.º 2
0
 def test_merge_deep_copy(self):
     """
     merged validation errors should not share underlying data structures
     """
     ve1 = _ExtendedValidationError({'foo': ['bar']})
     ve2 = _ExtendedValidationError({})
     ve = ve1.merged(ve2)
     self.assertIsNot(ve.error_dict['foo'], ve1.error_dict['foo'])
Ejemplo n.º 3
0
    def test_is_empty(self):
        test_cases = (
            # (is_empty, message)
            (False, None),
            (False, ''),
            (False, ['']),
            (False, [ValidationError(None)]),
            (False, [ValidationError('')]),
            (False, {}),
            (False, {'my_field': []}),
            (False, {'my_field': ['']}),
            (False, {'my_field': [None]}),
            (False, 'foo'),
            (False, ['foo']),
            (True, _NO_VALIDATION_ERROR),
        )

        for i, (is_empty, message) in enumerate(test_cases):
            ve = _ExtendedValidationError(message)
            self.assertEqual(is_empty, ve._is_empty(), 'test case failed: ' + repr(message))

            ve = _ExtendedValidationError(ValidationError(message))
            self.assertEqual(is_empty, ve._is_empty(), 'test case failed: ' + repr(message))

            ve = _ExtendedValidationError(_ExtendedValidationError(message))
            self.assertEqual(is_empty, ve._is_empty(), 'test case failed: ' + repr(message))

            ve = _ExtendedValidationError(ValidationError(ValidationError(message)))
            self.assertEqual(is_empty, ve._is_empty(), 'test case failed: ' + repr(message))

            ve = _ExtendedValidationError(_ExtendedValidationError(_ExtendedValidationError(message)))
            self.assertEqual(is_empty, ve._is_empty(), 'test case failed: ' + repr(message))
Ejemplo n.º 4
0
    def test_add_error_fieldname(self):
        ve = _ExtendedValidationError(_NO_VALIDATION_ERROR)

        with self.assertRaises(TypeError):
            ve.add_error('aaa', {'field': 'value'})

        with self.assertRaises(TypeError):
            ve.add_error('aaa', ValidationError({'field': 'value'}))
Ejemplo n.º 5
0
    def test_merge(self):
        """
        Can merge ValidationErrors
        """

        def cast_to_list(x):
            if isinstance(x, list):
                return x
            return [x]

        # we test each combination of these test inputs
        list_scalar_test_cases = [
            _NO_VALIDATION_ERROR,
            None,
            '',
            'abc',
            [None],
            [''],
            ['abc', None, ''],
        ]

        def calculate_output(in1, in2):
            if in1 is _NO_VALIDATION_ERROR:
                return in2
            elif in2 is _NO_VALIDATION_ERROR:
                return in1
            else:
                return cast_to_list(in1) + cast_to_list(in2)

        # these ones are complicated enough that if we try to automate it we're just
        # reimplementing the code we're testing, so we create manual test cases
        processed_test_cases = [
            # (input1, input2, output)
            (
                None, # None is not the same as _NO_VALIDATION_ERROR
                {'field': 'foo'},
                {'field': ['foo'], NON_FIELD_ERRORS: [None]},
            ),
            (
                None,
                {NON_FIELD_ERRORS: 'foo'},
                {NON_FIELD_ERRORS: [None, 'foo']},
            ),
            (
                {NON_FIELD_ERRORS: 'foo'},
                'abc',
                {NON_FIELD_ERRORS: ['foo', 'abc']},
            ),
            (
                {'field': 'f1', 'field2': ['f2', 'f2']},
                {'field2': 'ab'},
                {'field': ['f1'], 'field2': ['f2', 'f2', 'ab']},
            ),
            (
                _NO_VALIDATION_ERROR,
                'abc',
                'abc',
            ),
            (
                ['abc'],
                _NO_VALIDATION_ERROR,
                ['abc'],
            ),
            (
                {'abc': 'def'},
                _NO_VALIDATION_ERROR,
                {'abc': ['def']},
            ),
            (
                _NO_VALIDATION_ERROR,
                {'abc': ['def']},
                {'abc': ['def']},
            ),
        ]

        for in1 in list_scalar_test_cases:
            for in2 in list_scalar_test_cases:

                # if is a list, then also create a variant where items are ValidationError instances
                in1_candidates = [in1]
                if isinstance(in1, list):
                    in1_candidates.append([ValidationError(x) for x in in1])

                in2_candidates = [in2]
                if isinstance(in2, list):
                    in2_candidates.append([ValidationError(x) for x in in2])

                processed_test_cases.extend(itertools.product(in1_candidates, in2_candidates, [calculate_output(in1, in2)]))
                processed_test_cases.extend(itertools.product(in2_candidates, in1_candidates, [calculate_output(in2, in1)]))

        for i, (in1, in2, out) in enumerate(processed_test_cases):
            ve1 = _ExtendedValidationError(in1)
            ve2 = _ExtendedValidationError(in2)

            ve = ve1.merged(ve2)
            self.assertValidationErrorMatch(ve, out)
            self.assertIsNot(ve, ve1)
            self.assertIsNot(ve, ve2)

            ve = _ExtendedValidationError(ve1)
            ve.merge(ve2)
            self.assertValidationErrorMatch(ve, out)
            self.assertIsNot(ve, ve1)
            self.assertIsNot(ve, ve2)