Changeset View
Changeset View
Standalone View
Standalone View
swh/web/ui/tests/test_utils.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 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 datetime | import datetime | ||||
import dateutil | import dateutil | ||||
import unittest | import unittest | ||||
from unittest.mock import patch, call | from unittest.mock import patch, call | ||||
from nose.tools import istest | from nose.tools import istest, nottest | ||||
from swh.web.ui import utils | from swh.web.ui import utils | ||||
class UtilsTestCase(unittest.TestCase): | class UtilsTestCase(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
self.url_map = [dict(rule='/other/<slug>', | self.url_map = [dict(rule='/other/<slug>', | ||||
methods=set(['GET', 'POST', 'HEAD']), | methods=set(['GET', 'POST', 'HEAD']), | ||||
▲ Show 20 Lines • Show All 456 Lines • ▼ Show 20 Lines | def enrich_entity_with_sha1(self, mock_flask): | ||||
call('api_entity_by_uuid', | call('api_entity_by_uuid', | ||||
uuid='uuid-parent')]) | uuid='uuid-parent')]) | ||||
@patch('swh.web.ui.utils.flask') | @patch('swh.web.ui.utils.flask') | ||||
@istest | @istest | ||||
def enrich_revision_without_children_or_parent(self, mock_flask): | def enrich_revision_without_children_or_parent(self, mock_flask): | ||||
# given | # given | ||||
def url_for_test(fn, **data): | def url_for_test(fn, **data): | ||||
print(fn, data) | |||||
if fn == 'api_revision': | if fn == 'api_revision': | ||||
return '/api/revision/' + data['sha1_git'] + '/' | return '/api/revision/' + data['sha1_git'] + '/' | ||||
elif fn == 'api_revision_log': | elif fn == 'api_revision_log': | ||||
return '/api/revision/' + data['sha1_git'] + '/log/' | return '/api/revision/' + data['sha1_git'] + '/log/' | ||||
elif fn == 'api_directory': | elif fn == 'api_directory': | ||||
return '/api/directory/' + data['sha1_git'] + '/' | return '/api/directory/' + data['sha1_git'] + '/' | ||||
elif fn == 'api_person': | elif fn == 'api_person': | ||||
return '/api/person/' + data['person_id'] + '/' | return '/api/person/' + data['person_id'] + '/' | ||||
Show All 33 Lines | def enrich_revision_without_children_or_parent(self, mock_flask): | ||||
sha1_git='123')]) | sha1_git='123')]) | ||||
@patch('swh.web.ui.utils.flask') | @patch('swh.web.ui.utils.flask') | ||||
@istest | @istest | ||||
def enrich_revision_with_children_and_parent_no_dir(self, | def enrich_revision_with_children_and_parent_no_dir(self, | ||||
mock_flask): | mock_flask): | ||||
# given | # given | ||||
def url_for_test(fn, **data): | def url_for_test(fn, **data): | ||||
print(fn, data) | |||||
if fn == 'api_revision': | if fn == 'api_revision': | ||||
return '/api/revision/' + data['sha1_git'] + '/' | return '/api/revision/' + data['sha1_git'] + '/' | ||||
elif fn == 'api_revision_log': | elif fn == 'api_revision_log': | ||||
return '/api/revision/' + data['sha1_git'] + '/log/' | return '/api/revision/' + data['sha1_git'] + '/log/' | ||||
else: | else: | ||||
return '/api/revision/' + data['sha1_git_root'] + '/history/' + data['sha1_git'] + '/' # noqa | return '/api/revision/' + data['sha1_git_root'] + '/history/' + data['sha1_git'] + '/' # noqa | ||||
mock_flask.url_for.side_effect = url_for_test | mock_flask.url_for.side_effect = url_for_test | ||||
Show All 22 Lines | def enrich_revision_with_children_and_parent_no_dir(self, | ||||
call('api_revision_log', | call('api_revision_log', | ||||
sha1_git='rev-id'), | sha1_git='rev-id'), | ||||
call('api_revision_history', | call('api_revision_history', | ||||
sha1_git_root='sha1_git_root', | sha1_git_root='sha1_git_root', | ||||
sha1_git='123'), | sha1_git='123'), | ||||
call('api_revision_history', | call('api_revision_history', | ||||
sha1_git_root='sha1_git_root', | sha1_git_root='sha1_git_root', | ||||
sha1_git='456')]) | sha1_git='456')]) | ||||
@nottest | |||||
def url_for_rev_message_test(self, fn, **data): | |||||
if fn == 'api_revision': | |||||
return '/api/revision/' + data['sha1_git'] + '/' | |||||
elif fn == 'api_revision_log': | |||||
ardumont: Can you please remove the print statement? | |||||
return '/api/revision/' + data['sha1_git'] + '/log/' | |||||
elif fn == 'api_revision_raw_message': | |||||
return '/api/revision/' + data['sha1_git'] + '/raw/' | |||||
else: | |||||
return '/api/revision/' + data['sha1_git_root'] + '/history/' + data['sha1_git'] + '/' # noqa | |||||
@patch('swh.web.ui.utils.flask') | |||||
@istest | |||||
def enrich_revision_with_no_message(self, mock_flask): | |||||
# given | |||||
mock_flask.url_for.side_effect = self.url_for_rev_message_test | |||||
# when | |||||
actual_revision = utils.enrich_revision({ | |||||
'id': 'rev-id', | |||||
'message': None, | |||||
'parents': ['123'], | |||||
'children': ['456'], | |||||
}, context='sha1_git_root') | |||||
# then | |||||
self.assertEqual(actual_revision, { | |||||
'id': 'rev-id', | |||||
'url': '/api/revision/rev-id/', | |||||
'message': None, | |||||
'history_url': '/api/revision/rev-id/log/', | |||||
'parents': ['123'], | |||||
'parent_urls': ['/api/revision/sha1_git_root/history/123/'], | |||||
'children': ['456'], | |||||
'children_urls': ['/api/revision/sha1_git_root/history/456/'], | |||||
}) | |||||
mock_flask.url_for.assert_has_calls( | |||||
[call('api_revision', | |||||
sha1_git='rev-id'), | |||||
call('api_revision_log', | |||||
sha1_git='rev-id'), | |||||
call('api_revision_history', | |||||
sha1_git_root='sha1_git_root', | |||||
sha1_git='123'), | |||||
call('api_revision_history', | |||||
sha1_git_root='sha1_git_root', | |||||
sha1_git='456')]) | |||||
@patch('swh.web.ui.utils.flask') | |||||
@istest | |||||
def enrich_revision_with_invalid_message(self, mock_flask): | |||||
# given | |||||
mock_flask.url_for.side_effect = self.url_for_rev_message_test | |||||
# when | |||||
actual_revision = utils.enrich_revision({ | |||||
'id': 'rev-id', | |||||
'message': None, | |||||
'message_decoding_failed': True, | |||||
'parents': ['123'], | |||||
'children': ['456'], | |||||
Done Inline ActionsIf it's the same function as defined in prior test, define it globally once and reuse it. ardumont: If it's the same function as defined in prior test, define it globally once and reuse it.
Make… | |||||
}, context='sha1_git_root') | |||||
# then | |||||
self.assertEqual(actual_revision, { | |||||
'id': 'rev-id', | |||||
'url': '/api/revision/rev-id/', | |||||
'message': None, | |||||
'message_decoding_failed': True, | |||||
'message_url': '/api/revision/rev-id/raw/', | |||||
'history_url': '/api/revision/rev-id/log/', | |||||
'parents': ['123'], | |||||
'parent_urls': ['/api/revision/sha1_git_root/history/123/'], | |||||
'children': ['456'], | |||||
'children_urls': ['/api/revision/sha1_git_root/history/456/'], | |||||
}) | |||||
mock_flask.url_for.assert_has_calls( | |||||
[call('api_revision', | |||||
sha1_git='rev-id'), | |||||
call('api_revision_log', | |||||
sha1_git='rev-id'), | |||||
call('api_revision_history', | |||||
sha1_git_root='sha1_git_root', | |||||
sha1_git='123'), | |||||
call('api_revision_history', | |||||
sha1_git_root='sha1_git_root', | |||||
sha1_git='456')]) |
Can you please remove the print statement?