Пример #1
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'])
Пример #2
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()]
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)
Пример #6
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()]
Пример #7
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)
Пример #8
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)
Пример #9
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)
Пример #10
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)
Пример #11
0
    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()]

        self.assertEqual(len(questions), 4)

        self.__check_questions_contents(questions)
Пример #12
0
    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()]

        self.assertEqual(len(questions), 4)

        self.__check_questions_contents(questions)
Пример #13
0
    def test_initialization(self):
        """Test whether attributes are initializated"""

        kitsune = Kitsune(KITSUNE_SERVER_URL, origin='test')

        self.assertEqual(kitsune.url, KITSUNE_SERVER_URL)
        self.assertEqual(kitsune.origin, 'test')
        self.assertIsInstance(kitsune.client, KitsuneClient)

        # When origin 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)

        kitsune = Kitsune(KITSUNE_SERVER_URL, origin='')
        self.assertEqual(kitsune.url, KITSUNE_SERVER_URL)
        self.assertEqual(kitsune.origin, KITSUNE_SERVER_URL)
Пример #14
0
    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)
Пример #15
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"])
Пример #16
0
    def test_has_resuming(self):
        """Test if it returns True when has_resuming is called"""

        self.assertEqual(Kitsune.has_resuming(), True)
Пример #17
0
    def test_has_resuming(self):
        """Test if it returns True when has_resuming is called"""

        self.assertEqual(Kitsune.has_resuming(), True)