def test_fetch_no_hits(self, mock_utcnow):
        """Test whether it handles queries which have no hits"""

        mock_utcnow.return_value = datetime.datetime(
            2017, 1, 1, tzinfo=dateutil.tz.tzutc())
        setup_http_server(no_hits=True)

        backend = GoogleHits(['bitergia'])

        with self.assertLogs() as cm:
            items = [item for item in backend.fetch()]
            self.assertEqual(
                cm.output[-2],
                "WARNING:perceval.backends.core.googlehits:No hits for ['bitergia']"
            )

        self.assertEqual(len(items), 1)
        item = items[0]
        self.assertEqual(item['data']['fetched_on'], 1483228800.0)
        self.assertEqual(item['data']['id'],
                         '18e8f9809f9c539bfe0ac5ad2724323873931825')
        self.assertEqual(item['data']['hits'], 0)
        self.assertEqual(item['data']['keywords'], ['bitergia'])
        self.assertEqual(item['uuid'],
                         '119b60909d6560e2d22a579404c30c98f5cdc33d')
        self.assertEqual(item['origin'], 'https://www.google.com/search')
        self.assertEqual(item['updated_on'], 1483228800.0)
        self.assertEqual(item['category'], CATEGORY_HITS)
        self.assertEqual(item['tag'], 'https://www.google.com/search')
    def test_initialization(self):
        """Test whether attributes are initializated"""

        backend = GoogleHits(['bitergia'], tag='test')

        self.assertEqual(backend.keywords, ['bitergia'])
        self.assertEqual(backend.origin, GOOGLE_SEARCH_URL)
        self.assertEqual(backend.tag, 'test')
        self.assertIsNone(backend.client)
        self.assertEqual(backend.max_retries, MAX_RETRIES)
        self.assertEqual(backend.sleep_time, DEFAULT_SLEEP_TIME)

        # When tag is empty or None it will be set to the value in
        backend = GoogleHits(['bitergia', 'grimoirelab'])
        self.assertEqual(backend.keywords, ['bitergia', 'grimoirelab'])
        self.assertEqual(backend.origin, GOOGLE_SEARCH_URL)
        self.assertEqual(backend.tag, GOOGLE_SEARCH_URL)

        backend = GoogleHits(['bitergia', 'grimoirelab'], tag='')
        self.assertEqual(backend.keywords, ['bitergia', 'grimoirelab'])
        self.assertEqual(backend.origin, GOOGLE_SEARCH_URL)
        self.assertEqual(backend.tag, GOOGLE_SEARCH_URL)

        backend = GoogleHits(['bitergia', 'grimoirelab'], tag='', max_retries=1, sleep_time=100)
        self.assertEqual(backend.keywords, ['bitergia', 'grimoirelab'])
        self.assertEqual(backend.origin, GOOGLE_SEARCH_URL)
        self.assertEqual(backend.tag, GOOGLE_SEARCH_URL)
        self.assertEqual(backend.max_retries, 1)
        self.assertEqual(backend.sleep_time, 100)

        with self.assertRaises(BackendError):
            _ = GoogleHits([''], tag='')
    def test_search_fields(self, mock_utcnow):
        """Test whether the search_fields is properly set"""

        mock_utcnow.return_value = datetime.datetime(
            2017, 1, 1, tzinfo=dateutil.tz.tzutc())
        setup_http_server()

        backend = GoogleHits(['bitergia'])
        items = [item for item in backend.fetch()]

        item = items[0]
        self.assertEqual(backend.metadata_id(item['data']),
                         item['search_fields']['item_id'])
        self.assertListEqual(item['data']['keywords'], ['bitergia'])
        self.assertListEqual(item['data']['keywords'],
                             item['search_fields']['keywords'])
    def test_fetch(self, mock_utcnow):
        """Test whether it fetches data from the Google Search API"""

        mock_utcnow.return_value = datetime.datetime(2017, 1, 1,
                                                     tzinfo=dateutil.tz.tzutc())
        setup_http_server()

        backend = GoogleHits(['bitergia'])
        items = [item for item in backend.fetch()]

        self.assertEqual(len(items), 1)
        item = items[0]
        self.assertEqual(item['data']['fetched_on'], 1483228800.0)
        self.assertEqual(item['data']['id'], '18e8f9809f9c539bfe0ac5ad2724323873931825')
        self.assertEqual(item['data']['keywords'], ['bitergia'])
        self.assertEqual(item['uuid'], '119b60909d6560e2d22a579404c30c98f5cdc33d')
        self.assertEqual(item['origin'], 'https://www.google.com/search')
        self.assertEqual(item['updated_on'], 1483228800.0)
        self.assertEqual(item['category'], CATEGORY_HITS)
        self.assertEqual(item['tag'], 'https://www.google.com/search')

        backend = GoogleHits(['bitergia', 'grimoirelab'])
        items = [item for item in backend.fetch()]

        self.assertEqual(len(items), 1)
        item = items[0]

        self.assertEqual(item['data']['fetched_on'], 1483228800.0)
        self.assertEqual(item['data']['id'], '2aaa5f3ab512ca6c451cb3c21c77da3d4510f75c')
        self.assertEqual(item['data']['keywords'], ['bitergia', 'grimoirelab'])
        self.assertEqual(item['uuid'], '3a83dfa224891986d091e708a2afd165df59576b')
        self.assertEqual(item['origin'], 'https://www.google.com/search')
        self.assertEqual(item['updated_on'], 1483228800.0)
        self.assertEqual(item['category'], CATEGORY_HITS)
        self.assertEqual(item['tag'], 'https://www.google.com/search')
 def setUp(self):
     super().setUp()
     self.backend_write_archive = GoogleHits(['bitergia'],
                                             archive=self.archive)
     self.backend_read_archive = GoogleHits(['bitergia'],
                                            archive=self.archive)
    def test_has_resuming(self):
        """Test if it returns True when has_resuming is called"""

        self.assertEqual(GoogleHits.has_resuming(), True)