File: test_serializable.py

package info (click to toggle)
pyrlp 0.5.1-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 304 kB
  • sloc: python: 1,717; makefile: 234; sh: 16
file content (194 lines) | stat: -rw-r--r-- 5,945 bytes parent folder | download | duplicates (4)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from __future__ import unicode_literals

import pytest
from rlp import SerializationError
from rlp import infer_sedes, Serializable, encode, decode, make_immutable, make_mutable
from rlp.sedes import big_endian_int, binary, List


class Test1(Serializable):
    fields = [
        ('field1', big_endian_int),
        ('field2', binary),
        ('field3', List((big_endian_int, binary)))
    ]


class Test2(Serializable):
    fields = [
        ('field1', Test1),
        ('field2', List((Test1, Test1))),
    ]


def test_serializable():
    t1a_data = (5, 'a', (0, ''))
    t1b_data = (9, 'b', (2, ''))
    test1a = Test1(*t1a_data)
    test1b = Test1(*t1b_data)
    test2 = Test2(test1a, [test1a, test1b])

    # equality
    assert test1a == test1a
    assert test1b == test1b
    assert test2 == test2
    assert test1a != test1b
    assert test1b != test2
    assert test2 != test1a

    # mutability
    test1a.field1 += 1
    test1a.field2 = 'x'
    assert test1a.field1 == 6
    assert test1a.field2 == 'x'
    test1a.field1 -= 1
    test1a.field2 = 'a'
    assert test1a.field1 == 5
    assert test1a.field2 == 'a'

    # inference
    assert infer_sedes(test1a) == Test1
    assert infer_sedes(test1b) == Test1
    assert infer_sedes(test2) == Test2

    # serialization
    with pytest.raises(SerializationError):
        Test1.serialize(test2)
    with pytest.raises(SerializationError):
        Test2.serialize(test1a)
    with pytest.raises(SerializationError):
        Test2.serialize(test1b)
    serial_1a = Test1.serialize(test1a)
    serial_1b = Test1.serialize(test1b)
    serial_2 = Test2.serialize(test2)
    assert serial_1a == [b'\x05', b'a', [b'', b'']]
    assert serial_1b == [b'\x09', b'b', [b'\x02', b'']]
    assert serial_2 == [serial_1a, [serial_1a, serial_1b]]

    # deserialization
    test1a_d = Test1.deserialize(serial_1a)
    test1b_d = Test1.deserialize(serial_1b)
    test2_d = Test2.deserialize(serial_2, mutable=True)
    assert not test1a_d.is_mutable()
    assert not test1b_d.is_mutable()
    assert test2_d.is_mutable()
    for obj in (test1a_d, test1b_d):
        before1 = obj.field1
        before2 = obj.field2
        with pytest.raises(ValueError):
            obj.field1 += 1
        with pytest.raises(ValueError):
            obj.field2 = 'x'
        assert obj.field1 == before1
        assert obj.field2 == before2
    assert test1a_d == test1a
    assert test1b_d == test1b
    assert test2_d == test2

    # encoding and decoding
    for obj in (test1a, test1b, test2):
        rlp_code = encode(obj)
        assert obj._cached_rlp is None
        assert obj.is_mutable()

        assert encode(obj, cache=True) == rlp_code
        assert obj._cached_rlp == rlp_code
        assert not obj.is_mutable()

        assert encode(obj, cache=True) == rlp_code
        assert obj._cached_rlp == rlp_code
        assert not obj.is_mutable()

        assert encode(obj) == rlp_code
        assert obj._cached_rlp == rlp_code
        assert not obj.is_mutable()

        obj_decoded = decode(rlp_code, obj.__class__)
        assert obj_decoded == obj
        assert not obj_decoded.is_mutable()
        assert obj_decoded._cached_rlp == rlp_code


def test_make_immutable():
    assert make_immutable(1) == 1
    assert make_immutable('a') == 'a'
    assert make_immutable((1, 2, 3)) == (1, 2, 3)
    assert make_immutable([1, 2, 'a']) == (1, 2, 'a')
    assert make_immutable([[1], [2, [3], 4], 5, 6]) == ((1,), (2, (3,), 4), 5, 6)

    t1a_data = (5, 'a', (0, ''))
    t1b_data = (9, 'b', (2, ''))
    test1a = Test1(*t1a_data)
    test1b = Test1(*t1b_data)
    test2 = Test2(test1a, [test1a, test1b])

    assert test2.is_mutable()
    assert test2.field1.is_mutable()
    assert test2.field2[0].is_mutable()
    assert test2.field2[1].is_mutable()
    test2.make_immutable()
    assert not test2.is_mutable()
    assert not test1a.is_mutable()
    assert not test1b.is_mutable()
    assert test2.field1 == test1a
    assert test2.field2 == (test1a, test1b)

    test1a = Test1(*t1a_data)
    test1b = Test1(*t1b_data)
    test2 = Test2(test1a, [test1a, test1b])
    assert test2.is_mutable()
    assert test2.field1.is_mutable()
    assert test2.field2[0].is_mutable()
    assert test2.field2[1].is_mutable()
    assert make_immutable([test1a, [test2, test1b]]) == (test1a, (test2, test1b))
    assert not test2.is_mutable()
    assert not test1a.is_mutable()
    assert not test1b.is_mutable()


def test_make_mutable():
    assert make_mutable(1) == 1
    assert make_mutable('a') == 'a'
    assert make_mutable((1, 2, 3)) == [1, 2, 3]
    assert make_mutable([1, 2, 'a']) == [1, 2, 'a']
    assert make_mutable([[1], [2, [3], 4], 5, 6]) == [[1,], [2, [3,], 4], 5, 6]

    t1a_data = (5, 'a', (0, ''))
    t1b_data = (9, 'b', (2, ''))
    test1a = Test1(*t1a_data)
    test1b = Test1(*t1b_data)
    test2 = Test2(test1a, [test1a, test1b])

    test1a.make_immutable()
    test1b.make_immutable()
    test2.make_immutable()

    assert not test2.is_mutable()
    assert not test2.field1.is_mutable()
    assert not test2.field2[0].is_mutable()
    assert not test2.field2[1].is_mutable()
    test2.make_mutable()
    assert test2.is_mutable()
    assert test2.field2[0].is_mutable()
    assert test2.field2[1].is_mutable()
    assert test1a.is_mutable()
    assert test1b.is_mutable()
    assert test2.field1 == test1a
    assert test2.field2 == [test1a, test1b]

    test1a = Test1(*t1a_data)
    test1b = Test1(*t1b_data)
    test2 = Test2(test1a, [test1a, test1b])

    test1a.make_immutable()
    test1b.make_immutable()
    test2.make_immutable()

    assert not test2.is_mutable()
    assert not test2.field1.is_mutable()
    assert not test2.field2[0].is_mutable()
    assert not test2.field2[1].is_mutable()
    assert make_mutable([test1a, [test2, test1b]]) == [test1a, [test2, test1b]]
    assert test2.is_mutable()
    assert test1a.is_mutable()
    assert test1b.is_mutable()