Example #1
0
def test_serializer_hash_mutable():
    """tests whether the make_serializer returns a canonical hash"""
    # test special serializer
    encode = cache.make_serializer("hash_mutable")
    assert encode({"a": 1, "b": 2}) == encode({"b": 2, "a": 1})

    c1 = collections.OrderedDict([("a", 1), ("b", 2)])
    c2 = collections.OrderedDict([("b", 2), ("a", 1)])
    assert cache.hash_mutable(c1) != cache.hash_mutable(c2)
    assert cache.hash_mutable(dict(c1)) == cache.hash_mutable(dict(c2))

    class Test:
        """test class that neither implements __eq__ nor __hash__"""
        def __init__(self, a):
            self.a = a

    assert cache.hash_mutable(Test(1)) != cache.hash_mutable(Test(1))

    class TestEq:
        """test class that only implements __eq__ and not __hash__"""
        def __init__(self, a):
            self.a = a

        def __eq__(self, other):
            return self.a == other.a

    assert cache.hash_mutable(TestEq(1)) == cache.hash_mutable(TestEq(1))
    assert cache.hash_mutable(TestEq(1)) != cache.hash_mutable(TestEq(2))
Example #2
0
def test_unserializer():
    """tests whether the make_serializer and make_unserializer return the
    original objects"""
    data_list = [None, 1, [1, 2], {"b": 1, "a": 2}]
    for method in get_serialization_methods():
        encode = cache.make_serializer(method)
        decode = cache.make_unserializer(method)
        for data in data_list:
            assert data == decode(encode(data))
Example #3
0
def test_serializer(method):
    """tests whether the make_serializer returns a canonical hash"""
    encode = cache.make_serializer(method)

    assert encode(1) == encode(1)

    assert encode([1, 2, 3]) != encode([2, 3, 1])
    if method != "json":
        # json cannot encode sets
        assert encode({1, 2, 3}) == encode({2, 3, 1})
Example #4
0
def test_serializer_nonsense():
    """test whether errors are thrown for wrong input"""
    with pytest.raises(ValueError):
        cache.make_serializer("non-sense")
    with pytest.raises(ValueError):
        cache.make_unserializer("non-sense")