示例#1
0
    def test_initialization(self):
        """Test whether attributes are initializated"""

        discourse = Discourse(DISCOURSE_SERVER_URL, tag='test')

        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, 'test')
        self.assertIsNone(discourse.client)
        self.assertEqual(discourse.sleep_time, DEFAULT_SLEEP_TIME)
        self.assertEqual(discourse.max_retries, MAX_RETRIES)

        # When origin is empty or None it will be set to
        # the value in url
        discourse = Discourse(DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)

        discourse = Discourse(DISCOURSE_SERVER_URL, tag='')
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)

        discourse = Discourse(DISCOURSE_SERVER_URL,
                              sleep_time=60,
                              max_retries=30)
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.sleep_time, 60)
        self.assertEqual(discourse.max_retries, 30)
示例#2
0
    def test_missing_credentials(self):
        """Test whether an exception is thrown when `api_username` and `api_token` aren't defined together"""

        with self.assertRaises(BackendError):
            _ = Discourse(DISCOURSE_SERVER_URL, api_token='1234')

        with self.assertRaises(BackendError):
            _ = Discourse(DISCOURSE_SERVER_URL, api_username='******')
 def setUp(self):
     super().setUp()
     self.backend_write_archive = Discourse(DISCOURSE_SERVER_URL,
                                            sleep_time=0,
                                            api_token="aaaaa",
                                            archive=self.archive)
     self.backend_read_archive = Discourse(DISCOURSE_SERVER_URL,
                                           sleep_time=0,
                                           archive=self.archive)
示例#4
0
    def test_fetch_from_non_set_cache(self):
        """Test if a error is raised when the cache was not set"""

        discourse = Discourse(DISCOURSE_SERVER_URL)

        with self.assertRaises(CacheError):
            _ = [topic for topic in discourse.fetch_from_cache()]
示例#5
0
    def test_fetch_from_empty_cache(self):
        """Test if there are not any topics returned when the cache is empty"""

        cache = Cache(self.tmp_path)
        discourse = Discourse(DISCOURSE_SERVER_URL, cache=cache)
        cached_topics = [topic for topic in discourse.fetch_from_cache()]
        self.assertEqual(len(cached_topics), 0)
示例#6
0
    def test_initialization(self):
        """Test whether attributes are initializated"""

        discourse = Discourse(DISCOURSE_SERVER_URL, tag='test')

        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, 'test')
        self.assertIsNone(discourse.client)
        self.assertEqual(discourse.sleep_time, DEFAULT_SLEEP_TIME)
        self.assertEqual(discourse.max_retries, MAX_RETRIES)
        self.assertTrue(discourse.ssl_verify)

        # When origin is empty or None it will be set to
        # the value in url
        discourse = Discourse(DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)

        discourse = Discourse(DISCOURSE_SERVER_URL, tag='', ssl_verify=False)
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)
        self.assertFalse(discourse.ssl_verify)

        discourse = Discourse(DISCOURSE_SERVER_URL,
                              sleep_time=60,
                              max_retries=30)
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.sleep_time, 60)
        self.assertEqual(discourse.max_retries, 30)

        discourse = Discourse(DISCOURSE_SERVER_URL,
                              api_username='******',
                              api_token='1234')
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.api_token, '1234')
        self.assertEqual(discourse.api_username, 'user')
示例#7
0
    def test_initialization(self):
        """Test whether attributes are initializated"""

        discourse = Discourse(DISCOURSE_SERVER_URL, tag='test')

        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, 'test')
        self.assertIsInstance(discourse.client, DiscourseClient)

        # When origin is empty or None it will be set to
        # the value in url
        discourse = Discourse(DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)

        discourse = Discourse(DISCOURSE_SERVER_URL, tag='')
        self.assertEqual(discourse.url, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.origin, DISCOURSE_SERVER_URL)
        self.assertEqual(discourse.tag, DISCOURSE_SERVER_URL)
示例#8
0
    def test_fetch_empty(self):
        """Test whether it works when no topics are fetched"""

        body = read_file('data/discourse_topics_empty.json')
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               body=body, status=200)

        discourse = Discourse(DISCOURSE_SERVER_URL)
        topics = [topic for topic in discourse.fetch()]

        self.assertEqual(len(topics), 0)
    def test_fetch_topic_last_posted_at_null(self):
        """Test whether list of topics is returned when a topic has last_posted_at null"""

        bodies_topics = [
            read_file(
                'data/discourse/discourse_topics_last_posted_at_null.json'),
            read_file('data/discourse/discourse_topics_empty.json')
        ]
        body_topic_1149 = read_file('data/discourse/discourse_topic_1149.json')

        def request_callback(method, uri, headers):
            if uri.startswith(DISCOURSE_TOPICS_URL):
                body = bodies_topics.pop(0)
            elif uri.startswith(DISCOURSE_TOPIC_URL_1149):
                body = body_topic_1149
            else:
                raise Exception
            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                                   for _ in range(2)
                               ])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1149,
            responses=[httpretty.Response(body=request_callback)])

        # On this tests two topics will be retrieved.
        # One of them has last_posted_at with null
        discourse = Discourse(DISCOURSE_SERVER_URL, sleep_time=0)
        topics = [topic for topic in discourse.fetch(from_date=None)]

        self.assertEqual(len(topics), 1)

        self.assertEqual(topics[0]['data']['id'], 1149)
        self.assertEqual(len(topics[0]['data']['post_stream']['posts']), 2)
        self.assertEqual(topics[0]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(topics[0]['uuid'],
                         '18068b95de1323a84c8e11dee8f46fd137f10c86')
        self.assertEqual(topics[0]['updated_on'], 1464134770.909)
        self.assertEqual(topics[0]['category'], 'topic')
        self.assertEqual(topics[0]['tag'], DISCOURSE_SERVER_URL)
示例#10
0
    def test_refresh_project(self):
        """Test refresh project field for all sources"""

        # populate raw index
        perceval_backend = Discourse('https://example.com', api_token='1234', api_username='******')
        clean = True
        self.ocean_backend = self.connectors[self.connector][1](perceval_backend)
        elastic_ocean = get_elastic(self.es_con, self.ocean_index, clean, self.ocean_backend)
        self.ocean_backend.set_elastic(elastic_ocean)
        data2es(self.items, self.ocean_backend)

        # populate enriched index
        self.enrich_backend = self.connectors[self.connector][2](db_projects_map=DB_PROJECTS,
                                                                 db_user=self.db_user,
                                                                 db_password=self.db_password)

        elastic_enrich = get_elastic(self.es_con, self.enrich_index, clean, self.enrich_backend)
        self.enrich_backend.set_elastic(elastic_enrich)
        self.enrich_backend.enrich_items(self.ocean_backend)

        total = refresh_projects(self.enrich_backend)
示例#11
0
    def test_refresh_identities(self):
        """Test refresh identities"""

        # populate raw index
        perceval_backend = Discourse('https://example.com', api_token='1234', api_username='******')
        clean = True
        self.ocean_backend = self.connectors[self.connector][1](perceval_backend)
        elastic_ocean = get_elastic(self.es_con, self.ocean_index, clean, self.ocean_backend)
        self.ocean_backend.set_elastic(elastic_ocean)
        data2es(self.items, self.ocean_backend)

        # populate enriched index
        self.enrich_backend = self.connectors[self.connector][2]()
        load_identities(self.ocean_backend, self.enrich_backend)
        self.enrich_backend = self.connectors[self.connector][2](db_sortinghat=DB_SORTINGHAT,
                                                                 db_user=self.db_user,
                                                                 db_password=self.db_password)
        elastic_enrich = get_elastic(self.es_con, self.enrich_index, clean, self.enrich_backend)
        self.enrich_backend.set_elastic(elastic_enrich)
        self.enrich_backend.enrich_items(self.ocean_backend)

        total = refresh_identities(self.enrich_backend)
    def test_fetch_pinned(self):
        """Test whether the right list of topics is returned when some topics are pinned"""

        bodies_topics = [
            read_file('data/discourse/discourse_topics_pinned.json'),
            read_file('data/discourse/discourse_topics_empty.json')
        ]
        body_topic_1148 = read_file('data/discourse/discourse_topic_1148.json')
        body_topic_1149 = read_file('data/discourse/discourse_topic_1149.json')
        body_topic_1150 = read_file('data/discourse/discourse_topic_1150.json')
        body_post = read_file('data/discourse/discourse_post.json')

        def request_callback(method, uri, headers):
            if uri.startswith(DISCOURSE_TOPICS_URL):
                body = bodies_topics.pop(0)
            elif uri.startswith(DISCOURSE_TOPIC_URL_1148):
                body = body_topic_1148
            elif uri.startswith(DISCOURSE_TOPIC_URL_1149):
                body = body_topic_1149
            elif uri.startswith(DISCOURSE_TOPIC_URL_1150):
                body = body_topic_1150
            elif uri.startswith(DISCOURSE_POST_URL_1) or \
                    uri.startswith(DISCOURSE_POST_URL_2):
                body = body_post
            else:
                raise Exception
            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                                   for _ in range(2)
                               ])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1148,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1149,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1150,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_1,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_2,
            responses=[httpretty.Response(body=request_callback)])

        # On this tests two topics will be retrieved.
        # One of them was pinned but the date is in range.
        from_date = datetime.datetime(2016, 5, 25, 2, 0, 0)

        discourse = Discourse(DISCOURSE_SERVER_URL, sleep_time=0)
        topics = [topic for topic in discourse.fetch(from_date=from_date)]

        self.assertEqual(len(topics), 2)

        self.assertEqual(topics[0]['data']['id'], 1148)
        self.assertEqual(len(topics[0]['data']['post_stream']['posts']), 22)
        self.assertEqual(topics[0]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(topics[0]['uuid'],
                         '5298e4e8383c3f73c9fa7c9599779cbe987a48e4')
        self.assertEqual(topics[0]['updated_on'], 1464144769.526)
        self.assertEqual(topics[0]['category'], 'topic')
        self.assertEqual(topics[0]['tag'], DISCOURSE_SERVER_URL)

        self.assertEqual(topics[1]['data']['id'], 1150)
        self.assertEqual(len(topics[1]['data']['post_stream']['posts']), 2)
        self.assertEqual(topics[1]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(topics[1]['uuid'],
                         '373b597a2a389112875c3e544f197610373a7283')
        self.assertEqual(topics[1]['updated_on'], 1464274870.809)
        self.assertEqual(topics[1]['category'], 'topic')
        self.assertEqual(topics[0]['tag'], DISCOURSE_SERVER_URL)
    def test_fetch_from_date(self):
        """Test whether a list of topics is returned from a given date"""

        requests_http = []

        bodies_topics = [
            read_file('data/discourse/discourse_topics.json'),
            read_file('data/discourse/discourse_topics_empty.json')
        ]
        body_topic_1148 = read_file('data/discourse/discourse_topic_1148.json')
        body_topic_1149 = read_file('data/discourse/discourse_topic_1149.json')
        body_post = read_file('data/discourse/discourse_post.json')

        def request_callback(method, uri, headers):
            if uri.startswith(DISCOURSE_TOPICS_URL):
                body = bodies_topics.pop(0)
            elif uri.startswith(DISCOURSE_TOPIC_URL_1148):
                body = body_topic_1148
            elif uri.startswith(DISCOURSE_TOPIC_URL_1149):
                body = body_topic_1149
            elif uri.startswith(DISCOURSE_POST_URL_1) or \
                    uri.startswith(DISCOURSE_POST_URL_2):
                body = body_post
            else:
                raise Exception

            requests_http.append(httpretty.last_request())

            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                                   for _ in range(2)
                               ])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1148,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1149,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_1,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_2,
            responses=[httpretty.Response(body=request_callback)])

        # On this tests only one topic will be retrieved
        from_date = datetime.datetime(2016, 5, 25, 2, 0, 0)

        discourse = Discourse(DISCOURSE_SERVER_URL, sleep_time=0)
        topics = [topic for topic in discourse.fetch(from_date=from_date)]

        self.assertEqual(len(topics), 1)

        self.assertEqual(topics[0]['data']['id'], 1148)
        self.assertEqual(len(topics[0]['data']['post_stream']['posts']), 22)
        self.assertEqual(topics[0]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(topics[0]['uuid'],
                         '5298e4e8383c3f73c9fa7c9599779cbe987a48e4')
        self.assertEqual(topics[0]['updated_on'], 1464144769.526)
        self.assertEqual(topics[0]['category'], 'topic')
        self.assertEqual(topics[0]['tag'], DISCOURSE_SERVER_URL)

        # Check requests
        expected = [{'page': ['0']}, {}, {}, {}]

        self.assertEqual(len(requests_http), len(expected))

        for i in range(len(expected)):
            self.assertDictEqual(requests_http[i].querystring, expected[i])
    def test_fetch(self):
        """Test whether a list of topics is returned"""

        requests_http = []

        bodies_topics = [
            read_file('data/discourse/discourse_topics.json'),
            read_file('data/discourse/discourse_topics_empty.json')
        ]
        body_topic_1148 = read_file('data/discourse/discourse_topic_1148.json')
        body_topic_1149 = read_file('data/discourse/discourse_topic_1149.json')
        body_post = read_file('data/discourse/discourse_post.json')

        def request_callback(method, uri, headers):
            if uri.startswith(DISCOURSE_TOPICS_URL):
                body = bodies_topics.pop(0)
            elif uri.startswith(DISCOURSE_TOPIC_URL_1148):
                body = body_topic_1148
            elif uri.startswith(DISCOURSE_TOPIC_URL_1149):
                body = body_topic_1149
            elif uri.startswith(DISCOURSE_POST_URL_1) or \
                    uri.startswith(DISCOURSE_POST_URL_2):
                body = body_post
            else:
                raise Exception

            requests_http.append(httpretty.last_request())

            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                                   for _ in range(2)
                               ])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1148,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1149,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_1,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_2,
            responses=[httpretty.Response(body=request_callback)])

        # Test fetch topics
        discourse = Discourse(DISCOURSE_SERVER_URL, sleep_time=0)
        topics = [topic for topic in discourse.fetch()]

        self.assertEqual(len(topics), 2)

        # Topics are returned in reverse order
        # from oldest to newest
        self.assertEqual(topics[0]['data']['id'], 1149)
        self.assertEqual(len(topics[0]['data']['post_stream']['posts']), 2)
        self.assertEqual(topics[0]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(topics[0]['uuid'],
                         '18068b95de1323a84c8e11dee8f46fd137f10c86')
        self.assertEqual(topics[0]['updated_on'], 1464134770.909)
        self.assertEqual(topics[0]['category'], "topic")
        self.assertEqual(topics[0]['tag'], DISCOURSE_SERVER_URL)

        self.assertEqual(topics[1]['data']['id'], 1148)
        self.assertEqual(topics[1]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(topics[1]['uuid'],
                         '5298e4e8383c3f73c9fa7c9599779cbe987a48e4')
        self.assertEqual(topics[1]['updated_on'], 1464144769.526)
        self.assertEqual(topics[1]['category'], "topic")
        self.assertEqual(topics[1]['tag'], DISCOURSE_SERVER_URL)

        # The next assertions check the cases whether the chunk_size is
        # less than the number of posts of a topic
        self.assertEqual(len(topics[1]['data']['post_stream']['posts']), 22)
        self.assertEqual(topics[1]['data']['post_stream']['posts'][0]['id'],
                         18952)
        self.assertEqual(topics[1]['data']['post_stream']['posts'][20]['id'],
                         2500)

        # Check requests
        expected = [{'page': ['0']}, {'page': ['1']}, {}, {}, {}, {}]

        self.assertEqual(len(requests_http), len(expected))

        for i in range(len(expected)):
            self.assertDictEqual(requests_http[i].querystring, expected[i])
示例#15
0
 def setUp(self):
     super().setUp()
     self.backend = Discourse(DISCOURSE_SERVER_URL, archive=self.archive)
示例#16
0
    def test_fetch_from_cache(self):
        """Test whether the cache works"""

        requests_http = []

        bodies_topics = [read_file('data/discourse_topics.json'),
                         read_file('data/discourse_topics_empty.json')]
        body_topic_1148 = read_file('data/discourse_topic_1148.json')
        body_topic_1149 = read_file('data/discourse_topic_1149.json')
        body_post = read_file('data/discourse_post.json')

        def request_callback(method, uri, headers):
            if uri.startswith(DISCOURSE_TOPICS_URL):
                body = bodies_topics.pop(0)
            elif uri.startswith(DISCOURSE_TOPIC_URL_1148):
                body = body_topic_1148
            elif uri.startswith(DISCOURSE_TOPIC_URL_1149):
                body = body_topic_1149
            elif (uri.startswith(DISCOURSE_POST_URL_1) or
                  uri.startswith(DISCOURSE_POST_URL_2)):
                body = body_post
            else:
                raise

            requests_http.append(httpretty.last_request())

            return (200, headers, body)

        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                                   for _ in range(2)
                               ])
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPIC_URL_1148,
                               responses=[
                                   httpretty.Response(body=request_callback)
                               ])
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPIC_URL_1149,
                               responses=[
                                   httpretty.Response(body=request_callback)
                               ])
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_POST_URL_1,
                               responses=[
                                   httpretty.Response(body=request_callback)
                               ])
        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_POST_URL_2,
                               responses=[
                                   httpretty.Response(body=request_callback)
                               ])

        # First, we fetch the topics from the server, storing them
        # in a cache
        cache = Cache(self.tmp_path)
        discourse = Discourse(DISCOURSE_SERVER_URL, cache=cache)

        topics = [topic for topic in discourse.fetch()]
        self.assertEqual(len(requests_http), 6)

        # Now, we get the topics from the cache.
        # The contents should be the same and there won't be
        # any new request to the server
        cached_topics = [topic for topic in discourse.fetch_from_cache()]
        self.assertEqual(len(cached_topics), len(topics))

        self.assertEqual(len(cached_topics), 2)

        # Topics are returned in reverse order
        # from oldest to newest
        self.assertEqual(cached_topics[0]['data']['id'], 1149)
        self.assertEqual(len(cached_topics[0]['data']['post_stream']['posts']), 2)
        self.assertEqual(cached_topics[0]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(cached_topics[0]['uuid'], '18068b95de1323a84c8e11dee8f46fd137f10c86')
        self.assertEqual(cached_topics[0]['updated_on'], 1464134770.909)
        self.assertEqual(cached_topics[0]['category'], 'topic')
        self.assertEqual(cached_topics[0]['origin'], DISCOURSE_SERVER_URL)

        self.assertEqual(cached_topics[1]['data']['id'], 1148)
        self.assertEqual(cached_topics[1]['origin'], DISCOURSE_SERVER_URL)
        self.assertEqual(cached_topics[1]['uuid'], '5298e4e8383c3f73c9fa7c9599779cbe987a48e4')
        self.assertEqual(cached_topics[1]['updated_on'], 1464144769.526)
        self.assertEqual(cached_topics[1]['category'], 'topic')
        self.assertEqual(cached_topics[1]['origin'], DISCOURSE_SERVER_URL)

        # The next assertions check the cases whether the chunk_size is
        # less than the number of posts of a topic
        self.assertEqual(len(cached_topics[1]['data']['post_stream']['posts']), 22)
        self.assertEqual(cached_topics[1]['data']['post_stream']['posts'][0]['id'], 18952)
        self.assertEqual(cached_topics[1]['data']['post_stream']['posts'][20]['id'], 2500)

        # No more requests were sent
        self.assertEqual(len(requests_http), 6)
示例#17
0
    def test_search_fields(self):
        """Test whether the search_fields is properly set"""

        bodies_topics = [
            read_file('data/discourse/discourse_topics.json'),
            read_file('data/discourse/discourse_topics_empty.json')
        ]
        body_topic_1148 = read_file('data/discourse/discourse_topic_1148.json')
        body_topic_1149 = read_file('data/discourse/discourse_topic_1149.json')
        body_post = read_file('data/discourse/discourse_post.json')

        def request_callback(method, uri, headers):
            if uri.startswith(DISCOURSE_TOPICS_URL):
                body = bodies_topics.pop(0)
            elif uri.startswith(DISCOURSE_TOPIC_URL_1148):
                body = body_topic_1148
            elif uri.startswith(DISCOURSE_TOPIC_URL_1149):
                body = body_topic_1149
            elif uri.startswith(DISCOURSE_POST_URL_1) or \
                    uri.startswith(DISCOURSE_POST_URL_2):
                body = body_post
            else:
                raise Exception

            return 200, headers, body

        httpretty.register_uri(httpretty.GET,
                               DISCOURSE_TOPICS_URL,
                               responses=[
                                   httpretty.Response(body=request_callback)
                                   for _ in range(2)
                               ])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1148,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_TOPIC_URL_1149,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_1,
            responses=[httpretty.Response(body=request_callback)])
        httpretty.register_uri(
            httpretty.GET,
            DISCOURSE_POST_URL_2,
            responses=[httpretty.Response(body=request_callback)])

        # Test fetch topics
        discourse = Discourse(DISCOURSE_SERVER_URL, sleep_time=0)
        topics = [topic for topic in discourse.fetch()]

        topic = topics[0]
        self.assertEqual(discourse.metadata_id(topic['data']),
                         topic['search_fields']['item_id'])
        self.assertEqual(topic['data']['category_id'], 111)
        self.assertEqual(topic['data']['category_id'],
                         topic['search_fields']['category_id'])

        topic = topics[1]
        self.assertEqual(discourse.metadata_id(topic['data']),
                         topic['search_fields']['item_id'])
        self.assertEqual(topic['data']['category_id'], 111)
        self.assertEqual(topic['data']['category_id'],
                         topic['search_fields']['category_id'])