class SearchHandler(BaseHandler): def initialize(self): self.couch = CouchBase(couch_conf['base_url']) @asynchronous @gen.coroutine def get(self, database): """ /api/v1/<database>/search?p=<project_id> 返回项目和服务信息,服务按类别以数组的形式返回,格式如下 {"project": {"_id": "62f99ca284fb4b028ed2518364378fb1", "name": "tlw"}, "zookeeper": [ {"name": "zookeeper", "ip": "172.16.200.100", "port": "2181" ...}, {"name": "zookeeper", "ip": "172.16.200.99", "port": "2181" ...} ], "tomcat" : [{...}, {...}] } """ project_id = self.get_argument("p") resp = yield self.couch.client.fetch( "{0}/_design/project/_list/search/search?include_docs=true&key=\"{1}\"".format( database, project_id ) ) body = json_decode(resp.body) project_doc = yield self.couch.get_doc(database, project_id) del project_doc['_rev'] del project_doc['services'] del project_doc['type'] body.update({"project": project_doc}) self.write(body) self.finish()
def initialize(self): self.couch = CouchBase(couch_conf['base_url'])
def setUp(self): super(TestCouchBase, self).setUp() # setup io_loop self.couch = CouchBase(io_loop=self.io_loop)
class TestCouchBase(AsyncTestCase): @classmethod def setUpClass(cls): cls.server = CouchServer() cls.database = 'couch-base_test' cls.server.create(cls.database) cls.doc_ids = ["test_id_01", "test_id_02", "test_id_03"] @classmethod def tearDownClass(cls): cls.server = CouchServer() cls.database = 'couch-base_test' cls.server.delete(cls.database) def setUp(self): super(TestCouchBase, self).setUp() # setup io_loop self.couch = CouchBase(io_loop=self.io_loop) @gen_test(timeout=3) def test_1_update_doc(self): for doc_id in self.doc_ids: doc = {"_id": doc_id} response = yield self.couch.update_doc(self.database, doc_id, doc) json = json_decode(response) self.assertEqual(json['ok'], True) @gen_test(timeout=3) def test_2_list_docs(self): doc_list = yield self.couch.list_ids(self.database) self.assertEqual(doc_list, self.doc_ids) @gen_test(timeout=3) def test_4_has_doc(self): response = yield self.couch.has_doc(self.database, "test_id_01") self.assertEqual(response, True) response = yield self.couch.has_doc(self.database, 'not exist') self.assertEqual(response, False) @gen_test(timeout=3) def test_5_get_doc(self): response = yield self.couch.get_doc(self.database, "test_id_01") self.assertEqual(response['_id'], "test_id_01") # save rev for test_6_get_doc_rev TestCouchBase.rev = response['_rev'] # error service id try: yield self.couch.get_doc(self.database, 'not exist') except ValueError as e: self.assertEqual(e.message, 'missing') # error database name try: yield self.couch.get_doc('foo', 'test_id_01') except ValueError as e: self.assertEqual(e.message, 'no_db_file') @gen_test(timeout=3) def test_6_get_doc_rev(self): rev = TestCouchBase.rev response = yield self.couch.get_doc_rev(self.database, "test_id_01") self.assertEqual(response, rev) @gen_test(timeout=3) def test_7_update_doc_field(self): response = yield self.couch.update_doc_field( self.database, "test_id_01", filed1="1.2.3.4", filed2=4567 ) r = json_decode(response) self.assertEqual(r['ok'], True) doc = yield self.couch.get_doc(self.database, "test_id_01") self.assertEqual(doc['filed1'], "1.2.3.4") self.assertEqual(doc['filed2'], 4567) @gen_test(timeout=3) def test_8_del_doc(self): for doc_id in self.doc_ids: response = yield self.couch.del_doc(self.database, doc_id) self.assertEqual(response, 'true') @gen_test(timeout=3) def test_9_del_doc_not_exist(self): try: yield self.couch.del_doc(self.database, 'not exist') except Exception as e: self.assertIsInstance(e, ValueError) self.assertEqual(e.message, 'Document {0} not Exist'.format('not exist'))