##// END OF EJS Templates
- age tests cannot be dynamic, there are cases when age calculation...
marcink -
r3644:71860d07 beta
parent child Browse files
Show More
@@ -353,7 +353,7 b' def engine_from_config(configuration, pr'
353 353 return engine
354 354
355 355
356 def age(prevdate, show_short_version=False):
356 def age(prevdate, show_short_version=False, now=None):
357 357 """
358 358 turns a datetime into an age string.
359 359 If show_short_version is True, then it will generate a not so accurate but shorter string,
@@ -364,8 +364,7 b' def age(prevdate, show_short_version=Fal'
364 364 :rtype: unicode
365 365 :returns: unicode words describing age
366 366 """
367 now = datetime.datetime.now()
368 now = now.replace(microsecond=0)
367 now = now or datetime.datetime.now()
369 368 order = ['year', 'month', 'day', 'hour', 'minute', 'second']
370 369 deltas = {}
371 370 future = False
@@ -373,15 +372,13 b' def age(prevdate, show_short_version=Fal'
373 372 if prevdate > now:
374 373 now, prevdate = prevdate, now
375 374 future = True
376
375 if future:
376 prevdate = prevdate.replace(microsecond=0)
377 377 # Get date parts deltas
378 from dateutil import relativedelta
378 379 for part in order:
379 if future:
380 from dateutil import relativedelta
381 d = relativedelta.relativedelta(now, prevdate)
382 deltas[part] = getattr(d, part + 's')
383 else:
384 deltas[part] = getattr(now, part) - getattr(prevdate, part)
380 d = relativedelta.relativedelta(now, prevdate)
381 deltas[part] = getattr(d, part + 's')
385 382
386 383 # Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00,
387 384 # not 1 hour, -59 minutes and -59 seconds)
@@ -114,37 +114,42 b' class TestLibs(unittest.TestCase):'
114 114 ], key=lambda k: k.lower())
115 115 self.assertEqual(s, extract_mentioned_users(sample))
116 116
117 def test_age(self):
117 @parameterized.expand([
118 (dict(), u'just now'),
119 (dict(seconds= -1), u'1 second ago'),
120 (dict(seconds= -60 * 2), u'2 minutes ago'),
121 (dict(hours= -1), u'1 hour ago'),
122 (dict(hours= -24), u'1 day ago'),
123 (dict(hours= -24 * 5), u'5 days ago'),
124 (dict(months= -1), u'1 month ago'),
125 (dict(months= -1, days= -2), u'1 month and 2 days ago'),
126 (dict(years= -1, months= -1), u'1 year and 1 month ago'),
127 ])
128 def test_age(self, age_args, expected):
118 129 from rhodecode.lib.utils2 import age
119 130 from dateutil import relativedelta
120 n = datetime.datetime.now()
131 n = datetime.datetime(year=2012, month=5, day=17)
121 132 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
133 self.assertEqual(age(n + delt(**age_args), now=n), expected)
134
135 @parameterized.expand([
122 136
123 self.assertEqual(age(n), u'just now')
124 self.assertEqual(age(n + delt(seconds=-1)), u'1 second ago')
125 self.assertEqual(age(n + delt(seconds=-60 * 2)), u'2 minutes ago')
126 self.assertEqual(age(n + delt(hours=-1)), u'1 hour ago')
127 self.assertEqual(age(n + delt(hours=-24)), u'1 day ago')
128 self.assertEqual(age(n + delt(hours=-24 * 5)), u'5 days ago')
129 self.assertEqual(age(n + delt(months=-1)), u'1 month ago')
130 self.assertEqual(age(n + delt(months=-1, days=-2)), u'1 month and 2 days ago')
131 self.assertEqual(age(n + delt(years=-1, months=-1)), u'1 year and 1 month ago')
132
133 def test_age_in_future(self):
137 (dict(), u'just now'),
138 (dict(seconds=1), u'in 1 second'),
139 (dict(seconds=60 * 2), u'in 2 minutes'),
140 (dict(hours=1), u'in 1 hour'),
141 (dict(hours=24), u'in 1 day'),
142 (dict(hours=24 * 5), u'in 5 days'),
143 (dict(months=1), u'in 1 month'),
144 (dict(months=1, days=1), u'in 1 month and 1 day'),
145 (dict(years=1, months=1), u'in 1 year and 1 month')
146 ])
147 def test_age_in_future(self, age_args, expected):
134 148 from rhodecode.lib.utils2 import age
135 149 from dateutil import relativedelta
136 n = datetime.datetime.now()
150 n = datetime.datetime(year=2012, month=5, day=17)
137 151 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
138
139 self.assertEqual(age(n), u'just now')
140 self.assertEqual(age(n + delt(seconds=1)), u'in 1 second')
141 self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes')
142 self.assertEqual(age(n + delt(hours=1)), u'in 1 hour')
143 self.assertEqual(age(n + delt(hours=24)), u'in 1 day')
144 self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days')
145 self.assertEqual(age(n + delt(months=1)), u'in 1 month')
146 self.assertEqual(age(n + delt(months=1, days=1)), u'in 1 month and 1 day')
147 self.assertEqual(age(n + delt(years=1, months=1)), u'in 1 year and 1 month')
152 self.assertEqual(age(n + delt(**age_args), now=n), expected)
148 153
149 154 def test_tag_exctrator(self):
150 155 sample = (
@@ -216,7 +221,7 b' class TestLibs(unittest.TestCase):'
216 221 :param text:
217 222 """
218 223 import re
219 #quickly change expected url[] into a link
224 # quickly change expected url[] into a link
220 225 URL_PAT = re.compile(r'(?:url\[)(.+?)(?:\])')
221 226
222 227 def url_func(match_obj):
General Comments 0
You need to be logged in to leave comments. Login now