def test_publishes_past_deadline(self): # Choose a "now" within publishing hours now = datetime(2014, 1, 1, 12, 0, 0) assert now.hour > settings.PUBLISHING_HOURS[0] assert now.hour < settings.PUBLISHING_HOURS[1] doc1 = Document.objects.create( author=User.objects.get(username='******'), editor=User.objects.get(username='******'), title="Expired 1", type="post", status="ready", created = now - timedelta(days=settings.MAX_READY_TO_PUBLISH_DAYS, seconds=1), ) doc2 = Document.objects.create( author=User.objects.get(username='******'), editor=User.objects.get(username='******'), title="Expired 2", type="post", status="ready", created = now - timedelta(days=settings.MAX_READY_TO_PUBLISH_DAYS), ) self.assertEquals(set(Document.objects.ready()), set([doc1, doc2])) publish_ready(now) self.assertEquals(set(Document.objects.ready()), set([doc2])) publish_ready(now) self.assertEquals(Document.objects.ready().count(), 0)
def test_doesnt_publish_outside_publishing_hours(self): for hour in range(0, 24): now = datetime(2014, 1, 1, hour, 0, 0) doc = Document.objects.create( author=User.objects.get(username='******'), editor=User.objects.get(username='******'), title="Expired 1", type="post", status="ready", created=now - timedelta(days=settings.MAX_READY_TO_PUBLISH_DAYS, seconds=1), ) publish_ready(now) # Refresh doc from db. doc = Document.objects.get(pk=doc.pk) if (now.hour < settings.PUBLISHING_HOURS[0] or now.hour > settings.PUBLISHING_HOURS[1]): self.assertEquals(doc.status, "ready") self.assertEquals(Document.objects.ready().count(), 1) else: self.assertEquals(doc.status, "published") self.assertEquals(Document.objects.ready().count(), 0) doc.delete()
def test_publish_ready(self): with self.settings(MAX_READY_TO_PUBLISH_DAYS=7, PUBLISHING_HOURS=[10, 14]): now = datetime(2014, 1, 1, 12, 0, 0) a0, a1, a2, a3, a4, a5, b1, b2, b3, c1, d1 = self._build_test_docs( now) def _refresh(doc): return Document.objects.get(pk=doc.pk) # Publish every 5 minutes for a week. total_seconds = 7 * 24 * 60 * 60 order = [] times = [] cur = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, now.tzinfo) for t in range(0, total_seconds, 5 * 60): cur = cur + timedelta(seconds=t) result = publish_ready(cur) if result is not None: order.append(result.title) times.append(cur) self.assertEquals(order, 'A0 B1 A1 B2 A2 C1 D1 A3 B3 A4 A5'.split()) # "just-so" results from a suspected good run. Since the interval # is calculated based on the time from the oldest ready document, # the results are only as linear as the times on the starting docs. self.assertEquals(times, [ datetime(2014, 1, 1, 12, 0), datetime(2014, 1, 1, 12, 50), datetime(2014, 1, 1, 13, 45), datetime(2014, 1, 2, 11, 0), datetime(2014, 1, 2, 13, 0), datetime(2014, 1, 3, 10, 45), datetime(2014, 1, 3, 13, 35), datetime(2014, 1, 4, 11, 45), datetime(2014, 1, 5, 10, 0), datetime(2014, 1, 5, 14, 0), datetime(2014, 1, 6, 11, 15), ]) self.assertEquals(Document.objects.ready().count(), 0)
def test_publish_ready(self): with self.settings(MAX_READY_TO_PUBLISH_DAYS=7, PUBLISHING_HOURS=[10,14]): now = datetime(2014, 1, 1, 12, 0, 0) a0, a1, a2, a3, a4, a5, b1, b2, b3, c1, d1 = self._build_test_docs(now) def _refresh(doc): return Document.objects.get(pk=doc.pk) # Publish every 5 minutes for a week. total_seconds = 7 * 24 * 60 * 60 order = [] times = [] cur = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, now.tzinfo) for t in range(0, total_seconds, 5*60): cur = cur + timedelta(seconds=t) result = publish_ready(cur) if result is not None: order.append(result.title) times.append(cur) self.assertEquals(order, 'A0 B1 A1 B2 A2 C1 D1 A3 B3 A4 A5'.split()) # "just-so" results from a suspected good run. Since the interval # is calculated based on the time from the oldest ready document, # the results are only as linear as the times on the starting docs. self.assertEquals(times, [ datetime(2014, 1, 1, 12, 0), datetime(2014, 1, 1, 12, 50), datetime(2014, 1, 1, 13, 45), datetime(2014, 1, 2, 11, 0), datetime(2014, 1, 2, 13, 0), datetime(2014, 1, 3, 10, 45), datetime(2014, 1, 3, 13, 35), datetime(2014, 1, 4, 11, 45), datetime(2014, 1, 5, 10, 0), datetime(2014, 1, 5, 14, 0), datetime(2014, 1, 6, 11, 15), ]) self.assertEquals(Document.objects.ready().count(), 0)