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_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_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'])