def test_aggregate_feed_listing_should_be_sorted():
    """Should produce a sorted listing of feed entries."""
    unsorted_entries = [
        FeedEntry(xkcd_feed, xkcd_items[1]),
        FeedEntry(xkcd_feed, xkcd_items[0])
    ]
    aggregate_feed = AggregateFeed()
    aggregate_feed.entries = unsorted_entries
    assert_equals(xkcd_output, FeedWriter().entry_listings(aggregate_feed))
def test_combine_feeds():
    """Should combine feeds into a list of FeedEntries"""
    subject = AggregateFeed()
    mock_feeds = [Mock(), Mock()]
    aggregate_feed = Mock()
    aggregate_feed.expects(once()).add_single_feed(same(aggregate_feed),
                                                   same(mock_feeds[0]))
    aggregate_feed.expects(once()).add_single_feed(same(aggregate_feed),
                                                   same(mock_feeds[1]))
    subject.combine_feeds(aggregate_feed, mock_feeds)
    aggregate_feed.verify()
def test_create_entry():
    """Create a xkcd_feed item from a xkcd_feed and a xkcd_feed entry"""
    agg_feed = AggregateFeed()
    agg_feed.feed_factory = Mock()
    (aggregate_feed, xkcd_feed, entry, converted) = (Mock(), Mock(), Mock(),
                                                     Mock())
    agg_feed.feed_factory.expects(once()).from_parsed_feed(
        same(xkcd_feed), same(entry)).will(return_value(converted))
    aggregate_feed.expects(once()).add(same(converted))
    agg_feed.create_entry(aggregate_feed, xkcd_feed, entry)
    aggregate_feed.verify()
def test_create_entry():
    """Should create an entry and add it to the collection"""
    subject = AggregateFeed()
    (feed, entry) = (dummy(), dummy())
    new_entry = dummy()
    override(rsreader.application, 'FeedEntry')\
    .expects(feed, entry)\
    .returns(new_entry)
    override(subject, 'add').expects(new_entry)
    replay()
    subject.create_entry(feed, entry)
    verify()
def test_add_single_feed():
    """Should create a new entry for each entry in the feed"""
    subject = AggregateFeed()
    entries = [dummy(), dummy()]
    feed = mock()
    feed.entries
    returns(entries)
    override(subject, 'create_entry').expects(feed, entries[0])
    override(subject, 'create_entry').expects(feed, entries[1])
    replay()
    subject.add_single_feed(feed)
    verify()
def test_get_feeds_from_urls():
    """Should get a feed for every URL"""
    urls = [Mock(), Mock()]
    feeds = [Mock(), Mock()]
    subject = AggregateFeed()
    subject.feedparser = Mock()
    subject.feedparser.expects(once()).parse(same(urls[0])).will(
        return_value(feeds[0]))
    subject.feedparser.expects(once()).parse(same(urls[1])).will(
        return_value(feeds[1]))
    returned_feeds = subject.feeds_from_urls(urls)
    assert_equals(feeds, returned_feeds)
    subject.feedparser.verify()
def test_print_agg_feed_listing_is_printed():
    """Should print listing of feed entries"""
    unsorted_entries = [
        FeedEntry(xkcd_feed, xkcd_items[1]),
        FeedEntry(xkcd_feed, xkcd_items[0])
    ]
    aggregate_feed = AggregateFeed()
    aggregate_feed.entries = unsorted_entries
    override(sys, 'stdout')
    method(sys.stdout, 'write').expects(xkcd_output + os.linesep)
    replay()
    FeedWriter().print_entry_listings(aggregate_feed)
    verify()
def test_is_empty():
    """Ensure that is_empty reports emptiness as expected"""
    empty_aggregate_feed = AggregateFeed()
    non_empty_aggregate_feed = AggregateFeed()
    non_empty_aggregate_feed.add("foo")
    assert empty_aggregate_feed.is_empty() is True
    assert non_empty_aggregate_feed.is_empty() is False
def test_from_urls():
    """Should retrieve feeds and add them to the aggregate"""
    urls = [dummy(), dummy()]
    feeds = [dummy(), dummy()]
    subject = AggregateFeed()
    #
    override(feedparser, 'parse').expects(urls[0]).returns(feeds[0])
    override(subject, 'add_single_feed').expects(feeds[0])
    #
    override(feedparser, 'parse').expects(urls[1]).returns(feeds[1])
    override(subject, 'add_single_feed').expects(feeds[1])
    #
    replay()
    subject.from_urls(urls)
    verify()
def test_print_entry_listing_does_nothing_with_an_empty_aggregate():
    """Ensure that nothing is printed with an empty aggregate"""
    empty_aggregate_feed = AggregateFeed()
    override(sys, 'stdout')
    replay()
    FeedWriter().print_entry_listings(empty_aggregate_feed)
    verify()
def test_from_urls():
    """Should get feeds from URLs and combine them"""
    urls = [Mock(), Mock()]
    feeds = [Mock(), Mock()]
    subject = AggregateFeed()
    aggregate_feed = Mock()
    subject.feedparser = Mock()
    #
    subject.feedparser.expects(once()).parse(same(urls[0])).will(
        return_value(feeds[0]))
    aggregate_feed.expects(once()).\
        add_single_feed(same(aggregate_feed), same(feeds[0]))
    #
    subject.feedparser.expects(once()).parse(same(urls[1])).will(
        return_value(feeds[1]))
    aggregate_feed.expects(once()).\
        add_single_feed(same(aggregate_feed), same(feeds[1]))
    #
    subject.from_urls(aggregate_feed, urls)
    subject.feedparser.verify()
    aggregate_feed.verify()
def test_add_singled_feed():
    """Should add a single xkcd_feed to a set of feeds"""
    entries = [Mock(), Mock()]
    xkcd_feed = {'entries': entries}
    aggregate_feed = Mock()
    aggregate_feed.expects(once()).create_entry(same(aggregate_feed),
                                                same(xkcd_feed),
                                                same(entries[0]))
    aggregate_feed.expects(once()).create_entry(same(aggregate_feed),
                                                same(xkcd_feed),
                                                same(entries[1]))
    AggregateFeed().add_single_feed(aggregate_feed, xkcd_feed)
    aggregate_feed.verify()
def test_add():
    """Add a xkcd_feed entry to the aggregate"""
    entry = Mock()
    subject = AggregateFeed()
    subject.add(entry)
    assert_equals(set([entry]), subject.entries)
def test_aggregate_feed_creates_factory():
    """Verify that the AggregatedFed object creates a factory
    when instantiated"""
    assert_equals(FeedEntry, AggregateFeed().feed_factory)
def test_is_empty():
    """Unsure is empty works"""
    aggregate_feed = AggregateFeed()
    assert aggregate_feed.is_empty()
    aggregate_feed.add("foo")
    assert not aggregate_feed.is_empty()
def test_aggregate_feed_initializes_feed_parser():
    """Ensure AggregateFeed initializes dependency on feedparser"""
    assert_equals(feedparser, AggregateFeed().feedparser)
def test_entries_is_always_defined():
    """The entries set should always be defined"""
    assert_equals(set(), AggregateFeed().entries)