Ejemplo n.º 1
0
class CouchbaseClient(object):

  # arguments parsed from the provided url
  host = ""
  bucket_name = ""
  password = ""

  # couchbase client
  cb = None

  # bucket object
  bucket = None

  # logger instance
  log = None

  meta_prefix = ":"

  def __init__(self, host, bucket_auth="default", logger=None):
    """
    Init and connect to a couchbase database.

    Args

    host <string> hostname (can include port separated by :)

    Keyword arguments
    
    bucket_auth <string> bucket to load with possibility of also providing a password delimited 
    by ":" - defaults to "default"
    logger <logger> python logger instance to log debug and info messages
    """

    try:

      # parse and store url information
      bucket_auth = bucket_auth.split(":")
      self.host = host 
      self.bucket_name = bucket_auth[0]
      if len(bucket_auth) > 1:
        self.password = bucket_auth[1]
      self.log = logger

      # attempt to connect to database
      self.cb = Couchbase(self.host, self.bucket_name, self.password)

      # get bucket
      self.dbg("Setting bucket: %s" % self.bucket_name)
      self.bucket = self.cb.bucket(self.bucket_name)


    except:
      raise


  #############################################################################

  def dbg(self, msg):
    
    str = "Couchbase(%s/%s): %s" % (self.host, self.bucket_name, msg)
    if self.log:
      self.log.debug(str)
      print str
    else:
      print str

  #############################################################################

  def unset(self, key):
    """
    Delete object indexed by <key>
    """

    try:
      try:
        self.bucket.delete(key)
      except couchbase.exception.MemcachedError, inst: 
        if str(inst) == "Memcached error #1:  Not found":
          # for some reason the py cb client raises an error when
          # a key isnt found, instead we just want a none value.
          return
        else:
          raise
      except:
        raise
    except:
class CouchbaseTest(Base):
    @nottest
    def setup_cb(self):
        self.cb = Couchbase(self.host + ':' + self.port,
                            self.username, self.password)

    @attr(cbv="1.0.0")
    def test_couchbase_object_construction(self):
        cb = Couchbase(self.host + ':' + self.port, self.username,
                       self.password)
        self.assertIsInstance(cb.servers, types.ListType)

    @attr(cbv="1.0.0")
    def test_couchbase_object_construction_without_port(self):
        if self.port != "8091":
            raise SkipTest
        cb = Couchbase(self.host, self.username, self.password)
        self.assertIsInstance(cb.servers, types.ListType)

    @attr(cbv="1.0.0")
    def test_vbucketawarecouchbaseclient_object_construction(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            # Trigger a warning.
            cb = VBucketAwareCouchbaseClient("http://" + self.host + ':'
                                             + self.port + "/pools/default",
                                             self.bucket_name, "")
            self.assertIsInstance(cb.servers, types.ListType)
            # Verify some things
            self.assertTrue(len(w) == 1)
            self.assertTrue("deprecated" in str(w[-1].message))

    @attr(cbv="1.0.0")
    def test_bucket(self):
        self.setup_cb()
        self.assertIsInstance(self.cb.bucket(self.bucket_name), Bucket)

    @attr(cbv="1.0.0")
    def test_buckets(self):
        self.setup_cb()
        buckets = self.cb.buckets()
        self.assertIsInstance(buckets, types.ListType)
        self.assertIsInstance(buckets[0], Bucket)

    @attr(cbv="1.0.0")
    def test_create(self):
        self.setup_cb()
        bucket_name = str(uuid.uuid4())
        bucket = self.cb.create(bucket_name)
        self.assertIsInstance(bucket, Bucket)
        exists = [b for b in self.cb.buckets() if b.name == bucket_name]
        self.assertTrue(len(exists))
        self.cb.delete(bucket_name)

    @attr(cbv="1.0.0")
    def test_delete(self):
        self.setup_cb()
        bucket_name = str(uuid.uuid4())
        self.cb.create(bucket_name)
        self.assertIsInstance(self.cb[bucket_name], Bucket)
        self.cb.delete(bucket_name)
        self.assertNotIn(bucket_name, self.cb)
Ejemplo n.º 3
0
class CouchbaseClient(object):

    # arguments parsed from the provided url
    host = ""
    bucket_name = ""
    password = ""

    # couchbase client
    cb = None

    # bucket object
    bucket = None

    # logger instance
    log = None

    meta_prefix = ":"

    def __init__(self, host, bucket_auth="default", logger=None):
        """
    Init and connect to a couchbase database.

    Args

    host <string> hostname (can include port separated by :)

    Keyword arguments
    
    bucket_auth <string> bucket to load with possibility of also providing a password delimited 
    by ":" - defaults to "default"
    logger <logger> python logger instance to log debug and info messages
    """

        try:

            # parse and store url information
            bucket_auth = bucket_auth.split(":")
            self.host = host
            self.bucket_name = bucket_auth[0]
            if len(bucket_auth) > 1:
                self.password = bucket_auth[1]
            self.log = logger

            # attempt to connect to database
            self.cb = Couchbase(self.host, self.bucket_name, self.password)

            # get bucket
            self.dbg("Setting bucket: %s" % self.bucket_name)
            self.bucket = self.cb.bucket(self.bucket_name)

        except:
            raise

    #############################################################################

    def dbg(self, msg):

        str = "Couchbase(%s/%s): %s" % (self.host, self.bucket_name, msg)
        if self.log:
            self.log.debug(str)
            print str
        else:
            print str

    #############################################################################

    def unset(self, key):
        """
    Delete object indexed by <key>
    """

        try:
            try:
                self.bucket.delete(key)
            except couchbase.exception.MemcachedError, inst:
                if str(inst) == "Memcached error #1:  Not found":
                    # for some reason the py cb client raises an error when
                    # a key isnt found, instead we just want a none value.
                    return
                else:
                    raise
            except:
                raise
        except:
class CouchbaseTest(Base):
    @nottest
    def setup_cb(self):
        self.cb = Couchbase(self.host + ':' + self.port, self.username,
                            self.password)

    @attr(cbv="1.0.0")
    def test_couchbase_object_construction(self):
        cb = Couchbase(self.host + ':' + self.port, self.username,
                       self.password)
        self.assertIsInstance(cb.servers, types.ListType)

    @attr(cbv="1.0.0")
    def test_couchbase_object_construction_without_port(self):
        if self.port != "8091":
            raise SkipTest
        cb = Couchbase(self.host, self.username, self.password)
        self.assertIsInstance(cb.servers, types.ListType)

    @attr(cbv="1.0.0")
    def test_vbucketawarecouchbaseclient_object_construction(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            # Trigger a warning.
            cb = VBucketAwareCouchbaseClient(
                "http://" + self.host + ':' + self.port + "/pools/default",
                self.bucket_name, "")
            self.assertIsInstance(cb.servers, types.ListType)
            # Verify some things
            self.assertTrue(len(w) == 1)
            self.assertTrue("deprecated" in str(w[-1].message))

    @attr(cbv="1.0.0")
    def test_bucket(self):
        self.setup_cb()
        self.assertIsInstance(self.cb.bucket(self.bucket_name), Bucket)

    @attr(cbv="1.0.0")
    def test_buckets(self):
        self.setup_cb()
        buckets = self.cb.buckets()
        self.assertIsInstance(buckets, types.ListType)
        self.assertIsInstance(buckets[0], Bucket)

    @attr(cbv="1.0.0")
    def test_create(self):
        self.setup_cb()
        bucket_name = str(uuid.uuid4())
        bucket = self.cb.create(bucket_name)
        self.assertIsInstance(bucket, Bucket)
        exists = [b for b in self.cb.buckets() if b.name == bucket_name]
        self.assertTrue(len(exists))
        self.cb.delete(bucket_name)

    @attr(cbv="1.0.0")
    def test_delete(self):
        self.setup_cb()
        bucket_name = str(uuid.uuid4())
        self.cb.create(bucket_name)
        self.assertIsInstance(self.cb[bucket_name], Bucket)
        self.cb.delete(bucket_name)
        self.assertNotIn(bucket_name, self.cb)