def test_new_ddoc_add_view(self): mock_database = mock.Mock() ddoc = DesignDocument(mock_database, '_design/unittest') ddoc.add_view('view1', "MAP") self.assertIn('view1', ddoc['views']) self.assertEqual(ddoc['views']['view1'].map, 'MAP') self.assertEqual(ddoc['views']['view1'].reduce, None)
def create_db_sentiments(conn): """ Make "sentiments" database """ logging.info('Creating database "sentiments"...') db_sentiments = conn.create_database('sentiments') logging.info('Done creating database.') # create summary view logging.info('Creating views in database "sentiments"...') summary_ddoc = DesignDocument(db_sentiments, document_id='summary') with open('db/sentiments/summary.design.json') as f: ddoc_spec = json.load(f) for view_spec in ddoc_spec['views']: fns = ddoc_spec['views'][view_spec] map_fn = fns['map'] if 'reduce' in fns: reduce_fn = fns['reduce'] summary_ddoc.add_view(view_spec, map_fn, reduce_fn) else: summary_ddoc.add_view(view_spec, map_fn) summary_ddoc.create() logging.info('Finished creating views.')
def test_view_callable_with_invalid_javascript(self): """ Test error condition when Javascript errors exist. This test is only valid for CouchDB because the map function Javascript is validated on the Cloudant server when attempting to save a design document so invalid Javascript is not possible there. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view( 'view001', 'This is not valid Javascript' ) ddoc.save() # Verify that the ddoc and view were saved remotely # along with the invalid Javascript del ddoc ddoc = DesignDocument(self.db, 'ddoc001') ddoc.fetch() view = ddoc.get_view('view001') self.assertEqual(view.map, 'This is not valid Javascript') try: for row in view.result: self.fail('Above statement should raise an Exception') except requests.HTTPError as err: self.assertEqual(err.response.status_code, 500)
def initialize_views(self, database): for ddoc_definition in self.view_definitions: this_ddoc = DesignDocument(database, ddoc_definition['ddoc']) for view_definition in ddoc_definition['views']: this_ddoc.add_view(view_definition['name'], view_definition['map_func'], reduce_func=view_definition['reduce_func']) this_ddoc.save()
def test_delete_a_view(self): """ Test deleting a view from the DesignDocument dictionary. """ ddoc = DesignDocument(self.db, "_design/ddoc001") ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") self.assertEqual(ddoc.get("views")["view001"], {"map": "function (doc) {\n emit(doc._id, 1);\n}"}) ddoc.delete_view("view001") self.assertEqual(ddoc.get("views"), {})
def test_ddoc_update_view(self): mock_database = mock.Mock() ddoc = DesignDocument(mock_database, '_design/unittest') ddoc.add_view('view1', "MAP", "REDUCE") ddoc.update_view('view1', "UPDATED_MAP", "REDUCE") self.assertTrue('view1' in ddoc['views']) self.assertEqual(ddoc['views']['view1'].map, 'UPDATED_MAP') self.assertEqual(ddoc['views']['view1'].reduce, 'REDUCE')
def test_make_result(self): """ Ensure that the view results are wrapped in a Result object """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, "ddoc001") ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") ddoc.save() view = ddoc.get_view("view001") self.assertIsInstance(view.make_result(), Result)
def test_adding_query_index_view(self): """ Test that adding a query index view fails as expected. """ ddoc = DesignDocument(self.db, "_design/ddoc001") ddoc["language"] = "query" with self.assertRaises(CloudantException) as cm: ddoc.add_view("view001", {"foo": "bar"}) err = cm.exception self.assertEqual(str(err), "Cannot add a MapReduce view to a " "design document for query indexes.")
def test_update_a_view(self): """ Test that updating a view updates the contents of the correct View object in the DesignDocument dictionary. """ ddoc = DesignDocument(self.db, "_design/ddoc001") ddoc.add_view("view001", "not-a-valid-map-function") self.assertEqual(ddoc.get("views")["view001"], {"map": "not-a-valid-map-function"}) ddoc.update_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") self.assertEqual(ddoc.get("views")["view001"], {"map": "function (doc) {\n emit(doc._id, 1);\n}"})
def test_list_views(self): """ Test the retrieval of view name list from DesignDocument """ ddoc = DesignDocument(self.db, "_design/ddoc001") view_map = "function (doc) {\n emit(doc._id, 1);\n}" ddoc.add_view("view001", view_map) ddoc.add_view("view002", view_map) ddoc.add_view("view003", view_map) self.assertTrue(all(x in ddoc.list_views() for x in ["view001", "view002", "view003"]))
def test_existing_ddoc_add_view(self): mock_database = mock.Mock() ddoc = DesignDocument(mock_database, '_design/unittest') ddoc['views'] = { 'view1': {'map': "MAP", 'reduce': 'REDUCE'} } ddoc.add_view('view2', "MAP2") self.assertIn('view1', ddoc['views']) self.assertIn('view2', ddoc['views']) self.assertEqual(ddoc['views']['view2'].map, 'MAP2') self.assertEqual(ddoc['views']['view2'].reduce, None)
def test_adding_existing_view(self): """ Test that adding an existing view fails as expected. """ ddoc = DesignDocument(self.db, "_design/ddoc001") ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") try: ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 2);\n}") self.fail("Above statement should raise an Exception") except CloudantArgumentError as err: self.assertEqual(str(err), "View view001 already exists in this design doc")
def test_add_a_view(self): """ Test that adding a view adds a View object to the DesignDocument dictionary. """ ddoc = DesignDocument(self.db, "_design/ddoc001") self.assertEqual(ddoc.get("views"), {}) ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") self.assertListEqual(list(ddoc.get("views").keys()), ["view001"]) self.assertIsInstance(ddoc.get("views")["view001"], View) self.assertEqual(ddoc.get("views")["view001"], {"map": "function (doc) {\n emit(doc._id, 1);\n}"})
def test_mr_view_save_succeeds(self): """ Tests that save succeeds when no language is specified and views are map reduce views. """ ddoc = DesignDocument(self.db, "_design/ddoc001") view_map = "function (doc) {\n emit(doc._id, 1);\n}" view_reduce = "_count" db_copy = "{0}-copy".format(self.db.database_name) ddoc.add_view("view001", view_map, view_reduce, dbcopy=db_copy) ddoc.save() self.assertTrue(ddoc["_rev"].startswith("1-"))
def test_delete_a_view(self): """ Test deleting a view from the DesignDocument dictionary. """ ddoc = DesignDocument(self.db, '_design/ddoc001') ddoc.add_view('view001', 'function (doc) {\n emit(doc._id, 1);\n}') self.assertEqual( ddoc.get('views')['view001'], {'map': 'function (doc) {\n emit(doc._id, 1);\n}'} ) ddoc.delete_view('view001') self.assertEqual(ddoc.get('views'), {})
def test_mr_view_save_succeeds(self): """ Tests that save succeeds when no language is specified and views are map reduce views. """ ddoc = DesignDocument(self.db, '_design/ddoc001') view_map = 'function (doc) {\n emit(doc._id, 1);\n}' view_reduce = '_count' db_copy = '{0}-copy'.format(self.db.database_name) ddoc.add_view('view001', view_map, view_reduce, dbcopy=db_copy) ddoc.save() self.assertTrue(ddoc['_rev'].startswith('1-'))
def test_ddoc_delete_view(self): mock_database = mock.Mock() ddoc = DesignDocument(mock_database, '_design/unittest') ddoc.add_view('view1', "MAP", "REDUCE") ddoc.add_view('view2', "MAP", "REDUCE") self.assertIn('view1', ddoc['views']) self.assertIn('view2', ddoc['views']) ddoc.delete_view('view2') self.assertIn('view1', ddoc['views']) self.assertNotIn('view2', ddoc['views']) self.assertEqual(ddoc['views']['view1'].map, 'MAP') self.assertEqual(ddoc['views']['view1'].reduce, 'REDUCE')
def test_iterating_over_views(self): """ Test iterating over views within the DesignDocument """ ddoc = DesignDocument(self.db, "_design/ddoc001") view_map = "function (doc) {\n emit(doc._id, 1);\n}" ddoc.add_view("view001", view_map) ddoc.add_view("view002", view_map) ddoc.add_view("view003", view_map) view_names = [] for view_name, view in ddoc.iterviews(): self.assertIsInstance(view, View) view_names.append(view_name) self.assertTrue(all(x in view_names for x in ["view001", "view002", "view003"]))
def test_adding_query_index_view(self): """ Test that adding a query index view fails as expected. """ ddoc = DesignDocument(self.db, '_design/ddoc001') ddoc['language'] = 'query' with self.assertRaises(CloudantException) as cm: ddoc.add_view('view001', {'foo': 'bar'}) err = cm.exception self.assertEqual( str(err), 'Cannot add a MapReduce view to a ' 'design document for query indexes.' )
def test_mr_view_save_fails_when_lang_is_query(self): """ Tests that save fails when language is query but views are map reduce views. """ ddoc = DesignDocument(self.db, "_design/ddoc001") view_map = "function (doc) {\n emit(doc._id, 1);\n}" view_reduce = "_count" db_copy = "{0}-copy".format(self.db.database_name) ddoc.add_view("view001", view_map, view_reduce, dbcopy=db_copy) ddoc["language"] = "query" with self.assertRaises(CloudantException) as cm: ddoc.save() err = cm.exception self.assertEqual(str(err), "View view001 must be of type QueryIndexView.")
def test_partitioned_view(self): ddoc = DesignDocument(self.db, 'partitioned_view', partitioned=True) ddoc.add_view('view1', 'function(doc) { emit(doc._id, 1); }') ddoc.save() for partition_key in self.populate_db_with_partitioned_documents( 2, 10): results = self.db.get_partitioned_view_result( partition_key, ddoc['_id'], 'view1') i = 0 for result in results: self.assertTrue(result['id'].startswith(partition_key + ':')) i += 1 self.assertEquals(i, 10)
def get_timestamp(symptoms_db): """ Update view and get last timestamp """ try: ddoc = DesignDocument(symptoms_db, 'timestamp') view = View(ddoc, 'timestamp') for row in view(descending=True, limit=1)['rows']: timestamp = (row['key']) except: ddoc = DesignDocument(symptoms_db, 'timestamp') ddoc.add_view('timestamp', 'function(doc){\n emit(doc.timestamp,1)}') ddoc.save() timestamp = 1 return timestamp
def test_view_callable_raw_json(self): """ Test that the View __call__ method which is invoked by calling the view object returns the appropriate raw JSON response. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, "ddoc001") ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") ddoc.save() view = ddoc.get_view("view001") ids = [] # view(limit=3) calls the view object and passes it the limit parameter for row in view(limit=3)["rows"]: ids.append(row["id"]) expected = ["julia000", "julia001", "julia002"] self.assertTrue(all(x in ids for x in expected))
def test_list_views(self): """ Test the retrieval of view name list from DesignDocument """ ddoc = DesignDocument(self.db, '_design/ddoc001') view_map = 'function (doc) {\n emit(doc._id, 1);\n}' ddoc.add_view('view001', view_map) ddoc.add_view('view002', view_map) ddoc.add_view('view003', view_map) self.assertTrue( all(x in ddoc.list_views() for x in [ 'view001', 'view002', 'view003' ]) )
def test_get_view_callable_raw_json(self): """ Test that the GET request of the View __call__ method that is invoked when calling the view object returns the appropriate raw JSON response. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view('view001', 'function (doc) {\n emit(doc._id, 1);\n}') ddoc.save() view = ddoc.get_view('view001') ids = [] # view(limit=3) calls the view object and passes it the limit parameter # where a HTTP GET request is made. for row in view(limit=3)['rows']: ids.append(row['id']) expected = ['julia000', 'julia001', 'julia002'] self.assertTrue(all(x in ids for x in expected))
def test_add_a_view(self): """ Test that adding a view adds a View object to the DesignDocument dictionary. """ ddoc = DesignDocument(self.db, '_design/ddoc001') self.assertEqual(ddoc.get('views'), {}) ddoc.add_view( 'view001', 'function (doc) {\n emit(doc._id, 1);\n}' ) self.assertEqual(ddoc.get('views').keys(), ['view001']) self.assertIsInstance(ddoc.get('views')['view001'], View) self.assertEqual( ddoc.get('views')['view001'], {'map': 'function (doc) {\n emit(doc._id, 1);\n}'} )
def test_view_callable_view_result(self): """ Test that by referencing the .result attribute the view callable method is invoked and the data returned is wrapped as a Result. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view('view001', 'function (doc) {\n emit(doc._id, 1);\n}') ddoc.save() view = ddoc.get_view('view001') rslt = view.result self.assertIsInstance(rslt, Result) ids = [] # rslt[:3] limits the Result to the first 3 elements for row in rslt[:3]: ids.append(row['id']) expected = ['julia000', 'julia001', 'julia002'] self.assertTrue(all(x in ids for x in expected))
def test_view_callable_with_invalid_javascript(self): """ Test error condition when Javascript errors exist. This test is only valid for CouchDB because the map function Javascript is validated on the Cloudant server when attempting to save a design document so invalid Javascript is not possible there. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view('view001', 'This is not valid Javascript') with self.assertRaises(requests.HTTPError) as cm: ddoc.save() err = cm.exception self.assertTrue( str(err).startswith( '400 Client Error: Bad Request compilation_error Compilation of the map function ' 'in the \'view001\' view failed: Expression does not eval to a function.' ))
def test_view_callable_view_result(self): """ Test that by referencing the .result attribute the view callable method is invoked and the data returned is wrapped as a Result. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, "ddoc001") ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") ddoc.save() view = ddoc.get_view("view001") rslt = view.result self.assertIsInstance(rslt, Result) ids = [] # rslt[:3] limits the Result to the first 3 elements for row in rslt[:3]: ids.append(row["id"]) expected = ["julia000", "julia001", "julia002"] self.assertTrue(all(x in ids for x in expected))
def test_mr_view_save_fails_when_lang_is_query(self): """ Tests that save fails when language is query but views are map reduce views. """ ddoc = DesignDocument(self.db, '_design/ddoc001') view_map = 'function (doc) {\n emit(doc._id, 1);\n}' view_reduce = '_count' db_copy = '{0}-copy'.format(self.db.database_name) ddoc.add_view('view001', view_map, view_reduce, dbcopy=db_copy) ddoc['language'] = 'query' with self.assertRaises(CloudantException) as cm: ddoc.save() err = cm.exception self.assertEqual( str(err), 'View view001 must be of type QueryIndexView.' )
def create_latest_recommendations_index(): ddoc_fn = ''' function(doc) { emit([doc.user, doc.timestamp], null); } ''' db = cloudant_client[CL_RECOMMENDDB] index_name = 'latest-recommendation-index' ddoc = DesignDocument(db, index_name) if ddoc.exists(): ddoc.fetch() ddoc.update_view(index_name, ddoc_fn) print('updated', index_name) else: ddoc.add_view(index_name, ddoc_fn) print('created', index_name) ddoc.save()
def test_update_a_view(self): """ Test that updating a view updates the contents of the correct View object in the DesignDocument dictionary. """ ddoc = DesignDocument(self.db, '_design/ddoc001') ddoc.add_view('view001', 'not-a-valid-map-function') self.assertEqual( ddoc.get('views')['view001'], {'map': 'not-a-valid-map-function'} ) ddoc.update_view( 'view001', 'function (doc) {\n emit(doc._id, 1);\n}' ) self.assertEqual( ddoc.get('views')['view001'], {'map': 'function (doc) {\n emit(doc._id, 1);\n}'} )
def test_get_view_callable_raw_json(self): """ Test that the GET request of the View __call__ method that is invoked when calling the view object returns the appropriate raw JSON response. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view( 'view001', 'function (doc) {\n emit(doc._id, 1);\n}' ) ddoc.save() view = ddoc.get_view('view001') ids = [] # view(limit=3) calls the view object and passes it the limit parameter # where a HTTP GET request is made. for row in view(limit=3)['rows']: ids.append(row['id']) expected = ['julia000', 'julia001', 'julia002'] self.assertTrue(all(x in ids for x in expected))
def test_custom_result_context_manager(self): """ Test that the context manager for custom results returns the expected Results """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, "ddoc001") ddoc.add_view("view001", "function (doc) {\n emit(doc._id, 1);\n}") ddoc.save() view = ddoc.get_view("view001") # Return a custom result by including documents with view.custom_result(include_docs=True, reduce=False) as rslt: i = 0 for row in rslt: self.assertEqual(row["doc"]["_id"], "julia{0:03d}".format(i)) self.assertTrue(row["doc"]["_rev"].startswith("1-")) self.assertEqual(row["doc"]["name"], "julia") self.assertEqual(row["doc"]["age"], i) i += 1 self.assertEqual(i, 100)
def test_custom_result_context_manager(self): """ Test that the context manager for custom results returns the expected Results """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view('view001', 'function (doc) {\n emit(doc._id, 1);\n}') ddoc.save() view = ddoc.get_view('view001') # Return a custom result by including documents with view.custom_result(include_docs=True, reduce=False) as rslt: i = 0 for row in rslt: self.assertEqual(row['doc']['_id'], 'julia{0:03d}'.format(i)) self.assertTrue(row['doc']['_rev'].startswith('1-')) self.assertEqual(row['doc']['name'], 'julia') self.assertEqual(row['doc']['age'], i) i += 1 self.assertEqual(i, 100)
def test_post_view_callable_raw_json(self): """ Using the "keys" parameter test that the POST request of the View __call__ method that is invoked when calling the view object returns the appropriate raw JSON response. """ # Create 200 documents with ids julia000, julia001, julia002, ..., julia199 self.populate_db_with_documents(200) # Generate keys list for every other document created # with ids julia000, julia002, julia004, ..., julia198 keys_list = ['julia{0:03d}'.format(i) for i in range(0, 200, 2)] self.assertEqual(len(keys_list), 100) ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view('view001', 'function (doc) {\n emit(doc._id, 1);\n}') ddoc.save() view = ddoc.get_view('view001') # view(keys=keys_list) calls the view object and passes keys parameter ids = [row['id'] for row in view(keys=keys_list)['rows']] self.assertEqual(len(ids), 100) self.assertTrue(all(x in ids for x in keys_list))
def test_fetch_map_reduce(self): """ Ensure that the document fetch from the database returns the DesignDocument format as expected when retrieving a design document containing MapReduce views. """ ddoc = DesignDocument(self.db, '_design/ddoc001') view_map = 'function (doc) {\n emit(doc._id, 1);\n}' view_reduce = '_count' db_copy = '{0}-copy'.format(self.db.database_name) ddoc.add_view('view001', view_map, view_reduce) ddoc.add_view('view002', view_map, view_reduce, dbcopy=db_copy) ddoc.add_view('view003', view_map) ddoc.save() ddoc_remote = DesignDocument(self.db, '_design/ddoc001') self.assertNotEqual(ddoc_remote, ddoc) ddoc_remote.fetch() self.assertEqual(ddoc_remote, ddoc) self.assertTrue(ddoc_remote['_rev'].startswith('1-')) self.assertEqual(ddoc_remote, { '_id': '_design/ddoc001', '_rev': ddoc['_rev'], 'views': { 'view001': {'map': view_map, 'reduce': view_reduce}, 'view002': {'map': view_map, 'reduce': view_reduce, 'dbcopy': db_copy}, 'view003': {'map': view_map} } }) self.assertIsInstance(ddoc_remote['views']['view001'], View) self.assertIsInstance(ddoc_remote['views']['view002'], View) self.assertIsInstance(ddoc_remote['views']['view003'], View)
def test_fetch(self): """ Ensure that the document fetch from the database returns the DesignDocument format as expected. """ ddoc = DesignDocument(self.db, '_design/ddoc001') view_map = 'function (doc) {\n emit(doc._id, 1);\n}' view_reduce = '_count' ddoc.add_view('view001', view_map) ddoc.add_view('view002', view_map, view_reduce) ddoc.add_view('view003', view_map) ddoc.save() ddoc_remote = DesignDocument(self.db, '_design/ddoc001') self.assertNotEqual(ddoc_remote, ddoc) ddoc_remote.fetch() self.assertEqual(ddoc_remote, ddoc) self.assertEqual(len(ddoc_remote['views']), 3) reduce_count = 0 for x in xrange(1, 4): name = 'view{0:03d}'.format(x) view = ddoc_remote['views'][name] self.assertIsInstance(view, View) self.assertEqual(view.map, view_map) if name == 'view002': reduce_count += 1 self.assertEqual(view.reduce, view_reduce) else: self.assertIsNone(view.reduce) self.assertEqual(reduce_count, 1)
def test_fetch_map_reduce(self): """ Ensure that the document fetch from the database returns the DesignDocument format as expected when retrieving a design document containing MapReduce views. """ ddoc = DesignDocument(self.db, "_design/ddoc001") view_map = "function (doc) {\n emit(doc._id, 1);\n}" view_reduce = "_count" db_copy = "{0}-copy".format(self.db.database_name) ddoc.add_view("view001", view_map, view_reduce) ddoc.add_view("view002", view_map, view_reduce, dbcopy=db_copy) ddoc.add_view("view003", view_map) ddoc.save() ddoc_remote = DesignDocument(self.db, "_design/ddoc001") self.assertNotEqual(ddoc_remote, ddoc) ddoc_remote.fetch() self.assertEqual(ddoc_remote, ddoc) self.assertTrue(ddoc_remote["_rev"].startswith("1-")) self.assertEqual( ddoc_remote, { "_id": "_design/ddoc001", "_rev": ddoc["_rev"], "views": { "view001": {"map": view_map, "reduce": view_reduce}, "view002": {"map": view_map, "reduce": view_reduce, "dbcopy": db_copy}, "view003": {"map": view_map}, }, }, ) self.assertIsInstance(ddoc_remote["views"]["view001"], View) self.assertIsInstance(ddoc_remote["views"]["view002"], View) self.assertIsInstance(ddoc_remote["views"]["view003"], View)
def create_authdb_indexes(): db = cloudant_client[CL_AUTHDB] ddoc_fn = ''' function(doc){ if (doc.email) { emit(doc.email); } } ''' view_name = 'authdb-email-index' ddoc = DesignDocument(db, view_name) if ddoc.exists(): ddoc.fetch() ddoc.update_view(view_name, ddoc_fn) print('updated', view_name) else: ddoc.add_view(view_name, ddoc_fn) print('created', view_name) ddoc.save()
def test_view_callable_with_invalid_javascript(self): """ Test error condition when Javascript errors exist. This test is only valid for CouchDB because the map function Javascript is validated on the Cloudant server when attempting to save a design document so invalid Javascript is not possible there. """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view('view001', 'This is not valid Javascript') ddoc.save() # Verify that the ddoc and view were saved remotely # along with the invalid Javascript del ddoc ddoc = DesignDocument(self.db, 'ddoc001') ddoc.fetch() view = ddoc.get_view('view001') self.assertEqual(view.map, 'This is not valid Javascript') try: for row in view.result: self.fail('Above statement should raise an Exception') except requests.HTTPError as err: self.assertEqual(err.response.status_code, 500)
def test_custom_result_context_manager(self): """ Test that the context manager for custom results returns the expected Results """ self.populate_db_with_documents() ddoc = DesignDocument(self.db, 'ddoc001') ddoc.add_view( 'view001', 'function (doc) {\n emit(doc._id, 1);\n}' ) ddoc.save() view = ddoc.get_view('view001') # Return a custom result by including documents with view.custom_result(include_docs=True, reduce=False) as rslt: i = 0 for row in rslt: self.assertEqual(row['doc']['_id'], 'julia{0:03d}'.format(i)) self.assertTrue(row['doc']['_rev'].startswith('1-')) self.assertEqual(row['doc']['name'], 'julia') self.assertEqual(row['doc']['age'], i) i += 1 self.assertEqual(i, 100)
def create_views(client): """ Create views if doesn't exist, and connect to databases """ try: symptoms_db = client['symptoms'] except: symptoms_db = client.create_database('symptoms') ddoc_symptoms = DesignDocument(symptoms_db, 'symptoms') ddoc_symptoms.add_view( 'symptoms_location', 'function (doc) {\n for(var i in doc.symptoms){\n if(doc.symptoms[i]=="cough"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fever"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="throat"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fatigue"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="breathing"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="headache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="bodyache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="stloss"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="vomit"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="diarrhoea"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}}}', '_count') view = View(ddoc_symptoms, 'symptoms_location') #Create a mapreduce function to group the documents by symptoms and location ddoc_symptoms.save() covidsymptom_db = client['tweet-covid-symptom'] #CovidSafe Database and create views #Create a covidsafe designdocument for views #Create a mapreduce function to group the documents by sentiment and location covidsafe_db = client['tweet-covid-covidsafe'] covidsafe_ddoc = DesignDocument(covidsafe_db, 'covidsafe') covidsafe_ddoc.add_view( 'sentiment_location', 'function(doc){\n emit([doc.sentiment,doc.SA4],1)}', '_count') try: covidsafe_ddoc.save() except: pass #Covid Database, create views, and mapreduce functions covid_db = client['tweet-covid'] covid_ddoc = DesignDocument(covid_db, 'covid') covid_ddoc.add_view('sentiment_location', 'function(doc){\n emit([doc.sentiment,doc.SA4],1)}', '_count') try: covid_ddoc.save() except: pass return symptoms_db, covidsymptom_db, covidsafe_db, covid_db
DBNAME = 'historical_data' DBNAME2 = 'processed_data' #initial connection with couchdb server client = CouchDB(USERNAME, PASSWORD, url=URL, connect=True) my_database = client[DBNAME] my_database2 = client.create_database(DBNAME2) map_fun = '''function(doc) { emit(doc.doc.coordinates.coordinates, doc.doc.text); }''' ddoc_id = 'ddoc001' view_id = 'location_text' ddoc = DesignDocument(my_database, 'ddoc001', partitioned=False) ddoc.add_view(view_id, map_fun) my_database.create_document(ddoc) view1 = my_database.get_design_document(ddoc_id).get_view(view_id) #open statistical areas 4 shapes and records to classify tweets location in to regions shp = shapefile.Reader(r'../sa4/SA4_2016_AUST') shape_recs = shp.shapeRecords() mel_areas = [] for shape_record in shape_recs: if (shape_record.record[-4] == 'Greater Melbourne'): mel_areas.append(shape_record) batch_size = 250 #initialize the sentiment analysis classifier sid = SentimentIntensityAnalyzer() with view1.custom_result() as rslt:
from cloudant.client import CouchDB from cloudant.view import View from cloudant.error import CloudantClientException vote_map = """\ function(doc) { if(doc.pollID && doc.votes) { emit(doc.pollID, { pollID: doc.pollID, votes: doc.votes }); } } """ client = CouchDB(None, None, url='http://' + os.environ['DB_NAME'] + ':5984', admin_party=True, connect=True) try: couch = client.create_database(dbname=os.environ['DB_NAME'], throw_on_exists=True) except CloudantClientException: couch = client[os.environ['DB_NAME']] ddoc = DesignDocument(couch, '_design/' + os.environ['DB_NAME']) ddoc.add_view(os.environ['DB_NAME'], vote_map) if ddoc.exists() is not True: ddoc.save() vote_view = View(ddoc, os.environ['DB_NAME'])
from cloudant.client import CouchDB from cloudant.design_document import DesignDocument from cloudant.view import View #connect couchDB client = CouchDB("admin", "password", url='http://172.26.131.173:5984', connect=True) session = client.session() #print('Username: {0}'.format(session['userCtx']['name'])) #print('Databases: {0}'.format(client.all_dbs())) #connect database in the couchDB db = client['tweet-covid'] #Create a designdocument for views ddoc = DesignDocument(db, 'covid') #Create a mapreduce function to group the documents by sentiment and location ddoc.add_view('sentiment_location', 'function(doc){\n emit([doc.sentiment,doc.SA4],1)}', '_count') ddoc.save()
#maping function and reduce function-> can be modified map_fun = '''function(doc) { emit(doc.region_code, doc.compound); }''' #reduce_fun = '_count' reduce_fun = '_stats' #specify which database you want to access, returns all the document within the database my_database = client[DBNAME] #creating a example using the mapping function and reduce function, adding it to the database viewName = 'mapreduce' ddoc = DesignDocument(my_database, 'ddoc001', partitioned=False) ddoc.add_view('mapreduce', map_fun, reduce_fun) my_database.create_document(ddoc) #how to access created view with reducing functions to group results to a group ddoc_id = 'ddoc001' view_id = 'mapreduce' my_database2 = client.get(DBNAME, remote=True) view1 = my_database2.get_design_document(ddoc_id).get_view(view_id) #with view1.custom_result(group = True) as rslt: with view1.custom_result(group=True) as rslt: for elem in rslt: print(elem['key'], elem['value']['count'], elem['value']['sum'] / elem['value']['count']) #example of how to access view created from web #http://127.0.0.1:5984/haha/_design/ddoc001/_view/mapreduce?group=true
if db.exists(): print("Created a python-cloudant database.") # create Document if not existed doc_exist = False try: ddoc = DesignDocument(db, document_id="_design/python-cloudant") except: doc_exist = True if not doc_exist: # create Views # clients ddoc.add_view(view_name='clients', map_func='''function(doc) { if (doc.type == "client") { emit(doc._id, doc); } }''') # opened sessions ddoc.add_view(view_name='opened_sessions', map_func='''function(doc) { if (doc.type == "session" && doc.status == "open") { emit(doc.opened, doc._id); } }''') # clients in a session ddoc.add_view(view_name='client_in_session', map_func='''function(doc) { if (doc.type == "client") { emit(doc.session, doc._id); }
class UnitTestDbBase(unittest.TestCase): """ The base class for all unit tests targeting a database """ @classmethod def setUpClass(cls): """ If targeting CouchDB, Set up a CouchDB instance otherwise do nothing. Note: Admin Party is currently unsupported so we must create a CouchDB user for tests to function with a CouchDB instance if one is not provided. """ if os.environ.get('RUN_CLOUDANT_TESTS') is None: if os.environ.get('DB_URL') is None: os.environ['DB_URL'] = 'http://127.0.0.1:5984' if (os.environ.get('ADMIN_PARTY') and os.environ.get('ADMIN_PARTY') == 'true'): if os.environ.get('DB_USER'): del os.environ['DB_USER'] if os.environ.get('DB_PASSWORD'): del os.environ['DB_PASSWORD'] return if os.environ.get('DB_USER') is None: os.environ['DB_USER_CREATED'] = '1' os.environ['DB_USER'] = '******'.format( unicode_(uuid.uuid4())) os.environ['DB_PASSWORD'] = '******' resp = requests.put( '{0}/_config/admins/{1}'.format(os.environ['DB_URL'], os.environ['DB_USER']), data='"{0}"'.format(os.environ['DB_PASSWORD'])) resp.raise_for_status() @classmethod def tearDownClass(cls): """ If necessary, clean up CouchDB instance once all tests are complete. """ if (os.environ.get('RUN_CLOUDANT_TESTS') is None and os.environ.get('DB_USER_CREATED') is not None): resp = requests.delete( '{0}://{1}:{2}@{3}/_config/admins/{4}'.format( os.environ['DB_URL'].split('://', 1)[0], os.environ['DB_USER'], os.environ['DB_PASSWORD'], os.environ['DB_URL'].split('://', 1)[1], os.environ['DB_USER'])) del os.environ['DB_USER_CREATED'] del os.environ['DB_USER'] resp.raise_for_status() def setUp(self): """ Set up test attributes for unit tests targeting a database """ self.set_up_client() def set_up_client(self, auto_connect=False, encoder=None): if os.environ.get('RUN_CLOUDANT_TESTS') is None: admin_party = False if (os.environ.get('ADMIN_PARTY') and os.environ.get('ADMIN_PARTY') == 'true'): admin_party = True self.user = os.environ.get('DB_USER', None) self.pwd = os.environ.get('DB_PASSWORD', None) self.url = os.environ['DB_URL'] self.client = CouchDB(self.user, self.pwd, admin_party, url=self.url, connect=auto_connect, encoder=encoder) else: self.account = os.environ.get('CLOUDANT_ACCOUNT') self.user = os.environ.get('DB_USER') self.pwd = os.environ.get('DB_PASSWORD') self.url = os.environ.get( 'DB_URL', 'https://{0}.cloudant.com'.format(self.account)) self.client = Cloudant(self.user, self.pwd, url=self.url, x_cloudant_user=self.account, connect=auto_connect, encoder=encoder) def tearDown(self): """ Ensure the client is new for each test """ del self.client def db_set_up(self): """ Set up test attributes for Database tests """ self.client.connect() self.test_dbname = self.dbname() self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname) self.db.create() def db_tear_down(self): """ Reset test attributes for each test """ self.db.delete() self.client.disconnect() del self.test_dbname del self.db def dbname(self, database_name='db'): return '{0}-{1}'.format(database_name, unicode_(uuid.uuid4())) def populate_db_with_documents(self, doc_count=100, **kwargs): off_set = kwargs.get('off_set', 0) docs = [{ '_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i } for i in range(off_set, off_set + doc_count)] return self.db.bulk_docs(docs) def create_views(self): """ Create a design document with views for use with tests. """ self.ddoc = DesignDocument(self.db, 'ddoc001') self.ddoc.add_view('view001', 'function (doc) {\n emit(doc._id, 1);\n}') self.ddoc.add_view('view002', 'function (doc) {\n emit(doc._id, 1);\n}', '_count') self.ddoc.add_view( 'view003', 'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}') self.ddoc.add_view( 'view004', 'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}', '_count') self.ddoc.add_view( 'view005', 'function (doc) {\n emit([doc.name, doc.age], 1);\n}') self.ddoc.add_view( 'view006', 'function (doc) {\n emit([doc.name, doc.age], 1);\n}', '_count') self.ddoc.save() self.view001 = self.ddoc.get_view('view001') self.view002 = self.ddoc.get_view('view002') self.view003 = self.ddoc.get_view('view003') self.view004 = self.ddoc.get_view('view004') self.view005 = self.ddoc.get_view('view005') self.view006 = self.ddoc.get_view('view006') def create_search_index(self): """ Create a design document with search indexes for use with search query tests. """ self.search_ddoc = DesignDocument(self.db, 'searchddoc001') self.search_ddoc['indexes'] = { 'searchindex001': { 'index': 'function (doc) {\n index("default", doc._id); \n ' 'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} ' 'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} ' } } self.search_ddoc.save()
class UnitTestDbBase(unittest.TestCase): """ The base class for all unit tests targeting a database """ @classmethod def setUpClass(cls): """ If targeting CouchDB, Set up a CouchDB instance otherwise do nothing. """ if os.environ.get('RUN_CLOUDANT_TESTS') is None: if os.environ.get('DB_URL') is None: os.environ['DB_URL'] = 'http://127.0.0.1:5984' if (os.environ.get('ADMIN_PARTY') and os.environ.get('ADMIN_PARTY') == 'true'): if os.environ.get('DB_USER'): del os.environ['DB_USER'] if os.environ.get('DB_PASSWORD'): del os.environ['DB_PASSWORD'] return if os.environ.get('DB_USER') is None: os.environ['DB_USER_CREATED'] = '1' os.environ['DB_USER'] = '******'.format( unicode_(uuid.uuid4())) os.environ['DB_PASSWORD'] = '******' resp = requests.put( '{0}/_config/admins/{1}'.format(os.environ['DB_URL'], os.environ['DB_USER']), data='"{0}"'.format(os.environ['DB_PASSWORD'])) resp.raise_for_status() @classmethod def tearDownClass(cls): """ If necessary, clean up CouchDB instance once all tests are complete. """ if (os.environ.get('RUN_CLOUDANT_TESTS') is None and os.environ.get('DB_USER_CREATED') is not None): resp = requests.delete( '{0}://{1}:{2}@{3}/_config/admins/{4}'.format( os.environ['DB_URL'].split('://', 1)[0], os.environ['DB_USER'], os.environ['DB_PASSWORD'], os.environ['DB_URL'].split('://', 1)[1], os.environ['DB_USER'])) del os.environ['DB_USER_CREATED'] del os.environ['DB_USER'] resp.raise_for_status() def setUp(self): """ Set up test attributes for unit tests targeting a database """ self.set_up_client() def set_up_client(self, auto_connect=False, auto_renew=False, encoder=None, timeout=(30, 300)): self.user = os.environ.get('DB_USER', None) self.pwd = os.environ.get('DB_PASSWORD', None) self.use_cookie_auth = True if os.environ.get('RUN_CLOUDANT_TESTS') is None: self.url = os.environ['DB_URL'] admin_party = False if os.environ.get('ADMIN_PARTY') == 'true': admin_party = True self.use_cookie_auth = False # construct Cloudant client (using admin party mode) self.client = CouchDB(self.user, self.pwd, admin_party, url=self.url, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout) else: self.account = os.environ.get('CLOUDANT_ACCOUNT') self.url = os.environ.get( 'DB_URL', 'https://{0}.cloudant.com'.format(self.account)) if os.environ.get('RUN_BASIC_AUTH_TESTS'): self.use_cookie_auth = False # construct Cloudant client (using basic access authentication) self.client = Cloudant( self.user, self.pwd, url=self.url, x_cloudant_user=self.account, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout, use_basic_auth=True, ) elif os.environ.get('IAM_API_KEY'): self.use_cookie_auth = False # construct Cloudant client (using IAM authentication) self.client = Cloudant( None, # username is not required os.environ.get('IAM_API_KEY'), url=self.url, x_cloudant_user=self.account, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout, use_iam=True, ) else: # construct Cloudant client (using cookie authentication) self.client = Cloudant(self.user, self.pwd, url=self.url, x_cloudant_user=self.account, connect=auto_connect, auto_renew=auto_renew, encoder=encoder, timeout=timeout) def tearDown(self): """ Ensure the client is new for each test """ del self.client def db_set_up(self): """ Set up test attributes for Database tests """ self.client.connect() self.test_dbname = self.dbname() self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname) self.db.create() def db_tear_down(self): """ Reset test attributes for each test """ self.db.delete() self.client.disconnect() del self.test_dbname del self.db def dbname(self, database_name='db'): return '{0}-{1}-{2}'.format(database_name, self._testMethodName, unicode_(uuid.uuid4())) def populate_db_with_documents(self, doc_count=100, **kwargs): off_set = kwargs.get('off_set', 0) docs = [{ '_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i } for i in range(off_set, off_set + doc_count)] return self.db.bulk_docs(docs) def create_views(self): """ Create a design document with views for use with tests. """ self.ddoc = DesignDocument(self.db, 'ddoc001') self.ddoc.add_view('view001', 'function (doc) {\n emit(doc._id, 1);\n}') self.ddoc.add_view('view002', 'function (doc) {\n emit(doc._id, 1);\n}', '_count') self.ddoc.add_view( 'view003', 'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}') self.ddoc.add_view( 'view004', 'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}', '_count') self.ddoc.add_view( 'view005', 'function (doc) {\n emit([doc.name, doc.age], 1);\n}') self.ddoc.add_view( 'view006', 'function (doc) {\n emit([doc.name, doc.age], 1);\n}', '_count') self.ddoc.save() self.view001 = self.ddoc.get_view('view001') self.view002 = self.ddoc.get_view('view002') self.view003 = self.ddoc.get_view('view003') self.view004 = self.ddoc.get_view('view004') self.view005 = self.ddoc.get_view('view005') self.view006 = self.ddoc.get_view('view006') def create_search_index(self): """ Create a design document with search indexes for use with search query tests. """ self.search_ddoc = DesignDocument(self.db, 'searchddoc001') self.search_ddoc['indexes'] = { 'searchindex001': { 'index': 'function (doc) {\n index("default", doc._id); \n ' 'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} ' 'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} ' } } self.search_ddoc.save() def load_security_document_data(self): """ Create a security document in the specified database and assign attributes to be used during unit tests """ self.sdoc = { 'admins': { 'names': ['foo'], 'roles': ['admins'] }, 'members': { 'names': ['foo1', 'foo2'], 'roles': ['developers'] } } self.mod_sdoc = { 'admins': { 'names': ['bar'], 'roles': ['admins'] }, 'members': { 'names': ['bar1', 'bar2'], 'roles': ['developers'] } } if os.environ.get('RUN_CLOUDANT_TESTS') is not None: self.sdoc = { 'cloudant': { 'foo1': ['_reader', '_writer'], 'foo2': ['_reader'] } } self.mod_sdoc = { 'cloudant': { 'bar1': ['_reader', '_writer'], 'bar2': ['_reader'] } } resp = self.client.r_session.put( '/'.join([self.db.database_url, '_security']), data=json.dumps(self.sdoc), headers={'Content-Type': 'application/json'}) self.assertEqual(resp.status_code, 200)
client = CouchDB("admin", "password", url='http://172.26.132.48:5984', connect=True) session = client.session() except Exception as e: print('Connection unsuccessful') sys.exit() try: symptoms_db = client['symptoms'] except: symptoms_db = client.create_database('symptoms') ddoc_symptoms = DesignDocument(symptoms_db, 'symptoms') ddoc_symptoms.add_view( 'symptoms_location', 'function (doc) {\n for(var i in doc.symptoms){\n if(doc.symptoms[i]=="cough"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fever"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="throat"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fatigue"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="breathing"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="headache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="bodyache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="stloss"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="vomit"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="diarrhoea"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}}}', '_count') view = View(ddoc_symptoms, 'symptoms_location') #Create a mapreduce function to group the documents by symptoms and location ddoc_symptoms.save() #Connect to covidsymptom database in the couchDB covidsymptom_db = client['tweet-covid-symptom'] #CovidSafe Database and create views #Create a covidsafe designdocument for views #Create a mapreduce function to group the documents by sentiment and location covidsafe_db = client['tweet-covid-covidsafe'] covidsafe_ddoc = DesignDocument(covidsafe_db, 'covidsafe') covidsafe_ddoc.add_view('sentiment_location',
#from cloudant.query import Query from cloudant.result import Result, ResultByKey from cloudant.view import View #from cloudant.database import CouchDatabase import json #connect couchDB client = CouchDB("admin", "password", url='http://172.26.131.173:5984', connect=True) session = client.session() #print('Username: {0}'.format(session['userCtx']['name'])) #print('Databases: {0}'.format(client.all_dbs())) #connect database in the couchDB db = client['symptoms'] #Create a designdocument for views ddoc = DesignDocument(db, 'symptoms') ddoc.add_view( 'symptoms_location', 'function (doc) {\n for(var i in doc.symptoms){\n if(doc.symptoms[i]=="cough"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fever"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="throat"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fatigue"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="breathing"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="headache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="bodyache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="stloss"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="vomit"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="diarrhoea"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}}}', '_count') view = View(ddoc, 'symptoms_location') #Create a mapreduce function to group the documents by symptoms and location ddoc.save()