当前位置: 首页>>代码示例>>Python>>正文


Python Server.create_db方法代码示例

本文整理汇总了Python中couchdbkit.Server.create_db方法的典型用法代码示例。如果您正苦于以下问题:Python Server.create_db方法的具体用法?Python Server.create_db怎么用?Python Server.create_db使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在couchdbkit.Server的用法示例。


在下文中一共展示了Server.create_db方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: setUp

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
 def setUp(self):
     self.settings = {
         "couchdb_uri": "http://admin:[email protected]:5984",
         "couchdb_db": "desk_tester",
     }
     s = Server(self.settings["couchdb_uri"])
     self.s = s
     s.create_db(self.settings['couchdb_db'])
     self.up = CouchdbUploader(path=os.path.dirname(__file__), auth=('admin', 'admin'), **self.settings)
开发者ID:yvess,项目名称:desk,代码行数:11,代码来源:test_utils.py

示例2: CouchDBServer

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class CouchDBServer(object):
    def __init__(self):
        self.__get_server_uri()
        self.__authenticate()
        self.__connect()

    def __get_server_uri(self):
        couchdb_port = config.couchdb.port if config.couchdb.protocol == 'http' else config.couchdb.ssl_port
        self.__couchdb_uri = "%s://%s:%s" % (config.couchdb.protocol, config.couchdb.host, couchdb_port)

    def __authenticate(self):
        user, passwd = config.couchdb.user, config.couchdb.password
        if all((user, passwd)):
            auth = restkit.BasicAuth(user, passwd)
            self.__auth_resource = CouchdbResource(filters=[auth])
        else:
            self.__auth_resource = None

    def __connect(self):
        self.__server = Server(uri=self.__couchdb_uri, resource_instance=self.__auth_resource)

    def list_workspaces(self):
        return filter(is_usable_workspace, self.__server.all_dbs())

    def get_workspace_handler(self, ws_name):
        return self.__server.get_db(ws_name)

    def get_or_create_db(self, ws_name):
        return self.__server.get_or_create_db(ws_name)

    def create_db(self, ws_name):
        return self.__server.create_db(ws_name)

    def delete_db(self, ws_name):
        return self.__server.delete_db(ws_name)
开发者ID:MrMugiwara,项目名称:faraday,代码行数:37,代码来源:couchdb.py

示例3: create_db

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
def create_db():
    """create the db if it don't exist"""
    from couchdbkit import Server
    server = Server(settings.SERVER_URI)
    try:
        db = server.create_db(settings.DATABASE_NAME)
    except:
        pass
开发者ID:haxwithaxe,项目名称:couchit,代码行数:10,代码来源:__init__.py

示例4: setUp

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
    def setUp(self):
        self.db_conf = {
            "couchdb_uri": "http://admin:[email protected]:5984",
            "couchdb_db": "desk_tester",
        }
        self.conf = {
            "powerdns_backend": "sqlite",
            "powerdns_db": "/var/services/data/powerdns/pdns_dnsa.test.sqlite3",
            "powerdns_name": "dnsa.test",
            "powerdns_primary": "dnsa.test",
            "worker_is_foreman": True,
        }
        self.conf.update(self.db_conf)
        s = Server(self.db_conf["couchdb_uri"])
        self.s = s
        s.create_db(self.db_conf['couchdb_db'])
        self.db = self.s.get_db(self.db_conf["couchdb_db"])
        self.couch_up = CouchdbUploader(
            path=os.path.dirname(__file__),
            auth=('admin', 'admin'), **self.db_conf
        )
        status_code = self.couch_up.put(
            data="@fixtures/couchdb-design.json",
            doc_id="_design/{couchdb_db}"
        )
        if not status_code == 201:
            s.delete_db(self.db_conf["couchdb_db"])

        worker_id = "worker-foreman"
        d = {
            "_id": worker_id, "type": "worker", "hostname": "foreman",
            "provides": {
                "domain": [{"backend": "powerdns", "name": "dnsa.test"}]
            }
        }
        self.assertTrue(
            self.couch_up.put(data=json.dumps(d), doc_id=worker_id) == 201
        )
        self.assertTrue(
            self.couch_up.put(data="@fixtures/couchdb-template-dns.json",
                        doc_id="template-email") == 201
        )
        self.assertTrue(
            self.couch_up.put(data="@fixtures/couchdb-map-ips.json",
                        doc_id="map-ips") == 201
        )
开发者ID:yvess,项目名称:desk,代码行数:48,代码来源:test_worker.py

示例5: Couch

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class Couch():
    def __init__(self):
        self.server = Server()
        self.db = self.server.get_or_create_db('test')
        if self.db.info()['doc_count'] > 0:
            self.server.delete_db('test')
            self.db = self.server.create_db('test')

    def populate(self):
        things = [
            {"name": "Vishnu"},
            {"name": "Lakshmi"},
            {"name": "Ganesha"},
            {"name": "Krishna"},
            {"name": "Murugan"}
        ]
        self.db.save_docs(things)

    def count(self):
        return self.db.all_docs().count()
开发者ID:OneRedOak,项目名称:sample-python-couchdb-1,代码行数:22,代码来源:couch.py

示例6: CouchDbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class CouchDbManager(AbstractPersistenceManager):
    """
    This is a couchdb manager for the workspace,
    it will load from the couchdb databases
    """
    def __init__(self, uri):
        super(CouchDbManager, self).__init__()
        getLogger(self).debug(
            "Initializing CouchDBManager for url [%s]" % uri)
        self._lostConnection = False
        self.__uri = uri
        self.__serv = NoConectionServer()
        self._available = False
        try:
            if uri is not None:
                self.testCouchUrl(uri)
                url = urlparse(uri)
                getLogger(self).debug(
                    "Setting user,pass %s %s" % (url.username, url.password))
                self.__serv = Server(uri=uri)
                self.__serv.resource_class.credentials = (url.username, url.password)
                self._available = True
                self.pushReports()
                self._loadDbs()
        except:
            getLogger(self).warn("No route to couchdb server on: %s" % uri)
            getLogger(self).debug(traceback.format_exc())

    #@trap_timeout
    def _create(self, name):
        db = self.__serv.create_db(name.lower())
        return CouchDbConnector(db)

    #@trap_timeout
    def _delete(self, name):
        self.__serv.delete_db(name)

    #@trap_timeout
    def _loadDbs(self):
        conditions = lambda x: not x.startswith("_") and x != 'reports'
        for dbname in filter(conditions, self.__serv.all_dbs()):
            if dbname not in self.dbs.keys():
                getLogger(self).debug(
                    "Asking for dbname[%s], registering for lazy initialization" % dbname)
                self.dbs[dbname] = lambda x: self._loadDb(x)

    def _loadDb(self, dbname):
        db = self.__serv.get_db(dbname)
        seq = db.info()['update_seq']
        self.dbs[dbname] = CouchDbConnector(db, seq_num=seq) 
        return self.dbs[dbname]


    #@trap_timeout
    def pushReports(self):
        vmanager = ViewsManager()
        reports = os.path.join(os.getcwd(), "views", "reports")
        workspace = self.__serv.get_or_create_db("reports")
        vmanager.addView(reports, workspace)
        return self.__uri + "/reports/_design/reports/index.html"

    def lostConnectionResolv(self):
        self._lostConnection = True
        self.__dbs.clear()
        self.__serv = NoConectionServer()

    def reconnect(self):
        ret_val = False
        ur = self.__uri
        if CouchDbManager.testCouch(ur):
            self.__serv = Server(uri = ur)
            self.__dbs.clear()
            self._lostConnection = False
            ret_val = True

        return ret_val

    @staticmethod
    def testCouch(uri):
        if uri is not None:
            host, port = None, None
            try:
                import socket
                url = urlparse(uri)
                proto = url.scheme
                host = url.hostname
                port = url.port

                port = port if port else socket.getservbyname(proto)
                s = socket.socket()
                s.settimeout(1)
                s.connect((host, int(port)))
            except:
                return False
            #getLogger(CouchdbManager).info("Connecting Couch to: %s:%s" % (host, port))
            return True

    def testCouchUrl(self, uri):
        if uri is not None:
            url = urlparse(uri)
#.........这里部分代码省略.........
开发者ID:Gr3yR0n1n,项目名称:faraday,代码行数:103,代码来源:persistence_managers.py

示例7: CouchdbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class CouchdbManager(PersistenceManager):
    """ This is a couchdb manager for the workspace, it will load from the 
    couchdb databases"""
    def __init__(self, uri):
        self._last_seq_ack = 0
        model.api.log("Initializing CouchDBManager for url [%s]" % uri)
        self._lostConnection = False
        self.__uri = uri
        self.__dbs = {} 
        self.__seq_nums = {}
        self.__serv = NoConectionServer()
        self.mutex = threading.Lock()
        self._available = False
        try:
            self.testCouchUrl(uri)
            self.__serv = Server(uri = uri)
            self._available = True
        except:
            model.api.log("No route to couchdb server on: %s" % uri)

    def isAvailable(self):
        return self._available

    def lostConnectionResolv(self): 
        self._lostConnection = True
        self.__dbs.clear()
        self.__serv = NoConectionServer()

    def reconnect(self):
        ret_val = False
        ur = self.__uri
        if CouchdbManager.testCouch(ur):
            self.__serv = Server(uri = ur)
            self.__dbs.clear()
            self._lostConnection = False
            ret_val = True

        return ret_val



    @staticmethod
    def testCouch(uri):
        host, port = None, None
        try:
            import socket
            proto, netloc, _, _, _ = urlsplit(uri)
            host, port = splitport(netloc)
            port = port if port else socket.getservbyname(proto)
            s = socket.socket()
            s.settimeout(1)
            s.connect((host, int(port)))
        except:
            return False
        model.api.log("Connecting Couch to: %s:%s" % (host, port))
        return True



    def testCouchUrl(self, uri):
        _, netloc, _, _, _ = urlsplit(uri)
        host, port = splitport(netloc)
        self.test(host, int(port))

    def test(self, address, port):
        import socket
        s = socket.socket()
        s.settimeout(1)
        s.connect((address, port))


    @trap_timeout
    def getWorkspacesNames(self):
        return filter(lambda x: not x.startswith("_"), self.__serv.all_dbs())

    def workspaceExists(self, name):
        return name in self.getWorkspacesNames()


    @trap_timeout
    def addWorkspace(self, aWorkspace):
        self.__serv.create_db(aWorkspace.lower())
        return self.__getDb(aWorkspace)

    @trap_timeout
    def addDocument(self, aWorkspaceName, documentId, aDocument):
        self.incrementSeqNumber(aWorkspaceName)
        self.__getDb(aWorkspaceName)[documentId] = aDocument

    @trap_timeout
    def saveDocument(self, aWorkspaceName, aDocument):
        self.incrementSeqNumber(aWorkspaceName)
        model.api.log("Saving document in remote workspace %s" % aWorkspaceName)
        self.__getDb(aWorkspaceName).save_doc(aDocument, use_uuids = True, force_update = True)

    @trap_timeout
    def __getDb(self, aWorkspaceName): 
        aWorkspaceName = aWorkspaceName.lower()
        model.api.log("Getting workspace [%s]" % aWorkspaceName)
        workspacedb = self.__dbs.get(aWorkspaceName, self.__serv.get_db(aWorkspaceName))
#.........这里部分代码省略.........
开发者ID:Behk,项目名称:faraday,代码行数:103,代码来源:workspace.py

示例8: CouchdbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]

#.........这里部分代码省略.........
                s = socket.socket()
                s.settimeout(1)
                s.connect((host, int(port)))
            except:
                return False
            getLogger(CouchdbManager).info("Connecting Couch to: %s:%s" % (host, port))
            return True

    def testCouchUrl(self, uri):
        if uri is not None:
            url = urlparse(uri)
            proto = url.scheme
            host = url.hostname
            port = url.port
            self.test(host, int(port))

    def test(self, address, port):
        import socket
        s = socket.socket()
        s.settimeout(1)
        s.connect((address, port))


    @trap_timeout
    def getWorkspacesNames(self):
        return filter(lambda x: not x.startswith("_"), self.__serv.all_dbs())

    def workspaceExists(self, name):
        return name in self.getWorkspacesNames()


    @trap_timeout
    def addWorkspace(self, aWorkspace):
        self.__serv.create_db(aWorkspace.lower())
        return self._getDb(aWorkspace)

    @trap_timeout
    def addDocument(self, aWorkspaceName, documentId, aDocument):
        self._getDb(aWorkspaceName)
        self.incrementSeqNumber(aWorkspaceName)
        self._getDb(aWorkspaceName)[documentId] = aDocument

    @trap_timeout
    def saveDocument(self, aWorkspaceName, aDocument):
        self.incrementSeqNumber(aWorkspaceName)
        getLogger(self).debug("Saving document in remote workspace %s" % aWorkspaceName)
        return self._getDb(aWorkspaceName).save_doc(aDocument, use_uuids = True, force_update = True)

    def _getDb(self, aWorkspaceName):
        if not self.__dbs.has_key(aWorkspaceName):
            self.__getDb(aWorkspaceName)
        return self.__dbs.get(aWorkspaceName, None)

    @trap_timeout
    def __getDb(self, aWorkspaceName): 
        aWorkspaceName = aWorkspaceName.lower()
        getLogger(self).debug("Getting workspace [%s]" % aWorkspaceName)
        workspacedb = self.__dbs.get(aWorkspaceName, self.__serv.get_db(aWorkspaceName))
        if not self.__dbs.has_key(aWorkspaceName): 
            getLogger(self).debug("Asking couchdb for workspace [%s]" % aWorkspaceName)
            self.__dbs[aWorkspaceName] = workspacedb
            self.__seq_nums[aWorkspaceName] = workspacedb.info()['update_seq'] 


        return workspacedb
开发者ID:0x24bin,项目名称:BurpSuite,代码行数:69,代码来源:all.py

示例9: CouchDbManager

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class CouchDbManager(AbstractPersistenceManager):
    """
    This is a couchdb manager for the workspace,
    it will load from the couchdb databases
    """
    def __init__(self, uri, couch_exception_callback):
        super(CouchDbManager, self).__init__()
        getLogger(self).debug(
            "Initializing CouchDBManager for url [%s]" % uri)
        self._lostConnection = False
        self.__uri = uri
        self._available = False
        self.couch_exception_callback = couch_exception_callback
        test_couch_thread = threading.Thread(target=self.continuosly_check_connection)
        test_couch_thread.daemon = True
        test_couch_thread.start()
        try:
            if uri is not None:
                self.testCouchUrl(uri)
                url = urlparse(uri)
                getLogger(self).debug(
                    "Setting user,pass %s %s" % (url.username, url.password))
                self.__serv = Server(uri=uri)
                self.__serv.resource_class.credentials = (url.username, url.password)
                self._available = True
                self.pushReports()
                self._loadDbs()
        except:
            getLogger(self).warn("No route to couchdb server on: %s" % uri)
            getLogger(self).debug(traceback.format_exc())

    def continuosly_check_connection(self):
        """Intended to use on a separate thread. Call module-level
        function testCouch every second to see if response to the server_uri
        of the DB is still 200. Call the exception_callback if we can't access
        the server three times in a row.
        """
        tolerance = 0
        server_uri = self.__uri
        while True:
            time.sleep(1)
            test_was_successful = test_couch(server_uri)
            if test_was_successful:
                tolerance = 0
            else:
                tolerance += 1
                if tolerance == 3:
                    self.couch_exception_callback()
                    return False  # kill the thread if something went wrong

    def _create(self, name):
        db = self.__serv.create_db(name.lower())
        return CouchDbConnector(db)

    def _delete(self, name):
        self.__serv.delete_db(name)

    def _loadDbs(self):

        def conditions(database):
            begins_with_underscore = database.startswith("_")
            is_blacklisted = database in CONST_BLACKDBS
            return not begins_with_underscore and not is_blacklisted

        try:
            for dbname in filter(conditions, self.__serv.all_dbs()):
                if dbname not in self.dbs.keys():
                    getLogger(self).debug(
                        "Asking for dbname[%s], registering for lazy initialization" % dbname)
                    self.dbs[dbname] = lambda x: self._loadDb(x)
        except restkit.errors.RequestError as req_error:
            getLogger(self).error("Couldn't load databases. "
                                  "The connection to the CouchDB was probably lost. ")

    def _loadDb(self, dbname):
        db = self.__serv.get_db(dbname)
        seq = db.info()['update_seq']
        self.dbs[dbname] = CouchDbConnector(db, seq_num=seq)
        return self.dbs[dbname]

    def refreshDbs(self):
        """Refresh databases using inherited method. On exception, asume
        no databases are available.
        """
        try:
            return AbstractPersistenceManager.refreshDbs()
        except:
            return []

    def pushReports(self):
        vmanager = ViewsManager()
        reports = os.path.join(os.getcwd(), "views", "reports")
        try:
            workspace = self.__serv.get_or_create_db("reports")
            vmanager.addView(reports, workspace)
        except:
            getLogger(self).warn(
                "Reports database couldn't be uploaded. You need to be an admin to do it")
        return self.__uri + "/reports/_design/reports/index.html"

#.........这里部分代码省略.........
开发者ID:FreedomCoder,项目名称:faraday,代码行数:103,代码来源:persistence_managers.py

示例10: ClientDatabaseTestCase

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class ClientDatabaseTestCase(unittest.TestCase):
    def setUp(self):
        self.couchdb = CouchdbResource()
        self.Server = Server()

    def tearDown(self):
        try:
            del self.Server["couchdbkit_test"]
        except:
            pass

    def testCreateDatabase(self):
        db = self.Server.create_db("couchdbkit_test")
        self.assertIsInstance(db, Database)
        info = db.info()
        self.assertEqual(info["db_name"], "couchdbkit_test")
        del self.Server["couchdbkit_test"]

    def testDbFromUri(self):
        db = self.Server.create_db("couchdbkit_test")

        db1 = Database("http://127.0.0.1:5984/couchdbkit_test")
        self.assertIs(hasattr(db1, "dbname"), True)
        self.assertEqual(db1.dbname, "couchdbkit_test")
        info = db1.info()
        self.assertEqual(info["db_name"], "couchdbkit_test")

    def testCreateEmptyDoc(self):
        db = self.Server.create_db("couchdbkit_test")
        doc = {}
        db.save_doc(doc)
        del self.Server["couchdbkit_test"]
        self.assertIn("_id", doc)

    def testCreateDoc(self):
        db = self.Server.create_db("couchdbkit_test")
        # create doc without id
        doc = {"string": "test", "number": 4}
        db.save_doc(doc)
        self.assertTrue(db.doc_exist(doc["_id"]))
        # create doc with id
        doc1 = {"_id": "test", "string": "test", "number": 4}
        db.save_doc(doc1)
        self.assertTrue(db.doc_exist("test"))
        doc2 = {"string": "test", "number": 4}
        db["test2"] = doc2
        self.assertTrue(db.doc_exist("test2"))
        del self.Server["couchdbkit_test"]

        db = self.Server.create_db("couchdbkit/test")
        doc1 = {"_id": "test", "string": "test", "number": 4}
        db.save_doc(doc1)
        self.assertTrue(db.doc_exist("test"))
        del self.Server["couchdbkit/test"]

    def testUpdateDoc(self):
        db = self.Server.create_db("couchdbkit_test")
        doc = {"string": "test", "number": 4}
        db.save_doc(doc)
        doc.update({"number": 6})
        db.save_doc(doc)
        doc = db.get(doc["_id"])
        self.assertEqual(doc["number"], 6)
        del self.Server["couchdbkit_test"]

    def testDocWithSlashes(self):
        db = self.Server.create_db("couchdbkit_test")
        doc = {"_id": "a/b"}
        db.save_doc(doc)
        self.assertIn("a/b", db)

        doc = {"_id": "http://a"}
        db.save_doc(doc)
        self.assertIn("http://a", db)
        doc = db.get("http://a")
        self.assertIsNotNone(doc)

        def not_found():
            doc = db.get("http:%2F%2Fa")

        self.assertRaises(ResourceNotFound, not_found)

        self.assertEqual("http://a", doc.get("_id"))
        doc.get("_id")

        doc = {"_id": "http://b"}
        db.save_doc(doc)
        self.assertIn("http://b", db)

        doc = {"_id": "_design/a"}
        db.save_doc(doc)
        self.assertIn("_design/a", db)
        del self.Server["couchdbkit_test"]

    def testGetRev(self):
        db = self.Server.create_db("couchdbkit_test")
        doc = {}
        db.save_doc(doc)
        rev = db.get_rev(doc["_id"])
        self.assertEqual(doc["_rev"], rev)
#.........这里部分代码省略.........
开发者ID:georgemarshall,项目名称:couchdbkit,代码行数:103,代码来源:test_client.py

示例11: ClientViewTestCase

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class ClientViewTestCase(unittest.TestCase):
    def setUp(self):
        self.couchdb = CouchdbResource()
        self.Server = Server()

    def tearDown(self):
        try:
            del self.Server["couchdbkit_test"]
        except:
            pass

        try:
            self.Server.delete_db("couchdbkit_test2")
        except:
            pass

    def testView(self):
        db = self.Server.create_db("couchdbkit_test")
        # save 2 docs
        doc1 = {"_id": "test", "string": "test", "number": 4, "docType": "test"}
        db.save_doc(doc1)
        doc2 = {"_id": "test2", "string": "test", "number": 2, "docType": "test"}
        db.save_doc(doc2)

        design_doc = {
            "_id": "_design/test",
            "language": "javascript",
            "views": {
                "all": {
                    "map": """function(doc) { if (doc.docType == "test") { emit(doc._id, doc);
}}"""
                }
            },
        }
        db.save_doc(design_doc)

        doc3 = db.get("_design/test")
        self.assertIsNotNone(doc3)
        results = db.view("test/all")
        self.assertEqual(len(results), 2)
        del self.Server["couchdbkit_test"]

    def testAllDocs(self):
        db = self.Server.create_db("couchdbkit_test")
        # save 2 docs
        doc1 = {"_id": "test", "string": "test", "number": 4, "docType": "test"}
        db.save_doc(doc1)
        doc2 = {"_id": "test2", "string": "test", "number": 2, "docType": "test"}
        db.save_doc(doc2)

        self.assertEqual(db.view("_all_docs").count(), 2)
        self.assertEqual(db.view("_all_docs").all(), db.all_docs().all())

        del self.Server["couchdbkit_test"]

    def testCount(self):
        db = self.Server.create_db("couchdbkit_test")
        # save 2 docs
        doc1 = {"_id": "test", "string": "test", "number": 4, "docType": "test"}
        db.save_doc(doc1)
        doc2 = {"_id": "test2", "string": "test", "number": 2, "docType": "test"}
        db.save_doc(doc2)

        design_doc = {
            "_id": "_design/test",
            "language": "javascript",
            "views": {"all": {"map": """function(doc) { if (doc.docType == "test") { emit(doc._id, doc); }}"""}},
        }
        db.save_doc(design_doc)
        count = db.view("/test/all").count()
        self.assertEqual(count, 2)
        del self.Server["couchdbkit_test"]

    def testTemporaryView(self):
        db = self.Server.create_db("couchdbkit_test")
        # save 2 docs
        doc1 = {"_id": "test", "string": "test", "number": 4, "docType": "test"}
        db.save_doc(doc1)
        doc2 = {"_id": "test2", "string": "test", "number": 2, "docType": "test"}
        db.save_doc(doc2)

        design_doc = {
            "map": """function(doc) { if (doc.docType == "test") { emit(doc._id, doc);
}}"""
        }

        results = db.temp_view(design_doc)
        self.assertEqual(len(results), 2)
        del self.Server["couchdbkit_test"]

    def testView2(self):
        db = self.Server.create_db("couchdbkit_test")
        # save 2 docs
        doc1 = {"_id": "test1", "string": "test", "number": 4, "docType": "test"}
        db.save_doc(doc1)
        doc2 = {"_id": "test2", "string": "test", "number": 2, "docType": "test"}
        db.save_doc(doc2)
        doc3 = {"_id": "test3", "string": "test", "number": 2, "docType": "test2"}
        db.save_doc(doc3)
        design_doc = {
#.........这里部分代码省略.........
开发者ID:georgemarshall,项目名称:couchdbkit,代码行数:103,代码来源:test_client.py

示例12: ClientServerTestCase

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class ClientServerTestCase(unittest.TestCase):
    def setUp(self):
        self.couchdb = CouchdbResource()
        self.Server = Server()

    def tearDown(self):
        try:
            del self.Server["couchdbkit_test"]
            del self.Server["couchdbkit/test"]
        except:
            pass

    def testGetInfo(self):
        info = self.Server.info()
        self.assertIn("version", info)

    def testCreateDb(self):
        res = self.Server.create_db("couchdbkit_test")
        self.assertIsInstance(res, Database)
        all_dbs = self.Server.all_dbs()
        self.assertIn("couchdbkit_test", all_dbs)
        del self.Server["couchdbkit_test"]
        res = self.Server.create_db("couchdbkit/test")
        self.assertIn("couchdbkit/test", self.Server.all_dbs())
        del self.Server["couchdbkit/test"]

    def testGetOrCreateDb(self):
        # create the database
        gocdb = self.Server.get_or_create_db("get_or_create_db")
        self.assertEqual(gocdb.dbname, "get_or_create_db")
        self.assertIn("get_or_create_db", self.Server)
        self.Server.delete_db("get_or_create_db")
        # get the database (already created)
        self.assertNotIn("get_or_create_db", self.Server)
        db = self.Server.create_db("get_or_create_db")
        self.assertIn("get_or_create_db", self.Server)
        gocdb = self.Server.get_or_create_db("get_or_create_db")
        self.assertEqual(db.dbname, gocdb.dbname)
        self.Server.delete_db("get_or_create_db")

    def testCreateInvalidDbName(self):
        def create_invalid():
            res = self.Server.create_db("123ab")

        self.assertRaises(ValueError, create_invalid)

    def testServerLen(self):
        res = self.Server.create_db("couchdbkit_test")
        self.assertGreaterEqual(len(self.Server), 1)
        self.assertTrue(self.Server)
        del self.Server["couchdbkit_test"]

    def testServerContain(self):
        res = self.Server.create_db("couchdbkit_test")
        self.assertIn("couchdbkit_test", self.Server)
        del self.Server["couchdbkit_test"]

    def testGetUUIDS(self):
        uuid = self.Server.next_uuid()
        self.assertIsInstance(uuid, basestring)
        self.assertEqual(len(self.Server._uuids), 999)
        uuid2 = self.Server.next_uuid()
        self.assertNotEqual(uuid, uuid2)
        self.assertEqual(len(self.Server._uuids), 998)
开发者ID:georgemarshall,项目名称:couchdbkit,代码行数:66,代码来源:test_client.py

示例13: CouchjockTestCase

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class CouchjockTestCase(unittest2.TestCase):
    server_url = 'http://localhost:5984/'
    db_name = 'couchjock__test'

    schema = couchjock

    def setUp(self):
        self.server = Server(uri=self.server_url)
        self.db = self.server.create_db(self.db_name)

    def tearDown(self):
        self.server.delete_db(self.db_name)

    def test_save(self):
        class Foo(self.schema.Document):
            _db = self.db
            pass
        foo = Foo()
        foo.save()
        foo_id = foo._id
        self.assertIsNotNone(foo_id)
        foo2 = Foo.get(foo_id)
        self.assertEqual(foo2._id, foo_id)

    def test_simple_schema(self):
        class Foo(self.schema.Document):
            _db = self.db
            string = self.schema.StringProperty()
            boolean = self.schema.BooleanProperty(default=True)

        foo1 = Foo()
        foo1.save()
        foo1_id = foo1._id
        foo1_rev = foo1._rev
        self.assertIsNotNone(foo1_id)
        self.assertIsNotNone(foo1_rev)
        foo1 = Foo.get(foo1_id)
        self.assertEqual(foo1.to_json(), {
            'doc_type': 'Foo',
            '_id': foo1_id,
            '_rev': foo1_rev,
            'string': None,
            'boolean': True,
        })

        foo1._doc.update({'boolean': False})
        self.assertEqual(foo1.boolean, False)

    def _individual_save(self, docs):
        for doc in docs:
            doc.save()

    def _bulk_save(self, docs):
        self.db.bulk_save(docs)

    def _test_simple_view(self, save_fn):
        class Foo(self.schema.Document):
            _db = self.db
            string = self.schema.StringProperty()
        foo1 = Foo(string='fun one')
        foo2 = Foo(string='poop')
        save_fn([foo1, foo2])

        self.assertEqual(
            map(lambda x: x.to_json(), Foo.view('_all_docs', include_docs=True).all()),
            map(lambda x: x.to_json(), sorted([foo1, foo2], key=attrgetter('_id')))
        )

    def test_simple_view(self):
        self._test_simple_view(self._individual_save)

    def test_bulk_save(self):
        self._test_simple_view(self._bulk_save)
开发者ID:dannyroberts,项目名称:couchjock,代码行数:75,代码来源:test.py

示例14: print_red

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
config = ConfigParser.ConfigParser()
config.read('config.ini')

#Config
ConsumerKey = config.get('Twitter', 'ConsumerKey', 0)
ConsumerSecret = config.get('Twitter', 'ConsumerSecret', 0)
AccessToken = config.get('Twitter', 'AccessToken', 0)
AccessSecret = config.get('Twitter', 'AccessSecret', 0)
TweetDbName = config.get('Twitter', 'DbName', 0)
ReweetDbName = config.get('Twitter', 'ReweetDbName', 0)
Filter = config.get('Twitter', 'Filter', 0)
UserIds = config.get('Twitter', 'UserIds', 0)
Languages = config.get('Twitter', 'Languages', 0)


Tweetdb = CouchDbServer.create_db(TweetDbName)
ReTweetdb = CouchDbServer.create_db(ReweetDbName)

#Print Red
def print_red(text): print ("\033[91m {}\033[00m" .format(text))

#Print Green
def print_green(text): print ("\033[92m {}\033[00m" .format(text))

#PRint Yellow
def print_yellow(text): print ("\033[93m {}\033[00m" .format(text))

class listener(StreamListener):
    
    def on_data(self, data):
        #Load Json
开发者ID:stuartwilsondev,项目名称:twitter-connector.py,代码行数:33,代码来源:twitter_stream.py

示例15: DbConnectorCouchTestSuite

# 需要导入模块: from couchdbkit import Server [as 别名]
# 或者: from couchdbkit.Server import create_db [as 别名]
class DbConnectorCouchTestSuite(unittest.TestCase):
    def setUp(self):
        self.couch_srv = Server(uri=CONF.getCouchURI())
        self.db_name = new_random_workspace_name()
        self.db = self.couch_srv.create_db(self.db_name)

    def tearDown(self):
        self.couch_srv.delete_db(self.db_name)
        time.sleep(3)

    def test_save_Document(self):
        couchConnector = CouchDbConnector(self.db)
        doc = {"_id": "123", "data": "some data"}
        couchConnector.saveDocument(doc)

        doc_from_db = self.db.get("123")

        self.assertNotEquals(doc_from_db, None, "Document should be retrieved")

        self.assertEquals(doc_from_db.get("data"), "some data", "Data retrieved should be the same as data saved")

    def test_get_Document(self):
        couchConnector = CouchDbConnector(self.db)
        doc = {"_id": "123", "data": "some data"}
        couchConnector.saveDocument(doc)

        doc_retrieved = couchConnector.getDocument("123")

        self.assertNotEquals(doc_retrieved, None, "Document should be retrieved")

        self.assertEquals(doc_retrieved.get("data"), "some data", "Data retrieved should be the same as data saved")

    def test_remove_Document(self):
        couchConnector = CouchDbConnector(self.db)
        doc = {"_id": "123", "data": "some data"}
        couchConnector.saveDocument(doc)

        couchConnector.remove("123")

        try:
            doc_from_db = self.db.get("123")
        except ResourceNotFound:
            doc_from_db = None

        self.assertEquals(doc_from_db, None, "Document should be None")

    def test_get_by_parent_and_type(self):
        couchConnector = CouchDbConnector(self.db)
        doc = {"_id": "123", "type": "father", "parent": None}
        couchConnector.saveDocument(doc)

        doc = {"_id": "456", "type": "child", "parent": "123"}
        couchConnector.saveDocument(doc)

        doc = {"_id": "789", "type": "child", "parent": "123"}
        couchConnector.saveDocument(doc)

        ids = couchConnector.getDocsByFilter(parentId="123", type="child")

        self.assertEquals(len(ids), 2, "There should be two 'childs' with parent '123'")

        self.assertIn("456", ids, "Child '456' should be in the list of childs")

        self.assertIn("789", ids, "Child '789' should be in the list of childs")

        ids = couchConnector.getDocsByFilter(parentId="123", type="son")

        self.assertEquals(len(ids), 0, "There shouldn't be any 'son' with parent '123'")

        ids = couchConnector.getDocsByFilter(parentId="456", type="child")

        self.assertEquals(len(ids), 0, "There shouldn't be any 'child' with parent '456'")
开发者ID:followboy1999,项目名称:faraday,代码行数:74,代码来源:db_connector.py


注:本文中的couchdbkit.Server.create_db方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。