Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/common/test_utils.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 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 | ||||
from nose.tools import istest | |||||
from unittest.mock import patch | from unittest.mock import patch | ||||
from swh.web.common import utils | from swh.web.common import utils | ||||
from swh.web.common.exc import BadInputExc | from swh.web.common.exc import BadInputExc | ||||
from swh.web.tests.testcase import SWHWebTestCase | from swh.web.tests.testcase import SWHWebTestCase | ||||
class UtilsTestCase(SWHWebTestCase): | class UtilsTestCase(SWHWebTestCase): | ||||
@istest | def test_shorten_path_noop(self): | ||||
def shorten_path_noop(self): | |||||
noops = [ | noops = [ | ||||
'/api/', | '/api/', | ||||
'/browse/', | '/browse/', | ||||
'/content/symbol/foobar/' | '/content/symbol/foobar/' | ||||
] | ] | ||||
for noop in noops: | for noop in noops: | ||||
self.assertEqual( | self.assertEqual( | ||||
utils.shorten_path(noop), | utils.shorten_path(noop), | ||||
noop | noop | ||||
) | ) | ||||
@istest | def test_shorten_path_sha1(self): | ||||
def shorten_path_sha1(self): | |||||
sha1 = 'aafb16d69fd30ff58afdd69036a26047f3aebdc6' | sha1 = 'aafb16d69fd30ff58afdd69036a26047f3aebdc6' | ||||
short_sha1 = sha1[:8] + '...' | short_sha1 = sha1[:8] + '...' | ||||
templates = [ | templates = [ | ||||
'/api/1/content/sha1:%s/', | '/api/1/content/sha1:%s/', | ||||
'/api/1/content/sha1_git:%s/', | '/api/1/content/sha1_git:%s/', | ||||
'/api/1/directory/%s/', | '/api/1/directory/%s/', | ||||
'/api/1/content/sha1:%s/ctags/', | '/api/1/content/sha1:%s/ctags/', | ||||
] | ] | ||||
for template in templates: | for template in templates: | ||||
self.assertEqual( | self.assertEqual( | ||||
utils.shorten_path(template % sha1), | utils.shorten_path(template % sha1), | ||||
template % short_sha1 | template % short_sha1 | ||||
) | ) | ||||
@istest | def test_shorten_path_sha256(self): | ||||
def shorten_path_sha256(self): | |||||
sha256 = ('aafb16d69fd30ff58afdd69036a26047' | sha256 = ('aafb16d69fd30ff58afdd69036a26047' | ||||
'213add102934013a014dfca031c41aef') | '213add102934013a014dfca031c41aef') | ||||
short_sha256 = sha256[:8] + '...' | short_sha256 = sha256[:8] + '...' | ||||
templates = [ | templates = [ | ||||
'/api/1/content/sha256:%s/', | '/api/1/content/sha256:%s/', | ||||
'/api/1/directory/%s/', | '/api/1/directory/%s/', | ||||
'/api/1/content/sha256:%s/filetype/', | '/api/1/content/sha256:%s/filetype/', | ||||
] | ] | ||||
for template in templates: | for template in templates: | ||||
self.assertEqual( | self.assertEqual( | ||||
utils.shorten_path(template % sha256), | utils.shorten_path(template % sha256), | ||||
template % short_sha256 | template % short_sha256 | ||||
) | ) | ||||
@istest | def test_parse_timestamp(self): | ||||
def parse_timestamp(self): | |||||
input_timestamps = [ | input_timestamps = [ | ||||
None, | None, | ||||
'2016-01-12', | '2016-01-12', | ||||
'2016-01-12T09:19:12+0100', | '2016-01-12T09:19:12+0100', | ||||
'Today is January 1, 2047 at 8:21:00AM', | 'Today is January 1, 2047 at 8:21:00AM', | ||||
'1452591542', | '1452591542', | ||||
] | ] | ||||
output_dates = [ | output_dates = [ | ||||
None, | None, | ||||
datetime.datetime(2016, 1, 12, 0, 0), | datetime.datetime(2016, 1, 12, 0, 0), | ||||
datetime.datetime(2016, 1, 12, 8, 19, 12, | datetime.datetime(2016, 1, 12, 8, 19, 12, | ||||
tzinfo=datetime.timezone.utc), | tzinfo=datetime.timezone.utc), | ||||
datetime.datetime(2047, 1, 1, 8, 21), | datetime.datetime(2047, 1, 1, 8, 21), | ||||
datetime.datetime(2016, 1, 12, 9, 39, 2, | datetime.datetime(2016, 1, 12, 9, 39, 2, | ||||
tzinfo=datetime.timezone.utc), | tzinfo=datetime.timezone.utc), | ||||
] | ] | ||||
for ts, exp_date in zip(input_timestamps, output_dates): | for ts, exp_date in zip(input_timestamps, output_dates): | ||||
self.assertEquals(utils.parse_timestamp(ts), exp_date) | self.assertEquals(utils.parse_timestamp(ts), exp_date) | ||||
@istest | def test_format_utc_iso_date(self): | ||||
def format_utc_iso_date(self): | |||||
self.assertEqual(utils.format_utc_iso_date('2017-05-04T13:27:13+02:00'), # noqa | self.assertEqual(utils.format_utc_iso_date('2017-05-04T13:27:13+02:00'), # noqa | ||||
'04 May 2017, 11:27 UTC') | '04 May 2017, 11:27 UTC') | ||||
@istest | def test_gen_path_info(self): | ||||
def gen_path_info(self): | |||||
input_path = '/home/user/swh-environment/swh-web/' | input_path = '/home/user/swh-environment/swh-web/' | ||||
expected_result = [ | expected_result = [ | ||||
{'name': 'home', 'path': 'home'}, | {'name': 'home', 'path': 'home'}, | ||||
{'name': 'user', 'path': 'home/user'}, | {'name': 'user', 'path': 'home/user'}, | ||||
{'name': 'swh-environment', 'path': 'home/user/swh-environment'}, | {'name': 'swh-environment', 'path': 'home/user/swh-environment'}, | ||||
{'name': 'swh-web', 'path': 'home/user/swh-environment/swh-web'} | {'name': 'swh-web', 'path': 'home/user/swh-environment/swh-web'} | ||||
] | ] | ||||
path_info = utils.gen_path_info(input_path) | path_info = utils.gen_path_info(input_path) | ||||
self.assertEquals(path_info, expected_result) | self.assertEquals(path_info, expected_result) | ||||
input_path = 'home/user/swh-environment/swh-web' | input_path = 'home/user/swh-environment/swh-web' | ||||
path_info = utils.gen_path_info(input_path) | path_info = utils.gen_path_info(input_path) | ||||
self.assertEquals(path_info, expected_result) | self.assertEquals(path_info, expected_result) | ||||
@patch('swh.web.common.utils.service') | @patch('swh.web.common.utils.service') | ||||
@istest | def test_get_origin_visits(self, mock_service): | ||||
def get_origin_visits(self, mock_service): | |||||
mock_service.MAX_LIMIT = 2 | mock_service.MAX_LIMIT = 2 | ||||
def _lookup_origin_visits(*args, **kwargs): | def _lookup_origin_visits(*args, **kwargs): | ||||
if kwargs['last_visit'] is None: | if kwargs['last_visit'] is None: | ||||
return [{'visit': 1, | return [{'visit': 1, | ||||
'date': '2017-05-06T00:59:10+00:00', | 'date': '2017-05-06T00:59:10+00:00', | ||||
'metadata': {}}, | 'metadata': {}}, | ||||
{'visit': 2, | {'visit': 2, | ||||
Show All 13 Lines | def test_get_origin_visits(self, mock_service): | ||||
'type': 'git', | 'type': 'git', | ||||
'url': 'https://github.com/foo/bar', | 'url': 'https://github.com/foo/bar', | ||||
} | } | ||||
origin_visits = utils.get_origin_visits(origin_info) | origin_visits = utils.get_origin_visits(origin_info) | ||||
self.assertEqual(len(origin_visits), 3) | self.assertEqual(len(origin_visits), 3) | ||||
@istest | def test_get_swh_persisent_id(self): | ||||
def get_swh_persisent_id(self): | |||||
swh_object_type = 'content' | swh_object_type = 'content' | ||||
sha1_git = 'aafb16d69fd30ff58afdd69036a26047f3aebdc6' | sha1_git = 'aafb16d69fd30ff58afdd69036a26047f3aebdc6' | ||||
expected_swh_id = 'swh:1:cnt:' + sha1_git | expected_swh_id = 'swh:1:cnt:' + sha1_git | ||||
self.assertEqual(utils.get_swh_persistent_id(swh_object_type, sha1_git), # noqa | self.assertEqual(utils.get_swh_persistent_id(swh_object_type, sha1_git), # noqa | ||||
expected_swh_id) | expected_swh_id) | ||||
with self.assertRaises(BadInputExc) as cm: | with self.assertRaises(BadInputExc) as cm: | ||||
utils.get_swh_persistent_id('foo', sha1_git) | utils.get_swh_persistent_id('foo', sha1_git) | ||||
self.assertIn('Invalid object', cm.exception.args[0]) | self.assertIn('Invalid object', cm.exception.args[0]) | ||||
with self.assertRaises(BadInputExc) as cm: | with self.assertRaises(BadInputExc) as cm: | ||||
utils.get_swh_persistent_id(swh_object_type, 'not a valid id') | utils.get_swh_persistent_id(swh_object_type, 'not a valid id') | ||||
self.assertIn('Invalid object', cm.exception.args[0]) | self.assertIn('Invalid object', cm.exception.args[0]) |