-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_annotation_functional.py
More file actions
255 lines (204 loc) · 5.88 KB
/
test_annotation_functional.py
File metadata and controls
255 lines (204 loc) · 5.88 KB
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
"""Functional test of 'dtool annotation' CLI command."""
from click.testing import CliRunner
from . import tmp_dataset_fixture # NOQA
def test_annotation_basic(tmp_dataset_fixture): # NOQA
from dtool_annotation.cli import annotation
runner = CliRunner()
result = runner.invoke(annotation, [
"set",
tmp_dataset_fixture.uri,
"project",
"world-peace"
])
assert result.exit_code == 0
result = runner.invoke(annotation, [
"get",
tmp_dataset_fixture.uri,
"project"
])
assert result.exit_code == 0
expected = "world-peace"
actual = result.output.strip()
assert actual == expected
result = runner.invoke(annotation, [
"delete",
tmp_dataset_fixture.uri,
"project"
])
assert result.exit_code == 0
result = runner.invoke(annotation, [
"get",
tmp_dataset_fixture.uri,
"project"
])
assert result.exit_code != 0
expected = "No annotation named: 'project'"
actual = result.output.strip()
assert actual == expected
def test_annotation_invalid_name(tmp_dataset_fixture): # NOQA
from dtool_annotation.cli import annotation
runner = CliRunner()
# Spaces, slashes, etc are not allowed.
result = runner.invoke(annotation, [
"set",
tmp_dataset_fixture.uri,
"project name",
"world-peace"
])
assert result.exit_code == 400
expected_lines = [
"Invalid annotation name 'project name'",
"Name must be 80 characters or less",
"Names may only contain the characters: 0-9 a-z A-Z - _ .",
"Example: citation-index",
]
for line in expected_lines:
assert result.output.find(line) != -1
def test_get_non_existing_annotation(tmp_dataset_fixture): # NOQA
from dtool_annotation.cli import annotation
runner = CliRunner()
result = runner.invoke(annotation, [
"get",
tmp_dataset_fixture.uri,
"project",
])
assert result.exit_code == 401
expected = "No annotation named: 'project'"
assert result.output.strip() == expected
def test_delete_non_existing_annotation(tmp_dataset_fixture): # NOQA
from dtool_annotation.cli import annotation
runner = CliRunner()
result = runner.invoke(annotation, [
"delete",
tmp_dataset_fixture.uri,
"project",
])
assert result.exit_code == 0
def test_annotation_types(tmp_dataset_fixture): # NOQA
from dtool_annotation.cli import annotation
runner = CliRunner()
# Default to string
result = runner.invoke(annotation, [
"set",
tmp_dataset_fixture.uri,
"one_as_str",
"1"
])
assert result.exit_code == 0
assert tmp_dataset_fixture.get_annotation("one_as_str") == "1"
# Explicit set to string.
result = runner.invoke(annotation, [
"set",
"--type",
"str",
tmp_dataset_fixture.uri,
"one_as_str_explicit",
"1"
])
assert result.exit_code == 0
assert tmp_dataset_fixture.get_annotation("one_as_str_explicit") == "1"
# Explicit set to int.
result = runner.invoke(annotation, [
"set",
"--type",
"int",
tmp_dataset_fixture.uri,
"one_as_int_explicit",
"1"
])
assert result.exit_code == 0
assert tmp_dataset_fixture.get_annotation("one_as_int_explicit") == 1
# Explicit set to float.
result = runner.invoke(annotation, [
"set",
"--type",
"float",
tmp_dataset_fixture.uri,
"one_as_float_explicit",
"1"
])
assert result.exit_code == 0
ann = tmp_dataset_fixture.get_annotation("one_as_float_explicit")
offset = 0.00000001
assert ann > 1 - offset
assert ann < 1 + offset
assert isinstance(ann, float)
# Explicit set to bool.
result = runner.invoke(annotation, [
"set",
"--type",
"bool",
tmp_dataset_fixture.uri,
"true_as_bool_explicit",
"1"
])
assert result.exit_code == 0
ann = tmp_dataset_fixture.get_annotation("true_as_bool_explicit")
assert ann
assert isinstance(ann, bool)
result = runner.invoke(annotation, [
"set",
"--type",
"bool",
tmp_dataset_fixture.uri,
"false_as_bool_explicit",
"0"
])
assert result.exit_code == 0
ann = tmp_dataset_fixture.get_annotation("false_as_bool_explicit")
assert not ann
assert isinstance(ann, bool)
# Explicit set to json.
result = runner.invoke(annotation, [
"set",
"--type",
"json",
tmp_dataset_fixture.uri,
"json_explicit",
'{"x": 3, "y": 5}'
])
assert result.exit_code == 0
ann = tmp_dataset_fixture.get_annotation("json_explicit")
assert ann == {"x": 3, "y": 5}
def test_ls_command(tmp_dataset_fixture): # NOQA
from dtool_annotation.cli import annotation
runner = CliRunner()
result = runner.invoke(annotation, [
"set",
tmp_dataset_fixture.uri,
"project",
"world-peace"
])
assert result.exit_code == 0
result = runner.invoke(annotation, [
"set",
tmp_dataset_fixture.uri,
"stars",
"3",
"--type",
"int"
])
assert result.exit_code == 0
result = runner.invoke(annotation, [
"set",
tmp_dataset_fixture.uri,
"params",
'{"x": 3}',
"--type",
"json"
])
assert result.exit_code == 0
result = runner.invoke(annotation, [
"ls",
tmp_dataset_fixture.uri,
])
assert result.exit_code == 0
expectations = [
("params", "x': 3}"),
("project", "world-peace"),
("stars", "3")
]
for e, a in zip(expectations, result.output.strip().split("\n")):
assert a.count("\t") == 1
assert a.startswith(e[0])
assert a.endswith(e[1])