def assertEqual(self, first, second, msg=None):
    """Fail if *first* does not satisfy *second* as determined by
    appropriate validation comparison.

    If *first* and *second* are comparable, a failure will raise a
    DataError containing the differences between the two.

    If the *second* argument is a helper-function (or other
    callable), it is used as a key which must return True for
    acceptable values.
    """
    if not isinstance(first, BaseCompare):
        if isinstance(first, str) or not isinstance(first, Container):
            first = CompareSet([first])
        elif isinstance(first, Set):
            first = CompareSet(first)
        elif isinstance(first, Mapping):
            first = CompareDict(first)

    if callable(second):
        equal = first.all(second)
        default_msg = 'first object contains invalid items'
    else:
        equal = first == second
        default_msg = 'first object does not match second object'

    if not equal:
        differences = first.compare(second)
        self.fail(msg or default_msg, differences)
Ejemplo n.º 2
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='>'))