forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_utils_test.py
564 lines (518 loc) · 17.9 KB
/
schema_utils_test.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# coding: utf-8
#
# Copyright 2014 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for object schema definitions."""
# pylint: disable=relative-import
import inspect
from core.tests import test_utils
import schema_utils
SCHEMA_KEY_ITEMS = schema_utils.SCHEMA_KEY_ITEMS
SCHEMA_KEY_LEN = schema_utils.SCHEMA_KEY_LEN
SCHEMA_KEY_PROPERTIES = schema_utils.SCHEMA_KEY_PROPERTIES
SCHEMA_KEY_TYPE = schema_utils.SCHEMA_KEY_TYPE
SCHEMA_KEY_POST_NORMALIZERS = schema_utils.SCHEMA_KEY_POST_NORMALIZERS
SCHEMA_KEY_CHOICES = schema_utils.SCHEMA_KEY_CHOICES
SCHEMA_KEY_NAME = schema_utils.SCHEMA_KEY_NAME
SCHEMA_KEY_SCHEMA = schema_utils.SCHEMA_KEY_SCHEMA
SCHEMA_KEY_OBJ_TYPE = schema_utils.SCHEMA_KEY_OBJ_TYPE
SCHEMA_KEY_VALIDATORS = schema_utils.SCHEMA_KEY_VALIDATORS
SCHEMA_KEY_DESCRIPTION = 'description'
SCHEMA_KEY_UI_CONFIG = 'ui_config'
# The following keys are always accepted as optional keys in any schema.
OPTIONAL_SCHEMA_KEYS = [
SCHEMA_KEY_CHOICES, SCHEMA_KEY_POST_NORMALIZERS, SCHEMA_KEY_UI_CONFIG,
SCHEMA_KEY_VALIDATORS]
SCHEMA_TYPE_BOOL = schema_utils.SCHEMA_TYPE_BOOL
# 'Custom' objects undergo an entirely separate normalization process, defined
# in the relevant extensions/objects/models/objects.py class.
SCHEMA_TYPE_CUSTOM = schema_utils.SCHEMA_TYPE_CUSTOM
SCHEMA_TYPE_DICT = schema_utils.SCHEMA_TYPE_DICT
SCHEMA_TYPE_FLOAT = schema_utils.SCHEMA_TYPE_FLOAT
SCHEMA_TYPE_HTML = schema_utils.SCHEMA_TYPE_HTML
SCHEMA_TYPE_INT = schema_utils.SCHEMA_TYPE_INT
SCHEMA_TYPE_LIST = schema_utils.SCHEMA_TYPE_LIST
SCHEMA_TYPE_UNICODE = schema_utils.SCHEMA_TYPE_UNICODE
ALLOWED_SCHEMA_TYPES = [
SCHEMA_TYPE_BOOL, SCHEMA_TYPE_CUSTOM, SCHEMA_TYPE_DICT, SCHEMA_TYPE_FLOAT,
SCHEMA_TYPE_HTML, SCHEMA_TYPE_INT, SCHEMA_TYPE_LIST, SCHEMA_TYPE_UNICODE]
ALLOWED_CUSTOM_OBJ_TYPES = [
'Filepath', 'LogicQuestion', 'MathLatexString', 'MusicPhrase',
'ParameterName', 'SanitizedUrl', 'Graph', 'ImageWithRegions']
# Schemas for the UI config for the various types. All of these configuration
# options are optional additions to the schema, and, if omitted, should not
# result in any errors.
# Note to developers: please keep this in sync with
# https://github.com/oppia/oppia/wiki/Schema-Based-Forms
UI_CONFIG_SPECS = {
SCHEMA_TYPE_BOOL: {},
SCHEMA_TYPE_DICT: {},
SCHEMA_TYPE_FLOAT: {},
SCHEMA_TYPE_HTML: {
'hide_complex_extensions': {
'type': SCHEMA_TYPE_BOOL,
}
},
SCHEMA_TYPE_INT: {},
SCHEMA_TYPE_LIST: {
'add_element_text': {
'type': SCHEMA_TYPE_UNICODE
}
},
SCHEMA_TYPE_UNICODE: {
'rows': {
'type': SCHEMA_TYPE_INT,
'validators': [{
'id': 'is_at_least',
'min_value': 1,
}]
},
'coding_mode': {
'type': SCHEMA_TYPE_UNICODE,
'choices': ['none', 'python', 'coffeescript'],
}
},
}
# Schemas for validators for the various types.
VALIDATOR_SPECS = {
SCHEMA_TYPE_BOOL: {},
SCHEMA_TYPE_DICT: {},
SCHEMA_TYPE_FLOAT: {
'is_at_least': {
'min_value': {
'type': SCHEMA_TYPE_FLOAT
}
},
'is_at_most': {
'max_value': {
'type': SCHEMA_TYPE_FLOAT
}
},
},
SCHEMA_TYPE_HTML: {},
SCHEMA_TYPE_INT: {
'is_at_least': {
'min_value': {
'type': SCHEMA_TYPE_INT
}
},
'is_at_most': {
'max_value': {
'type': SCHEMA_TYPE_INT
}
},
},
SCHEMA_TYPE_LIST: {
'has_length_at_least': {
'min_value': {
'type': SCHEMA_TYPE_INT,
'validators': [{
'id': 'is_at_least',
'min_value': 1,
}],
}
},
'has_length_at_most': {
'max_value': {
'type': SCHEMA_TYPE_INT,
'validators': [{
'id': 'is_at_least',
'min_value': 1,
}],
}
},
'is_uniquified': {},
},
SCHEMA_TYPE_UNICODE: {
'matches_regex': {
'regex': {
'type': SCHEMA_TYPE_UNICODE,
'validators': [{
'id': 'is_regex',
}]
}
},
'is_nonempty': {},
'is_regex': {},
},
}
def _validate_ui_config(obj_type, ui_config):
"""Validates the value of a UI configuration."""
reference_dict = UI_CONFIG_SPECS[obj_type]
assert set(ui_config.keys()) <= set(reference_dict.keys())
for key, value in ui_config.iteritems():
schema_utils.normalize_against_schema(
value, reference_dict[key])
def _validate_validator(obj_type, validator):
"""Validates the value of a 'validator' field."""
reference_dict = VALIDATOR_SPECS[obj_type]
assert 'id' in validator and validator['id'] in reference_dict
customization_keys = validator.keys()
customization_keys.remove('id')
assert (set(customization_keys) ==
set(reference_dict[validator['id']].keys()))
for key in customization_keys:
value = validator[key]
schema = reference_dict[validator['id']][key]
try:
schema_utils.normalize_against_schema(value, schema)
except Exception as e:
raise AssertionError(e)
# Check that the id corresponds to a valid normalizer function.
validator_fn = schema_utils._Validators.get(validator['id']) # pylint: disable=protected-access
assert set(inspect.getargspec(validator_fn).args) == set(
customization_keys + ['obj'])
def _validate_dict_keys(dict_to_check, required_keys, optional_keys):
"""Checks that all of the required keys, and possibly some of the optional
keys, are in the given dict.
Raises:
AssertionError: if the validation fails.
"""
assert set(required_keys) <= set(dict_to_check.keys()), (
'Missing keys: %s' % dict_to_check)
assert set(dict_to_check.keys()) <= set(required_keys + optional_keys), (
'Extra keys: %s' % dict_to_check)
def validate_schema(schema):
"""Validates a schema.
This is meant to be a utility function that should be used by tests to
ensure that all schema definitions in the codebase are valid.
Each schema is a dict with at least a key called 'type'. The 'type' can
take one of the SCHEMA_TYPE_* values declared above. In addition, there
may be additional keys for specific types:
- 'list' requires an additional 'items' property, which specifies the type
of the elements in the list. It also allows for an optional 'len'
property which specifies the len of the list.
- 'dict' requires an additional 'properties' property, which specifies the
names of the keys in the dict, and schema definitions for their values.
There may also be an optional 'post_normalizers' key whose value is a list
of normalizers.
Raises:
AssertionError: if the schema is not valid.
"""
assert isinstance(schema, dict)
assert SCHEMA_KEY_TYPE in schema
assert schema[SCHEMA_KEY_TYPE] in ALLOWED_SCHEMA_TYPES
if schema[SCHEMA_KEY_TYPE] == SCHEMA_TYPE_CUSTOM:
_validate_dict_keys(
schema,
[SCHEMA_KEY_TYPE, SCHEMA_KEY_OBJ_TYPE],
[])
assert schema[SCHEMA_KEY_OBJ_TYPE] in ALLOWED_CUSTOM_OBJ_TYPES, schema
elif schema[SCHEMA_KEY_TYPE] == SCHEMA_TYPE_LIST:
_validate_dict_keys(
schema,
[SCHEMA_KEY_ITEMS, SCHEMA_KEY_TYPE],
OPTIONAL_SCHEMA_KEYS + [SCHEMA_KEY_LEN])
validate_schema(schema[SCHEMA_KEY_ITEMS])
if SCHEMA_KEY_LEN in schema:
assert isinstance(schema[SCHEMA_KEY_LEN], int)
assert schema[SCHEMA_KEY_LEN] > 0
elif schema[SCHEMA_KEY_TYPE] == SCHEMA_TYPE_DICT:
_validate_dict_keys(
schema,
[SCHEMA_KEY_PROPERTIES, SCHEMA_KEY_TYPE],
OPTIONAL_SCHEMA_KEYS)
assert isinstance(schema[SCHEMA_KEY_PROPERTIES], list)
for prop in schema[SCHEMA_KEY_PROPERTIES]:
_validate_dict_keys(
prop,
[SCHEMA_KEY_NAME, SCHEMA_KEY_SCHEMA],
[SCHEMA_KEY_DESCRIPTION])
assert isinstance(prop[SCHEMA_KEY_NAME], basestring)
validate_schema(prop[SCHEMA_KEY_SCHEMA])
if SCHEMA_KEY_DESCRIPTION in prop:
assert isinstance(prop[SCHEMA_KEY_DESCRIPTION], basestring)
else:
_validate_dict_keys(schema, [SCHEMA_KEY_TYPE], OPTIONAL_SCHEMA_KEYS)
if SCHEMA_KEY_UI_CONFIG in schema:
_validate_ui_config(
schema[SCHEMA_KEY_TYPE], schema[SCHEMA_KEY_UI_CONFIG])
if SCHEMA_KEY_CHOICES in schema and SCHEMA_KEY_POST_NORMALIZERS in schema:
raise AssertionError(
'Schema cannot contain both a \'choices\' and a '
'\'post_normalizers\' key.')
if SCHEMA_KEY_POST_NORMALIZERS in schema:
assert isinstance(schema[SCHEMA_KEY_POST_NORMALIZERS], list)
for post_normalizer in schema[SCHEMA_KEY_POST_NORMALIZERS]:
assert isinstance(post_normalizer, dict)
assert 'id' in post_normalizer
# Check that the id corresponds to a valid normalizer function.
schema_utils.Normalizers.get(post_normalizer['id'])
# TODO(sll): Check the arguments too.
if SCHEMA_KEY_VALIDATORS in schema:
assert isinstance(schema[SCHEMA_KEY_VALIDATORS], list)
for validator in schema[SCHEMA_KEY_VALIDATORS]:
assert isinstance(validator, dict)
assert 'id' in validator
_validate_validator(schema[SCHEMA_KEY_TYPE], validator)
class SchemaValidationUnitTests(test_utils.GenericTestBase):
"""Test validation of schemas."""
def test_schemas_are_correctly_validated(self):
"""Test validation of schemas."""
invalid_schemas = [[
'type'
], {
'type': 'invalid'
}, {
'type': 'dict',
}, {
'type': 'list',
'items': {}
}, {
'type': 'list',
'items': {
'type': 'unicode'
},
'len': -1
}, {
'type': 'list',
'items': {
'type': 'unicode'
},
'len': 0
}, {
'type': 'list',
'items': {
'type': 'unicode'
},
'validators': [{
'id': 'has_length_at_most',
'max_value': 0
}]
}, {
'type': 'dict',
'items': {
'type': 'float'
}
}, {
'type': 'dict',
'properties': {
123: {
'type': 'unicode'
}
}
}, {
'type': 'unicode',
'validators': [{
'id': 'fake_validator',
}]
}, {
'type': 'unicode',
'validators': [{
'id': 'is_nonempty',
'fake_arg': 'unused_value',
}]
}, {
'type': 'unicode',
'validators': [{
'id': 'matches_regex',
}]
}, {
'type': 'float',
'validators': [{
'id': 'is_at_least',
'min_value': 'value_of_wrong_type',
}]
}, {
'type': 'unicode',
'ui_config': {
'rows': -1,
}
}, {
'type': 'unicode',
'ui_config': {
'coding_mode': 'invalid_mode',
}
}]
valid_schemas = [{
'type': 'float'
}, {
'type': 'bool'
}, {
'type': 'dict',
'properties': [{
'name': 'str_property',
'schema': {
'type': 'unicode'
}
}]
}, {
'type': 'list',
'items': {
'type': 'list',
'items': {
'type': 'list',
'items': {
'type': 'bool'
},
'len': 100
}
}
}, {
'type': 'list',
'items': {
'type': 'unicode'
},
'validators': [{
'id': 'has_length_at_most',
'max_value': 3
}]
}, {
'type': 'float',
'validators': [{
'id': 'is_at_least',
'min_value': 3.0,
}]
}, {
'type': 'unicode',
'ui_config': {
'rows': 5,
}
}, {
'type': 'unicode',
'ui_config': {
'coding_mode': 'python',
}
}]
for schema in valid_schemas:
validate_schema(schema)
for schema in invalid_schemas:
with self.assertRaises((AssertionError, KeyError)):
validate_schema(schema)
class SchemaNormalizationUnitTests(test_utils.GenericTestBase):
"""Test schema-based normalization of objects."""
def check_normalization(self, schema, mappings, invalid_items):
"""Validates the schema and tests that values are normalized correctly.
Args:
schema: the schema to normalize the value against.
mappings: a list of 2-element tuples. The first element of
each item is expected to be normalized to the second.
invalid_items: a list of values. Each of these is expected to raise
an AssertionError when normalized.
"""
validate_schema(schema)
for raw_value, expected_value in mappings:
self.assertEqual(
schema_utils.normalize_against_schema(raw_value, schema),
expected_value)
for value in invalid_items:
with self.assertRaises(Exception):
schema_utils.normalize_against_schema(value, schema)
def test_float_schema(self):
schema = {
'type': schema_utils.SCHEMA_TYPE_FLOAT,
}
mappings = [(1.2, 1.2), (3, 3.0), (-1, -1.0), ('1', 1.0)]
invalid_vals = [[13], 'abc', None]
self.check_normalization(schema, mappings, invalid_vals)
def test_list_schema_with_len(self):
schema = {
'type': schema_utils.SCHEMA_TYPE_LIST,
'items': {
'type': schema_utils.SCHEMA_TYPE_UNICODE,
},
'len': 2,
}
mappings = [
(['a', 'b'], ['a', 'b']),
(['abc', ''], ['abc', '']),
(['adaA13', '13'], ['adaA13', '13'])]
invalid_vals = [['1', 13], {'a': 'b'}, {}, None, 123, 'abc', ['c'], []]
self.check_normalization(schema, mappings, invalid_vals)
def test_list_schema(self):
schema = {
'type': schema_utils.SCHEMA_TYPE_LIST,
'items': {
'type': schema_utils.SCHEMA_TYPE_UNICODE,
}
}
mappings = [
(['a', 'b'], ['a', 'b']),
(['c'], ['c']),
(['abc', ''], ['abc', '']),
([], []),
(['adaA13', '13'], ['adaA13', '13'])]
invalid_vals = [['1', 13], {'a': 'b'}, {}, None, 123, 'abc']
self.check_normalization(schema, mappings, invalid_vals)
def test_dict_schema(self):
schema = {
'type': schema_utils.SCHEMA_TYPE_DICT,
'properties': [{
'name': 'unicodeListProp',
'schema': {
'type': schema_utils.SCHEMA_TYPE_LIST,
'items': {
'type': schema_utils.SCHEMA_TYPE_UNICODE
}
},
}, {
'name': 'intProp',
'schema': {
'type': schema_utils.SCHEMA_TYPE_INT
},
}, {
'name': 'dictProp',
'schema': {
'type': schema_utils.SCHEMA_TYPE_DICT,
'properties': [{
'name': 'floatProp',
'schema': {
'type': schema_utils.SCHEMA_TYPE_FLOAT
}
}]
}
}]
}
mappings = [({
'unicodeListProp': [],
'intProp': 1,
'dictProp': {
'floatProp': 3
}
}, {
'unicodeListProp': [],
'intProp': 1,
'dictProp': {
'floatProp': 3.0
}
}), ({
'intProp': 10,
'unicodeListProp': ['abc', 'def'],
'dictProp': {
'floatProp': -1.0
}
}, {
'intProp': 10,
'unicodeListProp': ['abc', 'def'],
'dictProp': {
'floatProp': -1.0
}
})]
invalid_vals = [{
'unicodeListProp': [],
'intPROP': 1,
'dictProp': {
'floatProp': 3.0
}
}, {
'unicodeListProp': ['aaa'],
'intProp': 1,
}, {
'unicodeListProp': [],
'intProp': 3,
'dictProp': {},
}, [
'unicodeListProp', 'intProp', 'dictProp'
], None, 123, 'abc']
self.check_normalization(schema, mappings, invalid_vals)