Exemplo n.º 1
0
    def test_exit_context(self):
        """The __exit__() method should re-raise exceptions that are
        not accepted and it should return True when there are no errors
        or if all differences have been accepted (see PEP 343 for
        context manager protocol).
        """
        try:
            raise ValidationError([Missing('A'), Extra('B')], 'error description')
        except ValidationError:
            type, value, traceback = sys.exc_info()  # Get exception info.

        with self.assertRaises(ValidationError) as cm:
            acceptance = MinimalAcceptance('acceptance message')
            acceptance.__exit__(type, value, traceback)

        description = cm.exception.description
        self.assertEqual(description, 'acceptance message: error description')

        # Test with no error description.
        try:
            raise ValidationError([Missing('A'), Extra('B')])  # <- No description.
        except ValidationError:
            type, value, traceback = sys.exc_info()  # Get exception info.

        with self.assertRaises(ValidationError) as cm:
            acceptance = MinimalAcceptance('acceptance message')
            acceptance.__exit__(type, value, traceback)

        description = cm.exception.description
        self.assertEqual(description, 'acceptance message')
Exemplo n.º 2
0
    def test_precedence_relations(self):
        """Should implement specified precedence order for element (e),
        group (g), and whole (w) scoped acceptances:

            e < ge < g < we < wge < wg < w
        """
        element = AcceptedDifferences([Missing(1)], scope='element')
        group = AcceptedDifferences([Missing(1)], scope='group')
        whole = AcceptedDifferences([Missing(1)], scope='whole')

        self.assertPrecedenceLess(
            element,
            (group | element),
        )
        self.assertPrecedenceLess(
            (group | element),
            group,
        )
        self.assertPrecedenceLess(
            group,
            (whole | element),
        )
        self.assertPrecedenceLess(
            (whole | element),
            (whole | group | element),
        )
        self.assertPrecedenceLess(
            (whole | group | element),
            (whole | group),
        )
        self.assertPrecedenceLess(
            (whole | group),
            whole,
        )
Exemplo n.º 3
0
    def test_accepted_extra(self):
        differences =  [Extra('X'), Extra('Y'), Missing('X')]

        with self.assertRaises(ValidationError) as cm:
            with AcceptedDifferences(Extra):  # <- Apply acceptance!
                raise ValidationError(differences)
        remaining_diffs = cm.exception.differences
        self.assertEqual(list(remaining_diffs), [Missing('X')])
Exemplo n.º 4
0
 def test_mapping_vs_difference(self):
     differences = {
         'a': [Missing('X')],
         'b': [Missing('Y'), Missing('X')],
     }
     acceptance = AcceptedDifferences(Missing('X'))
     expected = {'b': Missing('Y')}
     self.assertAcceptance(differences, acceptance, expected)
Exemplo n.º 5
0
    def test_missing(self):
        diff = Missing('foo')

        with self.assertRaises(AttributeError):
            diff.attr = ('bar',)

        with self.assertRaises(AttributeError):
            diff.new_attribute = 'baz'
Exemplo n.º 6
0
    def test_instantiation(self):
        DataError('column names', Missing('foo'))
        DataError('column names', [Missing('foo')])
        DataError('column names', {'foo': Missing('bar')})
        DataError('column names', {('foo', 'bar'): Missing('baz')})

        with self.assertRaises(ValueError,
                               msg='Empty error should raise exception.'):
            DataError(msg='', differences={})
Exemplo n.º 7
0
    def test_difference_vs_type(self):
        differences =  Missing('X')
        acceptance = AcceptedDifferences(Extra)
        expected = [Missing('X')]
        self.assertAcceptance(differences, acceptance, expected)

        differences =  Missing('X')
        acceptance = AcceptedDifferences(Missing)
        with acceptance:  # <- No error, all diffs accepted
            raise ValidationError(differences)
Exemplo n.º 8
0
    def test_accept_string(self):
        with self.assertRaises(ValidationError) as cm:

            with AcceptedKeys('aaa'):  # <- Accept by string!
                raise ValidationError({
                    'aaa': Missing(1),
                    'bbb': Missing(2),
                })

        remaining_diffs = cm.exception.differences
        self.assertEqual(dict(remaining_diffs), {'bbb': Missing(2)})
Exemplo n.º 9
0
    def test_get_deviation_expected_missing(self):
        func = AcceptedTolerance._get_deviation_expected
        self.assertEqual(func(Missing(2)), (-2, 2))
        self.assertEqual(func(Missing(-2)), (2, -2))
        self.assertEqual(func(Missing(0)), (0, 0))

        with self.assertRaises(TypeError):
            func(Missing((1, 2)))

        with self.assertRaises(TypeError):
            func(Missing('abc'))
Exemplo n.º 10
0
    def test_scope(self):
        with self.assertRaises(ValidationError) as cm:
            with AcceptedCount(2, scope='group'):  # <- Accepts 2 per group.
                raise ValidationError({
                    'foo': [Extra('xxx'), Extra('yyy')],
                    'bar': [Missing('xxx'), Missing('yyy')],
                    'baz': [Invalid('xxx'), Invalid('yyy'), Invalid('zzz')],
                })

        remaining = cm.exception.differences
        self.assertEqual(remaining, {'baz': Invalid('zzz')})
Exemplo n.º 11
0
    def test_composite_key(self):
        with self.assertRaises(ValidationError) as cm:

            with AcceptedKeys(('a', 7)):  # <- Accept using tuple!
                raise ValidationError({
                    ('a', 7): Missing(1),
                    ('b', 7): Missing(2)
                })

        remaining_diffs = cm.exception.differences
        self.assertEqual(dict(remaining_diffs), {('b', 7): Missing(2)})
Exemplo n.º 12
0
    def test_instantiation(self):
        DataAssertionError('column names', Missing('foo'))
        DataAssertionError('column names', [Missing('foo')])
        DataAssertionError('column names',
                           {'Explanation here.': Missing('foo')})
        DataAssertionError('column names',
                           {'Explanation here.': [Missing('foo')]})

        with self.assertRaises(ValueError,
                               msg='Empty error should raise exception.'):
            DataAssertionError(msg='', differences={})
Exemplo n.º 13
0
    def test_string_predicate(self):
        with self.assertRaises(ValidationError) as cm:

            with AcceptedArgs('bbb'):  # <- Acceptance!
                raise ValidationError([
                    Missing('aaa'),
                    Missing('bbb'),
                    Extra('bbb'),
                ])

        remaining_diffs = cm.exception.differences
        self.assertEqual(list(remaining_diffs), [Missing('aaa')])
Exemplo n.º 14
0
    def test_nonmapping_container(self):
        """When differences container is not a mapping, the keys that
        AcceptedKeys() sees are all None.
        """
        with self.assertRaises(ValidationError) as cm:

            with AcceptedKeys('foo'):  # <- Accept keys that equal 'foo'.
                differences = [Missing(1), Extra(2)]  # <- List has no keys!
                raise ValidationError(differences)

        remaining_diffs = cm.exception.differences
        self.assertEqual(list(remaining_diffs), [Missing(1), Extra(2)])
Exemplo n.º 15
0
    def test_accept_function(self):
        with self.assertRaises(ValidationError) as cm:

            def function(key):
                return key == 'aaa'

            with AcceptedKeys(function):  # <- Accept by function!
                raise ValidationError({
                    'aaa': Missing(1),
                    'bbb': Missing(2),
                })

        remaining_diffs = cm.exception.differences
        self.assertEqual(dict(remaining_diffs), {'bbb': Missing(2)})
Exemplo n.º 16
0
    def test_combination_of_cases(self):
        """This is a bit of an integration test."""
        differences = {
            'foo': [Extra('xxx'), Missing('yyy')],
            'bar': [Extra('xxx')],
            'baz': [Extra('xxx'), Missing('yyy'), Extra('zzz')],
        }
        #accepted = {Ellipsis: [Extra('xxx'), Missing('yyy')]}
        accepted = [Extra('xxx'), Missing('yyy')]
        with self.assertRaises(ValidationError) as cm:
            with AcceptedDifferences(accepted):
                raise ValidationError(differences)

        actual = cm.exception.differences
        self.assertEqual(actual, {'baz': Extra('zzz')})
Exemplo n.º 17
0
    def test_over_limit(self):
        with self.assertRaises(ValidationError) as cm:
            with AcceptedCount(1):  # <- Accepts 1 but there are 2.
                raise ValidationError([Extra('xxx'), Missing('yyy')])

        remaining = list(cm.exception.differences)
        self.assertEqual(remaining, [Missing('yyy')])

        with self.assertRaises(ValidationError) as cm:
            with AcceptedCount(1):  # <- Accepts 1 and there are 2.
                raise ValidationError({'foo': Extra('xxx'), 'bar': Missing('yyy')})

        remaining = cm.exception.differences
        self.assertIsInstance(remaining, Mapping)
        self.assertEqual(len(remaining), 1)
Exemplo n.º 18
0
    def test_UnionedAcceptance(self):
        original_diffs = [Missing('a'), Extra('a'), Missing('b'), Extra('b')]

        with self.assertRaises(ValidationError) as cm:
            with UnionedAcceptance(self.accepted_missing, self.accepted_letter_a):
                raise ValidationError(original_diffs)
        differences = cm.exception.differences
        self.assertEqual(list(differences), [Extra('b')])

        # Test with acceptances in reverse-order (should give same result).
        with self.assertRaises(ValidationError) as cm:
            with UnionedAcceptance(self.accepted_letter_a, self.accepted_missing):
                raise ValidationError(original_diffs)
        differences = cm.exception.differences
        self.assertEqual(list(differences), [Extra('b')])
Exemplo n.º 19
0
    def test_non_deviation_diffs(self):
        diffs = [Missing('foo'), Extra('bar'), Invalid('baz')]
        with self.assertRaises(ValidationError) as cm:
            with AcceptedPercent(0.05):
                raise ValidationError(diffs)

        uncaught_diffs = cm.exception.differences
        self.assertEqual(diffs, uncaught_diffs)
Exemplo n.º 20
0
    def test_required_mapping(self):
        with self.assertRaises(ValidationError) as cm:
            data = {'AAA': 'a', 'BBB': 'x'}
            required = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c'}
            self.assertValid(data, required)

        differences = cm.exception.differences
        self.assertEqual(differences, {'BBB': Invalid('x', 'b'), 'CCC': Missing('c')})
Exemplo n.º 21
0
    def test_nonmapping(self):
        with self.assertRaises(ValidationError) as cm:
            data = set([1, 2, 3])
            required = set([1, 2, 4])
            self.assertValid(data, required)

        differences = cm.exception.differences
        self.assertEqual(differences, [Missing(4), Extra(3)])
Exemplo n.º 22
0
    def test_data_mapping(self):
        with self.assertRaises(ValidationError) as cm:
            data = {'a': set([1, 2]), 'b': set([1]), 'c': set([1, 2, 3])}
            required = set([1, 2])
            self.assertValid(data, required)

        differences = cm.exception.differences
        self.assertEqual(differences, {'b': [Missing(2)], 'c': [Extra(3)]})
Exemplo n.º 23
0
 def test_hashable(self):
     """Differences with hashable *args should be hashable."""
     # Following should all pass without error.
     hash(Missing('foo'))
     hash(Extra('bar'))
     hash(Invalid('baz'))
     hash(Invalid('baz', 'qux'))
     hash(Deviation(-1, 10))
Exemplo n.º 24
0
    def test_repr(self):
        error = DataAssertionError('different columns', [Missing('foo')])
        pattern = "DataAssertionError: different columns:\n Missing('foo')"
        self.assertEqual(repr(error), pattern)

        error = DataAssertionError('different columns', Missing('foo'))
        pattern = "DataAssertionError: different columns:\n Missing('foo')"
        self.assertEqual(repr(error), pattern)

        # Test pprint lists.
        error = DataAssertionError(
            'different columns',
            [Missing('foo'), Missing('bar')])
        pattern = ("DataAssertionError: different columns:\n"
                   " Missing('foo'),\n"
                   " Missing('bar')")
        self.assertEqual(repr(error), pattern)

        # Test dictionary with nested list.
        error = DataAssertionError(
            'different columns',
            {'Omitted': [Missing('foo'),
                         Missing('bar'),
                         Missing('baz')]})
        pattern = ("DataAssertionError: different columns:\n"
                   " 'Omitted': [Missing('foo'),\n"
                   "             Missing('bar'),\n"
                   "             Missing('baz')]")
        self.assertEqual(repr(error), pattern)
Exemplo n.º 25
0
    def test_novalue_comparisons(self):
        diff = _make_difference('a', NOVALUE)
        self.assertEqual(diff, Extra('a'))

        diff = _make_difference(5, NOVALUE)
        self.assertEqual(diff, Extra(5))

        diff = _make_difference(0, NOVALUE)
        self.assertEqual(diff, Extra(0))

        diff = _make_difference(NOVALUE, 'a')
        self.assertEqual(diff, Missing('a'))

        diff = _make_difference(NOVALUE, 5)
        self.assertEqual(diff, Missing(5))

        diff = _make_difference(NOVALUE, 0)
        self.assertEqual(diff, Missing(0))
Exemplo n.º 26
0
    def test_nonmapping_vs_mapping(self):
        """A mapping accpetance will not accept any non-mapping differences."""

        differences = [Missing('Y'), Extra('X')]
        acceptance = AcceptedDifferences({'a': Extra('X')})
        self.assertAcceptance(differences, acceptance, differences)

        differences = Extra('X')
        acceptance = AcceptedDifferences({'a': Extra('X')})
        self.assertAcceptance(differences, acceptance, [differences])
Exemplo n.º 27
0
    def test_subset_method(self):
        data = [1, 2, 3, 4]
        subset = Query.from_object([1, 2, 3])
        validate.subset(data, subset)

        with self.assertRaises(ValidationError) as cm:
            data = [1, 2, 3]
            subset = set([1, 2, 3, 4])
            validate.subset(data, subset)
        actual = cm.exception.differences
        expected = [Missing(4)]
        self.assertEqual(actual, expected)

        with self.assertRaises(ValidationError) as cm:
            data = {'A': [1, 2, 3], 'B': [3, 4, 5]}
            subset = {'A': iter([1, 2]), 'B': iter([2, 3])}
            validate.subset(data, subset)
        actual = cm.exception.differences
        expected = {'B': [Missing(2)]}
        self.assertEqual(actual, expected)
Exemplo n.º 28
0
    def test_set_method(self):
        data = [1, 2, 3, 4]
        requirement = Query.from_object([1, 2, 3, 4])
        validate.set(data, requirement)

        with self.assertRaises(ValidationError) as cm:
            data = [1, 2, 3, 5]
            requirement = set([1, 2, 3, 4])
            validate.set(data, requirement)
        actual = cm.exception.differences
        expected = [Missing(4), Extra(5)]
        self.assertEqual(actual, expected)

        with self.assertRaises(ValidationError) as cm:
            data = {'A': [1, 2, 3], 'B': [3]}
            requirement = {'A': iter([1, 2]), 'B': iter([3, 4])}
            validate.set(data, requirement)
        actual = cm.exception.differences
        expected = {'A': [Extra(3)], 'B': [Missing(4)]}
        self.assertEqual(actual, expected)
Exemplo n.º 29
0
    def test_filterfalse(self):
        class accepted_missing(MinimalAcceptance):
            def call_predicate(_self, item):
                return isinstance(item[1], Missing)

        acceptance = accepted_missing()
        result = acceptance._filterfalse([
            (None, Missing('A')),
            (None, Extra('B')),
        ])
        self.assertEqual(list(result), [(None, Extra('B'))])
Exemplo n.º 30
0
    def test_repr(self):
        error = DataError('different columns', [Missing('foo')])
        pattern = "DataError: different columns:\n Missing('foo')"
        self.assertEqual(repr(error), pattern)

        error = DataError('different columns', Missing('foo'))
        pattern = "DataError: different columns:\n Missing('foo')"
        self.assertEqual(repr(error), pattern)

        # Test pprint lists.
        error = DataError('different columns',
                          [Missing('foo'), Missing('bar')])
        pattern = ("DataError: different columns:\n"
                   " Missing('foo'),\n"
                   " Missing('bar')")
        self.assertEqual(repr(error), pattern)

        # Test dictionary.
        error = DataError('different columns', {'FOO': Missing('bar')})
        pattern = ("DataError: different columns:\n" " 'FOO': Missing('bar')")
        self.assertEqual(repr(error), pattern)