Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/utils.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import abc | import abc | ||||
import datetime | import datetime | ||||
import hashlib | import hashlib | ||||
import random | import random | ||||
from hypothesis import strategies | |||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_bytes, hash_to_hex | ||||
from swh.indexer.storage import INDEXER_CFG_KEY | from swh.indexer.storage import INDEXER_CFG_KEY | ||||
BASE_TEST_CONFIG = { | BASE_TEST_CONFIG = { | ||||
'storage': { | 'storage': { | ||||
'cls': 'memory', | 'cls': 'memory', | ||||
▲ Show 20 Lines • Show All 381 Lines • ▼ Show 20 Lines | 'translated_metadata': { | ||||
'schema:codeRepository': | 'schema:codeRepository': | ||||
'git+https://github.com/librariesio/yarn-parser.git', | 'git+https://github.com/librariesio/yarn-parser.git', | ||||
'description': | 'description': | ||||
'Tiny web service for parsing yarn.lock files', | 'Tiny web service for parsing yarn.lock files', | ||||
} | } | ||||
}] | }] | ||||
json_dict_keys = strategies.one_of( | |||||
strategies.characters(), | |||||
*map(strategies.just, ['type', 'url', 'name', 'email', '@id', | |||||
'@context', 'repository', 'license', | |||||
]), | |||||
) | |||||
"""Hypothesis strategy that generates strings, with an emphasis on those | |||||
that are often used as dictionary keys in metadata files.""" | |||||
generic_json_document = strategies.recursive( | |||||
strategies.none() | strategies.booleans() | strategies.floats() | | |||||
strategies.characters(), | |||||
lambda children: ( | |||||
strategies.lists(children, 1) | | |||||
strategies.dictionaries(json_dict_keys, children, min_size=1) | |||||
) | |||||
) | |||||
"""Hypothesis strategy that generates possible values for values of JSON | |||||
metadata files.""" | |||||
def json_document_strategy(keys=None): | |||||
"""Generates an hypothesis strategy that generates metadata files | |||||
for a format that uses the given keys.""" | |||||
if keys is None: | |||||
keys = strategies.characters() | |||||
else: | |||||
keys = strategies.one_of(map(strategies.just, keys)) | |||||
return strategies.dictionaries(keys, generic_json_document, min_size=2) | |||||
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 |