Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/utils.py
Show First 20 Lines • Show All 415 Lines • ▼ Show 20 Lines | generic_json_document = strategies.recursive( | ||||
) | ) | ||||
) | ) | ||||
"""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 |