-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_examples.py
194 lines (153 loc) · 4.72 KB
/
test_examples.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
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
"""
Tests for example generation.
"""
from __future__ import unicode_literals
from textwrap import dedent
import pytest
from yaml import safe_load
from traitlets import TraitError
from straitlets.serializable import Serializable
from straitlets.test_utils import assert_serializables_equal, multifixture
from straitlets.traits import (
Bool,
Dict,
Instance,
Integer,
Unicode,
)
class Point(Serializable):
x = Integer().example(100)
y = Integer().example(101)
class ExampleClass(Serializable):
bool_tag = Bool().example(True)
bool_default = Bool(default_value=False)
bool_both = Bool(default_value=False).example(True)
int_tag = Integer().example(1)
int_default = Integer(default_value=2)
int_both = Integer(default_value=100).example(3)
dict_tag = Dict().example({'a': 'b'})
dict_default = Dict(default_value={'c': 'd'})
dict_both = Dict(default_value={'z': 'z'}).example({'e': 'f', 'g': 'h'})
instance_tag = Instance(Point).example(Point(x=0, y=1))
instance_default = Instance(Point, default_value=Point(x=2, y=3))
instance_both = Instance(Point, default_value=Point(x=4, y=5)).example(
Point(x=6, y=7),
)
# Instance provides a default example if the value is a Serializable
# that can provide an example.
instance_neither = Instance(Point)
@pytest.fixture
def expected_instance():
return ExampleClass(
bool_tag=True,
bool_default=False,
bool_both=True,
int_tag=1,
int_default=2,
int_both=3,
dict_tag={'a': 'b'},
dict_default={'c': 'd'},
dict_both={'e': 'f', 'g': 'h'},
instance_tag=Point(x=0, y=1),
instance_default=Point(x=2, y=3),
instance_both=Point(x=6, y=7),
instance_neither=Point(x=100, y=101),
)
@pytest.fixture
def expected_yaml():
return dedent(
"""
bool_tag: true
bool_default: false
bool_both: true
int_tag: 1
int_default: 2
int_both: 3
dict_tag:
a: b
dict_default:
c: d
dict_both:
e: f
g: h
instance_tag:
x: 0
y: 1
instance_default:
x: 2
y: 3
instance_both:
x: 6
y: 7
instance_neither:
x: 100
y: 101
"""
)
@multifixture
def skip_names():
yield ()
yield ('bool_tag',)
yield ('bool_tag', 'int_tag')
yield ('dict_tag', 'instance_tag', 'instance_neither')
yield [
name for name in ExampleClass.class_trait_names()
if name.endswith('tag')
]
def test_example_instance(expected_yaml, expected_instance):
instance = ExampleClass.example_instance()
assert_serializables_equal(instance, expected_instance)
assert_serializables_equal(instance, ExampleClass.from_yaml(expected_yaml))
assert ExampleClass.example_yaml() == expected_instance.to_yaml()
def test_example_skip_names(expected_instance, skip_names):
instance = ExampleClass.example_instance(skip=skip_names)
assert_serializables_equal(instance, expected_instance, skip=skip_names)
for name in skip_names:
with pytest.raises(TraitError):
getattr(instance, name)
def test_write_example_yaml(tmpdir, expected_instance, skip_names):
path = tmpdir.join("test.yaml").strpath
ExampleClass.write_example_yaml(path, skip=skip_names)
from_file = ExampleClass.from_yaml_file(path)
assert_serializables_equal(
from_file,
expected_instance,
skip=skip_names,
)
for name in skip_names:
with pytest.raises(TraitError):
getattr(from_file, name)
def test_nested_example():
class C(Serializable):
point = Instance(Point)
unicode_ = Unicode().tag(example='foo')
class B(Serializable):
value = Integer().tag(example=ord('b'))
next_ = Instance(C)
class A(Serializable):
value = Integer().tag(example=ord('a'))
next_ = Instance(B)
expected = A(
value=ord('a'),
next_=B(
value=ord('b'),
next_=C(
point=Point.example_instance(),
unicode_='foo',
),
),
)
assert_serializables_equal(expected, A.example_instance())
def test_readme_example():
class Point(Serializable):
x = Integer().example(0)
y = Integer().example(0)
class Vector(Serializable):
head = Instance(Point)
tail = Instance(Point).example(Point(x=1, y=3))
example_yaml = Vector.example_instance().to_yaml()
expected = {
"head": {"x": 0, "y": 0},
"tail": {"x": 1, "y": 3},
}
assert safe_load(example_yaml) == expected