def test_fetchable(self): fctrl = FeedController() total = fctrl.read().count() unix = datetime(1970, 1, 1).replace(tzinfo=timezone.utc) count = 0 for fd in fctrl.list_late(): count += 1 self.assertEqual(unix, fd.last_retrieved) self.assertEqual(unix, fd.expires) self.assertEqual(total, count) fetchables = fctrl.list_fetchable() now = utc_now() for fd in fetchables: self.assert_in_range(now - timedelta(seconds=1), fd.last_retrieved, now) self.assertEqual(unix, fd.expires) self.assert_late_count( 0, "no late feed to report because all just fetched") fctrl.update({}, {'expires': unix}) now = utc_now() for fd in fctrl.read(): # expires should be corrected self.assert_in_range( now + timedelta(seconds=conf.feed.min_expires - 1), fd.expires, now + timedelta(seconds=conf.feed.min_expires + 1)) lr_not_matter = timedelta(seconds=conf.feed.min_expires + 10) self.update_all_no_ctrl(expires=utc_now() - timedelta(seconds=1), last_retrieved=utc_now() - lr_not_matter) self.assert_late_count(total, "all feed just expired") self.update_all_no_ctrl(expires=utc_now() + timedelta(seconds=1)) self.assert_late_count( 0, "all feed will expire in a second, none are expired")
def assert_late_count(self, count, msg): fctrl = FeedController() self.assertEqual(count, len(list(fctrl.list_late())), msg) self.assertEqual(count, len(fctrl.list_fetchable()), msg)