class Test_SubscriptionForm_save: def setup(self): Subscription.objects.all().delete() Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() self.library = ContentFeedLibrary(shared=False) self.library.register(ListItemFeed, 'my list items') self.subscriber = Subscriber.objects.create() self.feed = ListItemFeed('[1, 2, 3]') self.feed_record = self.library.get_record(self.feed) @istest def creates_a_subscription_for_the_subscriber_to_the_feed(self): form = SubscriptionForm({ 'subscriber': self.subscriber.pk, 'feed_record': self.feed_record.pk }) assert form.is_valid(), 'The form had errors: %r' % (form.errors, ) form.save() subscription = self.subscriber.subscription(self.feed, self.library) assert subscription is not None
def causes_the_same_feed_record_to_be_returned_on_different_calls_to_getRecord(self): library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'li') feed = ListItemFeed('[1, 2, 3]') record = library.get_record(feed) assert_is(record, library.get_record(feed))
def causes_the_same_feed_record_to_be_returned_on_different_calls_to_getRecord( self): library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'li') feed = ListItemFeed('[1, 2, 3]') record = library.get_record(feed) assert_is(record, library.get_record(feed))
def doesnt_return_the_same_feed_from_different_libraries(self): library1 = ContentFeedLibrary(shared=False) library1.register(ListItemFeed, 'li') library2 = ContentFeedLibrary(shared=False) library2.register(ListItemFeed, 'li') feed = ListItemFeed('[1, 2, 3]') record = library1.get_record(feed) assert_is_not(feed, library2.get_feed(record))
def setUp(self): class DoNothinView(object): def get_context_data(self, **kwargs): # This is just so that SingleSubscriptionMixin.get_context_data # super has something to call. return {} library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'my list item feed') class SubscriptionView(SingleSubscriptionMixin, DoNothinView): def get_content_feed_library(self): return library def get_content_feed(self): return ListItemFeed('[1,2,3]') self.view = SubscriptionView() self.view.request = Mock() self.view.feed_data = ContentFeed
def setUp(self): class DoNothinView (object): def get_context_data(self, **kwargs): # This is just so that SingleSubscriptionMixin.get_context_data # super has something to call. return {} library = ContentFeedLibrary(shared=False) library.register(ListItemFeed, 'my list item feed') class SubscriptionView (SingleSubscriptionMixin, DoNothinView): def get_content_feed_library(self): return library def get_content_feed(self): return ListItemFeed('[1,2,3]') self.view = SubscriptionView() self.view.request = Mock() self.view.feed_data = ContentFeed
class Test_ContentFeedUpdater_update(TestCase): def setUp(self): LegFile.objects.all().delete() key = 0 for intro, final in [ (datetime.date(2011, 1, 28), datetime.date(2011, 1, 29)), (datetime.date(2010, 7, 28), datetime.date(2010, 7, 29)), (datetime.date(2011, 8, 17), datetime.date(2011, 8, 18)), (datetime.date(2006, 12, 11), datetime.date(2006, 12, 12)), (datetime.date(2006, 12, 12), datetime.date(2006, 12, 13)) ]: LegFile.objects.create(intro_date=intro, final_date=final, key=key, date_scraped=datetime.date.today()) key += 1 self.library = ContentFeedLibrary(shared=False) self.library.register(NewLegFilesFeed, 'feed_class_123456') self.record = self.library.get_record(NewLegFilesFeed()) @istest def changes_the_lastUpdated_of_a_legfiles_feed_to_most_recent_intro_date( self): updater = ContentFeedRecordUpdater() updater.update(self.record, self.library) assert_equal(self.record.last_updated, datetime.datetime(2011, 8, 17, 0, 0)) @istest def returns_date_min_when_no_content_is_available(self): LegFile.objects.all().delete() updater = ContentFeedRecordUpdater() updater.update(self.record, self.library) assert_equal(self.record.last_updated, datetime.datetime.min)
class Test_ContentFeedUpdater_update (TestCase): def setUp(self): LegFile.objects.all().delete() key = 0 for intro, final in [ (datetime.date(2011, 1, 28), datetime.date(2011, 1, 29)), (datetime.date(2010, 7, 28), datetime.date(2010, 7, 29)), (datetime.date(2011, 8, 17), datetime.date(2011, 8, 18)), (datetime.date(2006, 12, 11), datetime.date(2006, 12, 12)), (datetime.date(2006, 12, 12), datetime.date(2006, 12, 13)) ]: LegFile.objects.create(intro_date=intro, final_date=final, key=key, date_scraped=datetime.date.today()) key += 1 self.library = ContentFeedLibrary(shared=False) self.library.register(NewLegFilesFeed, 'feed_class_123456') self.record = self.library.get_record(NewLegFilesFeed()) @istest def changes_the_lastUpdated_of_a_legfiles_feed_to_most_recent_intro_date(self): updater = ContentFeedRecordUpdater() updater.update(self.record, self.library) assert_equal(self.record.last_updated, datetime.datetime(2011, 8, 17, 0, 0)) @istest def returns_date_min_when_no_content_is_available(self): LegFile.objects.all().delete() updater = ContentFeedRecordUpdater() updater.update(self.record, self.library) assert_equal(self.record.last_updated, datetime.datetime.min)
class Test_SubscriptionForm_save: def setup(self): Subscription.objects.all().delete() Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() self.library = ContentFeedLibrary(shared=False) self.library.register(ListItemFeed, 'my list items') self.subscriber = Subscriber.objects.create() self.feed = ListItemFeed('[1, 2, 3]') self.feed_record = self.library.get_record(self.feed) @istest def creates_a_subscription_for_the_subscriber_to_the_feed(self): form = SubscriptionForm({'subscriber': self.subscriber.pk, 'feed_record': self.feed_record.pk}) assert form.is_valid(), 'The form had errors: %r' % (form.errors,) form.save() subscription = self.subscriber.subscription(self.feed, self.library) assert subscription is not None
def test_subscription_flow(): Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() del mail.outbox[:] library = ContentFeedLibrary(shared=True) # First, let's make some content content = [(datetime.date(2011, 12, 13), 1), (datetime.date(2011, 12, 13), 2), (datetime.date(2011, 12, 13), 3), (datetime.date(2011, 12, 13), 4), (datetime.date(2011, 12, 13), 5)] # Then, let's make a ContentFeed that watches it. class EasyContentFeed(ContentFeed): def get_content(self): return content def get_params(self): return {} def get_updates_since(self, previous): return [t for t in content if t[0] > previous.date()] def get_last_updated_time(self): return max([t[0] for t in content]) def get_changes_to(self, item, since): if since.date() < item[0]: return { 'value': item[1] }, datetime.datetime(2011, 12, 13, 14, 15) def get_label(self): return 'Easy...' library.register(EasyContentFeed, 'easy') feed = EasyContentFeed() # Now we want a subscriber to subscribe to the feed. subscriber = Subscriber.objects.create() subscription = subscriber.subscribe(feed, library=library) # Assume that we last sent the subscription before the current items. subscription.last_sent = datetime.date(2011, 11, 11) subscription.save() # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() # Check that we have a message to send. assert_equal(len(mail.outbox), 1) assert_equal(mail.outbox[0].subject[:19], 'Councilmatic') assert_equal(mail.outbox[0].body, ( '\n\nCouncilmatic!\n==========================\n\nYou are subscribed to the following feeds:\n\n\n* Easy...\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 5\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 2\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 4\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 1\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 3\n\nMore at http://example.com\n\n\n\nTo manage your subscriptions, visit http://example.com/subscriptions/\n' )) # Cool. Clear the mailbox. del mail.outbox[:] # Now, if we update the feed times and send the updated content, there # should be nothing to send. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 0) # Put something else into the feed. content.append((datetime.date(2012, 2, 4), 6)) # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 1)
def test_subscription_flow(): Subscriber.objects.all().delete() ContentFeedRecord.objects.all().delete() del mail.outbox[:] library = ContentFeedLibrary(shared=True) # First, let's make some content content = [ (datetime.date(2011, 12, 13), 1), (datetime.date(2011, 12, 13), 2), (datetime.date(2011, 12, 13), 3), (datetime.date(2011, 12, 13), 4), (datetime.date(2011, 12, 13), 5) ] # Then, let's make a ContentFeed that watches it. class EasyContentFeed (ContentFeed): def get_content(self): return content def get_params(self): return {} def get_updates_since(self, previous): return [t for t in content if t[0] > previous.date()] def get_last_updated_time(self): return max([t[0] for t in content]) def get_changes_to(self, item, since): if since.date() < item[0]: return {'value': item[1]}, datetime.datetime(2011, 12, 13, 14, 15) def get_label(self): return 'Easy...' library.register(EasyContentFeed, 'easy') feed = EasyContentFeed() # Now we want a subscriber to subscribe to the feed. subscriber = Subscriber.objects.create() subscription = subscriber.subscribe(feed, library=library) # Assume that we last sent the subscription before the current items. subscription.last_sent = datetime.date(2011, 11, 11) subscription.save() # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() # Check that we have a message to send. assert_equal(len(mail.outbox), 1) assert_equal(mail.outbox[0].subject[:19], 'Councilmatic') assert_equal(mail.outbox[0].body, ('\n\nCouncilmatic!\n==========================\n\nYou are subscribed to the following feeds:\n\n\n* Easy...\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 5\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 2\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 4\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 1\n\nMore at http://example.com\n\n\n--------------------------------------------------------------------------------\n\n \n\nvalue: 3\n\nMore at http://example.com\n\n\n\nTo manage your subscriptions, visit http://example.com/subscriptions/\n')) # Cool. Clear the mailbox. del mail.outbox[:] # Now, if we update the feed times and send the updated content, there # should be nothing to send. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 0) # Put something else into the feed. content.append((datetime.date(2012, 2, 4), 6)) # Now, update all the feeds dates/times and send out the updated content. update = updatefeeds.Command() update.handle() send = sendfeedupdates.Command() send.handle() assert_equal(len(mail.outbox), 1)