Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1698)

Unified Diff: appengine/monorail/framework/test/timestr_test.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: appengine/monorail/framework/test/timestr_test.py
diff --git a/appengine/monorail/framework/test/timestr_test.py b/appengine/monorail/framework/test/timestr_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..6827213fac946ab8c8fd19ad77ac59c90b864351
--- /dev/null
+++ b/appengine/monorail/framework/test/timestr_test.py
@@ -0,0 +1,104 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is govered by a BSD-style
+# license that can be found in the LICENSE file or at
+# https://developers.google.com/open-source/licenses/bsd
+
+"""Unittest for timestr module."""
+
+import datetime
+import time
+import unittest
+
+from framework import timestr
+
+
+class TimeStrTest(unittest.TestCase):
+ """Unit tests for timestr routines."""
+
+ def testFormatAbsoluteDate(self):
+ now = datetime.datetime(2008, 1, 1)
+
+ def GetDate(*args):
+ date = datetime.datetime(*args)
+ return timestr.FormatAbsoluteDate(
+ time.mktime(date.timetuple()), clock=lambda: now)
+
+ self.assertEquals(GetDate(2008, 1, 1), 'Today')
+ self.assertEquals(GetDate(2007, 12, 31), 'Yesterday')
+ self.assertEquals(GetDate(2007, 12, 30), 'Dec 30')
+ self.assertEquals(GetDate(2007, 1, 1), 'Jan 2007')
+ self.assertEquals(GetDate(2007, 1, 2), 'Jan 2007')
+ self.assertEquals(GetDate(2007, 12, 31), 'Yesterday')
+ self.assertEquals(GetDate(2006, 12, 31), 'Dec 2006')
+ self.assertEquals(GetDate(2007, 7, 1), 'Jul 1')
+ self.assertEquals(GetDate(2007, 6, 30), 'Jun 2007')
+ self.assertEquals(GetDate(2008, 1, 3), 'Jan 2008')
+
+ # Leap year fun
+ now = datetime.datetime(2008, 3, 1)
+ self.assertEquals(GetDate(2008, 2, 29), 'Yesterday')
+
+ # Clock skew
+ now = datetime.datetime(2008, 1, 1, 23, 59, 59)
+ self.assertEquals(GetDate(2008, 1, 2), 'Today')
+ now = datetime.datetime(2007, 12, 31, 23, 59, 59)
+ self.assertEquals(GetDate(2008, 1, 1), 'Today')
+ self.assertEquals(GetDate(2008, 1, 2), 'Jan 2008')
+
+ def testFormatRelativeDate(self):
+ now = time.mktime(datetime.datetime(2008, 1, 1).timetuple())
+
+ def TestSecsAgo(secs_ago, expected, expected_recent_only):
+ test_time = now - secs_ago
+ actual = timestr.FormatRelativeDate(
+ test_time, clock=lambda: now)
+ self.assertEquals(actual, expected)
+ actual_recent_only = timestr.FormatRelativeDate(
+ test_time, clock=lambda: now, recent_only=True)
+ self.assertEquals(actual_recent_only, expected_recent_only)
+
+ TestSecsAgo(10 * 24 * 60 * 60, ', 2007', '')
+ TestSecsAgo(5 * 24 * 60 * 60 - 1, ' (4 days ago)', '4 days ago')
+ TestSecsAgo(5 * 60 * 60 - 1, ' (4 hours ago)', '4 hours ago')
+ TestSecsAgo(5 * 60 - 1, ' (4 minutes ago)', '4 minutes ago')
+ TestSecsAgo(2 * 60 - 1, ' (1 minute ago)', '1 minute ago')
+ TestSecsAgo(60 - 1, ' (moments ago)', 'moments ago')
+ TestSecsAgo(0, ' (moments ago)', 'moments ago')
+ TestSecsAgo(-10, ' (moments ago)', 'moments ago')
+ TestSecsAgo(-100, ' (in the future)', '')
+
+ def testGetHumanScaleDate(self):
+ """Tests GetHumanScaleDate()."""
+ now = time.mktime(datetime.datetime(2008, 4, 10, 20, 50, 30).timetuple())
+
+ def GetDate(*args):
+ date = datetime.datetime(*args)
+ timestamp = time.mktime(date.timetuple())
+ return timestr.GetHumanScaleDate(timestamp, now=now)
+
+ self.assertEquals(
+ GetDate(2008, 4, 10, 15), ('Today', '5 hours ago'))
+ self.assertEquals(
+ GetDate(2008, 4, 10, 19, 55), ('Today', '55 min ago'))
+ self.assertEquals(
+ GetDate(2008, 4, 10, 20, 48, 35), ('Today', '1 min ago'))
+ self.assertEquals(
+ GetDate(2008, 4, 10, 20, 49, 35), ('Today', 'moments ago'))
+ self.assertEquals(
+ GetDate(2008, 4, 10, 20, 50, 55), ('Today', 'moments ago'))
+ self.assertEquals(
+ GetDate(2008, 4, 9, 15), ('Yesterday', '29 hours ago'))
+ self.assertEquals(
+ GetDate(2008, 4, 5, 15), ('Last 7 days', 'Apr 05, 2008'))
+ self.assertEquals(
+ GetDate(2008, 3, 22, 15), ('Last 30 days', 'Mar 22, 2008'))
+ self.assertEquals(
+ GetDate(2008, 1, 2, 15), ('Earlier this year', 'Jan 02, 2008'))
+ self.assertEquals(
+ GetDate(2007, 12, 31, 15), ('Older', 'Dec 31, 2007'))
+ self.assertEquals(
+ GetDate(2008, 4, 11, 20, 49, 35), ('Future', 'Later'))
+
+
+if __name__ == '__main__':
+ unittest.main()
« no previous file with comments | « appengine/monorail/framework/test/template_helpers_test.py ('k') | appengine/monorail/framework/test/validate_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698