Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_service.py
Show First 20 Lines • Show All 714 Lines • ▼ Show 20 Lines | class ServiceTestCase(unittest.TestCase): | ||||
def lookup_release_ko_id_checksum_not_ok_because_not_a_sha1(self, | def lookup_release_ko_id_checksum_not_ok_because_not_a_sha1(self, | ||||
mock_storage): | mock_storage): | ||||
# given | # given | ||||
mock_storage.release_get = MagicMock() | mock_storage.release_get = MagicMock() | ||||
with self.assertRaises(BadInputExc) as cm: | with self.assertRaises(BadInputExc) as cm: | ||||
# when | # when | ||||
service.lookup_release('not-a-sha1') | service.lookup_release('not-a-sha1') | ||||
self.assertIn('invalid checksum', cm.exception.args[0]) | self.assertIn('invalid checksum', cm.exception.args[0].lower()) | ||||
mock_storage.release_get.called = False | mock_storage.release_get.called = False | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_release_ko_id_checksum_ok_but_not_a_sha1(self, mock_storage): | def lookup_release_ko_id_checksum_ok_but_not_a_sha1(self, mock_storage): | ||||
# given | # given | ||||
mock_storage.release_get = MagicMock() | mock_storage.release_get = MagicMock() | ||||
# when | # when | ||||
with self.assertRaises(BadInputExc) as cm: | with self.assertRaises(BadInputExc) as cm: | ||||
service.lookup_release( | service.lookup_release( | ||||
'13c1d34d138ec13b5ebad226dc2528dc7506c956e4646f62d4daf5' | '13c1d34d138ec13b5ebad226dc2528dc7506c956e4646f62d4daf5' | ||||
'1aea892abe') | '1aea892abe') | ||||
self.assertIn('sha1_git supported', cm.exception.args[0]) | self.assertEqual('Only sha1_git is supported.', cm.exception.args[0]) | ||||
mock_storage.release_get.called = False | mock_storage.release_get.called = False | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_directory_with_path_not_found(self, mock_storage): | def lookup_directory_with_path_not_found(self, mock_storage): | ||||
# given | # given | ||||
mock_storage.lookup_directory_with_path = MagicMock(return_value=None) | mock_storage.lookup_directory_with_path = MagicMock(return_value=None) | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | def lookup_revision_with_context_ko_not_a_sha1_1(self): | ||||
# given | # given | ||||
sha1_git = '13c1d34d138ec13b5ebad226dc2528dc7506c956e4646f62d4' \ | sha1_git = '13c1d34d138ec13b5ebad226dc2528dc7506c956e4646f62d4' \ | ||||
'daf51aea892abe' | 'daf51aea892abe' | ||||
sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | ||||
# when | # when | ||||
with self.assertRaises(BadInputExc) as cm: | with self.assertRaises(BadInputExc) as cm: | ||||
service.lookup_revision_with_context(sha1_git_root, sha1_git) | service.lookup_revision_with_context(sha1_git_root, sha1_git) | ||||
self.assertIn('Only sha1_git is supported', cm.exception.args[0]) | self.assertIn('Only sha1_git is supported', cm.exception.args[0]) | ||||
@istest | @istest | ||||
def lookup_revision_with_context_ko_not_a_sha1_2(self): | def lookup_revision_with_context_ko_not_a_sha1_2(self): | ||||
# given | # given | ||||
sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | ||||
sha1_git = '13c1d34d138ec13b5ebad226dc2528dc7506c956e4646f6' \ | sha1_git = '13c1d34d138ec13b5ebad226dc2528dc7506c956e4646f6' \ | ||||
'2d4daf51aea892abe' | '2d4daf51aea892abe' | ||||
# when | # when | ||||
with self.assertRaises(BadInputExc) as cm: | with self.assertRaises(BadInputExc) as cm: | ||||
service.lookup_revision_with_context(sha1_git_root, sha1_git) | service.lookup_revision_with_context(sha1_git_root, sha1_git) | ||||
self.assertIn('Only sha1_git is supported', cm.exception.args[0]) | self.assertIn('Only sha1_git is supported', cm.exception.args[0]) | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_revision_with_context_ko_sha1_git_does_not_exist( | def lookup_revision_with_context_ko_sha1_git_does_not_exist( | ||||
self, | self, | ||||
mock_storage): | mock_storage): | ||||
# given | # given | ||||
sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | ||||
sha1_git = '777777bdf3629f916219feb3dcc7393ded1bc8db' | sha1_git = '777777bdf3629f916219feb3dcc7393ded1bc8db' | ||||
sha1_git_bin = hash_to_bytes(sha1_git) | sha1_git_bin = hash_to_bytes(sha1_git) | ||||
mock_storage.revision_get.return_value = None | mock_storage.revision_get.return_value = None | ||||
# when | # when | ||||
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 777777bdf3629f916219feb3dcc7393ded1bc8db' | self.assertIn('Revision 777777bdf3629f916219feb3dcc7393ded1bc8db' | ||||
' not found', cm.exception.args[0]) | ' not found', cm.exception.args[0]) | ||||
mock_storage.revision_get.assert_called_once_with( | mock_storage.revision_get.assert_called_once_with( | ||||
[sha1_git_bin]) | [sha1_git_bin]) | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_revision_with_context_ko_root_sha1_git_does_not_exist( | def lookup_revision_with_context_ko_root_sha1_git_does_not_exist( | ||||
self, | self, | ||||
mock_storage): | mock_storage): | ||||
# given | # given | ||||
sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | sha1_git_root = '65a55bbdf3629f916219feb3dcc7393ded1bc8db' | ||||
sha1_git = '777777bdf3629f916219feb3dcc7393ded1bc8db' | sha1_git = '777777bdf3629f916219feb3dcc7393ded1bc8db' | ||||
sha1_git_root_bin = hash_to_bytes(sha1_git_root) | sha1_git_root_bin = hash_to_bytes(sha1_git_root) | ||||
sha1_git_bin = hash_to_bytes(sha1_git) | sha1_git_bin = hash_to_bytes(sha1_git) | ||||
mock_storage.revision_get.side_effect = ['foo', None] | mock_storage.revision_get.side_effect = ['foo', None] | ||||
# when | # when | ||||
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 65a55bbdf3629f916219feb3dcc7393ded1bc8db' | self.assertIn('Revision root 65a55bbdf3629f916219feb3dcc7393ded1bc8db' | ||||
' not found', cm.exception.args[0]) | ' not found', cm.exception.args[0]) | ||||
mock_storage.revision_get.assert_has_calls([call([sha1_git_bin]), | mock_storage.revision_get.assert_has_calls([call([sha1_git_bin]), | ||||
call([sha1_git_root_bin])]) | call([sha1_git_root_bin])]) | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@patch('swh.web.common.service.query') | @patch('swh.web.common.service.query') | ||||
@istest | @istest | ||||
def lookup_revision_with_context(self, mock_query, mock_storage): | def lookup_revision_with_context(self, mock_query, mock_storage): | ||||
▲ Show 20 Lines • Show All 155 Lines • ▼ Show 20 Lines | def lookup_directory_with_revision_ko_revision_not_found(self, | ||||
# given | # given | ||||
mock_query.parse_hash_with_algorithms_or_throws.return_value = ('sha1', | mock_query.parse_hash_with_algorithms_or_throws.return_value = ('sha1', | ||||
b'123') | b'123') | ||||
mock_storage.revision_get.return_value = None | mock_storage.revision_get.return_value = None | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_directory_with_revision('123') | service.lookup_directory_with_revision('123') | ||||
self.assertIn('Revision 123 not found', cm.exception.args[0]) | self.assertIn('Revision 123 not found', cm.exception.args[0]) | ||||
mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with | mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with | ||||
('123', ['sha1'], 'Only sha1_git is supported.') | ('123', ['sha1'], 'Only sha1_git is supported.') | ||||
mock_storage.revision_get.assert_called_once_with([b'123']) | mock_storage.revision_get.assert_called_once_with([b'123']) | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@patch('swh.web.common.service.query') | @patch('swh.web.common.service.query') | ||||
@istest | @istest | ||||
Show All 12 Lines | def lookup_directory_with_revision_ko_revision_with_path_to_nowhere( | ||||
mock_storage.directory_entry_get_by_path.return_value = None | mock_storage.directory_entry_get_by_path.return_value = None | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_directory_with_revision( | service.lookup_directory_with_revision( | ||||
'123', | '123', | ||||
'path/to/something/unknown') | 'path/to/something/unknown') | ||||
self.assertIn("Directory/File 'path/to/something/unknown' " + | exception_text = cm.exception.args[0].lower() | ||||
"pointed to by revision 123 not found", | self.assertIn('directory or file', exception_text) | ||||
cm.exception.args[0]) | self.assertIn('path/to/something/unknown', exception_text) | ||||
self.assertIn('revision 123', exception_text) | |||||
self.assertIn('not found', exception_text) | |||||
mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with | mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with | ||||
('123', ['sha1'], 'Only sha1_git is supported.') | ('123', ['sha1'], 'Only sha1_git is supported.') | ||||
mock_storage.revision_get.assert_called_once_with([b'123']) | mock_storage.revision_get.assert_called_once_with([b'123']) | ||||
mock_storage.directory_entry_get_by_path.assert_called_once_with( | mock_storage.directory_entry_get_by_path.assert_called_once_with( | ||||
b'dir-id-as-sha1', [b'path', b'to', b'something', b'unknown']) | b'dir-id-as-sha1', [b'path', b'to', b'something', b'unknown']) | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
Show All 26 Lines | def lookup_directory_with_revision_ko_type_not_implemented( | ||||
mock_storage.content_get.return_value = stub_content | mock_storage.content_get.return_value = stub_content | ||||
# when | # when | ||||
with self.assertRaises(NotImplementedError) as cm: | with self.assertRaises(NotImplementedError) as cm: | ||||
service.lookup_directory_with_revision( | service.lookup_directory_with_revision( | ||||
'123', | '123', | ||||
'some/path/to/rev') | 'some/path/to/rev') | ||||
self.assertIn("Entity of type 'rev' not implemented.", | self.assertIn("Entity of type rev not implemented.", | ||||
cm.exception.args[0]) | cm.exception.args[0]) | ||||
# then | # then | ||||
mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with | mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with | ||||
('123', ['sha1'], 'Only sha1_git is supported.') | ('123', ['sha1'], 'Only sha1_git is supported.') | ||||
mock_storage.revision_get.assert_called_once_with([b'123']) | mock_storage.revision_get.assert_called_once_with([b'123']) | ||||
mock_storage.directory_entry_get_by_path.assert_called_once_with( | mock_storage.directory_entry_get_by_path.assert_called_once_with( | ||||
b'dir-id-as-sha1', [b'some', b'path', b'to', b'rev']) | b'dir-id-as-sha1', [b'some', b'path', b'to', b'rev']) | ||||
▲ Show 20 Lines • Show All 255 Lines • ▼ Show 20 Lines | def lookup_revision_msg_absent(self, mock_storage): | ||||
del stub_revision['message'] | del stub_revision['message'] | ||||
mock_storage.revision_get.return_value = stub_revision | mock_storage.revision_get.return_value = stub_revision | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_revision_message( | service.lookup_revision_message( | ||||
self.SHA1_SAMPLE) | self.SHA1_SAMPLE) | ||||
# then | # then | ||||
mock_storage.revision_get.assert_called_with( | mock_storage.revision_get.assert_called_with( | ||||
self.SHA1_SAMPLE_BIN) | [self.SHA1_SAMPLE_BIN]) | ||||
self.assertEqual(cm.exception.args[0], 'No message for revision ' | self.assertEqual( | ||||
'with sha1_git ' | cm.exception.args[0], | ||||
'18d8be353ed3480476f032475e7c233eff7371d5.') | 'No message for revision with sha1_git %s.' % self.SHA1_SAMPLE, | ||||
) | |||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_revision_msg_norev(self, mock_storage): | def lookup_revision_msg_norev(self, mock_storage): | ||||
# given | # given | ||||
mock_storage.revision_get.return_value = None | mock_storage.revision_get.return_value = None | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_revision_message( | service.lookup_revision_message( | ||||
self.SHA1_SAMPLE) | self.SHA1_SAMPLE) | ||||
# then | # then | ||||
mock_storage.revision_get.assert_called_with( | mock_storage.revision_get.assert_called_with( | ||||
self.SHA1_SAMPLE_BIN) | [self.SHA1_SAMPLE_BIN]) | ||||
self.assertEqual(cm.exception.args[0], 'Revision with sha1_git ' | self.assertEqual( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5 ' | cm.exception.args[0], | ||||
'not found.') | 'Revision with sha1_git %s not found.' % self.SHA1_SAMPLE, | ||||
) | |||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_revision_multiple(self, mock_storage): | def lookup_revision_multiple(self, mock_storage): | ||||
# given | # given | ||||
sha1 = self.SHA1_SAMPLE | sha1 = self.SHA1_SAMPLE | ||||
sha1_other = 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' | sha1_other = 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' | ||||
▲ Show 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | |||||
@istest | @istest | ||||
def lookup_content_raw_not_found(self, mock_storage): | def lookup_content_raw_not_found(self, mock_storage): | ||||
# given | # given | ||||
mock_storage.content_find = MagicMock(return_value=None) | mock_storage.content_find = MagicMock(return_value=None) | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_content_raw('sha1:' + self.SHA1_SAMPLE) | service.lookup_content_raw('sha1:' + self.SHA1_SAMPLE) | ||||
self.assertIn(cm.exc.args[0], | self.assertIn(cm.exception.args[0], | ||||
'Content with %s checksum equals to %s not found!' % | 'Content with %s checksum equals to %s not found!' % | ||||
('sha1', self.SHA1_SAMPLE)) | ('sha1', self.SHA1_SAMPLE)) | ||||
mock_storage.content_find.assert_called_with( | mock_storage.content_find.assert_called_with( | ||||
{'sha1': hash_to_bytes(self.SHA1_SAMPLE)}) | {'sha1': hash_to_bytes(self.SHA1_SAMPLE)}) | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_content_raw(self, mock_storage): | def lookup_content_raw(self, mock_storage): | ||||
# given | # given | ||||
Show All 20 Lines | |||||
def lookup_content_not_found(self, mock_storage): | def lookup_content_not_found(self, mock_storage): | ||||
# given | # given | ||||
mock_storage.content_find = MagicMock(return_value=None) | mock_storage.content_find = MagicMock(return_value=None) | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
# then | # then | ||||
service.lookup_content('sha1:%s' % self.SHA1_SAMPLE) | service.lookup_content('sha1:%s' % self.SHA1_SAMPLE) | ||||
self.assertIn(cm.exc.args[0], | self.assertIn(cm.exception.args[0], | ||||
'Content with %s checksum equals to %s not found!' % | 'Content with %s checksum equals to %s not found!' % | ||||
('sha1', self.SHA1_SAMPLE)) | ('sha1', self.SHA1_SAMPLE)) | ||||
mock_storage.content_find.assert_called_with( | mock_storage.content_find.assert_called_with( | ||||
{'sha1': self.SHA1_SAMPLE_BIN}) | {'sha1': self.SHA1_SAMPLE_BIN}) | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_content_with_sha1(self, mock_storage): | def lookup_content_with_sha1(self, mock_storage): | ||||
# given | # given | ||||
▲ Show 20 Lines • Show All 239 Lines • ▼ Show 20 Lines | |||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_revision_with_context_by_ko(self, mock_storage): | def lookup_revision_with_context_by_ko(self, mock_storage): | ||||
# given | # given | ||||
mock_storage.revision_get_by.return_value = None | mock_storage.revision_get_by.return_value = None | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | |||||
origin_id = 1 | origin_id = 1 | ||||
branch_name = 'master3' | branch_name = 'master3' | ||||
ts = None | ts = None | ||||
with self.assertRaises(NotFoundExc) as cm: | |||||
service.lookup_revision_with_context_by(origin_id, branch_name, ts, | service.lookup_revision_with_context_by(origin_id, branch_name, ts, | ||||
'sha1') | 'sha1') | ||||
# then | # then | ||||
self.assertIn( | self.assertIn( | ||||
'Revision with (origin_id: %s, branch_name: %s' | 'Revision with (origin_id: %s, branch_name: %s' | ||||
', ts: %s) not found.' % (origin_id, | ', ts: %s) not found.' % (origin_id, | ||||
branch_name, | branch_name, | ||||
ts), cm.exception.args[0]) | ts), cm.exception.args[0]) | ||||
mock_storage.revision_get_by.assert_called_once_with( | mock_storage.revision_get_by.assert_called_once_with( | ||||
origin_id, branch_name, ts) | origin_id, branch_name, limit=1, timestamp=ts) | ||||
@patch('swh.web.common.service.lookup_revision_with_context') | @patch('swh.web.common.service.lookup_revision_with_context') | ||||
@patch('swh.web.common.service.storage') | @patch('swh.web.common.service.storage') | ||||
@istest | @istest | ||||
def lookup_revision_with_context_by(self, mock_storage, | def lookup_revision_with_context_by(self, mock_storage, | ||||
mock_lookup_revision_with_context): | mock_lookup_revision_with_context): | ||||
# given | # given | ||||
stub_root_rev = {'id': 'root-rev-id'} | stub_root_rev = {'id': 'root-rev-id'} | ||||
▲ Show 20 Lines • Show All 184 Lines • Show Last 20 Lines |