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_non_existing_index(self): """ Tests how deleting a non-existing index is handled. """ ddoc = DesignDocument(self.db, '_design/ddoc001') index = Index(self.db, 'ddoc001', 'index001', fields=['name', 'age']) self.assertFalse(ddoc.exists()) with self.assertRaises(requests.HTTPError) as cm: index.delete() err = cm.exception self.assertEqual(err.response.status_code, 404)
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_index_to_dictionary(self): """ Test the conversion of an Index object into a dictionary representation of that object. """ index = Index(self.db, 'ddoc-id', 'index-name', foo={'bar': 'baz'}) self.assertEqual(index.as_a_dict(), { 'ddoc': 'ddoc-id', 'name': 'index-name', 'type': 'json', 'def': {'foo': {'bar': 'baz'}} })
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_as_a_dict_with_none_attributes(self): """ Test the conversion of an Index object that contains attributes set to None into a dictionary representation of that object. """ index = Index(self.db) self.assertEqual(index.as_a_dict(), { 'ddoc': None, 'name': None, 'type': 'json', 'def': {} })
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_deleting_index_without_index_name(self): """ Tests that deleting an index without an index name provided fails as expected. """ ddoc = DesignDocument(self.db, '_design/ddoc001') index = Index(self.db, 'ddoc001', fields=['name', 'age']) self.assertFalse(ddoc.exists()) with self.assertRaises(CloudantArgumentError) as cm: index.delete() err = cm.exception self.assertEqual( str(err), 'Deleting an index requires an index name be provided.' )
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)