##// END OF EJS Templates
fixed bad age tests
marcink -
r2994:af0d7d8a beta
parent child Browse files
Show More
@@ -1,212 +1,216 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.tests.test_libs
3 rhodecode.tests.test_libs
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
5
5
6
6
7 Package for testing various lib/helper functions in rhodecode
7 Package for testing various lib/helper functions in rhodecode
8
8
9 :created_on: Jun 9, 2011
9 :created_on: Jun 9, 2011
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software: you can redistribute it and/or modify
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
16 # (at your option) any later version.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from __future__ import with_statement
25 from __future__ import with_statement
26 import unittest
26 import unittest
27 import datetime
27 import datetime
28 import hashlib
28 import hashlib
29 import mock
29 import mock
30 from rhodecode.tests import *
30 from rhodecode.tests import *
31
31
32 proto = 'http'
32 proto = 'http'
33 TEST_URLS = [
33 TEST_URLS = [
34 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
34 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
35 '%s://127.0.0.1' % proto),
35 '%s://127.0.0.1' % proto),
36 ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
36 ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
37 '%s://127.0.0.1' % proto),
37 '%s://127.0.0.1' % proto),
38 ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
38 ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
39 '%s://127.0.0.1' % proto),
39 '%s://127.0.0.1' % proto),
40 ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'],
40 ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'],
41 '%s://127.0.0.1:8080' % proto),
41 '%s://127.0.0.1:8080' % proto),
42 ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'],
42 ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'],
43 '%s://domain.org' % proto),
43 '%s://domain.org' % proto),
44 ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org',
44 ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org',
45 '8080'],
45 '8080'],
46 '%s://domain.org:8080' % proto),
46 '%s://domain.org:8080' % proto),
47 ]
47 ]
48
48
49 proto = 'https'
49 proto = 'https'
50 TEST_URLS += [
50 TEST_URLS += [
51 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
51 ('%s://127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
52 '%s://127.0.0.1' % proto),
52 '%s://127.0.0.1' % proto),
53 ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
53 ('%s://marcink@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
54 '%s://127.0.0.1' % proto),
54 '%s://127.0.0.1' % proto),
55 ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
55 ('%s://marcink:pass@127.0.0.1' % proto, ['%s://' % proto, '127.0.0.1'],
56 '%s://127.0.0.1' % proto),
56 '%s://127.0.0.1' % proto),
57 ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'],
57 ('%s://127.0.0.1:8080' % proto, ['%s://' % proto, '127.0.0.1', '8080'],
58 '%s://127.0.0.1:8080' % proto),
58 '%s://127.0.0.1:8080' % proto),
59 ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'],
59 ('%s://domain.org' % proto, ['%s://' % proto, 'domain.org'],
60 '%s://domain.org' % proto),
60 '%s://domain.org' % proto),
61 ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org',
61 ('%s://user:pass@domain.org:8080' % proto, ['%s://' % proto, 'domain.org',
62 '8080'],
62 '8080'],
63 '%s://domain.org:8080' % proto),
63 '%s://domain.org:8080' % proto),
64 ]
64 ]
65
65
66
66
67 class TestLibs(unittest.TestCase):
67 class TestLibs(unittest.TestCase):
68
68
69 def test_uri_filter(self):
69 def test_uri_filter(self):
70 from rhodecode.lib.utils2 import uri_filter
70 from rhodecode.lib.utils2 import uri_filter
71
71
72 for url in TEST_URLS:
72 for url in TEST_URLS:
73 self.assertEqual(uri_filter(url[0]), url[1])
73 self.assertEqual(uri_filter(url[0]), url[1])
74
74
75 def test_credentials_filter(self):
75 def test_credentials_filter(self):
76 from rhodecode.lib.utils2 import credentials_filter
76 from rhodecode.lib.utils2 import credentials_filter
77
77
78 for url in TEST_URLS:
78 for url in TEST_URLS:
79 self.assertEqual(credentials_filter(url[0]), url[2])
79 self.assertEqual(credentials_filter(url[0]), url[2])
80
80
81 def test_str2bool(self):
81 def test_str2bool(self):
82 from rhodecode.lib.utils2 import str2bool
82 from rhodecode.lib.utils2 import str2bool
83 test_cases = [
83 test_cases = [
84 ('t', True),
84 ('t', True),
85 ('true', True),
85 ('true', True),
86 ('y', True),
86 ('y', True),
87 ('yes', True),
87 ('yes', True),
88 ('on', True),
88 ('on', True),
89 ('1', True),
89 ('1', True),
90 ('Y', True),
90 ('Y', True),
91 ('yeS', True),
91 ('yeS', True),
92 ('Y', True),
92 ('Y', True),
93 ('TRUE', True),
93 ('TRUE', True),
94 ('T', True),
94 ('T', True),
95 ('False', False),
95 ('False', False),
96 ('F', False),
96 ('F', False),
97 ('FALSE', False),
97 ('FALSE', False),
98 ('0', False),
98 ('0', False),
99 ('-1', False),
99 ('-1', False),
100 ('', False), ]
100 ('', False), ]
101
101
102 for case in test_cases:
102 for case in test_cases:
103 self.assertEqual(str2bool(case[0]), case[1])
103 self.assertEqual(str2bool(case[0]), case[1])
104
104
105 def test_mention_extractor(self):
105 def test_mention_extractor(self):
106 from rhodecode.lib.utils2 import extract_mentioned_users
106 from rhodecode.lib.utils2 import extract_mentioned_users
107 sample = (
107 sample = (
108 "@first hi there @marcink here's my email marcin@email.com "
108 "@first hi there @marcink here's my email marcin@email.com "
109 "@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three "
109 "@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three "
110 "@MARCIN @maRCiN @2one_more22 @john please see this http://org.pl "
110 "@MARCIN @maRCiN @2one_more22 @john please see this http://org.pl "
111 "@marian.user just do it @marco-polo and next extract @marco_polo "
111 "@marian.user just do it @marco-polo and next extract @marco_polo "
112 "user.dot hej ! not-needed maril@domain.org"
112 "user.dot hej ! not-needed maril@domain.org"
113 )
113 )
114
114
115 s = sorted([
115 s = sorted([
116 'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john',
116 'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john',
117 'marian.user', 'marco-polo', 'marco_polo'
117 'marian.user', 'marco-polo', 'marco_polo'
118 ], key=lambda k: k.lower())
118 ], key=lambda k: k.lower())
119 self.assertEqual(s, extract_mentioned_users(sample))
119 self.assertEqual(s, extract_mentioned_users(sample))
120
120
121 def test_age(self):
121 def test_age(self):
122 import calendar
122 import calendar
123 from rhodecode.lib.utils2 import age
123 from rhodecode.lib.utils2 import age
124 n = datetime.datetime.now()
124 n = datetime.datetime.now()
125 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
125 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
126 self.assertEqual(age(n), u'just now')
126 self.assertEqual(age(n), u'just now')
127 self.assertEqual(age(n - delt(seconds=1)), u'1 second ago')
127 self.assertEqual(age(n - delt(seconds=1)), u'1 second ago')
128 self.assertEqual(age(n - delt(seconds=60 * 2)), u'2 minutes ago')
128 self.assertEqual(age(n - delt(seconds=60 * 2)), u'2 minutes ago')
129 self.assertEqual(age(n - delt(hours=1)), u'1 hour ago')
129 self.assertEqual(age(n - delt(hours=1)), u'1 hour ago')
130 self.assertEqual(age(n - delt(hours=24)), u'1 day ago')
130 self.assertEqual(age(n - delt(hours=24)), u'1 day ago')
131 self.assertEqual(age(n - delt(hours=24 * 5)), u'5 days ago')
131 self.assertEqual(age(n - delt(hours=24 * 5)), u'5 days ago')
132 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[n.month - 1]))),
133 u'1 month ago')
132 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[n.month - 1] + 2))),
134 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[n.month - 1] + 2))),
133 u'1 month and 2 days ago')
135 u'1 month and 2 days ago')
134 self.assertEqual(age(n - delt(hours=24 * 400)), u'1 year and 1 month ago')
136 self.assertEqual(age(n - delt(hours=24 * 400)), u'1 year and 1 month ago')
135
137
136 def test_age_in_future(self):
138 def test_age_in_future(self):
137 import calendar
139 import calendar
138 from rhodecode.lib.utils2 import age
140 from rhodecode.lib.utils2 import age
139 n = datetime.datetime.now()
141 n = datetime.datetime.now()
140 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
142 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
141 self.assertEqual(age(n), u'just now')
143 self.assertEqual(age(n), u'just now')
142 self.assertEqual(age(n + delt(seconds=1)), u'in 1 second')
144 self.assertEqual(age(n + delt(seconds=1)), u'in 1 second')
143 self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes')
145 self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes')
144 self.assertEqual(age(n + delt(hours=1)), u'in 1 hour')
146 self.assertEqual(age(n + delt(hours=1)), u'in 1 hour')
145 self.assertEqual(age(n + delt(hours=24)), u'in 1 day')
147 self.assertEqual(age(n + delt(hours=24)), u'in 1 day')
146 self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days')
148 self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days')
147 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month - 1] + 2))),
149 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month]))),
150 u'in 1 month')
151 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month] + 1))),
148 u'in 1 month and 1 day')
152 u'in 1 month and 1 day')
149 self.assertEqual(age(n + delt(hours=24 * 400)), u'in 1 year and 1 month')
153 self.assertEqual(age(n + delt(hours=24 * 400)), u'in 1 year and 1 month')
150
154
151 def test_tag_exctrator(self):
155 def test_tag_exctrator(self):
152 sample = (
156 sample = (
153 "hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]"
157 "hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]"
154 "[requires] [stale] [see<>=>] [see => http://url.com]"
158 "[requires] [stale] [see<>=>] [see => http://url.com]"
155 "[requires => url] [lang => python] [just a tag]"
159 "[requires => url] [lang => python] [just a tag]"
156 "[,d] [ => ULR ] [obsolete] [desc]]"
160 "[,d] [ => ULR ] [obsolete] [desc]]"
157 )
161 )
158 from rhodecode.lib.helpers import desc_stylize
162 from rhodecode.lib.helpers import desc_stylize
159 res = desc_stylize(sample)
163 res = desc_stylize(sample)
160 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
164 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
161 self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res)
165 self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res)
162 self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res)
166 self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res)
163 self.assertTrue('<div class="metatag" tag="lang">python</div>' in res)
167 self.assertTrue('<div class="metatag" tag="lang">python</div>' in res)
164 self.assertTrue('<div class="metatag" tag="requires">requires =&gt; <a href="/url">url</a></div>' in res)
168 self.assertTrue('<div class="metatag" tag="requires">requires =&gt; <a href="/url">url</a></div>' in res)
165 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
169 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
166
170
167 def test_alternative_gravatar(self):
171 def test_alternative_gravatar(self):
168 from rhodecode.lib.helpers import gravatar_url
172 from rhodecode.lib.helpers import gravatar_url
169 _md5 = lambda s: hashlib.md5(s).hexdigest()
173 _md5 = lambda s: hashlib.md5(s).hexdigest()
170
174
171 def fake_conf(**kwargs):
175 def fake_conf(**kwargs):
172 from pylons import config
176 from pylons import config
173 config['app_conf'] = {}
177 config['app_conf'] = {}
174 config['app_conf']['use_gravatar'] = True
178 config['app_conf']['use_gravatar'] = True
175 config['app_conf'].update(kwargs)
179 config['app_conf'].update(kwargs)
176 return config
180 return config
177
181
178 class fake_url():
182 class fake_url():
179 @classmethod
183 @classmethod
180 def current(cls, *args, **kwargs):
184 def current(cls, *args, **kwargs):
181 return 'https://server.com'
185 return 'https://server.com'
182
186
183 with mock.patch('pylons.url', fake_url):
187 with mock.patch('pylons.url', fake_url):
184 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
188 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
185 with mock.patch('pylons.config', fake):
189 with mock.patch('pylons.config', fake):
186 from pylons import url
190 from pylons import url
187 assert url.current() == 'https://server.com'
191 assert url.current() == 'https://server.com'
188 grav = gravatar_url(email_address='test@foo.com', size=24)
192 grav = gravatar_url(email_address='test@foo.com', size=24)
189 assert grav == 'http://test.com/test@foo.com'
193 assert grav == 'http://test.com/test@foo.com'
190
194
191 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
195 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
192 with mock.patch('pylons.config', fake):
196 with mock.patch('pylons.config', fake):
193 grav = gravatar_url(email_address='test@foo.com', size=24)
197 grav = gravatar_url(email_address='test@foo.com', size=24)
194 assert grav == 'http://test.com/test@foo.com'
198 assert grav == 'http://test.com/test@foo.com'
195
199
196 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}')
200 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}')
197 with mock.patch('pylons.config', fake):
201 with mock.patch('pylons.config', fake):
198 em = 'test@foo.com'
202 em = 'test@foo.com'
199 grav = gravatar_url(email_address=em, size=24)
203 grav = gravatar_url(email_address=em, size=24)
200 assert grav == 'http://test.com/%s' % (_md5(em))
204 assert grav == 'http://test.com/%s' % (_md5(em))
201
205
202 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}')
206 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}')
203 with mock.patch('pylons.config', fake):
207 with mock.patch('pylons.config', fake):
204 em = 'test@foo.com'
208 em = 'test@foo.com'
205 grav = gravatar_url(email_address=em, size=24)
209 grav = gravatar_url(email_address=em, size=24)
206 assert grav == 'http://test.com/%s/%s' % (_md5(em), 24)
210 assert grav == 'http://test.com/%s/%s' % (_md5(em), 24)
207
211
208 fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}')
212 fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}')
209 with mock.patch('pylons.config', fake):
213 with mock.patch('pylons.config', fake):
210 em = 'test@foo.com'
214 em = 'test@foo.com'
211 grav = gravatar_url(email_address=em, size=24)
215 grav = gravatar_url(email_address=em, size=24)
212 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
216 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
General Comments 0
You need to be logged in to leave comments. Login now