Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/storage/__init__.py
# Copyright (C) 2018 The Software Heritage developers | # Copyright (C) 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 | ||||
from os import path | from os import path | ||||
import swh.storage | import swh.storage | ||||
from hypothesis.strategies import (binary, composite, sets, one_of, | from swh.model.hashutil import MultiHash | ||||
from hypothesis.strategies import (composite, sets, one_of, uuids, | |||||
tuples, sampled_from) | tuples, sampled_from) | ||||
SQL_DIR = path.join(path.dirname(swh.indexer.__file__), 'sql') | SQL_DIR = path.join(path.dirname(swh.indexer.__file__), 'sql') | ||||
MIMETYPES = [ | MIMETYPES = [ | ||||
b'application/json', | b'application/json', | ||||
b'application/octet-stream', | b'application/octet-stream', | ||||
b'application/xml', | b'application/xml', | ||||
b'text/plain', | b'text/plain', | ||||
] | ] | ||||
ENCODINGS = [ | ENCODINGS = [ | ||||
b'iso8859-1', | b'iso8859-1', | ||||
b'iso8859-15', | b'iso8859-15', | ||||
b'latin1', | b'latin1', | ||||
b'utf-8', | b'utf-8', | ||||
] | ] | ||||
def gen_content_id(): | |||||
"""Generate raw id strategy. | |||||
""" | |||||
return binary(min_size=20, max_size=20) | |||||
def gen_mimetype(): | def gen_mimetype(): | ||||
"""Generate one mimetype strategy. | """Generate one mimetype strategy. | ||||
""" | """ | ||||
return one_of(sampled_from(MIMETYPES)) | return one_of(sampled_from(MIMETYPES)) | ||||
def gen_encoding(): | def gen_encoding(): | ||||
Show All 19 Lines | def gen_content_mimetypes(draw, *, min_size=0, max_size=100): | ||||
Returns: | Returns: | ||||
List of content_mimetypes as expected by the | List of content_mimetypes as expected by the | ||||
content_mimetype_add api endpoint. | content_mimetype_add api endpoint. | ||||
""" | """ | ||||
_ids = draw( | _ids = draw( | ||||
sets( | sets( | ||||
tuples( | tuples( | ||||
gen_content_id(), | uuids(), | ||||
gen_mimetype(), | gen_mimetype(), | ||||
gen_encoding() | gen_encoding() | ||||
), | ), | ||||
min_size=min_size, max_size=max_size | min_size=min_size, max_size=max_size | ||||
) | ) | ||||
) | ) | ||||
content_mimetypes = [] | content_mimetypes = [] | ||||
for content_id, mimetype, encoding in _ids: | for uuid, mimetype, encoding in _ids: | ||||
content_id = MultiHash.from_data(uuid.bytes, {'sha1'}).digest()['sha1'] | |||||
content_mimetypes.append({ | content_mimetypes.append({ | ||||
'id': content_id, | 'id': content_id, | ||||
'mimetype': mimetype, | 'mimetype': mimetype, | ||||
'encoding': encoding, | 'encoding': encoding, | ||||
'indexer_configuration_id': 1, | 'indexer_configuration_id': 1, | ||||
}) | }) | ||||
return content_mimetypes | return content_mimetypes |