from nose.tools import (
    eq_,
    set_trace,
)
from . import DatabaseTest
from model import (
    Post,
)

class TestBot(DatabaseTest):
    
    def test_publishable_posts(self):
        bot = self._bot()

        eq_(False, bot.state_updated)

        # Since this bot has never posted, publishable_posts returns a
        # list containing a single new post.
        [new_post] = bot.publishable_posts
        assert isinstance(new_post, Post)
        eq_(new_post.content, bot.new_posts[0])

        # Since this bot has no state update schedule,
        # Bot.update_state() was not called.
        eq_(False, bot.state_updated)

        # Calling publishable_posts returns an empty list, since it's not yet
        # time for another post.
        eq_([], bot.publishable_posts)

    def test_publishable_posts_may_update_state(self):
        bot = self._bot(config=dict(state_update_schedule=1, schedule=1))
        eq_(True, bot.state_needs_update)
        bot.publishable_posts
        eq_(True, bot.state_updated)
        eq_(False, bot.state_needs_update)

    def test_publishable_posts_pops_backlog(self):
        bot = self._bot()
        bot.extend_backlog(["backlog_1", "backlog_2"])
        [post1] = bot.publishable_posts
        eq_("backlog_1", post1.content)

        # There's still a backlog item, but it's not time for another post,
        # so publishable_posts doesn't pop it.
        eq_(["backlog_2"], bot.backlog)
        eq_([], bot.publishable_posts)

@@ -1,12 +1,28 @@
+import datetime%0A
 from nose.to
@@ -117,16 +117,26 @@
   Post,%0A
+    _now,%0A
 )%0A%0Aclass
@@ -1485,28 +1485,1084 @@
 (%5B%5D, bot.publishable_posts)%0A
+%0A    def test_publishable_posts_returns_all_scheduled_posts(self):%0A        bot = self._bot()%0A        now = _now()%0A        yesterday = now - datetime.timedelta(days=1)%0A        day_before = now - datetime.timedelta(days=2)%0A        tomorrow = now + datetime.timedelta(days=1)%0A        publish_yesterday = self._post(%0A            bot.model, %22yesterday%22, publish_at=yesterday%0A        )%0A        publish_earlier = self._post(%0A            bot.model, %22day before%22, publish_at=day_before%0A        )%0A        publish_later = self._post(%0A            bot.model, %22tomorrow%22, publish_at=tomorrow%0A        )%0A%0A        # publishable_posts returns all posts that should have been%0A        # published by now.%0A        eq_(%5Bpublish_earlier, publish_yesterday%5D, bot.publishable_posts)%0A%0A        # Since the scheduled posts weren't _created_ by the%0A        # publishable_posts, they don't go away when you call%0A        # publishable_posts once. They will stick around until they're%0A        # published.%0A        eq_(%5Bpublish_earlier, publish_yesterday%5D, bot.publishable_posts)%0A        %0A

Test how publishable_posts behaves with scheduled posts.