diff --git a/swh/storage/tests/conftest.py b/swh/storage/tests/conftest.py --- a/swh/storage/tests/conftest.py +++ b/swh/storage/tests/conftest.py @@ -10,13 +10,14 @@ from pytest_postgresql.janitor import DatabaseJanitor, psycopg2 from os import path, environ -from hypothesis import settings, strategies +from random import choice, randint, shuffle +from hypothesis import settings from typing import Dict import swh.storage -from swh.model.hypothesis_strategies import origins, contents from swh.core.utils import numfile_sortkey as sortkey +from swh.model.hashutil import MultiHash SQL_DIR = path.join(path.dirname(swh.storage.__file__), 'sql') @@ -54,18 +55,48 @@ return storage -def gen_origins(n=20): - return strategies.lists( - origins().map(lambda x: x.to_dict()), - unique_by=lambda x: x['url'], - min_size=n, max_size=n).example() +PROTOCOLS = ['git', 'http', 'https', 'deb', 'svn', 'mock'] +DOMAINS = ['example.com', 'some.long.host.name', 'xn--n28h.tld'] +PATHS = ['', '/', '/stuff', '/stuff/', + '/path/to/resource', + '/path/with/anchor#id=42', + '/path/with/qargs?q=1&b'] +CONTENT_STATUS = ['visible', 'hidden', 'absent'] + + +def gen_all_origins(): + for protocol in PROTOCOLS: + for domain in DOMAINS: + for urlpath in PATHS: + yield {'url': '%s://%s%s' % (protocol, domain, urlpath)} + + +ORIGINS = list(gen_all_origins()) + + +def gen_origins(n=100): + origins = ORIGINS[:] + shuffle(origins) + return origins[:n] + + +def gen_content(): + size = randint(1, 10 * 1024) + data = bytes(randint(0, 255) for i in range(size)) + status = choice(CONTENT_STATUS) + h = MultiHash.from_data(data) + content = {'data': data, + 'status': status, + 'length': size, + **h.digest()} + if status == 'absent': + content['reason'] = 'why not' + content['data'] = None + return content def gen_contents(n=20): - return strategies.lists( - contents().map(lambda x: x.to_dict()), - unique_by=lambda x: (x['sha1'], x['sha1_git']), - min_size=n, max_size=n).example() + return [gen_content() for i in range(n)] @pytest.fixture diff --git a/swh/storage/tests/test_storage.py b/swh/storage/tests/test_storage.py --- a/swh/storage/tests/test_storage.py +++ b/swh/storage/tests/test_storage.py @@ -3235,10 +3235,10 @@ actual_origins = list( swh_storage.origin_get_range(origin_from=1, - origin_count=21)) - assert len(actual_origins) == 20 + origin_count=101)) + assert len(actual_origins) == 100 assert actual_origins[0]['id'] == 1 - assert actual_origins[-1]['id'] == 20 + assert actual_origins[-1]['id'] == 100 actual_origins = list( swh_storage.origin_get_range(origin_from=11, @@ -3253,11 +3253,11 @@ assert actual_origins[-1]['id'] == 20 actual_origins = list( - swh_storage.origin_get_range(origin_from=11, + swh_storage.origin_get_range(origin_from=91, origin_count=11)) assert len(actual_origins) == 10 - assert actual_origins[0]['id'] == 11 - assert actual_origins[-1]['id'] == 20 + assert actual_origins[0]['id'] == 91 + assert actual_origins[-1]['id'] == 100 def test_origin_count(self, swh_storage): new_origins = [