def test_fails_if_date_is_older_than_max_delta(self):
        a_few_seconds_ago = DateTime.now() - TimeDelta(seconds=30)
        self.assert_fail(a_few_seconds_ago)

        one_minute = TimeDelta(minutes=1)
        assert_almost_now(a_few_seconds_ago, max_delta=one_minute)
        yesterday = DateTime.now() - TimeDelta(days=1)
        self.assert_fail(yesterday, max_delta=one_minute)
    def test_passes_if_date_is_almost_now(self):
        now = DateTime.now()
        assert_almost_now(now)

        now = DateTime.now().replace(microsecond=0)
        assert_almost_now(now)

        now = DateTime.now() - TimeDelta(seconds=0.5)
        assert_almost_now(now)
 def assert_fail(self, date, **kwargs):
     with assert_raises(AssertionError) as exc_context:
         assert_almost_now(date, **kwargs)
     return exc_context.caught_exception
 def test_can_enforce_timezone_aware_checks(self):
     datetime = DateTime.now()
     assert_none(datetime.tzinfo)
     assert_almost_now(datetime)
     self.assert_fail(datetime, tz=UTC)
    def test_can_handle_timezone_aware_datetimes(self):
        datetime = DateTime.now(tz=UTC)
        assert_almost_now(datetime)

        a_few_seconds_ago = DateTime.now(tz=UTC) - TimeDelta(seconds=30)
        self.assert_fail(a_few_seconds_ago)
 def test_can_use_plain_integer_for_max_delta(self):
     a_few_seconds_ago = DateTime.now() - TimeDelta(seconds=5)
     assert_almost_now(a_few_seconds_ago, max_delta=10)
     # check that this are really seconds (i.e. not "minutes"/"hours")
     one_minute_ago = DateTime.now() - TimeDelta(minutes=1)
     self.assert_fail(one_minute_ago, max_delta=10)