forked from Thriftpy/thriftpy2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_base.py
77 lines (48 loc) · 1.94 KB
/
test_base.py
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
# -*- coding: utf-8 -*-
import linecache
import pytest
import thriftpy2
from thriftpy2.thrift import parse_spec, TType
def test_obj_equalcheck():
ab = thriftpy2.load("addressbook.thrift")
ab2 = thriftpy2.load("addressbook.thrift")
assert ab.Person(name="hello") == ab2.Person(name="hello")
def test_exc_equalcheck():
ab = thriftpy2.load("addressbook.thrift")
assert ab.PersonNotExistsError("exc") != ab.PersonNotExistsError("exc")
def test_cls_equalcheck():
ab = thriftpy2.load("addressbook.thrift")
ab2 = thriftpy2.load("addressbook.thrift")
assert ab.Person == ab2.Person
def test_isinstancecheck():
ab = thriftpy2.load("addressbook.thrift")
ab2 = thriftpy2.load("addressbook.thrift")
assert isinstance(ab.Person(), ab2.Person)
assert isinstance(ab.Person(name="hello"), ab2.Person)
assert isinstance(ab.PersonNotExistsError(), ab2.PersonNotExistsError)
def test_hashable():
ab = thriftpy2.load("addressbook.thrift")
# exception is hashable
hash(ab.PersonNotExistsError("test error"))
# container struct is not hashable
with pytest.raises(TypeError):
hash(ab.Person(name="Tom"))
def test_default_value():
ab = thriftpy2.load("addressbook.thrift")
assert ab.PhoneNumber().type == ab.PhoneType.MOBILE
def test_parse_spec():
ab = thriftpy2.load("addressbook.thrift")
cases = [
((TType.I32, None), "I32"),
((TType.STRUCT, ab.PhoneNumber), "PhoneNumber"),
((TType.LIST, TType.I32), "LIST<I32>"),
((TType.LIST, (TType.STRUCT, ab.PhoneNumber)), "LIST<PhoneNumber>"),
((TType.MAP, (TType.STRING, (
TType.LIST, (TType.MAP, (TType.STRING, TType.STRING))))),
"MAP<STRING, LIST<MAP<STRING, STRING>>>")
]
for spec, res in cases:
assert parse_spec(*spec) == res
def test_init_func():
thriftpy2.load("addressbook.thrift")
assert linecache.getline('<generated PhoneNumber.__init__>', 1) != ''