This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fieldmarshal.py
91 lines (59 loc) · 1.59 KB
/
test_fieldmarshal.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from fieldmarshal import Struct
import fieldmarshal
import json
from nose.tools import assert_equals
class Foo(Struct):
id = str
class Bar(Struct):
id = bool
class Baz(Struct):
id = int
class Bap(Struct):
id = None
class Nested(Struct):
foo = Foo
class SuperNested(Struct):
nested = Nested
class ArrayFoo(Struct):
foos = [Foo]
class HashFoo(Struct):
foos = dict
def test_array():
c = ArrayFoo()
assert_equals(len(c.foos), 0)
def test_hash():
c = HashFoo()
assert_equals(c.foos.items(), [])
def test_recursive_dumps():
c = Nested(foo = Foo(id = "foo"))
assert_equals(fieldmarshal.dumps(c), json.dumps({"foo": {"id": "foo"}}))
def test_recursive_load():
resp = json.dumps({"foo": {"id": "foo"}})
c = fieldmarshal.loads(Nested, resp)
assert_equals(c.foo.id, "foo")
def test_deeper_load():
resp = json.dumps({"nested": {"foo": {"id": "foo"}}})
c = fieldmarshal.loads(SuperNested, resp)
assert_equals(c.nested.foo.id, "foo")
def test_dumps():
c = Foo(id = "foo")
assert_equals(fieldmarshal.dumps(c), json.dumps({"id": "foo"}))
def test_zero_value_string():
c = Foo()
assert_equals(c.id, "")
def test_zero_value_none():
c = Bap()
assert_equals(c.id, None)
def test_zero_value_bool():
c = Bar()
assert_equals(c.id, False)
def test_zero_value_int():
c = Baz()
assert_equals(c.id, 0)
def test_zero_value_struct():
c = Nested()
assert_equals(c.foo.id, "")
def test_loads():
resp = json.dumps({"id": "foo"})
c = fieldmarshal.loads(Foo, resp)
assert_equals(c.id, "foo")