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 | ||||
anlambert: As you are only using the `DentryPerms` enum from that module, you should change your import… | |||||
from swh.model.from_disk import DentryPerms | |||||
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.strategies import ( | from swh.web.tests.strategies import ( | ||||
content, contents, unknown_content, unknown_contents, | content, contents, unknown_content, 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_revisions, | release, revision, unknown_revision, revisions, unknown_revisions, | ||||
ancestor_revisions, non_ancestor_revisions, invalid_sha1, sha256, | ancestor_revisions, non_ancestor_revisions, invalid_sha1, sha256, | ||||
▲ Show 20 Lines • Show All 269 Lines • ▼ Show 20 Lines | def test_lookup_revision_with_context_ko_sha1_git_does_not_exist( | ||||
service.lookup_revision_with_context(sha1_git_root, sha1_git) | service.lookup_revision_with_context(sha1_git_root, sha1_git) | ||||
self.assertIn('Revision %s not found' % sha1_git, cm.exception.args[0]) | self.assertIn('Revision %s not found' % sha1_git, cm.exception.args[0]) | ||||
@given(revision(), unknown_revision()) | @given(revision(), unknown_revision()) | ||||
def test_lookup_revision_with_context_ko_root_sha1_git_does_not_exist( | def test_lookup_revision_with_context_ko_root_sha1_git_does_not_exist( | ||||
self, revision, unknown_revision): | self, revision, unknown_revision): | ||||
sha1_git_root = unknown_revision | sha1_git_root = unknown_revision | ||||
sha1_git = revision | sha1_git = revision | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_revision_with_context(sha1_git_root, sha1_git) | service.lookup_revision_with_context(sha1_git_root, sha1_git) | ||||
self.assertIn('Revision root %s not found' % sha1_git_root, | self.assertIn('Revision root %s not found' % sha1_git_root, | ||||
cm.exception.args[0]) | cm.exception.args[0]) | ||||
@given(ancestor_revisions()) | @given(ancestor_revisions()) | ||||
def test_lookup_revision_with_context(self, ancestor_revisions): | def test_lookup_revision_with_context(self, ancestor_revisions): | ||||
sha1_git = ancestor_revisions['sha1_git'] | sha1_git = ancestor_revisions['sha1_git'] | ||||
Show All 28 Lines | class ServiceTestCase(WebTestCase): | ||||
@given(unknown_revision()) | @given(unknown_revision()) | ||||
def test_lookup_directory_with_revision_not_found(self, unknown_revision): | def test_lookup_directory_with_revision_not_found(self, unknown_revision): | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_directory_with_revision(unknown_revision) | service.lookup_directory_with_revision(unknown_revision) | ||||
self.assertIn('Revision %s not found' % unknown_revision, | self.assertIn('Revision %s not found' % unknown_revision, | ||||
cm.exception.args[0]) | cm.exception.args[0]) | ||||
@given(revision()) | @given(unknown_content(), unknown_revision(), unknown_directory()) | ||||
def test_lookup_directory_with_revision_ko_path_to_nowhere(self, revision): | def test_lookup_directory_with_revision_unknown_content( | ||||
self, unknown_content, unknown_revision, unknown_directory): | |||||
dir_path = 'README.md' | |||||
# Create a revision that points to a directory | |||||
# Which points to unknown content | |||||
Done Inline ActionsYou can remove that line vlorentz: You can remove that line | |||||
Done Inline ActionsYes, please remove it anlambert: Yes, please remove it | |||||
revision = { | |||||
'author': { | |||||
'name': b'abcd', | |||||
'email': b'abcd@company.org', | |||||
'fullname': b'abcd abcd' | |||||
}, | |||||
'committer': { | |||||
'email': b'aaaa@company.org', | |||||
'fullname': b'aaaa aaa', | |||||
'name': b'aaa' | |||||
}, | |||||
'committer_date': { | |||||
'negative_utc': False, | |||||
'offset': 0, | |||||
'timestamp': 1437511651 | |||||
}, | |||||
'date': { | |||||
'negative_utc': False, | |||||
'offset': 0, | |||||
'timestamp': 1437511651 | |||||
}, | |||||
'message': b'bleh', | |||||
'metadata': [], | |||||
'parents': [], | |||||
'synthetic': False, | |||||
'type': 'file', | |||||
'id': hash_to_bytes(unknown_revision), | |||||
Done Inline ActionsYou should use the hash_to_bytes function from swh.model.hashutil instead anlambert: You should use the `hash_to_bytes` function from `swh.model.hashutil` instead | |||||
'directory': hash_to_bytes(unknown_directory) | |||||
} | |||||
# A directory that points to unknown content | |||||
dir = { | |||||
Done Inline ActionsCode style nitpick: revision = { 'author': { 'name': b'abcd', ... vlorentz: Code style nitpick:
```
revision = {
'author': {
'name': b'abcd',
...
``` | |||||
'id': hash_to_bytes(unknown_directory), | |||||
'entries': [{ | |||||
Done Inline ActionsAgain some nitpicks regarding code formatting, please update that dict definition to: revision = { 'author': { 'name': b'abcd', 'email': b'abcd@company.org', 'fullname': b'abcd abcd' }, 'committer': { 'email': b'aaaa@company.org', 'fullname': b'aaaa aaa', 'name': b'aaa' }, 'committer_date': { 'negative_utc': False, 'offset': 0, 'timestamp': 1437511651 }, 'date': { 'negative_utc': False, 'offset': 0, 'timestamp': 1437511651 }, 'message': b'bleh', 'metadata': [], 'parents': [], 'synthetic': False, 'type': 'file', 'id': hash_to_bytes(unknown_revision), 'directory': hash_to_bytes(unknown_directory) } anlambert: Again some nitpicks regarding code formatting, please update that dict definition to… | |||||
'name': bytes(dir_path.encode('utf-8')), | |||||
'type': 'file', | |||||
Done Inline ActionsCan you replace unknown_revision[:len(unknown_revision)-4]+"bcd1" with just unknown_revision? vlorentz: Can you replace `unknown_revision[:len(unknown_revision)-4]+"bcd1"` with just… | |||||
'target': hash_to_bytes(unknown_content['sha1_git']), | |||||
Done Inline Actionsunindent here anlambert: unindent here | |||||
'perms': DentryPerms.content | |||||
}] | |||||
Done Inline Actionssame here anlambert: same here | |||||
} | |||||
# Add the directory and revision in mem | |||||
self.storage.directory_add([dir]) | |||||
self.storage.revision_add([revision]) | |||||
with self.assertRaises(NotFoundExc) as cm: | |||||
service.lookup_directory_with_revision(unknown_revision, dir_path) | |||||
self.assertIn('Content not found for revision %s' % unknown_revision, | |||||
cm.exception.args[0]) | |||||
Done Inline Actionsplease use unknown_revision() instead of revision(), that will spare some hacks in the test You can also use unknown_directory() to generate a directory id, and build the directory from scratch in the test (that's easier to read) vlorentz: please use `unknown_revision()` instead of `revision()`, that will spare some hacks in the test… | |||||
Done Inline ActionsSame here, update the dict definition to: dir = { 'id': hash_to_bytes(unknown_directory), 'entries': [{ 'name': bytes(dir_path.encode('utf-8')), 'type': 'file', 'target': hash_to_bytes(unknown_content['sha1_git']), 'perms': DentryPerms.content }] } anlambert: Same here, update the dict definition to:
```lang=python
dir = {
'id': hash_to_bytes… | |||||
@given(revision()) | |||||
def test_lookup_directory_with_revision_ko_path_to_nowhere( | |||||
self, revision): | |||||
invalid_path = 'path/to/something/unknown' | invalid_path = 'path/to/something/unknown' | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_directory_with_revision(revision, invalid_path) | service.lookup_directory_with_revision(revision, invalid_path) | ||||
exception_text = cm.exception.args[0].lower() | exception_text = cm.exception.args[0].lower() | ||||
self.assertIn('directory or file', exception_text) | self.assertIn('directory or file', exception_text) | ||||
self.assertIn(invalid_path, exception_text) | self.assertIn(invalid_path, exception_text) | ||||
self.assertIn('revision %s' % revision, exception_text) | self.assertIn('revision %s' % revision, exception_text) | ||||
Done Inline ActionsSame code formatting issue, replace with: with self.assertRaises(NotFoundExc) as cm: service.lookup_directory_with_revision(unknown_revision, dir_path) self.assertIn('Content not found for revision %s' % unknown_revision, cm.exception.args[0]) anlambert: Same code formatting issue, replace with:
```lang=python
with self.assertRaises(NotFoundExc) as… | |||||
self.assertIn('not found', exception_text) | self.assertIn('not found', exception_text) | ||||
@given(revision_with_submodules()) | @given(revision_with_submodules()) | ||||
def test_lookup_directory_with_revision_submodules( | def test_lookup_directory_with_revision_submodules( | ||||
self, revision_with_submodules): | self, revision_with_submodules): | ||||
rev_sha1_git = revision_with_submodules['rev_sha1_git'] | rev_sha1_git = revision_with_submodules['rev_sha1_git'] | ||||
rev_dir_path = revision_with_submodules['rev_dir_rev_path'] | rev_dir_path = revision_with_submodules['rev_dir_rev_path'] | ||||
▲ Show 20 Lines • Show All 460 Lines • Show Last 20 Lines |
As you are only using the DentryPerms enum from that module, you should change your import statement to: