Exemplo n.º 1
0
def test_deep_nest():
    a = {"error_key": "test1", "stuff": {"abba": [{"foo": 2}]}}
    b = {"error_key": "test1", "stuff": {"abba": [{"foo": 2}]}}

    deep_compare(a, b)

    with pytest.raises(AssertionError):
        b["stuff"]["abba"][0]["foo"] = "cake"
        deep_compare(a, b)
Exemplo n.º 2
0
def test_deep_compare():
    """
    Tests that deep_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:
        deep_compare(a, b)
        noexception = True
    except:
        pass
    # deep_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
Exemplo n.º 3
0
def test_dict():
    deep_compare({"foo": 1}, {"foo": 1})
    deep_compare({"foo": 1, "bar": 2}, {"bar": 2, "foo": 1})
    with pytest.raises(AssertionError):
        deep_compare({"foo": 1}, {"foo": 2})

    with pytest.raises(AssertionError):
        deep_compare({
            "foo": 1,
            "bar": [1, 2, 3]
        }, {
            "foo": 1,
            "bar": [0, 1, 2]
        })
Exemplo n.º 4
0
def test_deep_nest_dict_list():
    a = {'2': [22, 222], 3: [33, 333]}
    b = {3: [33, 333], '2': [22, 222]}
    deep_compare(a, b)
    with pytest.raises(AssertionError):
        b['2'] = [222, 22]
        deep_compare(a, b)

    a = {3: [33, [3, '33', 333]], 2: (22, 222)}
    b = {2: (22, 222), 3: [33, [3, '33', 333]]}
    with pytest.raises(AssertionError):
        b[3] = [33, ['33', 333, 3]]
        deep_compare(a, b)

    a = {3: [33, [set([4, 3]), '33', 333]], 2: (22, 222)}
    b = {2: (22, 222), 3: [33, [set([4, 3]), '33', 333]]}
    with pytest.raises(AssertionError):
        b[3][1] = set([3, 4])
        deep_compare(a, b)
Exemplo n.º 5
0
def test_list():
    deep_compare([1, 2, 3], [1, 2, 3])
    with pytest.raises(AssertionError):
        deep_compare([2, 3, 4], [4, 3, 2])
        deep_compare([1, 2, 3], [2, 3, 4])
Exemplo n.º 6
0
def test_num():
    deep_compare(1, 1)
    deep_compare(1.1, 1.1)
    with pytest.raises(AssertionError):
        deep_compare(1, 2)
Exemplo n.º 7
0
def test_str():
    deep_compare("foo", "foo")
    deep_compare(u"foo", u"foo")
    with pytest.raises(AssertionError):
        deep_compare("foo", "bar")
Exemplo n.º 8
0
def test_deep_nest_list_dict():
    a = [{2: 22}, {3: 33}, {4: 44}]
    b = [{2: 22}, {3: 33}, {4: 44}]
    deep_compare(a, b)
    with pytest.raises(AssertionError):
        b[1][3] = 30
        deep_compare(a, b)

    a = [{2: 22}, {3: [33, 333]}, {4: 44}]
    b = [{2: 22}, {3: [33, 333]}, {4: 44}]
    deep_compare(a, b)
    with pytest.raises(AssertionError):
        b[1][3] = [333, 33]
        deep_compare(a, b)

    a = [{2: 22, 5: '55'}, [33, 333, '3'], {6: [6], 4: 44}]
    b = [{5: '55', 2: 22}, [33, 333, '3'], {4: 44, 6: [6]}]
    deep_compare(a, b)
    with pytest.raises(AssertionError):
        b[1][2] = 3
        deep_compare(a, b)

    a = [
        {
            'ip_addr': '0.0.0.0',
            'process_name': 'qpidd',
            'port': '5672'
        },
        {
            'ip_addr': '127.0.0.1',
            'process_name': 'mongod',
            'port': '27017'
        },
        {
            'ip_addr': '127.0.0.1',
            'process_name': 'Passenger Rac',
            'port': '53644'
        },
        {
            'ip_addr': '0.0.0.0',
            'process_name': 'qdrouterd',
            'port': '5646'
        },
    ]
    b = [
        {
            'ip_addr': '0.0.0.0',
            'port': '5672',
            'process_name': 'qpidd'
        },
        {
            'ip_addr': '127.0.0.1',
            'port': '27017',
            'process_name': 'mongod'
        },
        {
            'ip_addr': '127.0.0.1',
            'port': '53644',
            'process_name': 'Passenger Rac'
        },
        {
            'ip_addr': '0.0.0.0',
            'port': '5646',
            'process_name': 'qdrouterd'
        },
    ]
    deep_compare(a, b)
Exemplo n.º 9
0
def test_deep_nest_tuple_dict():
    a = ({2: 22}, {3: 33}, {4: 44})
    b = ({2: 22}, {3: 33}, {4: 44})
    deep_compare(a, b)
    with pytest.raises(AssertionError):
        b = ({2: 22}, {3: 30}, {4: 44})
        deep_compare(a, b)

    a = ({2: 22}, {3: [33, 333]}, {4: 44})
    b = ({2: 22}, {3: [33, 333]}, {4: 44})
    deep_compare(a, b)
    with pytest.raises(AssertionError):
        b = ({2: 22}, {3: [333, 33]}, {4: 44})
        deep_compare(a, b)

    a = ({2: 22, 5: '55'}, [33, 333, '3'], {6: [6], 4: 44})
    b = ({5: '55', 2: 22}, [33, 333, '3'], {4: 44, 6: [6]})
    deep_compare(a, b)
    with pytest.raises(AssertionError):
        b = ({5: '55', 2: 22}, [33, 333, 3], {4: 44, 6: [6]})
        deep_compare(a, b)