Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_service.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 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 Affero General Public License version 3, or any later version | # License: GNU Affero 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 itertools | import itertools | ||||
import pytest | import pytest | ||||
import random | import random | ||||
from collections import defaultdict | from collections import defaultdict | ||||
from hypothesis import given | from hypothesis import given | ||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_bytes, hash_to_hex | ||||
from swh.model.from_disk import DentryPerms | from swh.model.from_disk import DentryPerms | ||||
from swh.model.identifiers import ( | |||||
CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | |||||
) | |||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.exc import BadInputExc, NotFoundExc | from swh.web.common.exc import BadInputExc, NotFoundExc | ||||
from swh.web.tests.data import random_sha1, random_content | from swh.web.tests.data import random_sha1, random_content | ||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
content, contents, unknown_contents, | content, unknown_content, contents, unknown_contents, | ||||
contents_with_ctags, origin, new_origin, visit_dates, directory, | contents_with_ctags, origin, new_origin, visit_dates, directory, | ||||
release, revision, unknown_revision, revisions, | unknown_directory, release, unknown_release, revision, unknown_revision, | ||||
ancestor_revisions, non_ancestor_revisions, invalid_sha1, sha256, | revisions, ancestor_revisions, non_ancestor_revisions, invalid_sha1, | ||||
revision_with_submodules, empty_directory, | sha256, revision_with_submodules, empty_directory, new_revision, | ||||
new_revision | snapshot, unknown_snapshot | ||||
) | ) | ||||
from swh.web.tests.conftest import ctags_json_missing, fossology_missing | from swh.web.tests.conftest import ctags_json_missing, fossology_missing | ||||
@given(contents()) | @given(contents()) | ||||
def test_lookup_multiple_hashes_all_present(contents): | def test_lookup_multiple_hashes_all_present(contents): | ||||
input_data = [] | input_data = [] | ||||
expected_output = [] | expected_output = [] | ||||
▲ Show 20 Lines • Show All 761 Lines • ▼ Show 20 Lines | dir_entries = [e for e in archive_data.directory_ls(rev_data['directory']) | ||||
if e['type'] == 'file'] | if e['type'] == 'file'] | ||||
dir_entry = random.choice(dir_entries) | dir_entry = random.choice(dir_entries) | ||||
assert service.lookup_directory_through_revision( | assert service.lookup_directory_through_revision( | ||||
{'sha1_git': revision}, dir_entry['name'], with_data=True | {'sha1_git': revision}, dir_entry['name'], with_data=True | ||||
) == (revision, | ) == (revision, | ||||
service.lookup_directory_with_revision(revision, dir_entry['name'], | service.lookup_directory_with_revision(revision, dir_entry['name'], | ||||
with_data=True)) | with_data=True)) | ||||
@given(content(), directory(), release(), revision(), snapshot()) | |||||
def test_lookup_known_objects(archive_data, content, directory, release, | |||||
moranegg: I see that tests are not documented.
Here valid objects means objects that exists in the… | |||||
Done Inline ActionsSounds better indeed, I will rename the test. anlambert: Sounds better indeed, I will rename the test. | |||||
revision, snapshot): | |||||
expected = archive_data.content_find(content) | |||||
assert service.lookup_object(CONTENT, content['sha1_git']) == expected | |||||
expected = archive_data.directory_get(directory) | |||||
assert service.lookup_object(DIRECTORY, directory) == expected | |||||
expected = archive_data.release_get(release) | |||||
assert service.lookup_object(RELEASE, release) == expected | |||||
expected = archive_data.revision_get(revision) | |||||
assert service.lookup_object(REVISION, revision) == expected | |||||
expected = archive_data.snapshot_get(snapshot) | |||||
assert service.lookup_object(SNAPSHOT, snapshot) == expected | |||||
@given(unknown_content(), unknown_directory(), unknown_release(), | |||||
unknown_revision(), unknown_snapshot()) | |||||
def test_lookup_unknown_objects(unknown_content, unknown_directory, | |||||
unknown_release, unknown_revision, | |||||
unknown_snapshot): | |||||
with pytest.raises(NotFoundExc) as e: | |||||
service.lookup_object(CONTENT, unknown_content['sha1_git']) | |||||
assert e.match(r'Content.*not found') | |||||
with pytest.raises(NotFoundExc) as e: | |||||
service.lookup_object(DIRECTORY, unknown_directory) | |||||
assert e.match(r'Directory.*not found') | |||||
with pytest.raises(NotFoundExc) as e: | |||||
service.lookup_object(RELEASE, unknown_release) | |||||
assert e.match(r'Release.*not found') | |||||
with pytest.raises(NotFoundExc) as e: | |||||
service.lookup_object(REVISION, unknown_revision) | |||||
assert e.match(r'Revision.*not found') | |||||
with pytest.raises(NotFoundExc) as e: | |||||
service.lookup_object(SNAPSHOT, unknown_snapshot) | |||||
assert e.match(r'Snapshot.*not found') | |||||
@given(invalid_sha1()) | |||||
def test_lookup_invalid_objects(invalid_sha1): | |||||
with pytest.raises(BadInputExc) as e: | |||||
service.lookup_object('foo', invalid_sha1) | |||||
assert e.match('Invalid swh object type') | |||||
with pytest.raises(BadInputExc) as e: | |||||
service.lookup_object(CONTENT, invalid_sha1) | |||||
assert e.match('Invalid hash') | |||||
with pytest.raises(BadInputExc) as e: | |||||
service.lookup_object(DIRECTORY, invalid_sha1) | |||||
assert e.match('Invalid checksum') | |||||
with pytest.raises(BadInputExc) as e: | |||||
service.lookup_object(RELEASE, invalid_sha1) | |||||
assert e.match('Invalid checksum') | |||||
with pytest.raises(BadInputExc) as e: | |||||
service.lookup_object(REVISION, invalid_sha1) | |||||
assert e.match('Invalid checksum') | |||||
with pytest.raises(BadInputExc) as e: | |||||
service.lookup_object(SNAPSHOT, invalid_sha1) | |||||
assert e.match('Invalid checksum') |
I see that tests are not documented.
Here valid objects means objects that exists in the archive?
Maybe known objects as the contrast of unknown objects is more appropriate...