Пример #1
0
    def test_fetch_news_items_raise_FeedReaderException(self):
        # TODO: create a mock RssNewsFeedParser object and assign it to a local
        # variable named `mock_news_feed_parser`
        mock_news_feed_parser = Mock(spec=RssNewsFeedParser)

        # TODO: configure the mock so that a call to its get_news() method has
        # the side effect of raising a FeedReaderException.
        mock_news_feed_parser.get_news.side_effect = FeedReaderException()

        # TODO: create a FeedReader instance and assign it to a local variable
        # named `feed_reader`
        feed_reader = FeedReader()

        # TODO: set the feed_reader.news_feed_parser attribute to
        # mock_news_feed_parser
        feed_reader.news_feed_parser = mock_news_feed_parser

        # TODO: call the feed_reader's fetch_news_items() method and save the
        # return value in a variable named `news`. (Pass any string as the
        # argument to fetch_news_items())
        news = feed_reader.fetch_news_items('quack')

        # TODO: assert that the `news` variable is an instance of list.
        self.assertTrue(isinstance(news, list))

        # TODO: assert that the length of the `news` list is 0
        self.assertEqual(0, len(news))
Пример #2
0
    def test_fetch_news_items_patch_decorator(self,
                                              mock_news_feed_parser_class):
        # Always patch an object where it is imported, not where it is defined.
        # Here, we're testing a method in
        # ticketmanor.rest_services.feed_reader.feed_reader.FeedReader, which
        # imports RssNewsFeedParser. So to mock RssNewsFeedParser, patch()
        # needs to know the package that is importing RssNewsFeedParser, not
        # the package that defines RssNewsFeedParser.

        # Note that the second argument to the test method is a
        # Mock class, not a Mock instance, so we need an additional step
        # to get a Mock instance for setting the get_news() return value.
        mock_news_feed_parser = mock_news_feed_parser_class.return_value
        mock_news_feed_parser.get_news.return_value = expected

        feed_reader = FeedReader()

        news = feed_reader.fetch_news_items("music", 2)

        # verify that the mock's get_news() method was called correctly
        # (for details, see Appendix B)
        mock_news_feed_parser.get_news.assert_called_once_with("music", 2)
        # verify results
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #3
0
    def test_fetch_news_items_raise_FeedReaderException(self):
        # TODO: create a mock RssNewsFeedParser object and assign it to a local
        # variable named `mock_news_feed_parser`
        mock_news_feed_parser = Mock(spec=RssNewsFeedParser)

        # TODO: configure the mock so that a call to its get_news() method has
        # the side effect of raising a FeedReaderException.
        mock_news_feed_parser.get_news.side_effect = FeedReaderException()

        # TODO: create a FeedReader instance and assign it to a local variable
        # named `feed_reader`
        feed_reader = FeedReader()

        # TODO: set the feed_reader.news_feed_parser attribute to
        # mock_news_feed_parser
        feed_reader.news_feed_parser = mock_news_feed_parser

        # TODO: call the feed_reader's fetch_news_items() method and save the
        # return value in a variable named `news`. (Pass any string as the
        # argument to fetch_news_items())
        news = feed_reader.fetch_news_items('quack')

        # TODO: assert that the `news` variable is an instance of list.
        self.assertTrue(isinstance(news, list))

        # TODO: assert that the length of the `news` list is 0
        self.assertEqual(0, len(news))
Пример #4
0
    def test_fetch_news_items_music(self):
        # TODO: create a mock RssNewsFeedParser object and assign it to a local
        # variable named `mock_news_feed_parser`
        mock_news_feed_parser = Mock(spec=RssNewsFeedParser)

        # TODO: set the return value of the mock's get_news() method to the
        # value of the variable `expected` (defined at the end of the file)
        mock_news_feed_parser.get_news.return_value = expected

        # TODO: create a FeedReader instance and assign it to a local variable
        # named `feed_reader`
        feed_reader = FeedReader()

        # TODO: set the feed_reader.news_feed_parser attribute to the
        # mock_news_feed_parser
        feed_reader.news_feed_parser = mock_news_feed_parser

        # TODO: note the call the call to feed_reader.fetch_news_items().
        # Because you changed the feed reader's `news_feed_parser` attribute in
        # the previous statement, the feed_reader will get news from the mock
        # object instead of a RssNewsFeedParser instance.
        # (no code change required)
        news = feed_reader.fetch_news_items("music")

        # TODO: note that we verify the result as usual.
        # (no code change required)
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #5
0
    def test_fetch_news_items_music(self):
        # TODO: create a mock RssNewsFeedParser object and assign it to a local
        # variable named `mock_news_feed_parser`
        mock_news_feed_parser = Mock(spec=RssNewsFeedParser)

        # TODO: set the return value of the mock's get_news() method to the
        # value of the variable `expected` (defined at the end of the file)
        mock_news_feed_parser.get_news.return_value = expected

        # TODO: create a FeedReader instance and assign it to a local variable
        # named `feed_reader`
        feed_reader = FeedReader()

        # TODO: set the feed_reader.news_feed_parser attribute to the
        # mock_news_feed_parser
        feed_reader.news_feed_parser = mock_news_feed_parser

        # TODO: note the call the call to feed_reader.fetch_news_items().
        # Because you changed the feed reader's `news_feed_parser` attribute in
        # the previous statement, the feed_reader will get news from the mock
        # object instead of a RssNewsFeedParser instance.
        # (no code change required)
        news = feed_reader.fetch_news_items("music")

        # TODO: note that we verify the result as usual.
        # (no code change required)
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #6
0
    def test_fetch_news_items_max_items_2(self):
        # test set up
        mock_news_feed_parser = Mock(spec=RssNewsFeedParser)
        mock_news_feed_parser.get_news.return_value = expected
        feed_reader = FeedReader()
        feed_reader.news_feed_parser = mock_news_feed_parser

        # call method under test
        news = feed_reader.fetch_news_items("music", 2)

        # verify that the mock's get_news() method was called correctly
        # (for details, see Appendix B)
        mock_news_feed_parser.get_news.assert_called_once_with("music", 2)
        # verify results
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #7
0
    def test_fetch_news_items_max_items_1(self):
        # test set up
        mock_news_feed_parser = Mock(spec=RssNewsFeedParser)
        mock_news_feed_parser.get_news.return_value = expected
        feed_reader = FeedReader()
        feed_reader.news_feed_parser = mock_news_feed_parser

        # call method under test
        news = feed_reader.fetch_news_items("music", 1)

        # verify that the mock's get_news() method was called correctly
        # (for details, see Appendix B)
        mock_news_feed_parser.get_news.assert_called_once_with("music", 1)
        # verify results
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #8
0
class TestFeedReader(TestCase):
    """Unit tests for FeedReader"""

    def setUp(self):
        # The arg to FeedReader.__init__() is the class of the news feed
        # parser. FeedReader will call the constructor of the news feed parser
        # class, so we'll let it create a Mock, then we can access the Mock as
        # an attribute of the feed parser.
        self.feed_reader = FeedReader(news_feed_parser=Mock)

    def test_get_news_music(self):
        # The Mock will create new mocks anytime an attribute is referenced.
        # In the following statement, mocks are created for the
        # _news_feed_parser and get_news attributes. Because get_news is a
        # method, we can also set the method's return value.
        self.feed_reader._news_feed_parser.get_news.return_value = expected

        # The feed reader's get_news() method delegates to the news feed
        # parser's get_news(), which is a mock that we created in the previous
        # statement. The parser's get_news() will return the value of the
        # "expected" list.
        news = self.feed_reader.fetch_news_items("music")

        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)

    def test_get_news_max_items_1(self):
        self.feed_reader._news_feed_parser.get_news.return_value = expected[:1]

        news = self.feed_reader.fetch_news_items("music", max_items=1)

        for expected_result, actual_result in zip_longest(expected[:1], news):
            self.assertEqual(expected_result, actual_result)

    def test_get_news_max_items_2(self):
        self.feed_reader._news_feed_parser.get_news.return_value = expected[:2]

        news = self.feed_reader.fetch_news_items("music", max_items=2)

        for expected_result, actual_result in zip_longest(expected[:2], news):
            self.assertEqual(expected_result, actual_result)
Пример #9
0
class TestFeedReader(TestCase):
    """Unit tests for FeedReader"""

    def setUp(self):
        # The arg to FeedReader.__init__() is the class of the news feed
        # parser. FeedReader will call the constructor of the news feed parser
        # class, so we'll let it create a Mock, then we can access the Mock as
        # an attribute of the feed parser.
        self.feed_reader = FeedReader(news_feed_parser_class=Mock)

    def test_get_news_music(self):
        # The Mock will create new mocks anytime an attribute is referenced.
        # In the following statement, mocks are created for the
        # _news_feed_parser and get_news attributes. Because get_news is a
        # method, we can also set the method's return value.
        self.feed_reader._news_feed_parser.get_news.return_value = expected

        # The feed reader's get_news() method delegates to the news feed
        # parser's get_news(), which is a mock that we created in the previous
        # statement. The parser's get_news() will return the value of the
        # "expected" list.
        news = self.feed_reader.get_news("music")

        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)

    def test_get_news_max_items_1(self):
        self.feed_reader._news_feed_parser.get_news.return_value = expected[:1]

        news = self.feed_reader.get_news("music", max_items=1)

        for expected_result, actual_result in zip_longest(expected[:1], news):
            self.assertEqual(expected_result, actual_result)

    def test_get_news_max_items_2(self):
        self.feed_reader._news_feed_parser.get_news.return_value = expected[:2]

        news = self.feed_reader.get_news("music", max_items=2)

        for expected_result, actual_result in zip_longest(expected[:2], news):
            self.assertEqual(expected_result, actual_result)
Пример #10
0
    def test_fetch_news_items_patch_object_decorator(self, mock_get_news_method):
        # Set the return value of the mocked method to the `expected`
        # variable (defined at the end of the file)
        mock_get_news_method.return_value = expected

        # Note the call to the FeedReader constructor. The call to the
        # RssNewsFeedParser will happen as usual, but because we added
        # @patch.object to this test method, the RssNewsFeedParser's get_news
        # attribute will be replaced by a Mock.
        feed_reader = FeedReader()

        # Note that the call to the feed_reader's fetch_news_items()
        # will call the Mock's get_news(), which you programmed to return
        # `expected`
        news = feed_reader.fetch_news_items("music", 1)

        # verify that the mock's get_news() method was called correctly
        # (for details, see Appendix B)
        mock_get_news_method.assert_called_once_with("music", 1)
        # verify results
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #11
0
    def test_fetch_news_items_patch_object_decorator(self,
                                                     mock_get_news_method):
        # Set the return value of the mocked method to the `expected`
        # variable (defined at the end of the file)
        mock_get_news_method.return_value = expected

        # Note the call to the FeedReader constructor. The call to the
        # RssNewsFeedParser will happen as usual, but because we added
        # @patch.object to this test method, the RssNewsFeedParser's get_news
        # attribute will be replaced by a Mock.
        feed_reader = FeedReader()

        # Note that the call to the feed_reader's fetch_news_items()
        # will call the Mock's get_news(), which you programmed to return
        # `expected`
        news = feed_reader.fetch_news_items("music", 1)

        # verify that the mock's get_news() method was called correctly
        # (for details, see Appendix B)
        mock_get_news_method.assert_called_once_with("music", 1)
        # verify results
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #12
0
    def test_fetch_news_items_patch_decorator(self, mock_news_feed_parser_class):
        # Always patch an object where it is imported, not where it is defined.
        # Here, we're testing a method in
        # ticketmanor.rest_services.feed_reader.feed_reader.FeedReader, which
        # imports RssNewsFeedParser. So to mock RssNewsFeedParser, patch()
        # needs to know the package that is importing RssNewsFeedParser, not
        # the package that defines RssNewsFeedParser.

        # Note that the second argument to the test method is a
        # Mock class, not a Mock instance, so we need an additional step
        # to get a Mock instance for setting the get_news() return value.
        mock_news_feed_parser = mock_news_feed_parser_class.return_value
        mock_news_feed_parser.get_news.return_value = expected

        feed_reader = FeedReader()

        news = feed_reader.fetch_news_items("music", 2)

        # verify that the mock's get_news() method was called correctly
        # (for details, see Appendix B)
        mock_news_feed_parser.get_news.assert_called_once_with("music", 2)
        # verify results
        for expected_result, actual_result in zip_longest(expected, news):
            self.assertEqual(expected_result, actual_result)
Пример #13
0
class AllNewsFeedReader:
    """Reads news feeds for music, concerts, and sports"""

    def __init__(self):
        self.feed_reader = FeedReader()

    def get_news(self, max_items=0):
        """
        Get news items of all news types.

        If max_items > 0, return no more than max_items news items.
        Otherwise, return all news items.
        """
        concert_news = self.worker("concerts", max_items)
        sports_news = self.worker("sports", max_items)
        movie_news = self.worker("movies", max_items)
        return {"music": concert_news, "sports": sports_news, "movie": movie_news}

    def worker(self, news_type, max_items):
        sleep(1)  # pause, to simulate a slow response (needed for ex 9.2)
        return self.feed_reader.fetch_news_items(news_type, max_items)
Пример #14
0
class AllNewsFeedReader:
    """Reads news feeds for music, concerts, and sports"""
    def __init__(self):
        self.feed_reader = FeedReader()

    def get_news(self, max_items=0):
        """
        Get news items of all news types.

        If max_items > 0, return no more than max_items news items.
        Otherwise, return all news items.
        """
        concert_news = self.worker('concerts', max_items)
        sports_news = self.worker('sports', max_items)
        movie_news = self.worker('movies', max_items)
        return {
            'music': concert_news,
            'sports': sports_news,
            'movie': movie_news
        }

    def worker(self, news_type, max_items):
        sleep(1)  # pause, to simulate a slow response (needed for ex 9.2)
        return self.feed_reader.fetch_news_items(news_type, max_items)
Пример #15
0
 def setUp(self):
     # The arg to FeedReader.__init__() is the class of the news feed
     # parser. FeedReader will call the constructor of the news feed parser
     # class, so we'll let it create a Mock, then we can access the Mock as
     # an attribute of the feed parser.
     self.feed_reader = FeedReader(news_feed_parser=Mock)
Пример #16
0
 def __init__(self):
     self.feed_reader = FeedReader()
Пример #17
0
 def __init__(self):
     self.feed_reader = FeedReader()
Пример #18
0
 def setUp(self):
     # The arg to FeedReader.__init__() is the class of the news feed
     # parser. FeedReader will call the constructor of the news feed parser
     # class, so we'll let it create a Mock, then we can access the Mock as
     # an attribute of the feed parser.
     self.feed_reader = FeedReader(news_feed_parser_class=Mock)