test_libs.py
133 lines
| 5.0 KiB
| text/x-python
|
PythonLexer
r1372 | # -*- coding: utf-8 -*- | |||
""" | ||||
rhodecode.tests.test_libs | ||||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
r1416 | Package for testing various lib/helper functions in rhodecode | |||
r1818 | ||||
r1372 | :created_on: Jun 9, 2011 | |||
r1824 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> | |||
r1532 | :license: GPLv3, see COPYING for more details. | |||
r1372 | """ | |||
r1532 | # This program is free software: you can redistribute it and/or modify | |||
# it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation, either version 3 of the License, or | ||||
# (at your option) any later version. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
r1372 | ||||
r2317 | import unittest | |||
import datetime | ||||
from rhodecode.tests import * | ||||
r1372 | ||||
proto = 'http' | ||||
TEST_URLS = [ | ||||
('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], | ||||
'%s://127.0.0.1' % proto), | ||||
('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], | ||||
'%s://127.0.0.1' % proto), | ||||
('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], | ||||
'%s://127.0.0.1' % proto), | ||||
('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'], | ||||
'%s://127.0.0.1:8080' % proto), | ||||
('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'], | ||||
'%s://domain.org' % proto), | ||||
('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org', | ||||
'8080'], | ||||
'%s://domain.org:8080' % proto), | ||||
] | ||||
proto = 'https' | ||||
TEST_URLS += [ | ||||
('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], | ||||
'%s://127.0.0.1' % proto), | ||||
('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], | ||||
'%s://127.0.0.1' % proto), | ||||
('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'], | ||||
'%s://127.0.0.1' % proto), | ||||
('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'], | ||||
'%s://127.0.0.1:8080' % proto), | ||||
('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'], | ||||
'%s://domain.org' % proto), | ||||
('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org', | ||||
'8080'], | ||||
'%s://domain.org:8080' % proto), | ||||
] | ||||
class TestLibs(unittest.TestCase): | ||||
def test_uri_filter(self): | ||||
r2109 | from rhodecode.lib.utils2 import uri_filter | |||
r1372 | ||||
for url in TEST_URLS: | ||||
self.assertEqual(uri_filter(url[0]), url[1]) | ||||
def test_credentials_filter(self): | ||||
r2109 | from rhodecode.lib.utils2 import credentials_filter | |||
r1372 | ||||
for url in TEST_URLS: | ||||
self.assertEqual(credentials_filter(url[0]), url[2]) | ||||
def test_str2bool(self): | ||||
r2109 | from rhodecode.lib.utils2 import str2bool | |||
r1372 | test_cases = [ | |||
('t', True), | ||||
('true', True), | ||||
('y', True), | ||||
('yes', True), | ||||
('on', True), | ||||
('1', True), | ||||
('Y', True), | ||||
('yeS', True), | ||||
('Y', True), | ||||
('TRUE', True), | ||||
('T', True), | ||||
('False', False), | ||||
('F', False), | ||||
('FALSE', False), | ||||
('0', False), | ||||
('-1', False), | ||||
('', False), ] | ||||
for case in test_cases: | ||||
self.assertEqual(str2bool(case[0]), case[1]) | ||||
r1713 | def test_mention_extractor(self): | |||
r2109 | from rhodecode.lib.utils2 import extract_mentioned_users | |||
r2201 | sample = ( | |||
"@first hi there @marcink here's my email marcin@email.com " | ||||
"@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three " | ||||
"@MARCIN @maRCiN @2one_more22 @john please see this http://org.pl " | ||||
"@marian.user just do it @marco-polo and next extract @marco_polo " | ||||
"user.dot hej ! not-needed maril@domain.org" | ||||
) | ||||
s = sorted([ | ||||
'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john', | ||||
'marian.user', 'marco-polo', 'marco_polo' | ||||
], key=lambda k: k.lower()) | ||||
r1713 | self.assertEqual(s, extract_mentioned_users(sample)) | |||
r2317 | ||||
def test_age(self): | ||||
r2371 | import calendar | |||
r2317 | from rhodecode.lib.utils2 import age | |||
n = datetime.datetime.now() | ||||
delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs) | ||||
self.assertEqual(age(n), u'just now') | ||||
self.assertEqual(age(n - delt(seconds=1)), u'1 second ago') | ||||
self.assertEqual(age(n - delt(seconds=60 * 2)), u'2 minutes ago') | ||||
self.assertEqual(age(n - delt(hours=1)), u'1 hour ago') | ||||
self.assertEqual(age(n - delt(hours=24)), u'1 day ago') | ||||
self.assertEqual(age(n - delt(hours=24 * 5)), u'5 days ago') | ||||
r2371 | self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[n.month-1] + 2))), | |||
u'1 month and 2 days ago') | ||||
r2317 | self.assertEqual(age(n - delt(hours=24 * 400)), u'1 year and 1 month ago') | |||