Exemple #1
0
class TestIndexer(unittest.TestCase):
    """ Tests the indexer class.

    """
    def setUp(self):
        self.co = Collector()
        self.indexer = ind.Indexer(self.co)

    def tearDown(self):
        rm_data_dir()

    def test_add_feed(self):
        """ Tests add_feed. 

        Add a feed to the collector.
        Add a feed to the indexer.

        For each item added:
         1- Check if the webpage url exists in the indexer. 

        """
        name, url, tag, _ = Manager.get_feeds_info()[0]
        self.co.add_feed(name, url, tag)
        self.indexer.add_feed(name)

        for _, item in self.co.get_items(name):
            query = self.indexer._Indexer__query("webpage_url", item.webpage_url)

            with self.indexer.ix.searcher() as s:
                self.assertGreater(len(s.search(query)), 0)

    def test_add_feeds(self):
        """ Tests add_feeds.

        Add feeds to the collector.
        Add feeds to the indexer.

        For each feed added, get the first item:
          1- Check if the webpage url exists in the indexer. 

        """
        for name, url, tag, _ in Manager.get_feeds_info():
            self.co.add_feed(name, url, tag)

        self.indexer.add_feeds()

        for name, _ in self.co.get_feeds():
            _, item = self.co.get_items(name).next()
            query = self.indexer._Indexer__query("webpage_url", item.webpage_url)

            with self.indexer.ix.searcher() as s:
                self.assertEquals(len(s.search(query)), 1)

    def test_rm_feed(self):
        """ Tests rm_feed.

        Add a feed to the collector.
        Add a feed to the indexer.

        Get the first item:
          1- Check if the webpage url exists in the indexer. 

        Remove the feed with the query
          2- Check if the same query retrieves no result.

        """
        name, url, tag, _ = Manager.get_feeds_info()[0]
        self.co.add_feed(name, url, tag)
        self.indexer.add_feed(name)
        _, item = self.co.get_items(name).next()

        field = "webpage_url"
        keyword = item.webpage_url
        
        query = self.indexer._Indexer__query(field, keyword)

        with self.indexer.ix.searcher() as s:
            self.assertEquals(len(s.search(query)), 1) # 1

        self.indexer.rm_feed(field, keyword, print_search=False)

        with self.indexer.ix.searcher() as s:
            self.assertEquals(len(s.search(query)), 0) # 2

    def test_search_feeds(self):
        """ Tests search_feeds.

        Add a feed to the collector.
        Add a feed to the indexer.

        1- Search feeds.
        2- Search feeds by setting a query.
        Result is visual and must be the same.

        """
        name, url, tag, _ = Manager.get_feeds_info()[0]
        self.co.add_feed(name, url, tag)
        self.indexer.add_feed(name)
        _, item = self.co.get_items(name).next()

        field = "webpage_url"
        keyword = item.webpage_url

        self.indexer.search_feeds(field, keyword) # 1

        query = self.indexer._Indexer__query(field, keyword)
        self.indexer.search_feeds(field, keyword, query) # 2

    def test___query(self):
        """ Tests __query.

        Add feeds to the collector.
        Add feeds to the indexer.

        Build a query:
          1- Check if the webpage url exists in the indexer. 

        """
        name, url, tag, _ = Manager.get_feeds_info()[0]
        self.co.add_feed(name, url, tag)
        self.indexer.add_feed(name)
        _, item = self.co.get_items(name).next()

        field = "webpage_url"
        keyword = item.webpage_url

        query = self.indexer._Indexer__query(field, keyword)
        with self.indexer.ix.searcher() as s:
            self.assertEquals(len(s.search(query)), 1) # 1

    def test___print_result(self):
        """ Tests __print_result.

        1- Print a result on the screen.
        2- Print no result on the screen.

        """
        results = [{
    "title":u"Les sodas accéléreraient le vieillissement de l'ADN et des cellules",
    "published":u"Wed Apr 23 9:38:43 CEST 2014",
    "abstract":u"Selon une étude américaine, la consommation de boissons gazeuses sucrées accélérerait le vieillissement de l'ADN et des cellules. Ce qui pourrait avoir des conséquences sur le développement de certaines maladies",
    "item_id":u"2141636843256855236",
    "webpage_url":u"http://www.bfmtv.com/societe/les-sodas-responsables-d-un-vieillissement-accelere-de-l-adn-et-des-cellules-840847.html",
    "language":"french",
    "tag":u"SPORT",
    "predite":u""}]

        self.indexer._Indexer__print_result(results) # 1
        self.indexer._Indexer__print_result([]) # 2

    def test_prompt(self):
        """ Tests the prompt.

        Simulate the "exit" input and quit.

        """
        def mock_raw_input_exit(s):
            return "exit"

        ind.raw_input = mock_raw_input_exit
        indexer = ind.Indexer(self.co)
        indexer.prompt()
Exemple #2
0
class TestCollector(unittest.TestCase):
    """ Tests the Collector class.

    """
    def setUp(self):
        self.co = Collector()
        self.feed_info = Manager.get_feeds_info()

    def tearDown(self):
        rm_data_dir()

    def test_add_feed(self):
        """ Tests add_feed.

        Add feeds with "add_feed":
         1- Test if the feeds has been added.
         2- Test if the feeds exists in the database.

        Try adding a feed already added: 
         3- Test if the feed has not been added.

        """
        for name, url, tag, _ in self.feed_info:
            feed = self.co.add_feed(name, url, tag)
            self.assertIsNotNone(feed) # 1
            self.assertTrue(self.co.has_feed(name)) # 2

        name, url, tag = self.feed_info[0][:3]
        feed = self.co.add_feed(name, url, tag)
        self.assertIsNone(feed) # 3

    def test_update_feed(self):
        """ Tests update_feed.

        Add feeds.

        Try updating feed with "update_feed":
         1- Test if the feed doesn't need to be updated.

        #TODO
        Find a way to update a feed in the test.

        """
        for name, url, tag, _ in self.feed_info:
            self.co.add_feed(name, url, tag)

            updated = self.co.update_feed(name)
            self.assertFalse(updated) # 1

    def test_rm_feed(self):
        """ Tests rm_feed.

        Add feeds:
         1- Test if the feeds exists first.

        Remove feeds with "rm_feed".
         2- Test if the feeds has been removed.

        """
        for name, url, tag, _ in self.feed_info:
            self.co.add_feed(name, url, tag)

            self.assertTrue(self.co.has_feed(name)) # 1
            self.co.rm_feed(name)
            self.assertFalse(self.co.has_feed(name)) # 2

    def test_update_item_tag(self):
        """ Tests update_item_tag.

        Add a feed:
         1- Test if an item has not the tag attribute.

        Add a category with "update_item_tag".
         2- Test if the item updated is the same with the id.
         3- Test if the item updated has the tag attribute. 
         4- Test if the item updated has the correct category.

        """
        category = "SPORT"

        name, url, tag = self.feed_info[0][:3]
        feed = self.co.add_feed(name, url, tag)
        item_id, item = self.co.get_items(name).next()
        self.assertFalse(hasattr(item, "tag")) # 1

        self.co.update_item_tag(feed.item_db_filename, item_id, category)
        item_id_updated, item_updated = self.co.get_items(name).next()

        self.assertEquals(item_id, item_id_updated) # 2
        self.assertTrue(hasattr(item_updated, "tag")) # 3
        self.assertEquals(item_updated.tag, category) # 4

    def test_has_feed(self):
        """ Tests has_feed.

        Verify with "has_feed" if the feed exists.
         1- The feed doesn't exists.

        Add a feed.

        Verify with "has_feed" if the feed exists.
         2- The feed exists.

        """
        name, url, tag = self.feed_info[0][:3]

        self.assertFalse(self.co.has_feed(name)) # 1
        self.co.add_feed(name, url, tag)
        self.assertTrue(self.co.has_feed(name)) # 2

    def test_print_feed(self):
        """ Tests print_feed.

        Add a feed.
        Print the feed.

        """
        name, url, tag = self.feed_info[0][:3]
        self.co.add_feed(name, url, tag)

        self.co.print_feed(name)

    def test_print_feeds(self):
        """ Tests print_feeds.

        Add feeds.
        print feeds.

        """
        for name, url, tag, _ in self.feed_info:
            self.co.add_feed(name, url, tag)

        self.co.print_feeds()

    def test_print_items(self):
        """ Tests print_items.

        Add a feed.
        Print items.

        """
        name, url, tag = self.feed_info[0][:3]
        self.co.add_feed(name, url, tag)

        self.co.print_items(name)

    def test_get_text_from_items(self):
        """ Tests get_text_from_items.

        Add a feed.

        For each item with "get_text_from_items" verify if: 
         1- the title is in the text.
         2- the abstract is in the text.
         3- the webpage text is in the text.

        """
        name, url, tag = self.feed_info[0][:3]
        self.co.add_feed(name, url, tag)

        for item, text in self.co.get_text_from_items(name):
            self.assertIn(item.title, text) # 1
            self.assertIn(item.abstract, text) # 2
            self.assertIn(item.webpage_text, text) # 3

    def test_get_feed(self):
        """ Tests get_feed.

        1,2- Test if get_feed retrieves the correct feed by comparing name. 
        3- Test if get_feed retrieve None for an unknown feed.

        """
        name_1, url, tag, _ = self.feed_info[0]
        feed_1_added = self.co.add_feed(name_1, url, tag)
        name_2, url, tag, _ = self.feed_info[1]
        feed_2_added = self.co.add_feed(name_2, url, tag)

        feed_1_get = self.co.get_feed(name_1)
        self.assertEquals(feed_1_get.name, feed_1_added.name) # 1

        feed_2_get = self.co.get_feed(name_2)
        self.assertNotEquals(feed_2_get.name, feed_1_added.name) # 2

        unknown_feed = self.co.get_feed("unknown feed")
        self.assertIsNone(unknown_feed) # 3

    def test_get_feeds(self):
        """ Tests get_feeds.

        Add feeds.

        Get feeds and:
         1- Comparing feed name from the data base with original feeds.
         2- Comparing feed name from feeds with original feeds.
         3- Verify if feeds name from the db are equals to feeds obj name attribute. 
             (1, 2 is not enough for that because the order is not preserved)

        """
        feeds_name = []
        for name, url, tag, _ in self.feed_info:
            self.co.add_feed(name, url, tag)
            feeds_name.append(name)

        feeds_name = set(feeds_name)
        feeds_name_1 = set([name for name, _ in self.co.get_feeds()])
        self.assertEquals(feeds_name, feeds_name_1) # 1

        feeds_name_2 = set([feed.name for _, feed in self.co.get_feeds()])
        self.assertEquals(feeds_name, feeds_name_2) # 2

        for name, feed in self.co.get_feeds():
            self.assertEquals(name, feed.name) # 3

    def test_get_items(self):
        """ Tests get_items.
        
        Add a feed.

        Get the first item:
         1- Verify if the item is not None.

        Get all items:
         2- Verify if the id from the db are equals to the item id attribute.

        """
        name, url, tag, _ = self.feed_info[0]
        feed = self.co.add_feed(name, url, tag)

        item_id, item = self.co.get_items(name).next()
        self.assertIsNotNone(item) # 1

        for item_id, item in self.co.get_items(name):
            self.assertEquals(item_id, str(item.id)) # 2