Esempio n. 1
0
    def test_fetch_from_empty_cache(self):
        """Test if there are not any event returned when the cache is empty"""

        cache = Cache(self.tmp_path)
        dockerhub = DockerHub('grimoirelab', 'perceval', cache=cache)
        cached_items = [item for item in dockerhub.fetch_from_cache()]
        self.assertEqual(len(cached_items), 0)
Esempio n. 2
0
    def test_fetch_from_non_set_cache(self):
        """Test if a error is raised when the cache was not set"""

        dockerhub = DockerHub('grimoirelab', 'perceval')

        with self.assertRaises(CacheError):
            _ = [item for item in dockerhub.fetch_from_cache()]
Esempio n. 3
0
    def test_fetch_from_cache(self, mock_utcnow):
        """Test whether the cache works"""

        setup_http_server()

        # First, we fetch the items from the server,
        # storing them in a cache. We do it twice as each call
        # to fetch only returns one item.
        cache = Cache(self.tmp_path)
        dockerhub = DockerHub('grimoirelab', 'perceval', cache=cache)

        mock_utcnow.return_value = datetime.datetime(
            2017, 1, 1, tzinfo=dateutil.tz.tzutc())
        items = [item for item in dockerhub.fetch()]

        mock_utcnow.return_value = datetime.datetime(
            2017, 1, 2, tzinfo=dateutil.tz.tzutc())
        aux = [item for item in dockerhub.fetch()]
        items.extend(aux)

        self.assertEqual(len(httpretty.httpretty.latest_requests), 2)

        # Now, we get the items from the cache.
        # The items should be the same and there won't be
        # any new request to the server
        cached_items = [item for item in dockerhub.fetch_from_cache()]
        self.assertEqual(len(cached_items), len(items))

        expected = [(1483228800.0, '0fa16dc4edab9130a14914a8d797f634d13b4ff4',
                     1483228800.0),
                    (1483315200.0, '0ce2bdf5ddeef42886d9ce4b573c98345e6f1b9a',
                     1483315200.0)]

        self.assertEqual(len(cached_items), len(expected))

        for x in range(len(cached_items)):
            item = cached_items[x]
            expc = expected[x]
            self.assertEqual(item['data']['fetched_on'], expc[0])
            self.assertEqual(item['uuid'], expc[1])
            self.assertEqual(item['origin'],
                             'https://hub.docker.com/grimoirelab/perceval')
            self.assertEqual(item['updated_on'], expc[2])
            self.assertEqual(item['category'], 'dockerhub-data')
            self.assertEqual(item['tag'],
                             'https://hub.docker.com/grimoirelab/perceval')

            # Compare chached and fetched task
            self.assertDictEqual(item['data'], items[x]['data'])

        # No more requests were sent
        self.assertEqual(len(httpretty.httpretty.latest_requests), 2)