def test_date(self): with mock_time(1954, 2, 3, 0, 0, 0): self.assertEqual( utils.Timestamp.from_string('00:00:00').date, '1954-02-03') self.assertEqual( utils.Timestamp.from_string('08:09:10').date, '1954-02-03') self.assertEqual( utils.Timestamp.from_string('23:59:59').date, '1954-02-03') self.assertEqual( utils.Timestamp.from_string('1954').date, '1954-01-01') self.assertEqual( utils.Timestamp.from_string('1954-08').date, '1954-08-01') self.assertEqual( utils.Timestamp.from_string('1954-08-09').date, '1954-08-09') self.assertEqual( utils.Timestamp.from_string('1954-08-09 23:59:59').date, '1954-08-09') self.assertEqual( utils.Timestamp.from_string('1954-08-10 00:00:00').date, '1954-08-10') self.assertEqual( utils.Timestamp.from_string('1954-08-10 11:12:13').date, '1954-08-10') self.assertEqual(utils.Timestamp(utils.Timestamp.NOW).date, 'now') self.assertEqual(utils.Timestamp(utils.Timestamp.SOON).date, 'soon') self.assertEqual( utils.Timestamp(utils.Timestamp.UNKNOWN).date, 'unknown') self.assertEqual( utils.Timestamp(utils.Timestamp.NOT_APPLICABLE).date, 'not applicable') self.assertEqual(utils.Timestamp(utils.Timestamp.NEVER).date, 'never')
def test_bool(self): for td in (utils.Timestamp(random.randint(-1000, 1000) * MIN), utils.Timestamp(random.randint(-1000, 1000) * HOUR), utils.Timestamp(random.randint(-1000, 1000) * DAY), utils.Timestamp(random.randint(-1000, 1000) * MONTH), utils.Timestamp(random.randint(-1000, 1000) * YEAR)): self.assertIs(bool(td), True) for td in (utils.Timestamp(utils.Timestamp.UNKNOWN), utils.Timestamp(utils.Timestamp.NOT_APPLICABLE), utils.Timestamp(utils.Timestamp.NEVER)): self.assertIs(bool(td), False) for td in (utils.Timestamp(utils.Timestamp.NOW), utils.Timestamp(utils.Timestamp.SOON)): self.assertIs(bool(td), True)
def test_time(self): self.assertEqual(utils.Timestamp.from_string('1954').time, '00:00:00') self.assertEqual( utils.Timestamp.from_string('1954-08').time, '00:00:00') self.assertEqual( utils.Timestamp.from_string('1954-08-09').time, '00:00:00') self.assertEqual( utils.Timestamp.from_string('1954-08-09 23:59:59').time, '23:59:59') self.assertEqual( utils.Timestamp.from_string('1954-08-10 11:12:13').time, '11:12:13') self.assertEqual(utils.Timestamp(utils.Timestamp.NOW).time, 'now') self.assertEqual(utils.Timestamp(utils.Timestamp.SOON).time, 'soon') self.assertEqual( utils.Timestamp(utils.Timestamp.UNKNOWN).time, 'unknown') self.assertEqual( utils.Timestamp(utils.Timestamp.NOT_APPLICABLE).time, 'not applicable') self.assertEqual(utils.Timestamp(utils.Timestamp.NEVER).time, 'never')
def test_timedelta(self): self.assertIsInstance(utils.Timestamp(1234).timedelta, utils.Timedelta) self.assertIsInstance( utils.Timestamp(utils.Timestamp.NOW).timedelta, utils.Timedelta) self.assertIsInstance( utils.Timestamp(utils.Timestamp.SOON).timedelta, utils.Timedelta) self.assertIsInstance( utils.Timestamp(utils.Timestamp.UNKNOWN).timedelta, utils.Timedelta) self.assertIsInstance( utils.Timestamp(utils.Timestamp.NOT_APPLICABLE).timedelta, utils.Timedelta) self.assertIsInstance( utils.Timestamp(utils.Timestamp.NEVER).timedelta, utils.Timedelta) with mock_time(2000, 1, 1, 0, 0, 0): self.assertEqual( utils.Timestamp.from_string('00:00:00').timedelta, utils.Timedelta(0)) self.assertEqual( utils.Timestamp.from_string('00:00:01').timedelta, utils.Timedelta(1)) self.assertEqual( utils.Timestamp.from_string('00:05:00').timedelta, utils.Timedelta(300)) self.assertEqual( utils.Timestamp.from_string('01-02 00:00:00').timedelta, utils.Timedelta(3600 * 24)) self.assertEqual( utils.Timestamp.from_string('02-01 00:00:00').timedelta, utils.Timedelta(3600 * 24 * 31))
def test_sorting(self): now = 1e6 lst = [ utils.Timestamp(utils.Timestamp.NOW), utils.Timestamp(utils.Timestamp.SOON), utils.Timestamp(now + (-2 * HOUR)), utils.Timestamp(now + (2 * MIN)), utils.Timestamp(now + (3 * MIN)), utils.Timestamp(now + (1 * DAY)), utils.Timestamp(now + (2.5 * YEAR)), utils.Timestamp(utils.Timestamp.UNKNOWN), utils.Timestamp(utils.Timestamp.NOT_APPLICABLE) ] def shuffle(lst): return random.sample(lst, k=len(lst)) for _ in range(10): self.assertEqual(sorted(shuffle(lst)), lst)
def test_full_property(self): with mock_time(1993, 2, 14, 5, 38, 12): self.assertEqual( utils.Timestamp(time.time()).full, '1993-02-14 05:38:12') self.assertEqual(utils.Timestamp(utils.Timestamp.NOW).full, 'now') self.assertEqual(utils.Timestamp(utils.Timestamp.SOON).full, 'soon') self.assertEqual( utils.Timestamp(utils.Timestamp.UNKNOWN).full, 'unknown') self.assertEqual( utils.Timestamp(utils.Timestamp.NOT_APPLICABLE).full, 'not applicable') self.assertEqual(utils.Timestamp(utils.Timestamp.NEVER).full, 'never')
def test_string_representation(self): with mock_time(1993, 2, 14, 5, 38, 12): self.assertEqual(str(utils.Timestamp(time.time())), '05:38:12') self.assertEqual(str(utils.Timestamp(time.time() - 60)), '05:37:12') self.assertEqual(str(utils.Timestamp(time.time() + 60)), '05:39:12') self.assertEqual(str(utils.Timestamp(time.time() - 3 * 60 * 60)), '02:38') self.assertEqual(str(utils.Timestamp(time.time() + 3 * 60 * 60)), '08:38') self.assertEqual( str(utils.Timestamp(time.time() - 7 * 24 * 60 * 60)), '1993-02-07') self.assertEqual( str(utils.Timestamp(time.time() + 7 * 24 * 60 * 60)), '1993-02-21') self.assertEqual(str(utils.Timestamp(utils.Timestamp.NOW)), 'now') self.assertEqual(str(utils.Timestamp(utils.Timestamp.SOON)), 'soon') self.assertEqual(str(utils.Timestamp(utils.Timestamp.UNKNOWN)), '?') self.assertEqual(str(utils.Timestamp(utils.Timestamp.NOT_APPLICABLE)), '') self.assertEqual(str(utils.Timestamp(utils.Timestamp.NEVER)), 'never')