/
test_automatic_attributes.py
83 lines (53 loc) · 2.01 KB
/
test_automatic_attributes.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
# pylint: disable=unused-variable
from datafiles import datafile
from datafiles.utils import logbreak
@datafile('../tmp/{self.name}.yml', auto_attr=True, auto_save=False)
class Sample:
name: str
def describe_auto_attr():
def with_builtin(expect):
sample = Sample('abc')
sample.datafile.text = "count: 1"
logbreak("Getting attribute")
expect(sample.count) == 1
logbreak("Setting attribute")
sample.count = 4.2
logbreak("Getting attribute")
expect(sample.count) == 4
def with_empty_list(expect):
sample = Sample('abc')
sample.datafile.text = "empty_items: []"
logbreak("Getting attribute")
expect(sample.empty_items) == []
logbreak("Setting attribute")
sample.empty_items.append(4.2)
sample.empty_items.append("abc")
logbreak("Getting attribute")
expect(sample.empty_items) == [4.2, "abc"]
def with_homogeneous_list(expect):
sample = Sample('abc')
sample.datafile.text = "same_items: [1, 2]"
logbreak("Getting attribute")
expect(sample.same_items) == [1, 2]
logbreak("Setting attribute")
sample.same_items.append(3.2)
logbreak("Getting attribute")
expect(sample.same_items) == [1, 2, 3]
def with_heterogeneous_list(expect):
sample = Sample('abc')
sample.datafile.text = "mixed_items: [1, 'abc']"
logbreak("Getting attribute")
expect(sample.mixed_items) == [1, "abc"]
logbreak("Setting attribute")
sample.mixed_items.append(3.2)
logbreak("Getting attribute")
expect(sample.mixed_items) == [1, "abc", 3.2]
def with_dict(expect):
sample = Sample('abc')
sample.datafile.text = "data: {'a': 1}"
logbreak("Getting attribute")
expect(sample.data) == {'a': 1}
logbreak("Setting attribute")
sample.data['b'] = 2.3
logbreak("Getting attribute")
expect(sample.data) == {'a': 1, 'b': 2.3}