예제 #1
0
    def test_failing_explicit_callable(self):
        self.subject = self.src2_records

        with self.assertRaises(DataError) as cm:
            required = lambda x: x in (65, 70)
            self.assertSubjectSum('value', ['label1'], required)

        differences = cm.exception.differences
        expected = [
            Invalid(Decimal(66), label1='a'),
            Invalid(Decimal(69), label1='b')
        ]
        #expected = [Invalid(66, label1='a'),
        #            Invalid(69, label1='b')]
        super(DataTestCase, self).assertEqual(set(differences), set(expected))
예제 #2
0
    def test_invalid(self):
        with self.assertRaises(DataError) as cm:
            required = lambda x: x in ('a', 'b')
            self.assertSubjectSet('label1', required)

        differences = cm.exception.differences
        self.assertEqual(differences, [Invalid('c')])
예제 #3
0
    def test_compareset_v_callable_fail(self):
        with self.assertRaises(DataError) as cm:
            first = CompareSet([1, 2, 3, 4, 5, 6, 7])
            second = lambda x: x <= 6
            self.assertEqual(first, second)

        differences = cm.exception.differences
        super(DataTestCase, self).assertEqual(differences, [Invalid(7)])
예제 #4
0
    def test_iterable(self):
        isalpha = lambda x: x.isalpha()

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

        data = iter(['a', 'b', 'c', '9'])
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [Invalid('9')])
예제 #5
0
    def test_sequence(self):
        isalpha = lambda x: x.isalpha()

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

        data = ['a', 'b', 'c', '9']
        result = _compare_other(data, isalpha)
        self.assertEqual(result, {3: Invalid('9')})
예제 #6
0
    def test_mapping(self):
        isalpha = lambda x: x.isalpha()

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

        data = {'AAA': 'a', 'BBB': 'b', 'CCC': 'c', 'DDD': '3'}
        result = _compare_other(data, isalpha)
        self.assertEqual(result, {'DDD': Invalid('3')})
예제 #7
0
    def test_set(self):
        isalpha = lambda x: x.isalpha()

        data = set(['a', 'b', 'c'])
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [])

        data = set(['a', 'b', 'c', '3'])
        result = _compare_other(data, isalpha)
        self.assertEqual(result, [Invalid('3')])
예제 #8
0
    def test_required_sequence(self):
        """When *required* is a sequence, _compare_sequence() should be
        called."""
        with self.assertRaises(DataError) as cm:
            required = ['a', 2, 'c', 4]
            data = ['a', 2, 'x', 3]
            self.assertValid(data, required)

        differences = cm.exception.differences
        self.assertEqual = super(DataTestCase, self).assertEqual
        self.assertEqual(differences, {2: Invalid('x', 'c'), 3: Deviation(-1, 4)})
예제 #9
0
    def test_required_other(self):
        """When *required* is a string or other object, _compare_other()
        should be called."""
        with self.assertRaises(DataError) as cm:
            required = lambda x: x.isupper()
            data = ['AAA', 'BBB', 'ccc', 'DDD']
            self.assertValid(data, required)

        differences = cm.exception.differences
        self.assertEqual = super(DataTestCase, self).assertEqual
        self.assertEqual(differences, {2: Invalid('ccc')})
예제 #10
0
    def test_invalid(self):
        data = [('LABEL1', 'value'), ('a', '6'), ('b', '7')]
        self.subject = MinimalSource(data)

        with self.assertRaises(DataError) as cm:

            def lowercase(x):  # <- Helper function!!!
                return x == x.lower()

            self.assertSubjectColumns(required=lowercase)  # <- test assert

        differences = cm.exception.differences
        self.assertEqual(set(differences), set([Invalid('LABEL1')]))
예제 #11
0
    def test_compare_function(self):
        a = CompareDict({'aaa': 'x', 'bbb': 'y', 'ccc': 'z'}, 'foo')

        # All True.
        result = a.compare(lambda x: len(x) == 1)
        self.assertEqual([], result)

        # Some False.
        result = a.compare(lambda a: a in ('x', 'y'))
        expected = [Invalid('z', foo='ccc')]
        self.assertEqual(expected, result)

        # All True, multiple args.
        a = CompareDict({'aaa': (1, 2), 'bbb': (1, 3), 'ccc': (4, 8)}, 'foo')
        result = a.compare(lambda x, y: x < y)
        self.assertEqual([], result)

        # Some False, multiple args.
        a = CompareDict({'aaa': (1, 0), 'bbb': (1, 3), 'ccc': (3, 2)}, 'foo')
        result = a.compare(lambda x, y: x < y)
        expected = [Invalid((1, 0), foo='aaa'), Invalid((3, 2), foo='ccc')]
        self.assertEqual(expected, result)
예제 #12
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: Extra('d')})

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

        data = ['a', 'x', 'c', 'y']
        result = _compare_sequence(data, required)
        self.assertEqual(result, {1: Invalid('x', 'b'), 3: Extra('y')})
예제 #13
0
파일: api08.py 프로젝트: drewdolan/datatest
    def wrapped(element):
        try:
            if isinstance(element, BaseElement):
                returned_value = function(element)
            else:
                returned_value = function(*element)
        except Exception:
            returned_value = False  # Raised errors count as False.

        if returned_value == True:
            return None  # <- EXIT!

        if returned_value == False:
            return Invalid(element)  # <- EXIT!

        if isinstance(returned_value, BaseDifference):
            return returned_value  # <- EXIT!

        callable_name = function.__name__
        message = \
            '{0!r} returned {1!r}, should return True, False or a difference instance'
        raise TypeError(message.format(callable_name, returned_value))
예제 #14
0
파일: api08.py 프로젝트: drewdolan/datatest
def _require_callable(data, function):
    if data is NOVALUE:
        return Invalid(None)  # <- EXIT!

    def wrapped(element):
        try:
            if isinstance(element, BaseElement):
                returned_value = function(element)
            else:
                returned_value = function(*element)
        except Exception:
            returned_value = False  # Raised errors count as False.

        if returned_value == True:
            return None  # <- EXIT!

        if returned_value == False:
            return Invalid(element)  # <- EXIT!

        if isinstance(returned_value, BaseDifference):
            return returned_value  # <- EXIT!

        callable_name = function.__name__
        message = \
            '{0!r} returned {1!r}, should return True, False or a difference instance'
        raise TypeError(message.format(callable_name, returned_value))

    if isinstance(data, BaseElement):
        return wrapped(data)  # <- EXIT!

    results = (wrapped(elem) for elem in data)
    diffs = (diff for diff in results if diff)
    first_element, diffs = iterpeek(diffs)
    if first_element:  # If not empty, return diffs.
        return diffs
    return None
예제 #15
0
 def test_required_string(self):
     data = set(['AAA', 'BBB'])
     string_val = 'AAA'
     result = _compare_other(data, string_val)
     self.assertEqual(result, [Invalid('BBB')])
예제 #16
0
    def test_not_regex_failing(self):
        with self.assertRaises(DataError) as cm:
            self.assertSubjectNotRegex('label2', '^\d{1,2}$')

        differences = cm.exception.differences
        super(DataTestCase, self).assertEqual(differences, [Invalid('2')])
예제 #17
0
 def test_required_regex(self):
     data = set(['a1', 'b2', 'c3', 'd', 'e5'])
     regex = re.compile('[a-z][0-9]+')
     result = _compare_other(data, regex)
     self.assertEqual(result, [Invalid('d')])
예제 #18
0
    def test_compare(self):
        a = CompareSet(['aaa', 'bbb', 'ddd'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        expected = [Extra('ddd'), Missing('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([Invalid('aaa'), Invalid('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([Invalid((1, 1)), Invalid((2, 2))])
        self.assertEqual(expected, set(result))

        # Test subset (less-than-or-equal).
        a = CompareSet(['aaa', 'bbb', 'ddd'])
        b = CompareSet(['aaa', 'bbb', 'ccc'])
        expected = [Extra('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([NotProperSubset()], a.compare(b, op='<'))

        # Test superset (greater-than-or-equal).
        a = CompareSet(['aaa', 'bbb', 'ccc'])
        b = CompareSet(['aaa', 'bbb', 'ddd'])
        expected = [Missing('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([NotProperSuperset()], a.compare(b, op='>'))
예제 #19
0
 def test_compare_strings(self):
     a = CompareDict({'aaa': 'x', 'bbb': 'y', 'ccc': 'z'}, 'foo')
     b = CompareDict({'aaa': 'x', 'bbb': 'z', 'ccc': 'z'}, 'foo')
     expected = [Invalid('y', 'z', foo='bbb')]
     self.assertEqual(expected, a.compare(b))