def test_create_an_index_using_design_prefix(self):
     """
     Test that a JSON index is created correctly in the remote database when
     the ddoc id is already prefixed by '_design/'
     """
     index = Index(self.db, '_design/ddoc001', 'index001', fields=['name', 'age'])
     index.create()
     self.assertEqual(index.design_document_id, '_design/ddoc001')
     self.assertEqual(index.name, 'index001')
     with DesignDocument(self.db, index.design_document_id) as ddoc:
         self.assertEqual(ddoc['language'], 'query')
         self.assertListEqual(list(ddoc['views'].keys()), ['index001'])
         self.assertIsInstance(ddoc.get_view('index001'), QueryIndexView)
         self.assertTrue(ddoc['_rev'].startswith('1-'))
         self.assertEqual(ddoc,
             {'_id': '_design/ddoc001',
              '_rev': ddoc['_rev'],
              'language': 'query',
              'views': {'index001': {'map': {'fields': {'name': 'asc', 
                                                        'age': 'asc'}},
                                     'reduce': '_count',
                                     'options': {'def': {'fields': ['name',
                                                                    'age']},
                                                 'w': 2}}}}
         )
 def test_create_an_index_with_empty_ddoc_index_name(self):
     """
     Test that a JSON index is created in the remote database.
     """
     index = Index(self.db, '', '', fields=['name', 'age'])
     index.create()
     self.assertIsNotNone(index.design_document_id)
     self.assertTrue(index.design_document_id.startswith('_design/'))
     self.assertIsNotNone(index.name)
     with DesignDocument(self.db, index.design_document_id) as ddoc:
         self.assertEqual(ddoc['language'], 'query')
         self.assertListEqual(list(ddoc['views'].keys()), [index.name])
         self.assertIsInstance(ddoc.get_view(index.name), QueryIndexView)
         self.assertTrue(ddoc['_rev'].startswith('1-'))
         self.assertEqual(ddoc,
             {'_id': index.design_document_id,
              '_rev': ddoc['_rev'],
              'language': 'query',
              'views': {index.name: {'map': {'fields': {'name': 'asc', 
                                                        'age': 'asc'}},
                                     'reduce': '_count',
                                     'options': {'def': {'fields': ['name',
                                                                    'age']},
                                                 'w': 2}}}}
         )
 def test_deleting_index(self):
     """
     Test that deleting an index works as expected.
     """
     ddoc = DesignDocument(self.db, '_design/ddoc001')
     self.assertFalse(ddoc.exists())
     index = Index(self.db, 'ddoc001', 'index001', fields=['name', 'age'])
     index.create()
     self.assertTrue(ddoc.exists())
     index.delete()
     self.assertFalse(ddoc.exists())
 def test_create_fails_due_to_index_name_validation(self):
     """
     Ensure that if the index name is not a string the create call fails.
     """
     index = Index(self.db, 'ddoc001', ['index001'], fields=['name', 'age'])
     with self.assertRaises(CloudantArgumentError) as cm:
         index.create()
     err = cm.exception
     self.assertEqual(
         str(err),
         'The index name: [\'index001\'] is not a string.'
     )
 def test_index_via_query(self):
     """
     Test that a created index will produce expected query results.
     """
     index = Index(self.db, 'ddoc001', 'index001', fields=['age'])
     index.create()
     self.populate_db_with_documents(100)
     query = Query(self.db)
     resp = query(
         fields=['name', 'age'],
         selector={'age': {'$eq': 6}}
     )
     self.assertEqual(resp['docs'], [{'name': 'julia', 'age': 6}])
 def test_create_fails_due_to_def_validation(self):
     """
     Ensure that if the index definition contains anything other than
     "fields" the create call fails.
     """
     index = Index(self.db, fields=['name', 'age'], selector={})
     with self.assertRaises(CloudantArgumentError) as cm:
         index.create()
     err = cm.exception
     self.assertEqual(
         str(err), (
             '{0} provided as argument(s).  A JSON index requires that '
             'only a \'fields\' argument is provided.'
         ).format({'fields': ['name', 'age'], 'selector': {}})
     )
 def test_index_usage_via_query(self):
     """
     Test that a query will fail if the indexes that exist do not satisfy the
     query selector.
     """
     index = Index(self.db, 'ddoc001', 'index001', fields=['name'])
     index.create()
     self.populate_db_with_documents(100)
     query = Query(self.db)
     with self.assertRaises(requests.HTTPError) as cm:
         resp = query(
             fields=['name', 'age'],
             selector={'age': {'$eq': 6}}
         )
     err = cm.exception
     self.assertEqual(err.response.status_code, 400)