Example #1
0
    def test_instantiation(self):
        DataError('column names', xMissing('foo'))
        DataError('column names', [xMissing('foo')])
        DataError('column names', {'foo': xMissing('bar')})
        DataError('column names', {('foo', 'bar'): xMissing('baz')})

        with self.assertRaises(ValueError, msg='Empty error should raise exception.'):
            DataError(msg='', differences={})
Example #2
0
    def test_repr(self):
        error = DataError('different columns', [xMissing('foo')])
        pattern = "DataError: different columns:\n xMissing('foo')"
        self.assertEqual(repr(error), pattern)

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

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

        # Test dictionary.
        error = DataError('different columns', {'FOO': xMissing('bar')})
        pattern = ("DataError: different columns:\n"
                   " 'FOO': xMissing('bar')")
        self.assertEqual(repr(error), pattern)
Example #3
0
    def test_mapping(self):
        required = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c'}

        data = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c'}
        result = _compare_mapping(data, required)
        self.assertEqual(result, {})

        data = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c', 'DDD': '3'}
        result = _compare_mapping(data, required)
        self.assertEqual(result, {'DDD': xExtra('3')})

        data = {'AAA': 'a', 'CCC': 'c', 'DDD': '3'}
        result = _compare_mapping(data, required)
        self.assertEqual(result, {'BBB': xMissing('b'), 'DDD': xExtra('3')})
Example #4
0
    def test_verbose_repr(self):
        reference = 'reference-data-source'
        subject = 'subject-data-source'
        error = DataError('different columns', [xMissing('foo')], subject, reference)
        error._verbose = True  # <- Set verbose flag, here!

        pattern = ("DataError: different columns:\n"
                   " xMissing('foo')\n"
                   "\n"
                   "SUBJECT:\n"
                   "subject-data-source\n"
                   "REQUIRED:\n"
                   "reference-data-source")
        self.assertEqual(repr(error), pattern)
Example #5
0
    def test_iterable(self):
        required = set(['a', 'b', 'c'])

        data = iter(['a', 'b', 'c'])
        result = _compare_set(data, required)
        self.assertEqual(result, [])

        data = iter(['a', 'b', 'c', '3'])
        result = _compare_set(data, required)
        self.assertEqual(result, [xExtra('3')])

        data = iter(['a', 'c', '3'])
        result = _compare_set(data, required)
        result = set(result)
        self.assertEqual(result, set([xMissing('b'), xExtra('3')]))
Example #6
0
    def test_mapping(self):
        required = set(['a', 'b', 'c'])

        data = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c'}
        result = _compare_set(data, required)
        self.assertEqual(result, [])

        data = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c', 'DDD': '3'}
        result = _compare_set(data, required)
        self.assertEqual(result, [xExtra('3')])

        data = {'AAA': 'a', 'CCC': 'c', 'DDD': '3'}
        result = _compare_set(data, required)
        result = set(result)
        self.assertEqual(result, set([xMissing('b'), xExtra('3')]))
Example #7
0
    def test_sequence(self):
        required = ['a', 'b', 'c']

        data = ['a', 'b', 'c']
        result = _compare_sequence(data, required)
        self.assertEqual(result, {})

        data = ['a', 'b', 'c', 'd']
        result = _compare_sequence(data, required)
        self.assertEqual(result, {3: xExtra('d')})

        data = ['a', 'b']
        result = _compare_sequence(data, required)
        self.assertEqual(result, {2: xMissing('c')})

        data = ['a', 'x', 'c', 'y']
        result = _compare_sequence(data, required)
        self.assertEqual(result, {1: xInvalid('x', 'b'), 3: xExtra('y')})
Example #8
0
    def test_notfound_comparisons(self):
        diff = _xgetdiff('a', _xNOTFOUND)
        self.assertEqual(diff, xExtra('a'))

        diff = _xgetdiff(_xNOTFOUND, 'b')
        self.assertEqual(diff, xMissing('b'))

        # For numeric comparisons, _xNOTFOUND behaves like None.
        diff = _xgetdiff(5, _xNOTFOUND)
        self.assertEqual(diff, xDeviation(+5, None))

        diff = _xgetdiff(0, _xNOTFOUND)
        self.assertEqual(diff, xDeviation(0, None))

        diff = _xgetdiff(_xNOTFOUND, 6)
        self.assertEqual(diff, xDeviation(-6, 6))  # <- Assymetric behavior
        #    (see None vs numeric)!

        diff = _xgetdiff(_xNOTFOUND, 0)
        self.assertEqual(diff, xDeviation(None, 0))
Example #9
0
    def test_compare(self):
        a = CompareSet(['aaa', 'bbb', 'ddd'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        expected = [xExtra('ddd'), xMissing('ccc')]
        self.assertEqual(expected, a.compare(b))

        a = CompareSet(['aaa', 'bbb', 'ccc'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        self.assertEqual([], a.compare(b), ('When there is no difference, '
                                            'compare should return an empty '
                                            'list.'))

        # Test callable other (all True).
        result = a.compare(lambda x: len(x) == 3)
        self.assertEqual([], result)

        # Test callable other (some False).
        result = a.compare(lambda x: x.startswith('b'))
        expected = set([xInvalid('aaa'), xInvalid('ccc')])
        self.assertEqual(expected, set(result))

        # Test callable other, multiple arguments (all True).
        a = CompareSet([(1, 1), (1, 2), (2, 1), (2, 2)])
        result = a.compare(lambda x, y: x + y > 0)
        self.assertEqual([], result)

        # Test callable other, using single vararg (all True).
        a = CompareSet([(1, 1), (1, 2), (2, 1), (2, 2)])
        result = a.compare(lambda *x: x[0] + x[1] > 0)
        self.assertEqual([], result)

        # Test callable other, multiple arguments (some False).
        a = CompareSet([(1, 1), (1, 2), (2, 1), (2, 2)])
        result = a.compare(lambda x, y: x != y)
        expected = set([xInvalid((1, 1)), xInvalid((2, 2))])
        self.assertEqual(expected, set(result))

        # Test subset (less-than-or-equal).
        a = CompareSet(['aaa', 'bbb', 'ddd'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        expected = [xExtra('ddd')]
        self.assertEqual(expected, a.compare(b, op='<='))

        # Test strict subset (less-than).
        a = CompareSet(['aaa', 'bbb'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        self.assertEqual([], a.compare(b, op='<'))

        # Test strict subset (less-than) assertion violation.
        a = CompareSet(['aaa', 'bbb', 'ccc'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        self.assertEqual([xNotProperSubset()], a.compare(b, op='<'))

        # Test superset (greater-than-or-equal).
        a = CompareSet(['aaa', 'bbb', 'ccc'])
        b = CompareSet(['aaa', 'bbb', 'ddd'])
        expected = [xMissing('ddd')]
        self.assertEqual(expected, a.compare(b, op='>='))

        # Test superset subset (greater-than).
        a = CompareSet(['aaa', 'bbb', 'ccc'])
        b = CompareSet(['aaa', 'bbb'])
        self.assertEqual([], a.compare(b, op='>'))

        # Test superset subset (greater-than) assertion violation.
        a = CompareSet(['aaa', 'bbb', 'ccc'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        self.assertEqual([xNotProperSuperset()], a.compare(b, op='>'))