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