This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
/
test_all.py
327 lines (266 loc) · 8.77 KB
/
test_all.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
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
#!/usr/bin/env python
# pylint:disable=R0201,C0111
"""Integration tests for the package."""
import pytest
from yorm import sync
from yorm.converters import Object, String, Integer, Float, Boolean
from yorm.converters import Markdown
from . import strip, refresh_file_modification_times
from .samples import * # pylint: disable=W0401,W0614
class TestStandard:
"""Integration tests for standard attribute types."""
class EmptyDictionary(Dictionary):
pass
def test_decorator(self, tmpdir):
"""Verify standard attribute types dump/load correctly (decorator)."""
tmpdir.chdir()
sample = SampleStandardDecorated('sample')
assert "path/to/default/sample.yml" == sample.yorm_mapper.path
# check defaults
assert {} == sample.object
assert [] == sample.array
assert "" == sample.string
assert 0 == sample.number_int
assert 0.0 == sample.number_real
assert True is sample.true
assert False is sample.false
assert None is sample.null
# change object values
sample.object = {'key2': 'value'}
sample.array = [0, 1, 2]
sample.string = "Hello, world!"
sample.number_int = 42
sample.number_real = 4.2
sample.true = False
sample.false = True
# check file values
assert strip("""
array:
- 0
- 1
- 2
'false': true
number_int: 42
number_real: 4.2
object:
key2: value
string: Hello, world!
'true': false
""") == sample.yorm_mapper.text
# change file values
refresh_file_modification_times()
sample.yorm_mapper.text = strip("""
array: [4, 5, 6]
'false': null
number_int: 42
number_real: '4.2'
object: {'status': false}
string: "abc"
'true': null
""")
# check object values
assert {'key2': "",
'status': False} == sample.object
assert [4, 5, 6] == sample.array
assert "abc" == sample.string
assert 42 == sample.number_int
assert 4.2 == sample.number_real
assert False is sample.true
assert False is sample.false
def test_function(self, tmpdir):
"""Verify standard attribute types dump/load correctly (function)."""
tmpdir.chdir()
_sample = SampleStandard()
attrs = {'object': self.EmptyDictionary,
'array': IntegerList,
'string': String,
'number_int': Integer,
'number_real': Float,
'true': Boolean,
'false': Boolean}
sample = sync(_sample, "path/to/directory/sample.yml", attrs)
assert "path/to/directory/sample.yml" == sample.yorm_mapper.path
# check defaults
assert {} == sample.object
assert [] == sample.array
assert "" == sample.string
assert 0 == sample.number_int
assert 0.0 == sample.number_real
assert True is sample.true
assert False is sample.false
assert None is sample.null
# change object values
sample.object = {'key': 'value'}
sample.array = [1, 2, 3]
sample.string = "Hello, world!"
sample.number_int = 42
sample.number_real = 4.2
sample.true = None
sample.false = 1
# check file values
assert strip("""
array:
- 1
- 2
- 3
'false': true
number_int: 42
number_real: 4.2
object:
key: value
string: Hello, world!
'true': false
""") == sample.yorm_mapper.text
def test_auto_off(self, tmpdir):
"""Verify file updates are disabled with auto off."""
tmpdir.chdir()
sample = SampleDecoratedAutoOff()
# check for default values
assert strip("""
string: ''
""") == sample.yorm_mapper.text
# store value
sample.string = "hello"
# check for unchanged file values
assert strip("""
string: ''
""") == sample.yorm_mapper.text
# enable auto and store value
sample.yorm_mapper.auto = True
sample.string = "world"
# check for changed file values
assert strip("""
string: world
""") == sample.yorm_mapper.text
class TestContainers:
"""Integration tests for attribute containers."""
def test_nesting(self, tmpdir):
"""Verify standard attribute types can be nested."""
tmpdir.chdir()
_sample = SampleNested()
attrs = {'count': Integer,
'results': StatusDictionaryList}
sample = sync(_sample, "sample.yml", attrs)
# check defaults
assert 0 == sample.count
assert [] == sample.results
# change object values
sample.count = 5
sample.results = [{'status': False, 'label': "abc"},
{'status': None, 'label': None},
{'label': "def"},
{'status': True},
{}]
# check file values
assert strip("""
count: 5
results:
- label: abc
status: false
- label: ''
status: false
- label: def
status: false
- label: ''
status: true
- label: ''
status: false
""") == sample.yorm_mapper.text
# change file values
refresh_file_modification_times()
sample.yorm_mapper.text = strip("""
count: 3
other: 4.2
results:
- label: abc
- label: null
status: false
- status: true
""")
# check object values
assert 3 == sample.count
assert 4.2 == sample.other
assert [{'label': 'abc', 'status': False},
{'label': '', 'status': False},
{'label': '', 'status': True}] == sample.results
def test_objects(self, tmpdir):
"""Verify containers are treated as objects when added."""
tmpdir.chdir()
sample = SampleEmptyDecorated()
# change file values
refresh_file_modification_times()
sample.yorm_mapper.text = strip("""
object: {'key': 'value'}
array: [1, '2', '3.0']
""")
# (a mapped attribute must be read first to trigger retrieving)
sample.yorm_mapper.fetch()
# check object values
assert {'key': 'value'} == sample.object
assert [1, '2', '3.0'] == sample.array
# check object types
assert Object == sample.yorm_mapper.attrs['object']
assert Object == sample.yorm_mapper.attrs['array']
# change object values
sample.object = None # pylint: disable=W0201
sample.array = "abc" # pylint: disable=W0201
# check file values
assert strip("""
array: abc
object: null
""") == sample.yorm_mapper.text
class TestExtended:
"""Integration tests for extended attribute types."""
def test_function(self, tmpdir):
"""Verify extended attribute types dump/load correctly."""
tmpdir.chdir()
_sample = SampleExtended()
attrs = {'text': Markdown}
sample = sync(_sample, "path/to/directory/sample.yml", attrs)
# check defaults
assert "" == sample.text
# change object values
refresh_file_modification_times()
sample.text = strip("""
This is the first sentence. This is the second sentence.
This is the third sentence.
""")
# check file values
assert strip("""
text: |
This is the first sentence.
This is the second sentence.
This is the third sentence.
""") == sample.yorm_mapper.text
# change file values
refresh_file_modification_times()
sample.yorm_mapper.text = strip("""
text: |
This is a
sentence.
""")
# check object values
assert "This is a sentence." == sample.text
class TestCustom:
"""Integration tests for custom attribute types."""
def test_decorator(self, tmpdir):
"""Verify custom attribute types dump/load correctly."""
tmpdir.chdir()
sample = SampleCustomDecorated('sample')
# check defaults
assert '1.0' == sample.level
# change values
sample.level = '1.2.3'
# check file values
assert strip("""
level: 1.2.3
""") == sample.yorm_mapper.text
# change file values
refresh_file_modification_times()
sample.yorm_mapper.text = strip("""
level: 1
""")
# check object values
assert '1' == sample.level
if __name__ == '__main__':
pytest.main()