def test_special_values(self): self.assertEqual(str(ttypes.Timedelta(0)), '0s') self.assertEqual(str(ttypes.Timedelta(0.4)), '0s') self.assertEqual(str(ttypes.Timedelta(-0.4)), '0s') self.assertEqual( str(ttypes.Timedelta(ttypes.Timedelta.NOT_APPLICABLE)), '') self.assertEqual(str(ttypes.Timedelta(ttypes.Timedelta.UNKNOWN)), '?')
def test_timedelta(self): self.assertIsInstance( ttypes.Timestamp(1234).timedelta, ttypes.Timedelta) self.assertIsInstance( ttypes.Timestamp(ttypes.Timestamp.NOW).timedelta, ttypes.Timedelta) self.assertIsInstance( ttypes.Timestamp(ttypes.Timestamp.SOON).timedelta, ttypes.Timedelta) self.assertIsInstance( ttypes.Timestamp(ttypes.Timestamp.UNKNOWN).timedelta, ttypes.Timedelta) self.assertIsInstance( ttypes.Timestamp(ttypes.Timestamp.NOT_APPLICABLE).timedelta, ttypes.Timedelta) self.assertIsInstance( ttypes.Timestamp(ttypes.Timestamp.NEVER).timedelta, ttypes.Timedelta) with mock_time(2000, 1, 1, 0, 0, 0): self.assertEqual( ttypes.Timestamp.from_string('00:00:00').timedelta, ttypes.Timedelta(0)) self.assertEqual( ttypes.Timestamp.from_string('00:00:01').timedelta, ttypes.Timedelta(1)) self.assertEqual( ttypes.Timestamp.from_string('00:05:00').timedelta, ttypes.Timedelta(300)) self.assertEqual( ttypes.Timestamp.from_string('01-02 00:00:00').timedelta, ttypes.Timedelta(3600 * 24)) self.assertEqual( ttypes.Timestamp.from_string('02-01 00:00:00').timedelta, ttypes.Timedelta(3600 * 24 * 31))
def test_bool(self): for td in (ttypes.Timedelta(random.randint(-1e5, 1e5) * MIN), ttypes.Timedelta(random.randint(-1e5, 1e5) * HOUR), ttypes.Timedelta(random.randint(-1e5, 1e5) * DAY)): self.assertEqual(bool(td), True) for td in (ttypes.Timedelta(ttypes.Timedelta.UNKNOWN), ttypes.Timedelta(ttypes.Timedelta.NOT_APPLICABLE)): self.assertEqual(bool(td), False)
def test_sorting(self): lst = [ttypes.Timedelta(-2 * HOUR), ttypes.Timedelta(2 * MIN), ttypes.Timedelta(3 * MIN), ttypes.Timedelta(1 * DAY), ttypes.Timedelta(2.5 * YEAR), ttypes.Timedelta(ttypes.Timedelta.UNKNOWN), ttypes.Timedelta(ttypes.Timedelta.NOT_APPLICABLE)] import random def shuffle(l): return random.sample(l, k=len(l)) for _ in range(10): self.assertEqual(sorted(shuffle(lst)), lst)
def test_preposition_string(self): self.assertEqual(ttypes.Timedelta(6 * DAY).with_preposition, 'in 6d') self.assertEqual(ttypes.Timedelta(-6 * DAY).with_preposition, '6d ago') self.assertEqual(ttypes.Timedelta(0.3).with_preposition, 'now') self.assertEqual(ttypes.Timedelta(-0.3).with_preposition, 'now')
def test_negative_delta(self): self.assertEqual(str(ttypes.Timedelta(-10)), '-10s') self.assertEqual(str(ttypes.Timedelta(-1 * 60 - 45)), '-1m45s') self.assertEqual(str(ttypes.Timedelta(-3 * DAY - 2 * HOUR)), '-3d2h')
def test_added_subunits_for_small_numbers(self): self.assertEqual(str(ttypes.Timedelta(9 * HOUR + 59 * MIN + 59)), '9h59m') self.assertEqual(str(ttypes.Timedelta(10 * HOUR + 59 * MIN + 59)), '10h')
def test_preposition_string(self): self.assertEqual(ttypes.Timedelta(7 * DAY).with_preposition, 'in 7d') self.assertEqual(ttypes.Timedelta(-7 * DAY).with_preposition, '7d ago')
def test_even_units(self): for unit,char in ((1, 's'), (MIN, 'm'), (HOUR, 'h'), (DAY, 'd'), (YEAR, 'y')): for i in range(1, 6): self.assertEqual(str(ttypes.Timedelta(i * unit)), '%d%s' % (i, char))