##// END OF EJS Templates
collapsable diff container in changeset, makes quick review easier
collapsable diff container in changeset, makes quick review easier

File last commit:

r3405:a9adca4b beta
r3419:efc00d36 beta
Show More
test_libs.py
290 lines | 12.0 KiB | text/x-python | PythonLexer
Added tests for RhodeCode libs
r1372 # -*- coding: utf-8 -*-
"""
rhodecode.tests.test_libs
~~~~~~~~~~~~~~~~~~~~~~~~~
test env update
r1416 Package for testing various lib/helper functions in rhodecode
auto white-space removal
r1818
Added tests for RhodeCode libs
r1372 :created_on: Jun 9, 2011
2012 copyrights
r1824 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
updated docstrings
r1532 :license: GPLv3, see COPYING for more details.
Added tests for RhodeCode libs
r1372 """
updated docstrings
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/>.
more py2.5 compatibility patches
r2790 from __future__ import with_statement
Fixed tests for new i18n changes...
r2317 import unittest
import datetime
added test for alternative gravatar
r2753 import hashlib
import mock
Fixed tests for new i18n changes...
r2317 from rhodecode.tests import *
Added tests for RhodeCode libs
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):
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 @parameterized.expand(TEST_URLS)
def test_uri_filter(self, test_url, expected, expected_creds):
utils/conf...
r2109 from rhodecode.lib.utils2 import uri_filter
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 self.assertEqual(uri_filter(test_url), expected)
Added tests for RhodeCode libs
r1372
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 @parameterized.expand(TEST_URLS)
def test_credentials_filter(self, test_url, expected, expected_creds):
from rhodecode.lib.utils2 import credentials_filter
self.assertEqual(credentials_filter(test_url), expected_creds)
Added tests for RhodeCode libs
r1372
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 @parameterized.expand([('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)
])
def test_str2bool(self, str_bool, expected):
utils/conf...
r2109 from rhodecode.lib.utils2 import str2bool
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 self.assertEqual(str2bool(str_bool), expected)
Added tests for RhodeCode libs
r1372
Tests updates, Session refactoring
r1713 def test_mention_extractor(self):
utils/conf...
r2109 from rhodecode.lib.utils2 import extract_mentioned_users
#426 fixed mention extracting regex
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())
Tests updates, Session refactoring
r1713 self.assertEqual(s, extract_mentioned_users(sample))
Fixed tests for new i18n changes...
r2317
def test_age(self):
from rhodecode.lib.utils2 import age
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
r3261 from dateutil import relativedelta
Fixed tests for new i18n changes...
r2317 n = datetime.datetime.now()
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
r3261 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
Fixed tests for new i18n changes...
r2317 self.assertEqual(age(n), u'just now')
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
r3261 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')
self.assertEqual(age(n + delt(months=-1)), u'1 month ago')
self.assertEqual(age(n + delt(months=-1, days=-2)), u'1 month and 2 days ago')
self.assertEqual(age(n + delt(years=-1, months=-1)), u'1 year and 1 month ago')
merged + fixed pull request #62: Implemented metatags and visualisation options....
r2674
fixed #597 commits in future get negative age.
r2902 def test_age_in_future(self):
from rhodecode.lib.utils2 import age
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
r3261 from dateutil import relativedelta
fixed #597 commits in future get negative age.
r2902 n = datetime.datetime.now()
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
r3261 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
fixed #597 commits in future get negative age.
r2902 self.assertEqual(age(n), u'just now')
self.assertEqual(age(n + delt(seconds=1)), u'in 1 second')
self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes')
self.assertEqual(age(n + delt(hours=1)), u'in 1 hour')
self.assertEqual(age(n + delt(hours=24)), u'in 1 day')
self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days')
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
r3261 self.assertEqual(age(n + delt(months=1)), u'in 1 month')
self.assertEqual(age(n + delt(months=1, days=1)), u'in 1 month and 1 day')
self.assertEqual(age(n + delt(years=1, months=1)), u'in 1 year and 1 month')
fixed #597 commits in future get negative age.
r2902
merged + fixed pull request #62: Implemented metatags and visualisation options....
r2674 def test_tag_exctrator(self):
sample = (
"hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]"
"[requires] [stale] [see<>=>] [see => http://url.com]"
"[requires => url] [lang => python] [just a tag]"
"[,d] [ => ULR ] [obsolete] [desc]]"
)
from rhodecode.lib.helpers import desc_stylize
res = desc_stylize(sample)
self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res)
self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res)
self.assertTrue('<div class="metatag" tag="lang">python</div>' in res)
self.assertTrue('<div class="metatag" tag="requires">requires =&gt; <a href="/url">url</a></div>' in res)
self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
added test for alternative gravatar
r2753
def test_alternative_gravatar(self):
from rhodecode.lib.helpers import gravatar_url
_md5 = lambda s: hashlib.md5(s).hexdigest()
def fake_conf(**kwargs):
from pylons import config
config['app_conf'] = {}
config['app_conf']['use_gravatar'] = True
config['app_conf'].update(kwargs)
return config
Implemented patch from andrewsh ref #565 Add support for {netloc} and {scheme}...
r2836
class fake_url():
@classmethod
def current(cls, *args, **kwargs):
return 'https://server.com'
with mock.patch('pylons.url', fake_url):
fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
with mock.patch('pylons.config', fake):
from pylons import url
fixed gravatar test typo
r2846 assert url.current() == 'https://server.com'
Implemented patch from andrewsh ref #565 Add support for {netloc} and {scheme}...
r2836 grav = gravatar_url(email_address='test@foo.com', size=24)
assert grav == 'http://test.com/test@foo.com'
fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
with mock.patch('pylons.config', fake):
grav = gravatar_url(email_address='test@foo.com', size=24)
assert grav == 'http://test.com/test@foo.com'
added test for alternative gravatar
r2753
Implemented patch from andrewsh ref #565 Add support for {netloc} and {scheme}...
r2836 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}')
with mock.patch('pylons.config', fake):
em = 'test@foo.com'
grav = gravatar_url(email_address=em, size=24)
assert grav == 'http://test.com/%s' % (_md5(em))
added test for alternative gravatar
r2753
Implemented patch from andrewsh ref #565 Add support for {netloc} and {scheme}...
r2836 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}')
with mock.patch('pylons.config', fake):
em = 'test@foo.com'
grav = gravatar_url(email_address=em, size=24)
assert grav == 'http://test.com/%s/%s' % (_md5(em), 24)
fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}')
with mock.patch('pylons.config', fake):
em = 'test@foo.com'
grav = gravatar_url(email_address=em, size=24)
assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
revision extraction function shouldn't be so eager, just extract commits that are...
r3385
fixed urlify changesets regex + tests
r3405 def _quick_url(self, text, tmpl="""<a class="revision-link" href="%s">%s</a>""", url_=None):
"""
Changes `some text url[foo]` => `some text <a href="/">foo</a>
:param text:
"""
import re
#quickly change expected url[] into a link
URL_PAT = re.compile(r'(?:url\[)(.+?)(?:\])')
def url_func(match_obj):
_url = match_obj.groups()[0]
return tmpl % (url_ or '/some-url', _url)
return URL_PAT.sub(url_func, text)
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 @parameterized.expand([
("",
""),
("git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68",
"git-svn-id: https://svn.apache.org/repos/asf/libcloud/trunk@1441655 13f79535-47bb-0310-9956-ffa450edef68"),
("from rev 000000000000",
"from rev url[000000000000]"),
("from rev 000000000000123123 also rev 000000000000",
"from rev url[000000000000123123] also rev url[000000000000]"),
("this should-000 00",
"this should-000 00"),
("longtextffffffffff rev 123123123123",
"longtextffffffffff rev url[123123123123]"),
("rev ffffffffffffffffffffffffffffffffffffffffffffffffff",
"rev ffffffffffffffffffffffffffffffffffffffffffffffffff"),
("ffffffffffff some text traalaa",
"url[ffffffffffff] some text traalaa"),
("""Multi line
whitespace cleanup
r3394 123123123123
fixed urlify changesets regex + tests
r3405 some text 123123123123
sometimes !
""",
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 """Multi line
whitespace cleanup
r3394 url[123123123123]
fixed urlify changesets regex + tests
r3405 some text url[123123123123]
sometimes !
""")
revision extraction function shouldn't be so eager, just extract commits that are...
r3385 ])
def test_urlify_changesets(self, sample, expected):
def fake_url(self, *args, **kwargs):
return '/some-url'
fixed urlify changesets regex + tests
r3405 expected = self._quick_url(expected)
revision extraction function shouldn't be so eager, just extract commits that are...
r3385
with mock.patch('pylons.url', fake_url):
from rhodecode.lib.helpers import urlify_changesets
self.assertEqual(urlify_changesets(sample, 'repo_name'), expected)
fixed urlify changesets regex + tests
r3405
@parameterized.expand([
("",
"",
""),
("https://svn.apache.org/repos",
"url[https://svn.apache.org/repos]",
"https://svn.apache.org/repos"),
("http://svn.apache.org/repos",
"url[http://svn.apache.org/repos]",
"http://svn.apache.org/repos"),
("from rev a also rev http://google.com",
"from rev a also rev url[http://google.com]",
"http://google.com"),
("""Multi line
https://foo.bar.com
some text lalala""",
"""Multi line
url[https://foo.bar.com]
some text lalala""",
"https://foo.bar.com")
])
def test_urlify_test(self, sample, expected, url_):
from rhodecode.lib.helpers import urlify_text
expected = self._quick_url(expected,
tmpl="""<a href="%s">%s</a>""", url_=url_)
self.assertEqual(urlify_text(sample), expected)