-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_setup.py
215 lines (162 loc) · 5.94 KB
/
test_setup.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
"""Tests for setting up a new class."""
# pylint: disable=unused-variable
import sys
from dataclasses import dataclass
from pathlib import Path
import pytest
import datafiles
from datafiles import datafile
def describe_automatic():
"""Test creating a datafile using the decorator."""
@pytest.fixture
def sample():
@datafile('../tmp/{self.key}.yml')
class Sample:
key: int
name: str
score: float = 1 / 2
return Sample(1, "a")
def it_inferrs_attrs(expect, sample):
expect(sample.datafile.attrs) == {
'name': datafiles.converters.String,
'score': datafiles.converters.Float,
}
def it_formats_path_from_pattern(expect, sample):
root = Path(__file__).parents[1]
expect(sample.datafile.path) == root / 'tmp' / '1.yml'
def it_converts_attributes(expect, sample):
expect(sample.key) == 1
expect(sample.name) == "a"
expect(sample.score) == 0.5
expect(sample.datafile.data) == {'name': "a"}
def describe_automatic_with_defaults():
"""Test creating a datafile using the decorator with defaults."""
def describe_flat():
@pytest.fixture
def sample():
@datafile('../tmp/{self.key}.yml', defaults=True)
class Sample:
key: int
name: str
score: float = 1 / 2
return Sample(1, "a")
def it_converts_attributes(expect, sample):
expect(sample.datafile.data) == {'name': "a", 'score': 0.5}
def describe_nested():
@pytest.fixture
def sample():
@dataclass
class Nested:
name: str
score: float = 1 / 4
@datafile('../tmp/{self.key}.yml', defaults=True)
class Sample:
key: int
nested: Nested
name: str
score: float = 1 / 2
return Sample(1, Nested(name="b"), "a")
def it_converts_attributes(expect, sample):
expect(sample.datafile.data) == {
'name': "a",
'score': 0.5,
'nested': {'name': "b", 'score': 0.25},
}
def describe_nested_override():
@pytest.fixture
def sample():
@dataclass
class Nested:
name: str
score: float = 1 / 4
class Meta:
datafile_defaults = False
@datafile('../tmp/{self.key}.yml', defaults=True)
class Sample:
key: int
nested: Nested
name: str
score: float = 1 / 2
return Sample(1, Nested(name="b"), "a")
@pytest.mark.xfail(reason='https://github.com/jacebrowning/datafiles/issues/64')
def it_converts_attributes(expect, sample):
expect(sample.datafile.data) == {
'name': "a",
'score': 0.5,
'nested': {'name': "b"},
}
def describe_manual():
"""Test creating a datafile using the model class."""
@pytest.fixture
def sample():
@dataclass
class Sample(datafiles.Model):
key: int
name: str
score: float = 1 / 4
return Sample(2, "b")
def it_inferrs_attrs(expect, sample):
expect(sample.datafile.attrs) == {
'key': datafiles.converters.Integer,
'name': datafiles.converters.String,
'score': datafiles.converters.Float,
}
def it_has_no_path(expect, sample):
expect(sample.datafile.path).is_(None)
def it_converts_attributes(expect, sample):
expect(sample.key) == 2
expect(sample.name) == "b"
expect(sample.score) == 0.25
expect(sample.datafile.data) == {'key': 2, 'name': "b"}
def describe_manual_with_attrs():
"""Test creating a datafile with explicit attrs."""
@pytest.fixture
def sample():
@dataclass
class Sample(datafiles.Model):
key: int
name: str
score: float = 1 / 8
extra: bool = True
class Meta:
datafile_attrs = {'name': datafiles.converters.String}
return Sample(3, "c")
def it_uses_attrs_from_meta(expect, sample):
expect(sample.datafile.attrs) == {'name': datafiles.converters.String}
def it_has_no_path(expect, sample):
expect(sample.datafile.path).is_(None)
def it_converts_attributes(expect, sample):
expect(sample.datafile.data) == {'name': "c"}
def describe_manual_with_attrs_and_pattern():
"""Test creating a datafile with explicit attrs and pattern."""
@pytest.fixture
def sample():
@dataclass
class Sample(datafiles.Model):
key: int
name: str
score: float = 1 / 16
class Meta:
datafile_attrs = {'name': datafiles.converters.String}
datafile_pattern = '../tmp/{self.key}.yml'
return Sample(4, "d")
def it_uses_attrs_from_meta(expect, sample):
expect(sample.datafile.attrs) == {'name': datafiles.converters.String}
def it_formats_path_from_pattern(expect, sample):
root = Path(__file__).parents[1]
expect(sample.datafile.path) == root / 'tmp' / '4.yml'
def it_converts_attributes(expect, sample):
expect(sample.datafile.data) == {'name': "d"}
def describe_absolute_pattern():
"""Test creating a datafile using the decorator with an absolute path."""
@pytest.fixture
def sample():
@datafile('/private/tmp/{self.key}.yml')
class Sample:
key: int
name: str
score: float = 1 / 2
return Sample(5, "a")
@pytest.mark.skipif(sys.platform != 'darwin', reason="Test only valid on macOS")
def it_formats_path_from_pattern(expect, sample):
expect(sample.datafile.path) == Path('/private/tmp') / '5.yml'