Ejemplo n.º 1
0
def test_deep_equals_dict_level3_fails_missing_key():
    "that() deep_equals(dict) failing on level 3 when missing a key"

    something = {
        'my::all_users': [
            {'name': 'John', 'age': 33},
        ],
    }

    def assertions():
        assert that(something).deep_equals({
            'my::all_users': [
                {'name': 'John', 'age': 30, 'foo': 'bar'},
            ],
        })

    assert that(assertions).raises(
        AssertionError, compat_repr(
            "given\n"
            "X = {{'my::all_users': [{{'age': 33, 'name': 'John'}}]}}\n"
            "    and\n"
            "Y = {{'my::all_users': [{{'age': 30, 'foo': 'bar', 'name': 'John'}}]}}\n"
            "X['my::all_users'][0] does not have the key \"{0}\" whereas Y['my::all_users'][0] has it"
        ).format(safe_repr('foo'))
    )
Ejemplo n.º 2
0
def test_deep_equals_dict_level3_fails_different_key():
    "that() deep_equals(dict) failing on level 3 when has an extra key"

    something = {
        'my::all_users': [
            {
                'name': 'John',
                'age': 33,
                'foo': 'bar'
            },
        ],
    }

    def assertions():
        assert that(something).deep_equals({
            'my::all_users': [
                {
                    'name': 'John',
                    'age': 33,
                    'bar': 'foo'
                },
            ],
        })

    assert that(assertions).raises(
        AssertionError,
        compat_repr(
            "given\n"
            "X = {{'my::all_users': [{{'age': 33, 'foo': 'bar', 'name': 'John'}}]}}\n"
            "    and\n"
            "Y = {{'my::all_users': [{{'age': 33, 'bar': 'foo', 'name': 'John'}}]}}\n"
            "X['my::all_users'][0] has the key \"{0}\" whereas Y['my::all_users'][0] does not"
        ).format(safe_repr('foo')))
Ejemplo n.º 3
0
    def compare_dicts(self, X, Y):
        c = self.get_context()

        x_keys = list(sorted(X.keys()))
        y_keys = list(sorted(Y.keys()))

        diff_x = list(set(x_keys).difference(set(y_keys)))
        diff_y = list(set(y_keys).difference(set(x_keys)))
        if diff_x:
            msg = "X{0} has the key {1!r} whereas Y{2} does not".format(
                red(c.current_X_keys),
                safe_repr(diff_x[0]),
                green(c.current_Y_keys))
            return DeepExplanation(msg)

        elif diff_y:
            msg = "X{0} does not have the key {1!r} whereas Y{2} has it".format(
                red(c.current_X_keys),
                safe_repr(diff_y[0]),
                green(c.current_Y_keys))
            return DeepExplanation(msg)

        elif X == Y:
            return True

        else:
            for key_X, key_Y in zip(x_keys, y_keys):
                self.key_X = key_X
                self.key_Y = key_Y
                value_X = X[key_X]
                value_Y = Y[key_Y]
                child = DeepComparison(
                    value_X,
                    value_Y,
                    epsilon=self.epsilon,
                    parent=self,
                ).compare()
                if isinstance(child, DeepExplanation):
                    return child
Ejemplo n.º 4
0
    def compare_dicts(self, X, Y):
        c = self.get_context()

        x_keys = list(sorted(X.keys()))
        y_keys = list(sorted(Y.keys()))

        diff_x = list(set(x_keys).difference(set(y_keys)))
        diff_y = list(set(y_keys).difference(set(x_keys)))
        if diff_x:
            msg = "X{0} has the key {1!r} whereas Y{2} does not".format(
                red(c.current_X_keys),
                safe_repr(diff_x[0]),
                green(c.current_Y_keys))
            return DeepExplanation(msg)

        elif diff_y:
            msg = "X{0} does not have the key {1!r} whereas Y{2} has it".format(
                red(c.current_X_keys),
                safe_repr(diff_y[0]),
                green(c.current_Y_keys))
            return DeepExplanation(msg)

        elif X == Y:
            return True

        else:
            for key_X, key_Y in zip(x_keys, y_keys):
                self.key_X = key_X
                self.key_Y = key_Y
                value_X = X[key_X]
                value_Y = Y[key_Y]
                child = DeepComparison(
                    value_X,
                    value_Y,
                    epsilon=self.epsilon,
                    parent=self,
                ).compare()
                if isinstance(child, DeepExplanation):
                    return child
Ejemplo n.º 5
0
def test_deep_equals_dict_level1_fails_missing_key_on_y():
    "that(X) deep_equals(Y) fails when Y is missing a key that X has"

    something = {
        'one': 'yeah',
    }

    def assertions():
        assert that(something).deep_equals({
            'two': 'yeah',
        })

    assert that(assertions).raises(
        AssertionError,
        compat_repr("given\n"
                    "X = {{'one': 'yeah'}}\n"
                    "    and\n"
                    "Y = {{'two': 'yeah'}}\n"
                    "X has the key \"{0}\" whereas Y does not").format(
                        safe_repr('one')))
Ejemplo n.º 6
0
    def compare_dicts(self, X, Y):
        """
        Difference from parent class is that it could do partial matching
        
        :param X: first dict
        :param Y: second dict
        :return: True if comparing is succesfull, otherwise it will throw AssertionError
        """
        c = self.get_context()

        x_keys = list(sorted(X.keys()))
        y_keys = list(sorted(Y.keys()))

        diff_y = list(set(y_keys).difference(set(x_keys)))
        if diff_y:
            msg = "Filter Param%s has the key %%r whereas Source Item%s does not" % (
                red(c.current_X_keys),
                green(c.current_Y_keys),
            ) % safe_repr(diff_y[0])
            return DeepExplanation(msg).as_assertion(X, Y)

        elif X == Y:
            return True
        else:
            for key in y_keys:
                self.key_X = key
                self.key_Y = key
                value_X = X[key]
                value_Y = Y[key]

                child = ComparisonWrapper(
                    value_X,
                    value_Y,
                    epsilon=self.epsilon,
                    parent=self,
                ).compare()

                if isinstance(child, DeepExplanation):
                    return child.as_assertion(value_X, value_Y)

            return True
Ejemplo n.º 7
0
def test_deep_equals_dict_level1_fails_missing_key_on_y():
    "that(X) deep_equals(Y) fails when Y is missing a key that X has"

    something = {
        'one': 'yeah',
    }

    def assertions():
        assert that(something).deep_equals({
            'two': 'yeah',
        })

    assert that(assertions).raises(
        AssertionError, compat_repr(
            "given\n"
            "X = {{'one': 'yeah'}}\n"
            "    and\n"
            "Y = {{'two': 'yeah'}}\n"
            "X has the key \"{0}\" whereas Y does not"
        ).format(safe_repr('one'))
    )
Ejemplo n.º 8
0
    def get_header(self, X, Y, suffix):
        params = (safe_repr(X), safe_repr(Y), text_type(suffix))
        header = "given\nX = {0}\n    and\nY = {1}\n{2}".format(*params)

        return yellow(header).strip()
Ejemplo n.º 9
0
    def get_header(self, X, Y, suffix):
        params = (safe_repr(X), safe_repr(Y), text_type(suffix))
        header = "given\nX = {0}\n    and\nY = {1}\n{2}".format(*params)

        return yellow(header).strip()
Ejemplo n.º 10
0
    def get_header(self, X, Y, suffix):
        params = (safe_repr(X), safe_repr(Y), text_type(suffix))
        header = "given\nX = %s\n    and\nY = %s\n%s" % params

        return yellow(header).strip()
Ejemplo n.º 11
0
    def get_header(self, X, Y, suffix):
        params = (safe_repr(X), safe_repr(Y), text_type(suffix))
        header = "given\nX = %s\n    and\nY = %s\n%s" % params

        return yellow(header).strip()