##// END OF EJS Templates
fixed bad date calculations
marcink -
r3132:5ba6249a beta
parent child Browse files
Show More
@@ -1,216 +1,217 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 prev_month = n.month - 1 if n.month != 1 else n.month - 2
126 self.assertEqual(age(n), u'just now')
127 self.assertEqual(age(n), u'just now')
127 self.assertEqual(age(n - delt(seconds=1)), u'1 second ago')
128 self.assertEqual(age(n - delt(seconds=1)), u'1 second ago')
128 self.assertEqual(age(n - delt(seconds=60 * 2)), u'2 minutes ago')
129 self.assertEqual(age(n - delt(seconds=60 * 2)), u'2 minutes ago')
129 self.assertEqual(age(n - delt(hours=1)), u'1 hour ago')
130 self.assertEqual(age(n - delt(hours=1)), u'1 hour ago')
130 self.assertEqual(age(n - delt(hours=24)), u'1 day ago')
131 self.assertEqual(age(n - delt(hours=24)), u'1 day ago')
131 self.assertEqual(age(n - delt(hours=24 * 5)), u'5 days ago')
132 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 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[prev_month]))),
133 u'1 month ago')
134 u'1 month ago')
134 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[n.month - 1] + 2))),
135 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[prev_month] + 2))),
135 u'1 month and 2 days ago')
136 u'1 month and 2 days ago')
136 self.assertEqual(age(n - delt(hours=24 * 400)), u'1 year and 1 month ago')
137 self.assertEqual(age(n - delt(hours=24 * 400)), u'1 year and 1 month ago')
137
138
138 def test_age_in_future(self):
139 def test_age_in_future(self):
139 import calendar
140 import calendar
140 from rhodecode.lib.utils2 import age
141 from rhodecode.lib.utils2 import age
141 n = datetime.datetime.now()
142 n = datetime.datetime.now()
142 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
143 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
143 self.assertEqual(age(n), u'just now')
144 self.assertEqual(age(n), u'just now')
144 self.assertEqual(age(n + delt(seconds=1)), u'in 1 second')
145 self.assertEqual(age(n + delt(seconds=1)), u'in 1 second')
145 self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes')
146 self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes')
146 self.assertEqual(age(n + delt(hours=1)), u'in 1 hour')
147 self.assertEqual(age(n + delt(hours=1)), u'in 1 hour')
147 self.assertEqual(age(n + delt(hours=24)), u'in 1 day')
148 self.assertEqual(age(n + delt(hours=24)), u'in 1 day')
148 self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days')
149 self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days')
149 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month]))),
150 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month]))),
150 u'in 1 month')
151 u'in 1 month')
151 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month] + 1))),
152 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month] + 1))),
152 u'in 1 month and 1 day')
153 u'in 1 month and 1 day')
153 self.assertEqual(age(n + delt(hours=24 * 400)), u'in 1 year and 1 month')
154 self.assertEqual(age(n + delt(hours=24 * 400)), u'in 1 year and 1 month')
154
155
155 def test_tag_exctrator(self):
156 def test_tag_exctrator(self):
156 sample = (
157 sample = (
157 "hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]"
158 "hello pta[tag] gog [[]] [[] sda ero[or]d [me =>>< sa]"
158 "[requires] [stale] [see<>=>] [see => http://url.com]"
159 "[requires] [stale] [see<>=>] [see => http://url.com]"
159 "[requires => url] [lang => python] [just a tag]"
160 "[requires => url] [lang => python] [just a tag]"
160 "[,d] [ => ULR ] [obsolete] [desc]]"
161 "[,d] [ => ULR ] [obsolete] [desc]]"
161 )
162 )
162 from rhodecode.lib.helpers import desc_stylize
163 from rhodecode.lib.helpers import desc_stylize
163 res = desc_stylize(sample)
164 res = desc_stylize(sample)
164 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
165 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
165 self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res)
166 self.assertTrue('<div class="metatag" tag="obsolete">obsolete</div>' in res)
166 self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res)
167 self.assertTrue('<div class="metatag" tag="stale">stale</div>' in res)
167 self.assertTrue('<div class="metatag" tag="lang">python</div>' in res)
168 self.assertTrue('<div class="metatag" tag="lang">python</div>' in res)
168 self.assertTrue('<div class="metatag" tag="requires">requires =&gt; <a href="/url">url</a></div>' in res)
169 self.assertTrue('<div class="metatag" tag="requires">requires =&gt; <a href="/url">url</a></div>' in res)
169 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
170 self.assertTrue('<div class="metatag" tag="tag">tag</div>' in res)
170
171
171 def test_alternative_gravatar(self):
172 def test_alternative_gravatar(self):
172 from rhodecode.lib.helpers import gravatar_url
173 from rhodecode.lib.helpers import gravatar_url
173 _md5 = lambda s: hashlib.md5(s).hexdigest()
174 _md5 = lambda s: hashlib.md5(s).hexdigest()
174
175
175 def fake_conf(**kwargs):
176 def fake_conf(**kwargs):
176 from pylons import config
177 from pylons import config
177 config['app_conf'] = {}
178 config['app_conf'] = {}
178 config['app_conf']['use_gravatar'] = True
179 config['app_conf']['use_gravatar'] = True
179 config['app_conf'].update(kwargs)
180 config['app_conf'].update(kwargs)
180 return config
181 return config
181
182
182 class fake_url():
183 class fake_url():
183 @classmethod
184 @classmethod
184 def current(cls, *args, **kwargs):
185 def current(cls, *args, **kwargs):
185 return 'https://server.com'
186 return 'https://server.com'
186
187
187 with mock.patch('pylons.url', fake_url):
188 with mock.patch('pylons.url', fake_url):
188 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
189 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
189 with mock.patch('pylons.config', fake):
190 with mock.patch('pylons.config', fake):
190 from pylons import url
191 from pylons import url
191 assert url.current() == 'https://server.com'
192 assert url.current() == 'https://server.com'
192 grav = gravatar_url(email_address='test@foo.com', size=24)
193 grav = gravatar_url(email_address='test@foo.com', size=24)
193 assert grav == 'http://test.com/test@foo.com'
194 assert grav == 'http://test.com/test@foo.com'
194
195
195 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
196 fake = fake_conf(alternative_gravatar_url='http://test.com/{email}')
196 with mock.patch('pylons.config', fake):
197 with mock.patch('pylons.config', fake):
197 grav = gravatar_url(email_address='test@foo.com', size=24)
198 grav = gravatar_url(email_address='test@foo.com', size=24)
198 assert grav == 'http://test.com/test@foo.com'
199 assert grav == 'http://test.com/test@foo.com'
199
200
200 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}')
201 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}')
201 with mock.patch('pylons.config', fake):
202 with mock.patch('pylons.config', fake):
202 em = 'test@foo.com'
203 em = 'test@foo.com'
203 grav = gravatar_url(email_address=em, size=24)
204 grav = gravatar_url(email_address=em, size=24)
204 assert grav == 'http://test.com/%s' % (_md5(em))
205 assert grav == 'http://test.com/%s' % (_md5(em))
205
206
206 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}')
207 fake = fake_conf(alternative_gravatar_url='http://test.com/{md5email}/{size}')
207 with mock.patch('pylons.config', fake):
208 with mock.patch('pylons.config', fake):
208 em = 'test@foo.com'
209 em = 'test@foo.com'
209 grav = gravatar_url(email_address=em, size=24)
210 grav = gravatar_url(email_address=em, size=24)
210 assert grav == 'http://test.com/%s/%s' % (_md5(em), 24)
211 assert grav == 'http://test.com/%s/%s' % (_md5(em), 24)
211
212
212 fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}')
213 fake = fake_conf(alternative_gravatar_url='{scheme}://{netloc}/{md5email}/{size}')
213 with mock.patch('pylons.config', fake):
214 with mock.patch('pylons.config', fake):
214 em = 'test@foo.com'
215 em = 'test@foo.com'
215 grav = gravatar_url(email_address=em, size=24)
216 grav = gravatar_url(email_address=em, size=24)
216 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
217 assert grav == 'https://server.com/%s/%s' % (_md5(em), 24)
General Comments 0
You need to be logged in to leave comments. Login now