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.assertIsInstance(ddoc.get_view(index.name), QueryIndexView) self.assertEquals(ddoc['_id'], index.design_document_id) self.assertTrue(ddoc['_rev'].startswith('1-')) self.assertEquals(ddoc['indexes'], {}) self.assertEquals(ddoc['language'], 'query') self.assertEquals(ddoc['lists'], {}) self.assertEquals(ddoc['shows'], {}) self.assertListEqual(list(ddoc['views'].keys()), [index.name]) view = ddoc['views'][index.name] self.assertEquals(view['map']['fields']['age'], 'asc') self.assertEquals(view['map']['fields']['name'], 'asc') self.assertEquals(view['options']['def']['fields'], ['name', 'age']) self.assertEquals(view['reduce'], '_count')
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'], 'indexes': {}, 'language': 'query', 'views': {index.name: {'map': {'fields': {'name': 'asc', 'age': 'asc'}}, 'reduce': '_count', 'options': {'def': {'fields': ['name', 'age']}, }}}, 'lists': {}, 'shows': {} } )
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'], 'indexes': {}, 'language': 'query', 'views': {'index001': {'map': {'fields': {'name': 'asc', 'age': 'asc'}}, 'reduce': '_count', 'options': {'def': {'fields': ['name', 'age']}, }}}, 'lists': {}, 'shows': {} } )
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.assertIsInstance(ddoc.get_view(index.name), QueryIndexView) self.assertEquals(ddoc['_id'], index.design_document_id) self.assertTrue(ddoc['_rev'].startswith('1-')) self.assertEquals(ddoc['indexes'], {}) self.assertEquals(ddoc['language'], 'query') self.assertEquals(ddoc['lists'], {}) self.assertEquals(ddoc['shows'], {}) self.assertListEqual(list(ddoc['views'].keys()), [index.name]) view = ddoc['views'][index.name] self.assertEquals(view['map']['fields']['age'], 'asc') self.assertEquals(view['map']['fields']['name'], 'asc') self.assertEquals(view['options']['def']['fields'], ['name', 'age']) self.assertEquals(view['reduce'], '_count')
def test_create_uses_custom_encoder(self): """ Test that the create method uses the custom encoder """ self.set_up_client(auto_connect=True, encoder="AEncoder") database = self.client[self.test_dbname] index = Index(database, '_design/ddoc001', 'index001', fields=['name', 'age']) with self.assertRaises(TypeError): index.create()
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_index_usage_via_query(self): """ Test that a query will warn 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) result = self.db.get_query_result(fields=['name', 'age'], selector={'age': {'$eq': 6}}, raw_result=True) self.assertTrue(str(result['warning']).startswith("no matching index found"))
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_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.assertTrue(str(err).endswith( 'A JSON index requires that only a \'fields\' argument is provided.'))
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_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)