Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_validate_file(self):
assert Schema(
Use(open)).validate(self.test_file_name).read().startswith('Copyright')
self.assertRaises(SchemaError, Schema(Use(open)).validate, 'NON-EXISTENT')
assert Schema(os.path.exists).validate('.') == '.'
self.assertRaises(SchemaError, Schema(os.path.exists).validate, './non-existent/')
assert Schema(os.path.isfile).validate(self.test_file_name) == self.test_file_name
self.assertRaises(SchemaError, Schema(os.path.isfile).validate, 'NON-EXISTENT')
def test_json_schema_or_only_one():
s = Schema({"test": Or(str, lambda x: len(x) < 5)})
assert s.json_schema("my-id") == {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "my-id",
"properties": {"test": {"type": "string"}},
"required": ["test"],
"additionalProperties": False,
"type": "object",
}
@promote.validate_json(Schema({'name': And(str, len)}))
def test_function(data):
return data
def test_optional_without_default_value(self):
r = RequestSchema(querystring={optional('param'): Use(int)})
assert r.validate_querystring({}) == {}
def test_dict_keys():
assert Schema({str: int}).validate({"a": 1, "b": 2}) == {"a": 1, "b": 2}
with SE:
Schema({str: int}).validate({1: 1, "b": 2})
assert Schema({Use(str): Use(int)}).validate({1: 3.14, 3.14: 1}) == {"1": 3, "3.14": 1}
# builds a schema from a list of nodes and events
# :param project: validation type
# :param structure: list of nodes (another list) and events
# :return: schema
sub_list = []
for item in list_or_dict(structure):
sub_list.append(subscription_schema(project, item, level=level+1))
sub_list.append(event_schema(level))
node_schema = {
'node': {
'id': Use(type(project._id), error='node_id{}'.format(level)),
'title': Use(type(project.title), error='node_title{}'.format(level)),
'url': Use(type(project.url), error='node_{}'.format(level))
},
'kind': And(str, Use(lambda s: s in ('node', 'folder'),
error="kind didn't match node or folder {}".format(level))),
'nodeType': Use(lambda s: s in ('project', 'component'), error='nodeType not project or component'),
'category': Use(lambda s: s in settings.NODE_CATEGORY_MAP, error='category not in settings.NODE_CATEGORY_MAP'),
'permissions': {
'view': Use(lambda s: s in (True, False), error='view permissions is not True/False')
},
'children': sub_list
}
if level == 0:
return Schema([node_schema])
return node_schema
def test_json_schema_object_or_array_of_object():
# Complex test where "test" accepts either an object or an array of that object
o = {"param1": "test1", Optional("param2"): "test2"}
s = Schema({"test": Or(o, [o])})
assert s.json_schema("my-id") == {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "my-id",
"properties": {
"test": {
"anyOf": [
{
"additionalProperties": False,
"properties": {"param1": {"const": "test1"}, "param2": {"const": "test2"}},
"required": ["param1"],
"type": "object",
},
{
"type": "array",
"items": {
"additionalProperties": False,
def get_options(cls):
return {
"name": str,
ConfigOption("description", default=None): Or(str, None),
ConfigOption("logger_level", default=logger.TEST_INFO): int,
ConfigOption("file_log_level", default=logger.DEBUG): Or(
int, None
),
ConfigOption("runpath", default=default_runpath): Or(
None, str, lambda x: callable(x)
),
ConfigOption("path_cleanup", default=True): bool,
ConfigOption("all_tasks_local", default=False): bool,
ConfigOption(
"shuffle", default=[]
): list, # list of string choices
ConfigOption(
"shuffle_seed", default=float(random.randint(1, 9999))
): float,
ConfigOption("exporters", default=None): Use(get_exporters),
ConfigOption("stdout_style", default=defaults.STDOUT_STYLE): Style,
ConfigOption("report_dir", default=defaults.REPORT_DIR): Or(
str, None
),
ConfigOption("xml_dir", default=None): Or(str, None),
int, None
),
ConfigOption("runpath", default=default_runpath): Or(
None, str, lambda x: callable(x)
),
ConfigOption("path_cleanup", default=True): bool,
ConfigOption("all_tasks_local", default=False): bool,
ConfigOption(
"shuffle", default=[]
): list, # list of string choices
ConfigOption(
"shuffle_seed", default=float(random.randint(1, 9999))
): float,
ConfigOption("exporters", default=None): Use(get_exporters),
ConfigOption("stdout_style", default=defaults.STDOUT_STYLE): Style,
ConfigOption("report_dir", default=defaults.REPORT_DIR): Or(
str, None
),
ConfigOption("xml_dir", default=None): Or(str, None),
ConfigOption("pdf_path", default=None): Or(str, None),
ConfigOption("json_path", default=None): Or(str, None),
ConfigOption("http_url", default=None): Or(str, None),
ConfigOption("pdf_style", default=defaults.PDF_STYLE): Style,
ConfigOption("report_tags", default=[]): [
Use(tagging.validate_tag_value)
],
ConfigOption("report_tags_all", default=[]): [
Use(tagging.validate_tag_value)
],
ConfigOption("merge_scheduled_parts", default=False): bool,
ConfigOption("browse", default=False): bool,
ConfigOption("ui_port", default=None): Or(None, int),
def get_options(cls):
return {
ConfigOption('http_url'): is_valid_url,
ConfigOption('timeout', default=60):
Or(None, And(Use(int), lambda n: n > 0))
}