-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_oyaml.py
208 lines (156 loc) · 5.8 KB
/
test_oyaml.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from collections import OrderedDict
from types import GeneratorType
import pytest
from yaml.constructor import ConstructorError
from yaml.representer import RepresenterError
import oyaml as yaml
from oyaml import _std_dict_is_order_preserving
data = OrderedDict([("x", 1), ("z", 3), ("y", 2)])
def test_dump():
assert yaml.dump(data, default_flow_style=None) == "{x: 1, z: 3, y: 2}\n"
def test_safe_dump():
assert yaml.safe_dump(data, default_flow_style=None) == "{x: 1, z: 3, y: 2}\n"
def test_dump_all():
assert (
yaml.dump_all(documents=[data, {}], default_flow_style=None)
== "{x: 1, z: 3, y: 2}\n--- {}\n"
)
def test_dump_and_safe_dump_match():
mydict = {"x": 1, "z": 2, "y": 3}
# don't know if mydict is ordered in the implementation or not (but don't care)
assert yaml.dump(mydict) == yaml.safe_dump(mydict)
def test_safe_dump_all():
assert (
yaml.safe_dump_all(documents=[data, {}], default_flow_style=None)
== "{x: 1, z: 3, y: 2}\n--- {}\n"
)
def test_load():
loaded = yaml.safe_load("{x: 1, z: 3, y: 2}")
assert loaded == {"x": 1, "z": 3, "y": 2}
def test_safe_load():
loaded = yaml.safe_load("{x: 1, z: 3, y: 2}")
assert loaded == {"x": 1, "z": 3, "y": 2}
def test_load_all():
gen = yaml.safe_load_all("{x: 1, z: 3, y: 2}\n--- {}\n")
assert isinstance(gen, GeneratorType)
ordered_data, empty_dict = gen
assert empty_dict == {}
assert ordered_data == data
@pytest.mark.skipif(_std_dict_is_order_preserving, reason="requires old dict impl")
def test_loads_to_ordered_dict():
loaded = yaml.safe_load("{x: 1, z: 3, y: 2}")
assert isinstance(loaded, OrderedDict)
@pytest.mark.skipif(not _std_dict_is_order_preserving, reason="requires new dict impl")
def test_loads_to_std_dict():
loaded = yaml.safe_load("{x: 1, z: 3, y: 2}")
assert not isinstance(loaded, OrderedDict)
assert isinstance(loaded, dict)
@pytest.mark.skipif(_std_dict_is_order_preserving, reason="requires old dict impl")
def test_safe_loads_to_ordered_dict():
loaded = yaml.safe_load("{x: 1, z: 3, y: 2}")
assert isinstance(loaded, OrderedDict)
@pytest.mark.skipif(not _std_dict_is_order_preserving, reason="requires new dict impl")
def test_safe_loads_to_std_dict():
loaded = yaml.safe_load("{x: 1, z: 3, y: 2}")
assert not isinstance(loaded, OrderedDict)
assert isinstance(loaded, dict)
class MyOrderedDict(OrderedDict):
pass
def test_subclass_dump():
data = MyOrderedDict([("x", 1), ("y", 2)])
assert "!!python/object/apply:test_oyaml.MyOrderedDict" in yaml.dump(data)
with pytest.raises(RepresenterError, match="cannot represent an object"):
yaml.safe_dump(data)
def test_anchors_and_references():
text = """
defaults:
all: &all
product: foo
development: &development
<<: *all
profile: bar
development:
platform:
<<: *development
host: baz
"""
expected_load = {
"defaults": {
"all": {"product": "foo"},
"development": {"product": "foo", "profile": "bar"},
},
"development": {
"platform": {"host": "baz", "product": "foo", "profile": "bar"}
},
}
assert yaml.safe_load(text) == expected_load
def test_omap():
text = """
Bestiary: !!omap
- aardvark: African pig-like ant eater. Ugly.
- anteater: South-American ant eater. Two species.
- anaconda: South-American constrictor snake. Scaly.
"""
expected_load = {
"Bestiary": (
[
("aardvark", "African pig-like ant eater. Ugly."),
("anteater", "South-American ant eater. Two species."),
("anaconda", "South-American constrictor snake. Scaly."),
]
)
}
assert yaml.safe_load(text) == expected_load
def test_omap_flow_style():
text = "Numbers: !!omap [ one: 1, two: 2, three : 3 ]"
expected_load = {"Numbers": ([("one", 1), ("two", 2), ("three", 3)])}
assert yaml.safe_load(text) == expected_load
def test_merge():
text = """
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }
# All the following maps are equal:
- # Explicit keys
x: 1
y: 2
r: 10
label: center/big
- # Merge one map
<< : *CENTER
r: 10
label: center/big
- # Merge multiple maps
<< : [ *CENTER, *BIG ]
label: center/big
- # Override
<< : [ *BIG, *LEFT, *SMALL ]
x: 1
label: center/big
"""
data = yaml.safe_load(text)
assert len(data) == 8
center, left, big, small, map1, map2, map3, map4 = data
assert center == {"x": 1, "y": 2}
assert left == {"x": 0, "y": 2}
assert big == {"r": 10}
assert small == {"r": 1}
expected = {"x": 1, "y": 2, "r": 10, "label": "center/big"}
assert map1 == expected
assert map2 == expected
assert map3 == expected
assert map4 == expected
def test_unhashable_error_context():
with pytest.raises(ConstructorError, match=r".*line.*column.*"):
yaml.safe_load("{foo: bar}: baz")
@pytest.mark.skipif(not hasattr(yaml, "CSafeLoader"), reason="requires cyaml loaders")
def test_explicit_loader():
data = yaml.load("{x: 1, z: 3, y: 2}", Loader=yaml.CSafeLoader)
assert data == {"x": 1, "z": 3, "y": 2}
assert list(data) == ["x", "z", "y"]
@pytest.mark.skipif(not hasattr(yaml, "CSafeDumper"), reason="requires cyaml dumpers")
def test_explicit_dumper():
data = OrderedDict([("x", 1), ("z", 3), ("y", 2)])
text = yaml.dump(data, Dumper=yaml.CSafeDumper, default_flow_style=None)
assert text == "{x: 1, z: 3, y: 2}\n"