def test_backup_and_recover_cache(self): """Test if it does a backup of the cache and recovers its data""" # Create a new job with and empty cache job = PercevalJob('arthur-job-1234567890', 'mytask', 'git', self.conn, 'items') job.initialize_cache(self.tmp_path) contents = [item for item in job.cache.retrieve()] self.assertEqual(len(contents), 0) items = [1, 2, 3, 4, 5] job.cache.store(*items) # Initialize a new job and make a backup of the data job = PercevalJob('arthur-job-1234567890-bis', 'mytask', 'git', self.conn, 'items') job.initialize_cache(self.tmp_path, backup=True) contents = [item for item in job.cache.retrieve()] self.assertListEqual(contents, items) # Hard cleaning of the cache data job.cache.clean() contents = [item for item in job.cache.retrieve()] self.assertListEqual(contents, []) # Recover the data job.recover_cache() contents = [item for item in job.cache.retrieve()] self.assertListEqual(contents, items)
def test_initialize_cache(self): """Test if the cache is initialized""" job = PercevalJob('arthur-job-1234567890', 'mytask', 'git', self.conn, 'items') job.initialize_cache(self.tmp_path) self.assertIsInstance(job.cache, Cache) self.assertEqual(job.cache.cache_path, self.tmp_path)
def test_invalid_path_for_cache(self): """Test whether it raises an exception when the cache path is invalid""" job = PercevalJob('arthur-job-1234567890', 'mytaks', 'git', self.conn, 'items') with self.assertRaises(ValueError): job.initialize_cache(None) with self.assertRaises(ValueError): job.initialize_cache("")
def test_fetch_from_cache(self): """Test run method fetching from the cache""" http_requests = setup_mock_bugzilla_server() expected = ['5a8a1e25dfda86b961b4146050883cbfc928f8ec', '1fd4514e56f25a39ffd78eab19e77cfe4dfb7769', '6a4cb244067c3cfa38f9f563e2ab3cd8ac21762f', '7e033ed0110032ead6b918be43c1f3f88cd98fd7', 'f90d12b380ffdb47f2b6e96b321f08000181a9d6', '4b166308f205121bc57704032acdc81b6c9bb8b1', 'b4009442d38f4241a4e22e3e61b7cd8ef5ced35c'] # First, we fetch the bugs from the server, storing them # in a cache args = { 'url': BUGZILLA_SERVER_URL, 'max_bugs': 5 } job = PercevalJob('arthur-job-1234567890', 'mytask', 'bugzilla', self.conn, 'items') job.initialize_cache(self.tmp_path) job.run(args, fetch_from_cache=False) bugs = self.conn.lrange('items', 0, -1) bugs = [pickle.loads(b) for b in bugs] bugs = [bug['uuid'] for bug in bugs] self.conn.ltrim('items', 1, 0) result = job.result self.assertEqual(result.job_id, job.job_id) self.assertEqual(result.task_id, 'mytask') self.assertEqual(result.backend, 'bugzilla') self.assertEqual(result.last_uuid, 'b4009442d38f4241a4e22e3e61b7cd8ef5ced35c') self.assertEqual(result.max_date, 1439404330.0) self.assertEqual(result.nitems, 7) self.assertEqual(result.offset, None) self.assertEqual(result.nresumed, 0) self.assertEqual(len(http_requests), 13) self.assertListEqual(bugs, expected) # Now, we get the bugs from the cache. # The contents should be the same and there won't be # any new request to the server job_cache = PercevalJob('arthur-job-1234567890-bis', 'mytask', 'bugzilla', self.conn, 'items') job_cache.initialize_cache(self.tmp_path) job_cache.run(args, fetch_from_cache=True) cached_bugs = self.conn.lrange('items', 0, -1) cached_bugs = [pickle.loads(b) for b in cached_bugs] cached_bugs = [bug['uuid'] for bug in cached_bugs] self.conn.ltrim('items', 1, 0) result = job_cache.result self.assertEqual(result.job_id, job_cache.job_id) self.assertEqual(result.task_id, 'mytask') self.assertEqual(result.backend, 'bugzilla') self.assertEqual(result.last_uuid, 'b4009442d38f4241a4e22e3e61b7cd8ef5ced35c') self.assertEqual(result.max_date, 1439404330.0) self.assertEqual(result.nitems, 7) self.assertEqual(result.offset, None) self.assertEqual(result.nresumed, 0) self.assertEqual(len(http_requests), 13) self.assertListEqual(cached_bugs, bugs)