Exemplo n.º 1
0
def test_hash():
    # pyrpds.pvector is order-agnostic
    # x = m(a=1, b=2, c=3)
    # y = m(a=1, b=2, c=3)
    x = m(a=1)
    y = m(a=1)

    assert hash(x) == hash(y)
Exemplo n.º 2
0
def test_not_equal():
    x = m(a=1, b=2, c=3)
    y = m(a=1, b=2)

    assert x != y
    assert not (x == y)

    assert y != x
    assert not (y == x)
Exemplo n.º 3
0
def test_m_constructor():
    container = m()
    assert len(container) == 0

    container = m(a="0", b=1, c="2")
    assert len(container) == 3
    assert container["a"] == "0"
    assert container["b"] == 1
    assert container["c"] == "2"
Exemplo n.º 4
0
def test_various_iterations():
    assert set(["a", "b"]) == set(m(a=1, b=2))
    assert ["a", "b"] == sorted(m(a=1, b=2).keys())
    assert isinstance(m().keys(), PVector)

    assert set([1, 2]) == set(m(a=1, b=2).itervalues())
    assert [1, 2] == sorted(m(a=1, b=2).values())
    assert isinstance(m().values(), PVector)

    assert set([("a", 1), ("b", 2)]) == set(m(a=1, b=2).iteritems())
    assert set([("a", 1), ("b", 2)]) == set(m(a=1, b=2).items())
    assert isinstance(m().items(), PVector)
Exemplo n.º 5
0
def test_remove_non_existing_element_raises_key_error():
    m1 = m(a=1)

    with pytest.raises(KeyError) as error:
        m1.remove("b")

    assert "'b'" in str(error.value)
Exemplo n.º 6
0
def test_evolver_remove_element_not_present():
    e = m(a=1000, b=2000).evolver()

    with pytest.raises(KeyError) as error:
        del e["c"]

    assert str(error.value) == "'c'"
Exemplo n.º 7
0
def test_update_with():
    assert m(a=1).update_with(add, m(a=2, b=4)) == m(a=3, b=4)
    assert m(a=1).update_with(lambda l, r: l, m(a=2, b=4)) == m(a=1, b=4)

    def map_add(l, r):
        return dict(list(l.items()) + list(r.items()))

    assert m(a={
        "c": 3
    }).update_with(map_add, m(a={"d": 4})) == m(a={
        "c": 3,
        "d": 4
    })
Exemplo n.º 8
0
def test_evolver_simple_update():
    x = m(a=1000, b=2000)
    e = x.evolver()
    e["b"] = 3000

    assert e["b"] == 3000
    assert e.persistent()["b"] == 3000
    assert x["b"] == 2000
Exemplo n.º 9
0
def test_not_equal_to_dict():
    x = m(a=1, b=2, c=3)
    y = dict(a=1, b=2, d=4)

    assert x != y
    assert not (x == y)

    assert y != x
    assert not (y == x)
Exemplo n.º 10
0
def test_dot_access_of_non_existing_element_raises_attribute_error():
    m1 = m(a=10)

    with pytest.raises(AttributeError) as error:
        m1.b

    error_message = str(error.value)

    assert "'b'" in error_message
    assert type(m1).__name__ in error_message
Exemplo n.º 11
0
def test_pmap_unorderable():
    with pytest.raises(TypeError):
        _ = m(a=1) < m(b=2)

    with pytest.raises(TypeError):
        _ = m(a=1) <= m(b=2)

    with pytest.raises(TypeError):
        _ = m(a=1) > m(b=2)

    with pytest.raises(TypeError):
        _ = m(a=1) >= m(b=2)
Exemplo n.º 12
0
def test_transform_base_case():
    # Works as set when called with only one key
    x = m(a=1, b=2)

    assert x.transform(["a"], 3) == m(a=3, b=2)
Exemplo n.º 13
0
def test_evolver_remove_element():
    e = m(a=1000, b=2000).evolver()
    assert "a" in e

    del e["a"]
    assert "a" not in e
Exemplo n.º 14
0
def test_update_with_multiple_arguments():
    # If same value is present in multiple sources, the rightmost is used.
    x = m(a=1, b=2, c=3)
    y = x.update(m(b=4, c=5), {"c": 6})

    assert y == m(a=1, b=4, c=6)
Exemplo n.º 15
0
def test_pickling_non_empty_map():
    assert pickle.loads(pickle.dumps(m(a=1, b=2), -1)) == m(a=1, b=2)
Exemplo n.º 16
0
def test_pickling_empty_map():
    assert pickle.loads(pickle.dumps(m(), -1)) == m()
Exemplo n.º 17
0
def test_update_one_argument():
    x = m(a=1)

    assert x.update(m(b=2)) == m(a=1, b=2)
Exemplo n.º 18
0
def test_update_no_arguments():
    x = m(a=1)

    assert x.update() == x
Exemplo n.º 19
0
def test_addition():
    assert m(x=1, y=2) + m(y=3, z=4) == m(x=1, y=3, z=4)
Exemplo n.º 20
0
def test_copy_returns_reference_to_self():
    m1 = m(a=10)
    assert m1.copy() is m1
Exemplo n.º 21
0
def test_transform_nested_maps():
    x = m(a=1, b=m(c=3, d=m(e=6, f=7)))

    assert x.transform(["b", "d", "e"], 999) == m(a=1,
                                                  b=m(c=3, d=m(e=999, f=7)))
Exemplo n.º 22
0
def test_transform_levels_missing():
    x = m(a=1, b=m(c=3))

    assert x.transform(["b", "d", "e"], 999) == m(a=1, b=m(c=3, d=m(e=999)))
Exemplo n.º 23
0
def test_supports_weakref():
    import weakref

    weakref.ref(m(a=1))
Exemplo n.º 24
0
def test_empty_truthiness():
    assert m(a=1)
    assert not m()
Exemplo n.º 25
0
def test_literalish_works():
    # assert m() is pmap()
    assert m(a=1, b=2) == pmap({"a": 1, "b": 2})
Exemplo n.º 26
0
def test_get_non_existing_raises_key_error():
    m1 = m()
    with pytest.raises(KeyError) as error:
        m1["foo"]

    assert "'foo'" in str(error.value)