~chiefnoah/pybare

pybare/bare/test_encoder.py -rw-r--r-- 7.6 KiB
5609f3aeNoah Pederson Bumps version a month ago
                                                                                
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from .types import *

# TODO: fix import structure, structs should be somewhere else
from .encoder import Struct, Map, Array, _ValidatedMap, ValidationError, Optional, Union
from collections import OrderedDict
import pytest
import enum
import io
import os
import inspect


class Nested(Struct):
    s = Str()
    # a = Array(Int(), 3)


class Example(Struct):

    testint = Int()
    teststr = Str()
    testuint = U8()
    n = Nested()


def test_example_struct():
    n = Nested(s="nested")
    ex = Example(testint=11, teststr="a test", m={"test": 1}, n=n)
    assert hasattr(ex, "testint")
    assert hasattr(ex, "teststr")
    assert hasattr(ex, "n")
    assert hasattr(ex.n, "s")
    assert ex.testint == 11
    assert ex.teststr == "a test"
    assert ex.n.s == "nested"
    ex2 = Example(testint=12, teststr="another test")
    assert ex2.testint == 12
    assert ex2.teststr == "another test"
    # Check that the values in the original instance haven't been modified
    assert ex.testint == 11
    assert ex.teststr == "a test"

class C():
    s = Str()

def test_assign_value():
    o = C()
    o.s = "some string"
    assert getattr(o, 's') == 'some string'
    assert isinstance(inspect.getmembers(C)[-1][1], Str)

class ExampleMap(Map):
    _keytype = Str
    _valuetype = Str


class ExampleMapStruct(Struct):
    m = Map(Str, Int)
    m2 = Map(Int, Str)
    m3 = ExampleMap()


def test_map():
    ex = ExampleMapStruct()
    ex.m["test"] = 2
    ex.m2 = {0: "test"}
    ex.m3 = {"test": "test"}
    assert isinstance(ex.m, _ValidatedMap)
    assert isinstance(ex.m2, _ValidatedMap)
    assert isinstance(ex.m3, _ValidatedMap)
    assert ex.m2[0] == "test"
    assert ex.m["test"] == 2
    assert ex.m3["test"] == "test"
    with pytest.raises(ValidationError):
        ex.m2[0] = 0
    with pytest.raises(ValidationError):
        ex.m2 = {"test": "test"}
    with pytest.raises(ValidationError):
        ex.m3["3"] = 3
    map = Map(Str(), Str(), value={"test": "test"})
    assert map.value["test"] == "test"
    map2 = Map(Str, Str)
    map2.value["another"] = "test"
    assert map2.value["another"] == "test"


class ArrayTest(Struct):
    a = Array(Int)
    n = Array(Nested, length=1)


def test_array_struct():
    ex = ArrayTest()
    ex.a = [1, 2, 3]
    ex.n = [Nested(s="test")]
    assert ex.a == [1, 2, 3]
    with pytest.raises(ValidationError):
        ex.a = ["a", "b", "c"]
    Array(Int).validate([1, 2, 3])


class OptionalStruct(Struct):
    i = Int()
    s = Optional(Str)
    nested = Optional(Nested)


def test_optional():
    ex = OptionalStruct(i=1, s=None)
    assert ex.s is None
    assert ex.nested is None
    ex.s = "test"
    assert ex.s == "test"
    with pytest.raises(ValidationError):
        ex.s = 1
    ex.s = None
    assert ex.s is None

    ex.nested = Nested(s="test")
    assert ex.nested.s == "test"
    with pytest.raises(ValidationError):
        ex.nested = "test"


class ExampleUnion(Union):
    _members = (Str, Int)


class UnionTest(Struct):
    e = ExampleUnion()
    b = Union(members=(Str, Int))
    c = Union(members=(OptionalStruct, ArrayTest))


def test_union():
    ex = UnionTest(
        e=1, b="test", c=ArrayTest(a=[1], n=[Nested(s="s")])
    )  # MUST specify values for union types when creating an object
    assert ex.e == 1
    ex.e = "1"
    assert ex.e == "1"
    with pytest.raises(ValidationError):
        ex.e = {"test": "test"}
    b = ex.pack()
    ex2 = UnionTest.unpack(b)
    assert ex.e == ex.e
    assert ex.b == ex.b
    assert ex.c.a == [1]
    assert ex.c.n[0].s == "s"
    assert ex.c.a == [1]


class EnumTest(enum.Enum):
    TEST = 0
    TEST2 = 1


class EnumTestStruct(Struct):
    e = Enum(EnumTest)


def test_enum():
    ex = EnumTestStruct(e=0)
    assert ex.e == 0
    with pytest.raises(ValidationError):
        ex.e = 100


class PublicKey(DataFixed):
    _length = 128


class Time(Str):
    pass


class Department(enum.Enum):
    ACCOUNTING = 0
    ADMINISTRATION = 1
    CUSTOMER_SERVICE = 2
    DEVELOPMENT = 3

    JSMITH = 99


class Address(Struct):
    address = Array(Str, length=4)
    city = Str()
    state = Str()
    country = Str()


class Order(Struct):
    orderID = I64()
    quantity = I32()


class Customer(Struct):
    name = Str()
    email = Str()
    address = Address()
    orders = Array(Order)
    metadata = Map(Str, Data)


class Employee(Struct):
    name = Str()
    email = Str()
    address = Address()
    department = Enum(Department)
    hireDate = Time()
    publicKey = Optional(PublicKey)
    metadata = Map(Str, Data)


class TerminatedEmployee(Void):
    pass


class Person(Union):
    _members = (Customer, Employee, TerminatedEmployee)


@pytest.mark.parametrize(
    "file", ["customer.bin", "employee.bin", "people.bin", "terminated.bin"]
)
def test_people(file):
    with open(os.path.join(os.path.dirname(__file__), "_examples", file), "br") as f:
        people = []
        while True:
            try:
                p = Person().unpack(f)
                people.append(p)
            except RuntimeError:
                break
        f.seek(0)
        f = f.read()
        buf = io.BytesIO()
        for person in people:
            person.pack(buf)
        assert buf.getvalue() == f


def test_varint():
    expected = b"\x18"
    i = Int(value=12)
    assert i.pack() == expected

    i = Int(value=12345)
    expected = b"\xf2\xc0\x01"
    assert i.pack() == expected
    i = Int(value=-12345678)
    expected = b"\x9b\x85\xe3\x0b"
    assert i.pack() == expected


def test_uvarint():
    expected = b"\xce\xc2\xf1\x05"
    i = UInt(value=12345678)
    assert i.pack() == expected


def test_string():
    expected = b"\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67"
    s = Str(value="a test string")
    assert s.pack() == expected
    s = Str(value="")
    assert s.pack() == b"\x00"


@pytest.mark.parametrize(
    "value",
    [
        (
            Str(value="a test string"),
            b"\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67",
        ),
        (
            Int(value=12345678),
            b"\x9c\x85\xe3\x0b\x9c\x85\xe3\x0b\x9c\x85\xe3\x0b\x9c\x85\xe3\x0b",
        ),
    ],
)
def test_fixed_array(value):
    a = Array(type=value[0].__class__, length=4, values=[value[0]] * 4)
    assert a.pack() == value[1]


@pytest.mark.parametrize(
    "value",
    [
        (
            Str(value="a test string"),
            b"\x04\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67",
        ),
        (Int(value=123456), b"\x04\x80\x89\x0f\x80\x89\x0f\x80\x89\x0f\x80\x89\x0f"),
    ],
)
def test_array(value):
    a = Array(type=value[0].__class__, values=[value[0]] * 4)
    packed = a.pack()
    assert a.pack() == value[1]
    buf = io.BytesIO(packed)
    unpacked = a.unpack(buf)
    assert unpacked.to_dict() == a.to_dict()


class B(Struct):
    c = Int()


class X(Struct):
    a = Str()
    b = B()


def test_struct():
    s = X(a="a test string", b=B(c=12345))
    expected = b"\x0d\x61\x20\x74\x65\x73\x74\x20\x73\x74\x72\x69\x6e\x67\xf2\xc0\x01"
    assert s.pack() == expected
    buf = io.BytesIO(expected)
    unpacked = s.unpack(buf)
    assert unpacked.to_dict() == s.to_dict()


def test_map():
    expected = b"\x02\x04\x74\x65\x73\x74\x04\x74\x65\x73\x74\x07\x61\x6e\x6f\x74\x68\x65\x72\x04\x63\x61\x73\x65"
    m = Map(Str, Str, value={"test": "test", "another": "case"})
    assert m.pack() == expected