Ejemplo n.º 1
0
    def test_cloudsearch_results_internal_consistancy(self):
        """Check the documents length matches the iterator details"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        self.assertEqual(len(results), len(results.docs))
Ejemplo n.º 2
0
    def test_cloudsearch_results_internal_consistancy(self):
        """Check the documents length matches the iterator details"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        self.assertEqual(len(results), len(results.docs))
Ejemplo n.º 3
0
class CloudSearchConnectionTest(unittest.TestCase):
    cloudsearch = True

    def setUp(self):
        super(CloudSearchConnectionTest, self).setUp()
        self.conn = SearchConnection(
            endpoint='test-domain.cloudsearch.amazonaws.com'
        )

    def test_expose_additional_error_info(self):
        mpo = mock.patch.object
        fake = FakeResponse()
        fake.content = 'Nopenopenope'

        # First, in the case of a non-JSON, non-403 error.
        with mpo(requests, 'get', return_value=fake) as mock_request:
            with self.assertRaises(SearchServiceException) as cm:
                self.conn.search(q='not_gonna_happen')

            self.assertTrue('non-json response' in str(cm.exception))
            self.assertTrue('Nopenopenope' in str(cm.exception))

        # Then with JSON & an 'error' key within.
        fake.content = json.dumps({
            'error': "Something went wrong. Oops."
        })

        with mpo(requests, 'get', return_value=fake) as mock_request:
            with self.assertRaises(SearchServiceException) as cm:
                self.conn.search(q='no_luck_here')

            self.assertTrue('Unknown error' in str(cm.exception))
            self.assertTrue('went wrong. Oops' in str(cm.exception))
Ejemplo n.º 4
0
class CloudSearchConnectionTest(unittest.TestCase):
    cloudsearch = True

    def setUp(self):
        super(CloudSearchConnectionTest, self).setUp()
        self.conn = SearchConnection(
            endpoint='test-domain.cloudsearch.amazonaws.com'
        )

    def test_expose_additional_error_info(self):
        mpo = mock.patch.object
        fake = FakeResponse()
        fake.content = b'Nopenopenope'

        # First, in the case of a non-JSON, non-403 error.
        with mpo(requests, 'get', return_value=fake) as mock_request:
            with self.assertRaises(SearchServiceException) as cm:
                self.conn.search(q='not_gonna_happen')

            self.assertTrue('non-json response' in str(cm.exception))
            self.assertTrue('Nopenopenope' in str(cm.exception))

        # Then with JSON & an 'error' key within.
        fake.content = json.dumps({
            'error': "Something went wrong. Oops."
        }).encode('utf-8')

        with mpo(requests, 'get', return_value=fake) as mock_request:
            with self.assertRaises(SearchServiceException) as cm:
                self.conn.search(q='no_luck_here')

            self.assertTrue('Unknown error' in str(cm.exception))
            self.assertTrue('went wrong. Oops' in str(cm.exception))
Ejemplo n.º 5
0
    def test_cloudsearch_facet_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet=["Author"])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['facet'], ["Author"])
Ejemplo n.º 6
0
    def test_cloudsearch_rank_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', rank=["date", "score"])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['rank'], ['date,score'])
Ejemplo n.º 7
0
    def test_cloudsearch_result_fields_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', return_fields=['author', 'title'])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['return-fields'], ['author,title'])
Ejemplo n.º 8
0
    def test_cloudsearch_facet_sort_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet_sort={'author': 'alpha'})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['facet-author-sort'], ['alpha'])
Ejemplo n.º 9
0
    def test_cloudsearch_top_n_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet_top_n={'author': 5})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['facet-author-top-n'], ['5'])
Ejemplo n.º 10
0
    def test_cloudsearch_t_field_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', t={'year': '2001..2007'})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b't-year'], [b'2001..2007'])
Ejemplo n.º 11
0
    def test_cloudsearch_facet_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet=["author", "cat"])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['facet'], ["author,cat"])
Ejemplo n.º 12
0
    def test_cloudsearch_results_info(self):
        """Check num_pages_needed is calculated correctly"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        # This relies on the default response which is fed into HTTPretty
        self.assertEqual(results.num_pages_needed, 3.0)
Ejemplo n.º 13
0
    def test_cloudsearch_t_field_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', t={'year':'2001..2007'})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['t-year'], ['2001..2007'])
Ejemplo n.º 14
0
    def test_cloudsearch_results_info(self):
        """Check num_pages_needed is calculated correctly"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        # This relies on the default response which is fed into HTTPretty
        self.assertEqual(results.num_pages_needed, 3.0)
Ejemplo n.º 15
0
    def test_cloudsearch_facet_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet=["Author"])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'facet'], [b"Author"])
Ejemplo n.º 16
0
    def test_cloudsearch_facet_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet=["author", "cat"])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'facet'], [b"author,cat"])
Ejemplo n.º 17
0
    def test_cloudsearch_result_fields_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', return_fields=['author', 'title'])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'return-fields'], [b'author,title'])
Ejemplo n.º 18
0
    def test_cloudsearch_facet_sort_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet_sort={'author': 'alpha'})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'facet-author-sort'], [b'alpha'])
Ejemplo n.º 19
0
    def test_cloudsearch_bqsearch(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(bq="'Test'")

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['bq'], ["'Test'"])
Ejemplo n.º 20
0
    def test_cloudsearch_rank_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', rank=["date", "score"])

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'rank'], [b'date,score'])
Ejemplo n.º 21
0
    def test_cloudsearch_top_n_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet_top_n={'author': 5})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'facet-author-top-n'], [b'5'])
Ejemplo n.º 22
0
    def test_cloudsearch_bqsearch(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(bq="'Test'")

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'bq'], [b"'Test'"])
Ejemplo n.º 23
0
    def test_cloudsearch_top_n_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet_top_n={'author': 5, 'cat': 10})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'facet-author-top-n'], [b'5'])
        self.assertEqual(args[b'facet-cat-top-n'], [b'10'])
Ejemplo n.º 24
0
    def test_cloudsearch_search_facets(self):
        #self.response['facets'] = {'tags': {}}

        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test', facet=['tags'])

        self.assertTrue('tags' not in results.facets)
        self.assertEqual(results.facets['animals'], {u'lions': u'1', u'fish': u'2'})
Ejemplo n.º 25
0
    def test_cloudsearch_t_field_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', t={'year': '2001..2007', 'score': '10..50'})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b't-year'], [b'2001..2007'])
        self.assertEqual(args[b't-score'], [b'10..50'])
Ejemplo n.º 26
0
    def test_cloudsearch_results_iterator(self):
        """Check the results iterator"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')
        results_correct = iter(["12341", "12342", "12343", "12344",
                                "12345", "12346", "12347"])
        for x in results:
            self.assertEqual(x['id'], results_correct.next())
Ejemplo n.º 27
0
    def test_cloudsearch_t_field_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', t={'year':'2001..2007', 'score':'10..50'})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['t-year'], ['2001..2007'])
        self.assertEqual(args['t-score'], ['10..50'])
Ejemplo n.º 28
0
    def test_cloudsearch_results_meta(self):
        """Check returned metadata is parsed correctly"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        # These rely on the default response which is fed into HTTPretty
        self.assertEqual(results.rank, "-text_relevance")
        self.assertEqual(results.match_expression, "Test")
Ejemplo n.º 29
0
    def test_cloudsearch_top_n_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', facet_top_n={'author': 5, 'cat': 10})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['facet-author-top-n'], ['5'])
        self.assertEqual(args['facet-cat-top-n'], ['10'])
Ejemplo n.º 30
0
    def test_cloudsearch_search_facets(self):
        #self.response['facets'] = {'tags': {}}

        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test', facet=['tags'])

        self.assertTrue('tags' not in results.facets)
        self.assertEqual(results.facets['animals'], {u'lions': u'1', u'fish': u'2'})
Ejemplo n.º 31
0
    def test_cloudsearch_results_iterator(self):
        """Check the results iterator"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')
        results_correct = iter(["12341", "12342", "12343", "12344",
                                "12345", "12346", "12347"])
        for x in results:
            self.assertEqual(x['id'], next(results_correct))
Ejemplo n.º 32
0
    def test_cloudsearch_results_meta(self):
        """Check returned metadata is parsed correctly"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        # These rely on the default response which is fed into HTTPretty
        self.assertEqual(results.rank, "-text_relevance")
        self.assertEqual(results.match_expression, "Test")
Ejemplo n.º 33
0
    def test_cloudsearch_qsearch(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test')

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'q'], [b"Test"])
        self.assertEqual(args[b'start'], [b"0"])
        self.assertEqual(args[b'size'], [b"10"])
Ejemplo n.º 34
0
    def test_cloudsearch_search_details(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', size=50, start=20)

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['q'], ["Test"])
        self.assertEqual(args['size'], ["50"])
        self.assertEqual(args['start'], ["20"])
Ejemplo n.º 35
0
    def test_cloudsearch_qsearch(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test')

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['q'], ["Test"])
        self.assertEqual(args['start'], ["0"])
        self.assertEqual(args['size'], ["10"])
Ejemplo n.º 36
0
    def test_cloudsearch_search_details(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test', size=50, start=20)

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'q'], [b"Test"])
        self.assertEqual(args[b'size'], [b"50"])
        self.assertEqual(args[b'start'], [b"20"])
Ejemplo n.º 37
0
    def test_cloudsearch_facet_constraint_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(
            q='Test',
            facet_constraints={'author': "'John Smith','Mark Smith'"})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['facet-author-constraints'],
                         ["'John Smith','Mark Smith'"])
Ejemplo n.º 38
0
    def test_cloudsearch_search_nextpage(self):
        """Check next page query is correct"""
        search = SearchConnection(endpoint=HOSTNAME)
        query1 = search.build_query(q='Test')
        query2 = search.build_query(q='Test')

        results = search(query2)

        self.assertEqual(results.next_page().query.start,
                         query1.start + query1.size)
        self.assertEqual(query1.q, query2.q)
Ejemplo n.º 39
0
    def test_cloudsearch_facet_constraint_single(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(
            q='Test',
            facet_constraints={'author': "'John Smith','Mark Smith'"})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'facet-author-constraints'],
                         [b"'John Smith','Mark Smith'"])
Ejemplo n.º 40
0
    def test_cloudsearch_search_nextpage(self):
        """Check next page query is correct"""
        search = SearchConnection(endpoint=HOSTNAME)
        query1 = search.build_query(q='Test')
        query2 = search.build_query(q='Test')

        results = search(query2)

        self.assertEqual(results.next_page().query.start,
                         query1.start + query1.size)
        self.assertEqual(query1.q, query2.q)
Ejemplo n.º 41
0
    def test_cloudsearch_results_hits(self):
        """Check that documents are parsed properly from AWS"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        hits = list(map(lambda x: x['id'], results.docs))

        # This relies on the default response which is fed into HTTPretty
        self.assertEqual(
            hits, ["12341", "12342", "12343", "12344",
                   "12345", "12346", "12347"])
Ejemplo n.º 42
0
    def test_cloudsearch_results_matched(self):
        """
        Check that information objects are passed back through the API
        correctly.
        """
        search = SearchConnection(endpoint=HOSTNAME)
        query = search.build_query(q='Test')

        results = search(query)

        self.assertEqual(results.search_service, search)
        self.assertEqual(results.query, query)
Ejemplo n.º 43
0
    def test_cloudsearch_results_matched(self):
        """
        Check that information objects are passed back through the API
        correctly.
        """
        search = SearchConnection(endpoint=HOSTNAME)
        query = search.build_query(q='Test')

        results = search(query)

        self.assertEqual(results.search_service, search)
        self.assertEqual(results.query, query)
Ejemplo n.º 44
0
    def test_cloudsearch_results_hits(self):
        """Check that documents are parsed properly from AWS"""
        search = SearchConnection(endpoint=HOSTNAME)

        results = search.search(q='Test')

        hits = map(lambda x: x['id'], results.docs)

        # This relies on the default response which is fed into HTTPretty
        self.assertEqual(
            hits, ["12341", "12342", "12343", "12344",
                   "12345", "12346", "12347"])
Ejemplo n.º 45
0
    def test_cloudsearch_facet_constraint_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(
            q='Test',
            facet_constraints={'author': "'John Smith','Mark Smith'",
                               'category': "'News','Reviews'"})

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args['facet-author-constraints'],
                         ["'John Smith','Mark Smith'"])
        self.assertEqual(args['facet-category-constraints'],
                         ["'News','Reviews'"])
Ejemplo n.º 46
0
    def test_cloudsearch_facet_constraint_multiple(self):
        search = SearchConnection(endpoint=HOSTNAME)

        search.search(q='Test',
                      facet_constraints={
                          'author': "'John Smith','Mark Smith'",
                          'category': "'News','Reviews'"
                      })

        args = self.get_args(HTTPretty.last_request.raw_requestline)

        self.assertEqual(args[b'facet-author-constraints'],
                         [b"'John Smith','Mark Smith'"])
        self.assertEqual(args[b'facet-category-constraints'],
                         [b"'News','Reviews'"])
Ejemplo n.º 47
0
	def search(cls, q=None, bq=None, rank=None, start=0, **kwargs):
		'''Search using CloudSearch. This requires a _cs_search_endpoint property to be set
		:param q: The optional TEXT search query
		:type q: str
		:param bq: The optional BOOLEAN search query
		:type bq: str
		:param rank: The optional Search rank
		:type rank: str
		:param start: The optional start point to begin the search
		:type start: int
		:param: Other KW args are supported and used as direct matches in the Boolean Query'''
		from boto.cloudsearch.search import SearchConnection
		if not cls._cs_search_endpoint:
			raise NotImplemented('No CloudSearch Domain Set')

		# Converts all the keyword arguments to a boolaen query
		if kwargs:
			query_parts = []
			if bq:
				query_parts.append(bq)

			# Build all the query parts
			for arg in kwargs:
				query_parts.append('%s:\'%s\'' % (arg, kwargs[arg]))

			# Reduce the query back to a single string
			bq = '(and %s)' % ' '.join(query_parts)

		# Build the search args
		args = {}
		if q:
			args['q'] = q
		if bq:
			args['bq'] = bq
		if rank:
			args['rank'] = rank
		if start:
			args['start'] = start

		conn = SearchConnection(endpoint=cls._cs_search_endpoint)
		return BatchItemFetcher(conn.search(**args), cls)
Ejemplo n.º 48
0
    def test_simplejson_jsondecodeerror(self):
        with mock.patch.object(json, 'loads', fake_loads_json_error):
            search = SearchConnection(endpoint=HOSTNAME)

            with self.assertRaisesRegexp(SearchServiceException, 'non-json'):
                search.search(q='test')
Ejemplo n.º 49
0
 def get_search_service(self):
     return SearchConnection(domain=self)
Ejemplo n.º 50
0
 def setUp(self):
     super(CloudSearchConnectionTest, self).setUp()
     self.conn = SearchConnection(
         endpoint='test-domain.cloudsearch.amazonaws.com'
     )
Ejemplo n.º 51
0
    def test_response(self):
        search = SearchConnection(endpoint=HOSTNAME)

        with self.assertRaisesRegexp(SearchServiceException, 'foo bar baz'):
            search.search(q='Test')
Ejemplo n.º 52
0
    def test_response(self):
        search = SearchConnection(endpoint=HOSTNAME)

        with self.assertRaisesRegexp(SearchServiceException, 'foo bar baz'):
            search.search(q='Test')
Ejemplo n.º 53
0
 def setUp(self):
     super(CloudSearchConnectionTest, self).setUp()
     self.conn = SearchConnection(
         endpoint='test-domain.cloudsearch.amazonaws.com'
     )
Ejemplo n.º 54
0
    def test_simplejson_jsondecodeerror(self):
        with mock.patch.object(json, 'loads', fake_loads_json_error):
            search = SearchConnection(endpoint=HOSTNAME)

            with self.assertRaisesRegexp(SearchServiceException, 'non-json'):
                search.search(q='test')