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_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_empty(self): """Test whether it works when no jobs are fetched""" HTTPServer.routes(empty=True) kitsune = Kitsune(KITSUNE_SERVER_URL) questions = [event for event in kitsune.fetch()] self.assertEqual(len(questions), 0)
def test_fetch_server_error(self): """Test whether it works when the server fails""" HTTPServer.routes(empty=True) kitsune = Kitsune(KITSUNE_SERVER_URL) offset = (KITSUNE_SERVER_FAIL_PAGE - 1) * KITSUNE_ITEMS_PER_PAGE questions = [event for event in kitsune.fetch(offset=offset)] # After the failing page there are a page with 2 questions self.assertEqual(len(questions), 2)
def test_search_fields(self): """Test whether the search_fields is properly set""" HTTPServer.routes() # Test fetch questions with their reviews kitsune = Kitsune(KITSUNE_SERVER_URL) questions = [question for question in kitsune.fetch(offset=None)] for question in questions: self.assertEqual(kitsune.metadata_id(question['data']), question['search_fields']['item_id'])
def test_fetch(self): """Test whether the questions are returned""" HTTPServer.routes() # Test fetch questions with their reviews kitsune = Kitsune(KITSUNE_SERVER_URL) questions = [question for question in kitsune.fetch(offset=None)] self.assertEqual(len(questions), 4) self.__check_questions_contents(questions)
def test_initialization(self): """Test whether attributes are initializated""" kitsune = Kitsune(KITSUNE_SERVER_URL, tag='test') self.assertEqual(kitsune.url, KITSUNE_SERVER_URL) self.assertEqual(kitsune.origin, KITSUNE_SERVER_URL) self.assertEqual(kitsune.tag, 'test') self.assertIsNone(kitsune.client) # When tag is empty or None it will be set to # the value in url kitsune = Kitsune(KITSUNE_SERVER_URL) self.assertEqual(kitsune.url, KITSUNE_SERVER_URL) self.assertEqual(kitsune.origin, KITSUNE_SERVER_URL) self.assertEqual(kitsune.tag, KITSUNE_SERVER_URL) kitsune = Kitsune(KITSUNE_SERVER_URL, tag='') self.assertEqual(kitsune.url, KITSUNE_SERVER_URL) self.assertEqual(kitsune.origin, KITSUNE_SERVER_URL) self.assertEqual(kitsune.tag, KITSUNE_SERVER_URL)
def test_fetch_offset(self): """Test whether the questions are returned offset""" HTTPServer.routes() # Test fetch questions with their reviews kitsune = Kitsune(KITSUNE_SERVER_URL) # Get all questions offset = 0 questions = [question for question in kitsune.fetch(offset=offset)] self.assertEqual(len(questions), 4) self.__check_questions_contents(questions) # 4 total questions minus 2, 2 questions returned offset = 2 questions = [question for question in kitsune.fetch(offset=offset)] self.assertEqual(len(questions), 2) self.assertEqual(questions[0]['offset'], 2) self.assertEqual(questions[1]['offset'], 3) offset = 4 questions = [question for question in kitsune.fetch(offset=offset)] self.assertEqual(len(questions), 0) # Get no questions: we have two pages offset = KitsuneClient.ITEMS_PER_PAGE * 2 with self.assertRaises(requests.exceptions.HTTPError): questions = [question for question in kitsune.fetch(offset=offset)] self.assertEqual(len(questions), 0)
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'])
def test_fetch_from_offset(self): """Test whether the fetch method with offset properly works""" perceval_backend = Kitsune('http://example.com') elastic = ElasticSearch(self.es_con, self.target_index, KitsuneOcean.mapping) # Load items items = json.loads(read_file('data/kitsune.json')) ocean = KitsuneOcean(perceval_backend) ocean.elastic = elastic ocean.feed_items(items) # Fetch total items eitems = ElasticItems(perceval_backend) eitems.elastic = elastic items = [ei for ei in eitems.fetch()] self.assertEqual(len(items), 4) # Fetch with offset eitems = ElasticItems(perceval_backend, offset=2) eitems.elastic = elastic items = [ei for ei in eitems.fetch()] self.assertEqual(len(items), 2)
def setUp(self): super().setUp() self.backend_write_archive = Kitsune(KITSUNE_SERVER_URL, archive=self.archive) self.backend_read_archive = Kitsune(KITSUNE_SERVER_URL, archive=self.archive)
def test_has_resuming(self): """Test if it returns True when has_resuming is called""" self.assertEqual(Kitsune.has_resuming(), True)