def test_fetch_from_empty_cache(self): """Test if there are not any questions returned when the cache is empty""" cache = Cache(self.tmp_path) kitsune = Kitsune(KITSUNE_SERVER_URL, cache=cache) cached_questions = [event for event in kitsune.fetch_from_cache()] self.assertEqual(len(cached_questions), 0)
def test_fetch_from_non_set_cache(self): """Test if a error is raised when the cache was not set""" kitsune = Kitsune(KITSUNE_SERVER_URL) with self.assertRaises(CacheError): _ = [event for event in kitsune.fetch_from_cache()]
def test_fetch_from_cache(self): """Test whether the cache works""" HTTPServer.routes() # First, we fetch the questions from the server, storing them # in a cache cache = Cache(self.tmp_path) kitsune = Kitsune(KITSUNE_SERVER_URL, cache=cache) questions = [event for event in kitsune.fetch()] requests_done = len(HTTPServer.requests_http) # Now, we get the questions from the cache. # The contents should be the same and there won't be # any new request to the server cached_questions = [event for event in kitsune.fetch_from_cache()] # No new requests to the server self.assertEqual(len(HTTPServer.requests_http), requests_done) # The contents should be the same self.assertEqual(len(cached_questions), len(questions)) for i in range(0, len(questions)): self.assertDictEqual(cached_questions[i]['data'], questions[i]['data'])