From 79457bb35a3965cde2c6e1a51378f12606916f4d Mon Sep 17 00:00:00 2001 From: Antoine Lambert <antoine.lambert@inria.fr> Date: Fri, 7 Dec 2018 15:27:00 +0100 Subject: [PATCH] api: Remove dead code related to unused entity and provenance Related T1411 --- docs/developers-info.rst | 2 - swh/web/api/urls.py | 1 - swh/web/api/utils.py | 14 ---- swh/web/api/views/content.py | 31 --------- swh/web/api/views/entity.py | 22 ------ swh/web/common/converters.py | 17 ----- swh/web/common/service.py | 35 ---------- swh/web/tests/api/test_utils.py | 33 --------- swh/web/tests/api/views/test_content.py | 65 +----------------- swh/web/tests/api/views/test_entity.py | 91 ------------------------- swh/web/tests/api/views/test_stat.py | 2 - swh/web/tests/common/test_converters.py | 26 ------- swh/web/tests/common/test_service.py | 69 ------------------- 13 files changed, 1 insertion(+), 407 deletions(-) delete mode 100644 swh/web/api/views/entity.py delete mode 100644 swh/web/tests/api/views/test_entity.py diff --git a/docs/developers-info.rst b/docs/developers-info.rst index 72f25993a..847886b3b 100644 --- a/docs/developers-info.rst +++ b/docs/developers-info.rst @@ -96,8 +96,6 @@ swh-web API application about contents. * :mod:`swh.web.api.views.directory`: Implementation of API endpoints for getting information about directories. - * :mod:`swh.web.api.views.entity`: Implementation of API endpoints for getting information - about entities. * :mod:`swh.web.api.views.origin`: Implementation of API endpoints for getting information about origins. * :mod:`swh.web.api.views.person`: Implementation of API endpoints for getting information diff --git a/swh/web/api/urls.py b/swh/web/api/urls.py index 12043f31d..afbcaf44e 100644 --- a/swh/web/api/urls.py +++ b/swh/web/api/urls.py @@ -5,7 +5,6 @@ import swh.web.api.views.content # noqa import swh.web.api.views.directory # noqa -import swh.web.api.views.entity # noqa import swh.web.api.views.identifiers # noqa import swh.web.api.views.origin # noqa import swh.web.api.views.origin_save # noqa diff --git a/swh/web/api/utils.py b/swh/web/api/utils.py index 6e65ca2f7..722be7bd1 100644 --- a/swh/web/api/utils.py +++ b/swh/web/api/utils.py @@ -160,20 +160,6 @@ def enrich_content(content, top_url=False, query_string=None): return content -def enrich_entity(entity): - """Enrich entity with - - """ - if 'uuid' in entity: - entity['uuid_url'] = reverse('api-entity', - url_args={'uuid': entity['uuid']}) - - if 'parent' in entity and entity['parent']: - entity['parent_url'] = reverse('api-entity', - url_args={'uuid': entity['parent']}) - return entity - - def _get_path_list(path_string): """Helper for enrich_revision: get a list of the sha1 id of the navigation breadcrumbs, ordered from the oldest to the most recent. diff --git a/swh/web/api/views/content.py b/swh/web/api/views/content.py index 751508611..f6e30ede6 100644 --- a/swh/web/api/views/content.py +++ b/swh/web/api/views/content.py @@ -16,37 +16,6 @@ from swh.web.api.apiurls import api_route from swh.web.api.views.utils import api_lookup -@api_route(r'/content/(?P<q>.+)/provenance/', 'api-content-provenance') -@api_doc('/content/provenance/', tags=['hidden']) -def api_content_provenance(request, q): - """Return content's provenance information if any. - - """ - def _enrich_revision(provenance): - p = provenance.copy() - p['revision_url'] = \ - reverse('api-revision', - url_args={'sha1_git': provenance['revision']}) - p['content_url'] = \ - reverse('api-content', - url_args={'q': 'sha1_git:%s' % provenance['content']}) - p['origin_url'] = \ - reverse('api-origin', url_args={'origin_id': provenance['origin']}) - p['origin_visits_url'] = \ - reverse('api-origin-visits', - url_args={'origin_id': provenance['origin']}) - p['origin_visit_url'] = \ - reverse('api-origin-visit', - url_args={'origin_id': provenance['origin'], - 'visit_id': provenance['visit']}) - return p - - return api_lookup( - service.lookup_content_provenance, q, - notfound_msg='Content with {} not found.'.format(q), - enrich_fn=_enrich_revision) - - @api_route(r'/content/(?P<q>.+)/filetype/', 'api-content-filetype') @api_doc('/content/filetype/') def api_content_filetype(request, q): diff --git a/swh/web/api/views/entity.py b/swh/web/api/views/entity.py deleted file mode 100644 index 53d8b9655..000000000 --- a/swh/web/api/views/entity.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright (C) 2015-2018 The Software Heritage developers -# See the AUTHORS file at the top-level directory of this distribution -# License: GNU Affero General Public License version 3, or any later version -# See top-level LICENSE file for more information - -from swh.web.common import service -from swh.web.api import utils -from swh.web.api.apidoc import api_doc -from swh.web.api.apiurls import api_route -from swh.web.api.views.utils import api_lookup - - -@api_route(r'/entity/(?P<uuid>.+)/', 'api-entity') -@api_doc('/entity/', tags=['hidden']) -def api_entity_by_uuid(request, uuid): - """Return content information if content is found. - - """ - return api_lookup( - service.lookup_entity_by_uuid, uuid, - notfound_msg="Entity with uuid '%s' not found." % uuid, - enrich_fn=utils.enrich_entity) diff --git a/swh/web/common/converters.py b/swh/web/common/converters.py index 608a34f70..dba2d7433 100644 --- a/swh/web/common/converters.py +++ b/swh/web/common/converters.py @@ -179,23 +179,6 @@ def from_swh(dict_swh, hashess={}, bytess={}, dates={}, blacklist={}, return new_dict -def from_provenance(provenance): - """Convert from a swh provenance information to a provenance dictionary. - - Args: - provenance (dict): Dictionary with the following keys: - - content (sha1_git): the content's identifier - - revision (sha1_git): the revision the content was seen - - origin (int): the origin the content was seen - - visit (int): the visit it occurred - - path (bytes): the path the content was seen at - - """ - return from_swh(provenance, - hashess={'content', 'revision'}, - bytess={'path'}) - - def from_origin(origin): """Convert from a swh origin to an origin dictionary. diff --git a/swh/web/common/service.py b/swh/web/common/service.py index d49ff90be..2a4eef9f7 100644 --- a/swh/web/common/service.py +++ b/swh/web/common/service.py @@ -107,23 +107,6 @@ def search_hash(q): return {'found': found is not None} -def lookup_content_provenance(q): - """Return provenance information from a specified content. - - Args: - q: query string of the form <hash_algo:hash> - - Yields: - provenance information (dict) list if the content is found. - - """ - algo, hash = query.parse_hash(q) - provenances = storage.content_find_provenance({algo: hash}) - if not provenances: - return None - return (converters.from_provenance(p) for p in provenances) - - def _lookup_content_sha1(q): """Given a possible input, query for the content's sha1. @@ -882,24 +865,6 @@ def lookup_latest_origin_snapshot(origin_id, allowed_statuses=None): return converters.from_snapshot(snapshot) -def lookup_entity_by_uuid(uuid): - """Return the entity's hierarchy from its uuid. - - Args: - uuid: entity's identifier. - - Returns: - List of hierarchy entities from the entity with uuid. - - """ - uuid = query.parse_uuid4(uuid) - for entity in storage.entity_get(uuid): - entity = converters.from_swh(entity, - convert={'last_seen', 'uuid'}, - convert_fn=lambda x: str(x)) - yield entity - - def lookup_revision_through(revision, limit=100): """Retrieve a revision from the criterion stored in revision dictionary. diff --git a/swh/web/tests/api/test_utils.py b/swh/web/tests/api/test_utils.py index 6d35d857e..749b8df2f 100644 --- a/swh/web/tests/api/test_utils.py +++ b/swh/web/tests/api/test_utils.py @@ -396,39 +396,6 @@ class UtilsTestCase(SWHWebTestCase): mock_django_reverse.reset() - def test_enrich_entity_identity(self): - # when/then - self.assertEqual(utils.enrich_content({'id': '123'}), - {'id': '123'}) - - @patch('swh.web.api.utils.reverse') - def test_enrich_entity_with_sha1(self, mock_django_reverse): - # given - def reverse_test(view_name, url_args): - return '/api/entity/' + url_args['uuid'] + '/' - - mock_django_reverse.side_effect = reverse_test - - # when - actual_entity = utils.enrich_entity({ - 'uuid': 'uuid-1', - 'parent': 'uuid-parent', - 'name': 'something' - }) - - # then - self.assertEqual(actual_entity, { - 'uuid': 'uuid-1', - 'uuid_url': '/api/entity/uuid-1/', - 'parent': 'uuid-parent', - 'parent_url': '/api/entity/uuid-parent/', - 'name': 'something', - }) - - mock_django_reverse.assert_has_calls( - [call('api-entity', url_args={'uuid': 'uuid-1'}), - call('api-entity', url_args={'uuid': 'uuid-parent'})]) - def _reverse_context_test(self, view_name, url_args): if view_name == 'api-revision': return '/api/revision/%s/' % url_args['sha1_git'] diff --git a/swh/web/tests/api/views/test_content.py b/swh/web/tests/api/views/test_content.py index 4b08a2baa..2856bfe91 100644 --- a/swh/web/tests/api/views/test_content.py +++ b/swh/web/tests/api/views/test_content.py @@ -4,7 +4,7 @@ # See top-level LICENSE file for more information from rest_framework.test import APITestCase -from unittest.mock import patch, MagicMock +from unittest.mock import patch from swh.web.tests.testcase import SWHWebTestCase @@ -307,65 +307,6 @@ class ContentApiTestCase(SWHWebTestCase, APITestCase): mock_service.lookup_content_license.assert_called_once_with( 'sha1:40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03') - @patch('swh.web.api.views.content.service') - def test_api_content_provenance(self, mock_service): - stub_provenances = [{ - 'origin': 1, - 'visit': 2, - 'revision': 'b04caf10e9535160d90e874b45aa426de762f19f', - 'content': '34571b8614fcd89ccd17ca2b1d9e66c5b00a6d03', - 'path': 'octavio-3.4.0/octave.html/doc_002dS_005fISREG.html' - }] - mock_service.lookup_content_provenance.return_value = stub_provenances - - # when - rv = self.client.get( - '/api/1/content/' - 'sha1_git:34571b8614fcd89ccd17ca2b1d9e66c5b00a6d03/provenance/') - - # then - self.assertEqual(rv.status_code, 200) - self.assertEqual(rv['Content-Type'], 'application/json') - self.assertEqual(rv.data, [{ - 'origin': 1, - 'visit': 2, - 'origin_url': '/api/1/origin/1/', - 'origin_visits_url': '/api/1/origin/1/visits/', - 'origin_visit_url': '/api/1/origin/1/visit/2/', - 'revision': 'b04caf10e9535160d90e874b45aa426de762f19f', - 'revision_url': '/api/1/revision/' - 'b04caf10e9535160d90e874b45aa426de762f19f/', - 'content': '34571b8614fcd89ccd17ca2b1d9e66c5b00a6d03', - 'content_url': '/api/1/content/' - 'sha1_git:34571b8614fcd89ccd17ca2b1d9e66c5b00a6d03/', - 'path': 'octavio-3.4.0/octave.html/doc_002dS_005fISREG.html' - }]) - - mock_service.lookup_content_provenance.assert_called_once_with( - 'sha1_git:34571b8614fcd89ccd17ca2b1d9e66c5b00a6d03') - - @patch('swh.web.api.views.content.service') - def test_api_content_provenance_sha_not_found(self, mock_service): - # given - mock_service.lookup_content_provenance.return_value = None - - # when - rv = self.client.get( - '/api/1/content/sha1:40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03/' - 'provenance/') - - # then - self.assertEqual(rv.status_code, 404) - self.assertEqual(rv['Content-Type'], 'application/json') - self.assertEqual(rv.data, { - 'exception': 'NotFoundExc', - 'reason': 'Content with sha1:40e71b8614fcd89ccd17ca2b1d9e6' - '6c5b00a6d03 not found.' - }) - - mock_service.lookup_content_provenance.assert_called_once_with( - 'sha1:40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03') - @patch('swh.web.api.views.content.service') def test_api_content_metadata(self, mock_service): # given @@ -416,7 +357,6 @@ class ContentApiTestCase(SWHWebTestCase, APITestCase): def test_api_content_not_found_as_json(self, mock_service): # given mock_service.lookup_content.return_value = None - mock_service.lookup_content_provenance = MagicMock() # when rv = self.client.get( @@ -434,13 +374,11 @@ class ContentApiTestCase(SWHWebTestCase, APITestCase): mock_service.lookup_content.assert_called_once_with( 'sha256:83c0e67cc80f60caf1fcbec2d84b0ccd7968b3' 'be4735637006560c') - mock_service.lookup_content_provenance.called = False @patch('swh.web.api.views.content.service') def test_api_content_not_found_as_yaml(self, mock_service): # given mock_service.lookup_content.return_value = None - mock_service.lookup_content_provenance = MagicMock() # when rv = self.client.get( @@ -460,7 +398,6 @@ class ContentApiTestCase(SWHWebTestCase, APITestCase): mock_service.lookup_content.assert_called_once_with( 'sha256:83c0e67cc80f60caf1fcbec2d84b0ccd7968b3' 'be4735637006560c') - mock_service.lookup_content_provenance.called = False @patch('swh.web.api.views.content.service') def test_api_content_raw_ko_not_found(self, mock_service): diff --git a/swh/web/tests/api/views/test_entity.py b/swh/web/tests/api/views/test_entity.py deleted file mode 100644 index 1a6d35c21..000000000 --- a/swh/web/tests/api/views/test_entity.py +++ /dev/null @@ -1,91 +0,0 @@ -# Copyright (C) 2015-2018 The Software Heritage developers -# See the AUTHORS file at the top-level directory of this distribution -# License: GNU Affero General Public License version 3, or any later version -# See top-level LICENSE file for more information - -from rest_framework.test import APITestCase -from unittest.mock import patch - -from swh.web.common.exc import BadInputExc - -from swh.web.tests.testcase import SWHWebTestCase - - -class EntityApiTestCase(SWHWebTestCase, APITestCase): - - @patch('swh.web.api.views.entity.service') - def test_api_lookup_entity_by_uuid_not_found(self, mock_service): - # when - mock_service.lookup_entity_by_uuid.return_value = [] - - # when - rv = self.client.get('/api/1/entity/' - '5f4d4c51-498a-4e28-88b3-b3e4e8396cba/') - - self.assertEqual(rv.status_code, 404) - self.assertEqual(rv['Content-Type'], 'application/json') - self.assertEqual(rv.data, { - 'exception': 'NotFoundExc', - 'reason': - "Entity with uuid '5f4d4c51-498a-4e28-88b3-b3e4e8396cba' not " + - "found."}) - - mock_service.lookup_entity_by_uuid.assert_called_once_with( - '5f4d4c51-498a-4e28-88b3-b3e4e8396cba') - - @patch('swh.web.api.views.entity.service') - def test_api_lookup_entity_by_uuid_bad_request(self, mock_service): - # when - mock_service.lookup_entity_by_uuid.side_effect = BadInputExc( - 'bad input: uuid malformed!') - - # when - rv = self.client.get('/api/1/entity/uuid malformed/') - - self.assertEqual(rv.status_code, 400) - self.assertEqual(rv['Content-Type'], 'application/json') - self.assertEqual(rv.data, { - 'exception': 'BadInputExc', - 'reason': 'bad input: uuid malformed!'}) - mock_service.lookup_entity_by_uuid.assert_called_once_with( - 'uuid malformed') - - @patch('swh.web.api.views.entity.service') - def test_api_lookup_entity_by_uuid(self, mock_service): - # when - stub_entities = [ - { - 'uuid': '34bd6b1b-463f-43e5-a697-785107f598e4', - 'parent': 'aee991a0-f8d7-4295-a201-d1ce2efc9fb2' - }, - { - 'uuid': 'aee991a0-f8d7-4295-a201-d1ce2efc9fb2' - } - ] - mock_service.lookup_entity_by_uuid.return_value = stub_entities - - expected_entities = [ - { - 'uuid': '34bd6b1b-463f-43e5-a697-785107f598e4', - 'uuid_url': '/api/1/entity/34bd6b1b-463f-43e5-a697-' - '785107f598e4/', - 'parent': 'aee991a0-f8d7-4295-a201-d1ce2efc9fb2', - 'parent_url': '/api/1/entity/aee991a0-f8d7-4295-a201-' - 'd1ce2efc9fb2/' - }, - { - 'uuid': 'aee991a0-f8d7-4295-a201-d1ce2efc9fb2', - 'uuid_url': '/api/1/entity/aee991a0-f8d7-4295-a201-' - 'd1ce2efc9fb2/' - } - ] - - # when - rv = self.client.get('/api/1/entity' - '/34bd6b1b-463f-43e5-a697-785107f598e4/') - - self.assertEqual(rv.status_code, 200) - self.assertEqual(rv['Content-Type'], 'application/json') - self.assertEqual(rv.data, expected_entities) - mock_service.lookup_entity_by_uuid.assert_called_once_with( - '34bd6b1b-463f-43e5-a697-785107f598e4') diff --git a/swh/web/tests/api/views/test_stat.py b/swh/web/tests/api/views/test_stat.py index afe9b9755..99f1f54cb 100644 --- a/swh/web/tests/api/views/test_stat.py +++ b/swh/web/tests/api/views/test_stat.py @@ -69,8 +69,6 @@ class StatApiTestCase(SWHWebTestCase, APITestCase): "directory_entry_dir": 209167, "directory_entry_file": 1807094, "directory_entry_rev": 0, - "entity": 0, - "entity_history": 0, "origin": 1096, "person": 0, "release": 8584, diff --git a/swh/web/tests/common/test_converters.py b/swh/web/tests/common/test_converters.py index 47e061cfe..43362c7c8 100644 --- a/swh/web/tests/common/test_converters.py +++ b/swh/web/tests/common/test_converters.py @@ -170,32 +170,6 @@ class ConvertersTestCase(SWHWebTestCase): # when self.assertIsNone(converters.from_swh(None)) - def test_from_provenance(self): - # given - input_provenance = { - 'origin': 10, - 'visit': 1, - 'content': hashutil.hash_to_bytes( - '321caf10e9535160d90e874b45aa426de762f19f'), - 'revision': hashutil.hash_to_bytes( - '123caf10e9535160d90e874b45aa426de762f19f'), - 'path': b'octave-3.4.0/doc/interpreter/octave/doc_002dS_005fISREG' - } - - expected_provenance = { - 'origin': 10, - 'visit': 1, - 'content': '321caf10e9535160d90e874b45aa426de762f19f', - 'revision': '123caf10e9535160d90e874b45aa426de762f19f', - 'path': 'octave-3.4.0/doc/interpreter/octave/doc_002dS_005fISREG' - } - - # when - actual_provenance = converters.from_provenance(input_provenance) - - # then - self.assertEqual(actual_provenance, expected_provenance) - def test_from_origin(self): # given origin_input = { diff --git a/swh/web/tests/common/test_service.py b/swh/web/tests/common/test_service.py index a9309d070..fe1028012 100644 --- a/swh/web/tests/common/test_service.py +++ b/swh/web/tests/common/test_service.py @@ -522,54 +522,6 @@ class ServiceTestCase(SWHWebTestCase): mock_idx_storage.content_fossology_license_get.assert_called_with( [hash_to_bytes('123caf10e9535160d90e874b45aa426de762f19f')]) - @patch('swh.web.common.service.storage') - def test_lookup_content_provenance(self, mock_storage): - # given - mock_storage.content_find_provenance = MagicMock( - return_value=(p for p in [{ - 'content': hash_to_bytes( - '123caf10e9535160d90e874b45aa426de762f19f'), - 'revision': hash_to_bytes( - '456caf10e9535160d90e874b45aa426de762f19f'), - 'origin': 100, - 'visit': 1, - 'path': b'octavio-3.4.0/octave.html/doc_002dS_005fISREG.html' - }])) - expected_provenances = [{ - 'content': '123caf10e9535160d90e874b45aa426de762f19f', - 'revision': '456caf10e9535160d90e874b45aa426de762f19f', - 'origin': 100, - 'visit': 1, - 'path': 'octavio-3.4.0/octave.html/doc_002dS_005fISREG.html' - }] - - # when - actual_provenances = service.lookup_content_provenance( - 'sha1_git:123caf10e9535160d90e874b45aa426de762f19f') - - # then - self.assertEqual(list(actual_provenances), expected_provenances) - - mock_storage.content_find_provenance.assert_called_with( - {'sha1_git': - hash_to_bytes('123caf10e9535160d90e874b45aa426de762f19f')}) - - @patch('swh.web.common.service.storage') - def test_lookup_content_provenance_not_found(self, mock_storage): - # given - mock_storage.content_find_provenance = MagicMock(return_value=None) - - # when - actual_provenances = service.lookup_content_provenance( - 'sha1_git:456caf10e9535160d90e874b45aa426de762f19f') - - # then - self.assertIsNone(actual_provenances) - - mock_storage.content_find_provenance.assert_called_with( - {'sha1_git': - hash_to_bytes('456caf10e9535160d90e874b45aa426de762f19f')}) - @patch('swh.web.common.service.storage') def test_stat_counters(self, mock_storage): # given @@ -579,8 +531,6 @@ class ServiceTestCase(SWHWebTestCase): "directory_entry_dir": 209167, "directory_entry_file": 1807094, "directory_entry_rev": 0, - "entity": 0, - "entity_history": 0, "origin": 1096, "person": 0, "release": 8584, @@ -1855,25 +1805,6 @@ class ServiceTestCase(SWHWebTestCase): mock_lookup_revision_with_context.assert_called_once_with( stub_root_rev, sha1_git, 100) - @patch('swh.web.common.service.storage') - @patch('swh.web.common.service.query') - def test_lookup_entity_by_uuid(self, mock_query, mock_storage): - # given - uuid_test = 'correct-uuid' - mock_query.parse_uuid4.return_value = uuid_test - stub_entities = [{'uuid': uuid_test}] - - mock_storage.entity_get.return_value = stub_entities - - # when - actual_entities = list(service.lookup_entity_by_uuid(uuid_test)) - - # then - self.assertEqual(actual_entities, stub_entities) - - mock_query.parse_uuid4.assert_called_once_with(uuid_test) - mock_storage.entity_get.assert_called_once_with(uuid_test) - def test_lookup_revision_through_ko_not_implemented(self): # then with self.assertRaises(NotImplementedError): -- GitLab