aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pyrsistent/py3/tests/freeze_test.py
blob: 158cf5d872a1501374ba470c6a39e25e49b3640f (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""Tests for freeze and thaw."""
import collections
from pyrsistent import v, m, s, freeze, thaw, PRecord, field, mutant


## Freeze (standard)

def test_freeze_basic():
    assert freeze(1) == 1
    assert freeze('foo') == 'foo'

def test_freeze_list():
    assert freeze([1, 2]) == v(1, 2)

def test_freeze_dict():
    result = freeze({'a': 'b'})
    assert result == m(a='b')
    assert type(freeze({'a': 'b'})) is type(m())

def test_freeze_defaultdict():
    test_dict = collections.defaultdict(dict)
    test_dict['a'] = 'b'
    result = freeze(test_dict)
    assert result == m(a='b')
    assert type(freeze({'a': 'b'})) is type(m())

def test_freeze_set():
    result = freeze(set([1, 2, 3]))
    assert result == s(1, 2, 3)
    assert type(result) is type(s())

def test_freeze_recurse_in_dictionary_values():
    result = freeze({'a': [1]})
    assert result == m(a=v(1))
    assert type(result['a']) is type(v())

def test_freeze_recurse_in_defaultdict_values():
    test_dict = collections.defaultdict(dict)
    test_dict['a'] = [1]
    result = freeze(test_dict)
    assert result == m(a=v(1))
    assert type(result['a']) is type(v())

def test_freeze_recurse_in_pmap_values():
    input = {'a': m(b={'c': 1})}
    result = freeze(input)
    # PMap and PVector are == to their mutable equivalents
    assert result == input
    assert type(result) is type(m())
    assert type(result['a']['b']) is type(m())

def test_freeze_recurse_in_lists():
    result = freeze(['a', {'b': 3}])
    assert result == v('a', m(b=3))
    assert type(result[1]) is type(m())

def test_freeze_recurse_in_pvectors():
    input = [1, v(2, [3])]
    result = freeze(input)
    # PMap and PVector are == to their mutable equivalents
    assert result == input
    assert type(result) is type(v())
    assert type(result[1][1]) is type(v())

def test_freeze_recurse_in_tuples():
    """Values in tuples are recursively frozen."""
    result = freeze(('a', {}))
    assert result == ('a', m())
    assert type(result[1]) is type(m())


## Freeze (weak)

def test_freeze_nonstrict_no_recurse_in_pmap_values():
    input = {'a': m(b={'c': 1})}
    result = freeze(input, strict=False)
    # PMap and PVector are == to their mutable equivalents
    assert result == input
    assert type(result) is type(m())
    assert type(result['a']['b']) is dict

def test_freeze_nonstrict_no_recurse_in_pvectors():
    input = [1, v(2, [3])]
    result = freeze(input, strict=False)
    # PMap and PVector are == to their mutable equivalents
    assert result == input
    assert type(result) is type(v())
    assert type(result[1][1]) is list


## Thaw

def test_thaw_basic():
    assert thaw(1) == 1
    assert thaw('foo') == 'foo'

def test_thaw_list():
    result = thaw(v(1, 2))
    assert result == [1, 2]
    assert type(result) is list

def test_thaw_dict():
    result = thaw(m(a='b'))
    assert result == {'a': 'b'}
    assert type(result) is dict

def test_thaw_set():
    result = thaw(s(1, 2))
    assert result == set([1, 2])
    assert type(result) is set

def test_thaw_recurse_in_mapping_values():
    result = thaw(m(a=v(1)))
    assert result == {'a': [1]}
    assert type(result['a']) is list

def test_thaw_recurse_in_dict_values():
    result = thaw({'a': v(1, m(b=2))})
    assert result == {'a': [1, {'b': 2}]}
    assert type(result['a']) is list
    assert type(result['a'][1]) is dict

def test_thaw_recurse_in_vectors():
    result = thaw(v('a', m(b=3)))
    assert result == ['a', {'b': 3}]
    assert type(result[1]) is dict

def test_thaw_recurse_in_lists():
    result = thaw(v(['a', m(b=1), v(2)]))
    assert result == [['a', {'b': 1}, [2]]]
    assert type(result[0]) is list
    assert type(result[0][1]) is dict

def test_thaw_recurse_in_tuples():
    result = thaw(('a', m()))
    assert result == ('a', {})
    assert type(result[1]) is dict

def test_thaw_can_handle_subclasses_of_persistent_base_types():
    class R(PRecord):
        x = field()

    result = thaw(R(x=1))
    assert result == {'x': 1}
    assert type(result) is dict


## Thaw (weak)

def test_thaw_non_strict_no_recurse_in_dict_values():
    result = thaw({'a': v(1, m(b=2))}, strict=False)
    assert result == {'a': [1, {'b': 2}]}
    assert type(result['a']) is type(v())
    assert type(result['a'][1]) is type(m())

def test_thaw_non_strict_no_recurse_in_lists():
    result = thaw(v(['a', m(b=1), v(2)]), strict=False)
    assert result == [['a', {'b': 1}, [2]]]
    assert type(result[0][1]) is type(m())

def test_mutant_decorator():
    @mutant
    def fn(a_list, a_dict):
        assert a_list == v(1, 2, 3)
        assert isinstance(a_dict, type(m()))
        assert a_dict == {'a': 5}

        return [1, 2, 3], {'a': 3}

    pv, pm = fn([1, 2, 3], a_dict={'a': 5})

    assert pv == v(1, 2, 3)
    assert pm == m(a=3)
    assert isinstance(pm, type(m()))