Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/utils.py
Show First 20 Lines • Show All 395 Lines • ▼ Show 20 Lines | YARN_PARSER_METADATA = { | ||||
'keywords': ['yarn', 'parse', 'lock', 'dependencies'], | 'keywords': ['yarn', 'parse', 'lock', 'dependencies'], | ||||
} | } | ||||
json_dict_keys = strategies.one_of( | json_dict_keys = strategies.one_of( | ||||
strategies.characters(), | strategies.characters(), | ||||
*map(strategies.just, ['type', 'url', 'name', 'email', '@id', | *map(strategies.just, ['type', 'url', 'name', 'email', '@id', | ||||
'@context', 'repository', 'license', | '@context', 'repository', 'license', | ||||
'repositories', 'licenses' | |||||
]), | ]), | ||||
) | ) | ||||
"""Hypothesis strategy that generates strings, with an emphasis on those | """Hypothesis strategy that generates strings, with an emphasis on those | ||||
that are often used as dictionary keys in metadata files.""" | that are often used as dictionary keys in metadata files.""" | ||||
generic_json_document = strategies.recursive( | generic_json_document = strategies.recursive( | ||||
strategies.none() | strategies.booleans() | strategies.floats() | | strategies.none() | strategies.booleans() | strategies.floats() | | ||||
strategies.characters(), | strategies.characters(), | ||||
lambda children: ( | lambda children: ( | ||||
strategies.lists(children, 1) | | strategies.lists(children, 1) | | ||||
strategies.dictionaries(json_dict_keys, children, min_size=1) | strategies.dictionaries(json_dict_keys, children, min_size=1) | ||||
) | ) | ||||
) | ) | ||||
"""Hypothesis strategy that generates possible values for values of JSON | """Hypothesis strategy that generates possible values for values of JSON | ||||
metadata files.""" | metadata files.""" | ||||
def json_document_strategy(keys=None): | def json_document_strategy(keys=None): | ||||
"""Generates an hypothesis strategy that generates metadata files | """Generates an hypothesis strategy that generates metadata files | ||||
for a format that uses the given keys.""" | for a JSON-based format that uses the given keys.""" | ||||
if keys is None: | if keys is None: | ||||
keys = strategies.characters() | keys = strategies.characters() | ||||
else: | else: | ||||
keys = strategies.one_of(map(strategies.just, keys)) | keys = strategies.one_of(map(strategies.just, keys)) | ||||
return strategies.dictionaries(keys, generic_json_document, min_size=1) | return strategies.dictionaries(keys, generic_json_document, min_size=1) | ||||
def xml_document_strategy(keys, root, xmlns): | |||||
"""Generates an hypothesis strategy that generates metadata files | |||||
for an XML format that uses the given keys.""" | |||||
def encode(s): | |||||
"Skips unpaired surrogates generated by json_document_strategy" | |||||
return s.encode('utf8', 'replace') | |||||
def to_xml(data, indent=b' '): | |||||
if data is None: | |||||
return b'' | |||||
elif isinstance(data, (bool, str, int, float)): | |||||
return encode(str(data)) | |||||
elif isinstance(data, list): | |||||
return b'\n'.join(to_xml(v, indent=indent) for v in data) | |||||
elif isinstance(data, dict): | |||||
lines = [] | |||||
for (key, value) in data.items(): | |||||
lines.append(indent + encode('<{}>'.format(key))) | |||||
lines.append(indent + to_xml(value, indent=indent+b' ')) | |||||
lines.append(indent + encode('</{}>'.format(key))) | |||||
return b'\n'.join(lines) | |||||
else: | |||||
raise TypeError(data) | |||||
def to_root_xml(data): | |||||
return b'\n'.join([ | |||||
'<{} xmlns="{}">'.format(root, xmlns).encode(), | |||||
to_xml(data), | |||||
'</{}>'.format(root).encode(), | |||||
]) | |||||
return strategies.builds(to_root_xml, json_document_strategy(keys)) | |||||
def filter_dict(d, keys): | def filter_dict(d, keys): | ||||
'return a copy of the dict with keys deleted' | 'return a copy of the dict with keys deleted' | ||||
if not isinstance(keys, (list, tuple)): | if not isinstance(keys, (list, tuple)): | ||||
keys = (keys, ) | keys = (keys, ) | ||||
return dict((k, v) for (k, v) in d.items() if k not in keys) | return dict((k, v) for (k, v) in d.items() if k not in keys) | ||||
def fill_obj_storage(obj_storage): | def fill_obj_storage(obj_storage): | ||||
▲ Show 20 Lines • Show All 226 Lines • Show Last 20 Lines |