aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Kuzminski <marcin@python-works.com>2013-03-31 19:31:50 +0200
committerMarcin Kuzminski <marcin@python-works.com>2013-03-31 19:31:50 +0200
commit9aa8f33bf29e9c6b74f46c2e015b2950d2c71360 (patch)
treed34d340a39b9b53cabfd69ef44d87bfd74523b72
parent7d2afa856c79fb561b33d2aa8c03e22cea83dc37 (diff)
- age tests cannot be dynamic, there are cases when age calculation
doesn't work - use parametrized to test age --HG-- branch : beta
-rw-r--r--rhodecode/lib/utils2.py17
-rw-r--r--rhodecode/tests/test_libs.py53
2 files changed, 36 insertions, 34 deletions
diff --git a/rhodecode/lib/utils2.py b/rhodecode/lib/utils2.py
index 399c28bc..611b0052 100644
--- a/rhodecode/lib/utils2.py
+++ b/rhodecode/lib/utils2.py
@@ -353,7 +353,7 @@ def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
return engine
-def age(prevdate, show_short_version=False):
+def age(prevdate, show_short_version=False, now=None):
"""
turns a datetime into an age string.
If show_short_version is True, then it will generate a not so accurate but shorter string,
@@ -364,8 +364,7 @@ def age(prevdate, show_short_version=False):
:rtype: unicode
:returns: unicode words describing age
"""
- now = datetime.datetime.now()
- now = now.replace(microsecond=0)
+ now = now or datetime.datetime.now()
order = ['year', 'month', 'day', 'hour', 'minute', 'second']
deltas = {}
future = False
@@ -373,15 +372,13 @@ def age(prevdate, show_short_version=False):
if prevdate > now:
now, prevdate = prevdate, now
future = True
-
+ if future:
+ prevdate = prevdate.replace(microsecond=0)
# Get date parts deltas
+ from dateutil import relativedelta
for part in order:
- if future:
- from dateutil import relativedelta
- d = relativedelta.relativedelta(now, prevdate)
- deltas[part] = getattr(d, part + 's')
- else:
- deltas[part] = getattr(now, part) - getattr(prevdate, part)
+ d = relativedelta.relativedelta(now, prevdate)
+ deltas[part] = getattr(d, part + 's')
# Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00,
# not 1 hour, -59 minutes and -59 seconds)
diff --git a/rhodecode/tests/test_libs.py b/rhodecode/tests/test_libs.py
index 135b5078..d13e52ce 100644
--- a/rhodecode/tests/test_libs.py
+++ b/rhodecode/tests/test_libs.py
@@ -114,37 +114,42 @@ class TestLibs(unittest.TestCase):
], key=lambda k: k.lower())
self.assertEqual(s, extract_mentioned_users(sample))
- def test_age(self):
+ @parameterized.expand([
+ (dict(), u'just now'),
+ (dict(seconds= -1), u'1 second ago'),
+ (dict(seconds= -60 * 2), u'2 minutes ago'),
+ (dict(hours= -1), u'1 hour ago'),
+ (dict(hours= -24), u'1 day ago'),
+ (dict(hours= -24 * 5), u'5 days ago'),
+ (dict(months= -1), u'1 month ago'),
+ (dict(months= -1, days= -2), u'1 month and 2 days ago'),
+ (dict(years= -1, months= -1), u'1 year and 1 month ago'),
+ ])
+ def test_age(self, age_args, expected):
from rhodecode.lib.utils2 import age
from dateutil import relativedelta
- n = datetime.datetime.now()
+ n = datetime.datetime(year=2012, month=5, day=17)
delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
+ self.assertEqual(age(n + delt(**age_args), now=n), expected)
- self.assertEqual(age(n), u'just now')
- 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')
+ @parameterized.expand([
- def test_age_in_future(self):
+ (dict(), u'just now'),
+ (dict(seconds=1), u'in 1 second'),
+ (dict(seconds=60 * 2), u'in 2 minutes'),
+ (dict(hours=1), u'in 1 hour'),
+ (dict(hours=24), u'in 1 day'),
+ (dict(hours=24 * 5), u'in 5 days'),
+ (dict(months=1), u'in 1 month'),
+ (dict(months=1, days=1), u'in 1 month and 1 day'),
+ (dict(years=1, months=1), u'in 1 year and 1 month')
+ ])
+ def test_age_in_future(self, age_args, expected):
from rhodecode.lib.utils2 import age
from dateutil import relativedelta
- n = datetime.datetime.now()
+ n = datetime.datetime(year=2012, month=5, day=17)
delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
-
- 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')
- 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')
+ self.assertEqual(age(n + delt(**age_args), now=n), expected)
def test_tag_exctrator(self):
sample = (
@@ -216,7 +221,7 @@ class TestLibs(unittest.TestCase):
:param text:
"""
import re
- #quickly change expected url[] into a link
+ # quickly change expected url[] into a link
URL_PAT = re.compile(r'(?:url\[)(.+?)(?:\])')
def url_func(match_obj):