How to use the cloudant.query.Query function in cloudant

To help you get started, we’ve selected a few cloudant examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cloudant / python-cloudant / tests / unit / query_result_tests.py View on Github external
def test_constructor_with_query_skip_limit_options_skip_limit(self):
        """
        Ensure that options skip and/or limit override the values in the query
        callable if present when constructing a QueryResult
        """
        query = Query(self.db, skip=10, limit=10)
        result = QueryResult(query, skip=100, limit=100)
        self.assertIsInstance(result, QueryResult)
        self.assertDictEqual(result.options, {'skip': 100, 'limit': 100})
        self.assertEqual(result._ref, query)
github cloudant / python-cloudant / tests / unit / index_tests.py View on Github external
def test_search_index_via_query(self):
        """
        Test that a created TEXT index will produce expected query results.
        """
        index = TextIndex(self.db, 'ddoc001', 'index001')
        index.create()
        self.populate_db_with_documents(100)
        with Document(self.db, 'julia006') as doc:
            doc['name'] = 'julia isabel'
        query = Query(self.db)
        resp = query(
            fields=['name', 'age'],
            selector={'$text': 'isabel'}
        )
        self.assertEqual(resp['docs'], [{'name': 'julia isabel', 'age': 6}])
github cloudant / python-cloudant / tests / unit / query_tests.py View on Github external
def test_retrieve_query_url(self):
        """
        Test constructing the query test url
        """
        query = Query(self.db)
        self.assertEqual(
            query.url,
            '/'.join((self.db.database_url, '_find'))
        )
github cloudant / python-cloudant / tests / unit / query_tests.py View on Github external
"""
        Test Query __call__ by passing in invalid selector
        """
        test_data = [
            {'selector': 'blah'},  # Should be a dict
            {'limit': 'blah'},     # Should be an int
            {'skip': 'blah'},      # Should be an int
            {'sort': 'blah'},      # Should be a list
            {'fields': 'blah'},    # Should be a list
            {'r': 'blah'},         # Should be an int
            {'bookmark': 1},       # Should be a basestring
            {'use_index': 1}       # Should be a basestring
        ]

        for argument in test_data:
            query = Query(self.db)
            try:
                query(**argument)
                self.fail('Above statement should raise an Exception')
            except CloudantArgumentError as err:
                self.assertTrue(str(err).startswith(
                    'Argument {0} is not an instance of expected type:'.format(
                        list(argument.keys())[0]
                    )
github cloudant / python-cloudant / tests / unit / query_tests.py View on Github external
def test_callable_with_invalid_argument(self):
        """
        Test Query __call__ by passing in invalid arguments
        """
        query = Query(self.db)
        try:
            query(foo={'bar': 'baz'})
            self.fail('Above statement should raise an Exception')
        except CloudantArgumentError as err:
            self.assertEqual(str(err), 'Invalid argument: foo')
github cloudant / python-cloudant / tests / unit / query_tests.py View on Github external
def test_callable_executes_query(self):
        """
        Test Query __call__ executes a query
        """
        self.populate_db_with_documents(100)
        query = Query(self.db)
        resp = query(
            selector={'_id': {'$lt': 'julia050'}},
            fields=['_id'],
            sort=[{'_id': 'desc'}],
            skip=10,
            limit=3,
            r=1
        )
        self.assertEqual(
            resp['docs'],
            [{'_id': 'julia039'}, {'_id': 'julia038'}, {'_id': 'julia037'}]
        )
github cloudant / python-cloudant / tests / unit / query_tests.py View on Github external
def test_constructor_without_kwargs(self):
        """
        Test instantiating a Query without parameters
        """
        query = Query(self.db)
        self.assertIsInstance(query, Query)
        self.assertIsInstance(query.result, QueryResult)
        self.assertEqual(query, {})
github ibm-watson-data-lab / watson-recipe-bot-python-cloudant / souschef / cloudant_recipe_store.py View on Github external
'reduce': '_sum'
                        },
                        'cuisines': {
                            'map': 'function (doc) {\n  if (doc.type && doc.type==\'userCuisineRequest\') {\n    emit(doc.cuisine_name, 1);\n  }\n}',
                            'reduce': '_sum'
                        },
                        'recipes': {
                            'map': 'function (doc) {\n  if (doc.type && doc.type==\'userRecipeRequest\') {\n    emit(doc.recipe_title, 1);\n  }\n}',
                            'reduce': '_sum'
                        }
                    },
                    'language': 'javascript'
                }
                db.create_document(design_doc)
            # see if the by_day_of_week design doc exists, if not then create it
            query = Query(db, selector={ '_id': '_design/by_day_of_week' })
            result = query()['docs']
            if result is None or len(result) <= 0:
                design_doc = {
                    '_id': '_design/by_day_of_week',
                    'views': {
                        'ingredients': {
                            'map': 'function (doc) {\n  if (doc.type && doc.type==\'userIngredientRequest\') {\n    var weekdays = [\'Sunday\',\'Monday\',\'Tuesday\',\'Wednesday\',\'Thursday\',\'Friday\',\'Saturday\'];\n    emit(weekdays[new Date(doc.date).getDay()], 1);\n  }\n}',
                            'reduce': '_sum'
                        },
                        'cuisines': {
                            'map': 'function (doc) {\n  if (doc.type && doc.type==\'userCuisineRequest\') {\n    var weekdays = [\'Sunday\',\'Monday\',\'Tuesday\',\'Wednesday\',\'Thursday\',\'Friday\',\'Saturday\'];\n    emit(weekdays[new Date(doc.date).getDay()], 1);\n  }\n}',
                            'reduce': '_sum'
                        },
                        'recipes': {
                            'map': 'function (doc) {\n  if (doc.type && doc.type==\'userRecipeRequest\') {\n    var weekdays = [\'Sunday\',\'Monday\',\'Tuesday\',\'Wednesday\',\'Thursday\',\'Friday\',\'Saturday\'];\n    emit(weekdays[new Date(doc.date).getDay()], 1);\n  }\n}',
                            'reduce': '_sum'
github IBM / watson-online-store / watsononlinestore / database / cloudant_online_store.py View on Github external
:param str property_name: property name to search for
        :param str property_value: value that should match for the specified
                                   property name

        :returns: doc from query or None
        :rtype: dict, None
        """
        try:
            self.client.connect()
            db = self.client[self.db_name]
            selector = {
                '_id': {'$gt': 0},
                'type': doc_type,
                property_name: property_value
            }
            query = Query(db, selector=selector)
            for doc in query()['docs']:
                return doc
            return None
        except Exception:
            LOG.exception("Cloudant DB exception:")
        finally:
            self.client.disconnect()