aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pyrsistent/py2/tests/checked_set_test.py
blob: f0be4963e2190e23c2926310f1cfda97c2f00a33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pickle
import pytest
from pyrsistent import CheckedPSet, PSet, InvariantException, CheckedType, CheckedPVector, CheckedValueTypeError


class Naturals(CheckedPSet):
    __type__ = int
    __invariant__ = lambda value: (value >= 0, 'Negative value')

def test_instantiate():
    x = Naturals([1, 2, 3, 3])

    assert list(x) == [1, 2, 3]
    assert isinstance(x, Naturals)
    assert isinstance(x, PSet)
    assert isinstance(x, CheckedType)

def test_add():
    x = Naturals()
    x2 = x.add(1)

    assert list(x2) == [1]
    assert isinstance(x2, Naturals)

def test_invalid_type():
    with pytest.raises(CheckedValueTypeError):
        Naturals([1, 2.0])

def test_breaking_invariant():
    try:
        Naturals([1, -1])
        assert False
    except InvariantException as e:
        assert e.invariant_errors == ('Negative value',)

def test_repr():
    x = Naturals([1, 2])

    assert str(x) == 'Naturals([1, 2])'

def test_default_serialization():
    x = Naturals([1, 2])

    assert x.serialize() == set([1, 2])

class StringNaturals(Naturals):
    @staticmethod
    def __serializer__(format, value):
        return format.format(value)

def test_custom_serialization():
    x = StringNaturals([1, 2])

    assert x.serialize("{0}") == set(["1", "2"])

class NaturalsVector(CheckedPVector):
    __type__ = Naturals

def test_multi_level_serialization():
    x = NaturalsVector.create([[1, 2], [3, 4]])

    assert str(x) == "NaturalsVector([Naturals([1, 2]), Naturals([3, 4])])"

    sx = x.serialize()
    assert sx == [set([1, 2]), set([3, 4])]
    assert isinstance(sx[0], set)

def test_create():
    assert Naturals.create([1, 2]) == Naturals([1, 2])

def test_evolver_returns_same_instance_when_no_updates():
    x = Naturals([1, 2])
    assert x.evolver().persistent() is x

def test_pickling():
    x = Naturals([1, 2])
    y = pickle.loads(pickle.dumps(x, -1))

    assert x == y
    assert isinstance(y, Naturals)


def test_supports_weakref():
    import weakref
    weakref.ref(Naturals([1, 2]))