forked from facebookresearch/PyTorch-BigGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_schema.py
419 lines (351 loc) · 13 KB
/
test_schema.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE.txt file in the root directory of this source tree.
from enum import Enum
from typing import ClassVar, Dict, List, Optional, Union
from unittest import TestCase, main
import attr
from torchbiggraph.schema import (
Dumper,
Loader,
Schema,
extract_nested_type,
inject_nested_value,
schema,
unpack_optional,
)
class TestUnpackOptional(TestCase):
def test_has_no_args(self):
with self.assertRaises(TypeError):
unpack_optional(None)
with self.assertRaises(TypeError):
unpack_optional(42)
with self.assertRaises(TypeError):
unpack_optional("foo")
with self.assertRaises(TypeError):
unpack_optional(bool)
with self.assertRaises(TypeError):
unpack_optional(float)
def test_is_no_union(self):
with self.assertRaises(TypeError):
unpack_optional(List[int])
with self.assertRaises(TypeError):
unpack_optional(Dict[str, str])
def test_is_no_optional(self):
with self.assertRaises(TypeError):
unpack_optional(Union[int]) # In fact this is int.
with self.assertRaises(TypeError):
unpack_optional(Union[int, str])
def test_is_optional(self):
self.assertIs(unpack_optional(Optional[int]), int)
self.assertIs(unpack_optional(Optional[str]), str)
class SampleEnum(Enum):
SPAM = "spam"
SPAM_ALIAS = "spam"
HAM = "bacon"
EGGS = "eggs"
@schema
class SampleConfigTypes(Schema):
NAME: ClassVar[str] = "my_types"
my_bool: bool = attr.ib()
my_int: int = attr.ib()
my_float: float = attr.ib()
my_str: str = attr.ib()
my_enum: SampleEnum = attr.ib()
my_optional_str: Optional[str] = attr.ib()
my_list: List[str] = attr.ib()
my_dict: Dict[str, int] = attr.ib()
@schema
class SampleConfigDefault(Schema):
NAME: ClassVar[str] = "my_default"
my_default: int = attr.ib(
default=1,
)
@schema
class SampleConfigHelp(Schema):
NAME: ClassVar[str] = "my_help"
my_help_text: str = attr.ib(
metadata={'help': "Pass a nice value here!"},
)
@schema
class SampleOuterConfig(Schema):
NAME: ClassVar[str] = "my_outer_name"
my_schema: Optional[SampleConfigTypes] = attr.ib()
my_list_of_schemas: List[SampleConfigDefault] = attr.ib()
my_dict_of_schemas: Dict[str, SampleConfigHelp] = attr.ib()
class BaseMapperMixin:
def test_map_bool(self):
self.assertIs(self.mapper.map_with_type(False, bool), False)
self.assertIs(self.mapper.map_with_type(True, bool), True)
with self.assertRaises(TypeError):
self.mapper.map_with_type(None, bool)
with self.assertRaises(TypeError):
self.mapper.map_with_type(0xf00, bool)
def test_map_int(self):
self.assertEqual(self.mapper.map_with_type(0xf00, int), 0xf00)
with self.assertRaises(TypeError):
self.mapper.map_with_type(None, int)
with self.assertRaises(TypeError):
self.mapper.map_with_type("foo", int)
def test_map_float(self):
self.assertEqual(self.mapper.map_with_type(4.2, float), 4.2)
with self.assertRaises(TypeError):
self.mapper.map_with_type(None, float)
with self.assertRaises(TypeError):
self.mapper.map_with_type("foo", float)
def test_map_str(self):
self.assertEqual(self.mapper.map_with_type("foo", str), "foo")
with self.assertRaises(TypeError):
self.mapper.map_with_type(None, str)
with self.assertRaises(TypeError):
self.mapper.map_with_type(0xf00, str)
def test_map_optional_str(self):
self.assertEqual(self.mapper.map_with_type(None, Optional[str]), None)
self.assertEqual(self.mapper.map_with_type("foo", Optional[str]), "foo")
with self.assertRaises(TypeError):
self.mapper.map_with_type(0xf00, Optional[str])
def test_map_list_of_basic(self):
self.assertEqual(self.mapper.map_with_type([], List[str]), [])
self.assertEqual(
self.mapper.map_with_type(["foo", "bar"], List[str]),
["foo", "bar"],
)
with self.assertRaises(TypeError):
self.mapper.map_with_type("[foo]", List[str])
with self.assertRaises(TypeError):
self.mapper.map_with_type(["foo", 0xba2], List[str])
def test_map_dict_of_basic(self):
self.assertEqual(self.mapper.map_with_type({}, Dict[str, int]), {})
self.assertEqual(
self.mapper.map_with_type({"foo": 42}, Dict[str, int]),
{"foo": 42},
)
with self.assertRaises(TypeError):
self.mapper.map_with_type("{}", Dict[str, int])
with self.assertRaises(TypeError):
self.mapper.map_with_type({0xf00: 4.2}, Dict[str, int])
with self.assertRaises(TypeError):
self.mapper.map_with_type({"foo": "bar"}, Dict[str, int])
class TestLoader(BaseMapperMixin, TestCase):
def setUp(self):
self.mapper = Loader()
def test_load_enum(self):
self.assertEqual(
self.mapper.map_with_type("spam", SampleEnum),
SampleEnum.SPAM,
)
self.assertEqual(
self.mapper.map_with_type("spam_alias", SampleEnum),
SampleEnum.SPAM,
)
self.assertEqual(
self.mapper.map_with_type("ham", SampleEnum),
SampleEnum.HAM,
)
with self.assertRaises(TypeError):
self.mapper.map_with_type(None, SampleEnum)
with self.assertRaises(TypeError):
self.mapper.map_with_type(0xf00, SampleEnum)
def test_load_schema_bad_type(self):
with self.assertRaises(TypeError):
self.mapper.map_with_type("{}", Schema)
with self.assertRaises(TypeError):
self.mapper.map_with_type([], Schema)
with self.assertRaises(TypeError):
self.mapper.map_with_type({"foo": "bar"}, Schema)
def test_load_schema_empty(self):
self.assertEqual(self.mapper.map_with_type({}, Schema), Schema())
def test_load_schema(self):
self.assertEqual(
self.mapper.map_with_type({
"my_schema": {
"my_bool": True,
"my_int": -2,
"my_float": 3.14,
# Test mixedCase to lower_case conversion.
"myStr": "bar",
"my_enum": "eggs",
"my_optional_str": None,
"my_list": ["spam", "ham"],
"my_dict": {"eggs": 6},
},
"my_list_of_schemas": [{}],
"my_dict_of_schemas": {"only": {
"my_help_text": "a nice value!",
}},
}, SampleOuterConfig),
SampleOuterConfig(
my_schema=SampleConfigTypes(
my_bool=True,
my_int=-2,
my_float=3.14,
my_str="bar",
my_enum=SampleEnum.EGGS,
my_optional_str=None,
my_list=["spam", "ham"],
my_dict={"eggs": 6},
),
my_list_of_schemas=[SampleConfigDefault(
my_default=1,
)],
my_dict_of_schemas={"only": SampleConfigHelp(
my_help_text="a nice value!",
)},
),
)
def test_load_schema_missing_no_default(self):
with self.assertRaises(TypeError):
self.mapper.map_with_type({}, SampleConfigTypes)
def test_load_schema_excess_field(self):
with self.assertRaisesRegex(TypeError, "something completely different"):
self.mapper.map_with_type({
"something completely differenty": None,
}, SampleConfigDefault)
def test_load_schema_bad_types(self):
with self.assertRaises(TypeError):
self.mapper.map_with_type({
"my_bool": None,
"my_int": None,
"my_float": None,
"my_str": None,
"my_enum": None,
"my_optional_str": False,
"my_list": None,
"my_dict": None,
}, SampleConfigTypes)
def test_load_schema_optional_types_are_not_optional_fields(self):
with self.assertRaisesRegex(TypeError, "my_optional_str"):
self.mapper.map_with_type({
"my_bool": True,
"my_int": -2,
"my_float": 3.14,
"my_str": "bar",
"my_enum": "ham",
"my_list": ["spam", "ham"],
"my_dict": {"eggs": 6},
}, SampleConfigTypes)
class TestDumper(BaseMapperMixin, TestCase):
def setUp(self):
self.mapper = Dumper()
def test_dump_enum(self):
self.assertEqual(
self.mapper.map_with_type(SampleEnum.SPAM, SampleEnum),
"spam",
)
self.assertEqual(
self.mapper.map_with_type(SampleEnum.SPAM_ALIAS, SampleEnum),
"spam",
)
self.assertEqual(
self.mapper.map_with_type(SampleEnum.HAM, SampleEnum),
"ham",
)
with self.assertRaises(TypeError):
self.mapper.map_with_type(None, SampleEnum)
with self.assertRaises(TypeError):
self.mapper.map_with_type(0xf00, SampleEnum)
def test_dump_schema(self):
self.assertEqual(
self.mapper.map_with_type(SampleOuterConfig(
my_schema=SampleConfigTypes(
my_bool=True,
my_int=-2,
my_float=3.14,
my_str="bar",
my_enum=SampleEnum.EGGS,
my_optional_str=None,
my_list=["spam", "ham"],
my_dict={"eggs": 6},
),
my_list_of_schemas=[SampleConfigDefault()],
my_dict_of_schemas={"only": SampleConfigHelp(
my_help_text="a nice value!",
)},
), SampleOuterConfig),
{
"my_schema": {
"my_bool": True,
"my_int": -2,
"my_float": 3.14,
"my_str": "bar",
"my_enum": "eggs",
"my_optional_str": None,
"my_list": ["spam", "ham"],
"my_dict": {"eggs": 6},
},
"my_list_of_schemas": [{
"my_default": 1,
}],
"my_dict_of_schemas": {"only": {
"my_help_text": "a nice value!",
}},
},
)
class TestConfig(TestCase):
def test_help(self):
self.assertEqual(
SampleOuterConfig.help(),
[
'my_types:',
'',
' my_bool (bool)',
' my_int (int)',
' my_float (float)',
' my_str (str)',
' my_enum ((spam|ham|eggs))',
' my_optional_str (?str)',
' my_list ([str])',
' my_dict ({str: int})',
'',
'',
'my_default:',
'',
' my_default (int)',
'',
'',
'my_help:',
'',
' my_help_text (str)',
'\tPass a nice value here!',
'',
'',
'my_outer_name:',
'',
' my_schema (?my_types)',
' my_list_of_schemas ([my_default])',
' my_dict_of_schemas ({str: my_help})',
'',
'',
]
)
class TestExtractNestedType(TestCase):
def test_empty(self):
self.assertIs(extract_nested_type(SampleConfigTypes, []), SampleConfigTypes)
def test_optional(self):
self.assertIs(
extract_nested_type(SampleOuterConfig,
["my_schema", "my_optional_str"]),
str)
def test_list(self):
self.assertIs(
extract_nested_type(SampleOuterConfig,
["my_dict_of_schemas", "key", "my_help_text"]),
str)
def test_dict(self):
self.assertIs(
extract_nested_type(SampleOuterConfig,
["my_list_of_schemas", "42", "my_default"]),
int)
class TestInjectNestedValue(TestCase):
def test_empty(self):
with self.assertRaises(ValueError):
inject_nested_value({"foo": 42}, [], {"bar": 43})
def test_mixed(self):
data = {"foo": [{"bar": True, "baz": [42, 43]}]}
inject_nested_value(data, ["foo", "0", "baz", "1"], 1)
self.assertEqual(data, {"foo": [{"bar": True, "baz": [42, 1]}]})
if __name__ == '__main__':
main()