Example #1
0
def test_deep_nest():
    a = {"error_key": "test1", "stuff": {"abba": [{"foo": 2}]}}
    b = {"error_key": "test1", "stuff": {"abba": [{"foo": 2}]}}

    unordered_compare(a, b)

    with pytest.raises(AssertionError):
        b["stuff"]["abba"][0]["foo"] = "cake"
        unordered_compare(a, b)
Example #2
0
def test_dict():
    unordered_compare({"foo": 1}, {"foo": 1})
    unordered_compare({"foo": 1, "bar": 2}, {"bar": 2, "foo": 1})
    with pytest.raises(AssertionError):
        unordered_compare({"foo": 1}, {"foo": 2})

    with pytest.raises(AssertionError):
        unordered_compare({
            "foo": 1,
            "bar": [1, 2, 3]
        }, {
            "foo": 1,
            "bar": [0, 1, 2]
        })
Example #3
0
def test_unordered_compare():
    """
    Tests that unordered_compare() doesn't modify the objects it compares.
    """
    x_list = [8, 5, 3, 1, 2, 9, 5]
    x_list_backup = [8, 5, 3, 1, 2, 9, 5]
    b_tuple = (8, 5, 3, 1, 2, 9, 5)
    b_tuple_backup = (8, 5, 3, 1, 2, 9, 5)
    a = {"4d": 20, "5a": 10, "3b": 15, "7c": 5, "x": x_list,
         "b": b_tuple}
    b = {"4d": 20, "5a": 10, "3b": 15, "7c": 5, "x": [8, 5, 3, 1, 2, 9, 5],
         "b": (8, 5, 3, 1, 2, 9, 5)}
    c = {"4d": 20, "5a": 10, "3b": 15, "7c": 5, "x": [8, 5, 3, 1, 2, 9, 5],
         "b": (8, 5, 3, 1, 2, 9, 5)}
    d = {"4d": 20, "5a": 10, "3b": 15, "7c": 5, "x": [0, 8, 5, 3, 1, 2, 9, 5],
         "b": (8, 5, 3, 1, 2, 9, 5)}
    # sanity test before any manipulation
    # python can compare dicts natively to this extent
    assert c != d
    assert a == b
    assert a == c
    assert b == c
    assert x_list == x_list_backup
    assert b_tuple == b_tuple_backup
    noexception = False
    try:
        unordered_compare(a, b)
        noexception = True
    except:
        pass
    # unordered_compare test - the original objects should stay unchanged
    assert noexception
    assert a == b
    assert a == c
    assert b == c
    assert x_list == x_list_backup
    assert b_tuple == b_tuple_backup
Example #4
0
def test_list():
    unordered_compare([1, 2, 3], [1, 2, 3])
    unordered_compare([2, 3, 4], [4, 3, 2])
    with pytest.raises(AssertionError):
        unordered_compare([1, 2, 3], [2, 3, 4])
Example #5
0
def test_num():
    unordered_compare(1, 1)
    unordered_compare(1.1, 1.1)
    with pytest.raises(AssertionError):
        unordered_compare(1, 2)
Example #6
0
def test_str():
    unordered_compare("foo", "foo")
    unordered_compare(u"foo", u"foo")
    with pytest.raises(AssertionError):
        unordered_compare("foo", "bar")