Permalink
Browse files

added test of other Readme example

Also made Couchbase class more accessible for imports:
  from couchbase import Couchbase
Updated the example code to use this, rather than the
deprecated Server class.

Change-Id: Ice336c8f823d5d40f5961aa8f07151ec73d65d99
Reviewed-on: http://review.couchbase.org/19085
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Reviewed-by: Pavel Paulau <pavel.paulau@gmail.com>
Tested-by: Benjamin Young <benjamin@couchbase.com>
Reviewed-by: Benjamin Young <benjamin@couchbase.com>
  • Loading branch information...
1 parent 3ba67f4 commit 18a341d3c0bc08cc288ff6f4d2474984643e09e0 @BigBlueHat BigBlueHat committed with Benjamin Young Jul 30, 2012
Showing with 82 additions and 5 deletions.
  1. +4 −4 README.md
  2. +1 −1 couchbase/__init__.py
  3. +77 −0 couchbase/tests/test_readme_examples.py
View
@@ -46,10 +46,10 @@ and then create a new bucket using the memcached and rest clients::
Example code that creates buckets and then does sets, gets and views using
the unified client::
- import couchbase
+ from couchbase import Couchbase
# connect to a couchbase server
- cb = couchbase.Server('localhost:8091',
+ cb = Couchbase('localhost:8091',
username='Administrator',
password='password')
@@ -61,12 +61,12 @@ the unified client::
# fetch a Bucket with subscript
default_bucket = cb['default']
- # set a value with subscript (equivilent to .set)
+ # set a value with subscript (nearly equivalent to .set)
default_bucket['key1'] = 'value1'
# fetch a bucket with a function
default_bucket2 = cb.bucket('default')
- # set a json value with subscript (equivilent to .set)
+ # set a json value with subscript (nearly equivalent to .set)
default_bucket2['key2'] = {'value':'value2','expiration':0,'flags':10}
# set a value with a function
@@ -1 +1 @@
-from couchbase.client import Server, Bucket
+from couchbase.client import Couchbase, Server, Bucket
@@ -42,3 +42,80 @@ def test_old_set_get_create_example(self):
self.assertTrue(RestHelper(rest).bucket_exists('newbucket'))
rest.delete_bucket('newbucket')
+
+ def test_old_unified_client_example(self):
+ from couchbase import Couchbase
+
+ # connect to a couchbase server
+ cb = Couchbase(self.host + ':' + self.port,
+ username=self.username,
+ password=self.password)
+
+ # create default bucket if it doesn't exist
+ #try:
+ # cb.create(self.bucket_name)
+ #except:
+ # pass
+
+ default_bucket = cb[self.bucket_name]
+ default_bucket['key1'] = 'value1'
+
+ default_bucket2 = cb.bucket(self.bucket_name)
+ default_bucket2['key2'] = {'value': 'value2', 'expiration': 0,
+ 'flags': 10}
+
+ default_bucket.set('key3', 0, 0, 'value3')
+
+ self.assertEqual(str(default_bucket.get('key1')[2]), 'value1')
+ self.assertEqual(str(default_bucket2.get('key2')[2]), 'value2')
+ self.assertEqual(str(default_bucket2['key3'][2]), 'value3')
+
+ # delete a bucket
+ #cb.delete(self.bucket_name)
+ #try:
+ # cb['default']
+ #except Exception as ex:
+ # print ex
+
+ # create a new bucket
+ try:
+ newbucket = cb.create('newbucket', ram_quota_mb=100, replica=1)
+ except:
+ newbucket = cb['newbucket']
+
+ # set a json document with a function
+ # this will translate $flags and $expiration to memcached protocol
+ # automatically generate the _id
+ doc_id = newbucket.save({'type': 'item',
+ 'value': 'json test',
+ '$flags': 25})
+ print doc_id + ' ' + str(newbucket[doc_id])
+ # use a provided _id
+ doc_id = newbucket.save({'_id': 'key4',
+ 'type': 'item',
+ 'value': 'json test',
+ '$flags': 25})
+ print doc_id + ' ' + str(newbucket[doc_id])
+
+ design = {
+ "_id": "_design/testing",
+ "language": "javascript",
+ "views": {
+ "all": {
+ "map": '''function (doc) {\n emit(doc, null);\n}'''
+ },
+ },
+ }
+ # save a design document
+ # right now with no _rev, we can only create, we can't update
+ try:
+ doc_id = newbucket.save(design)
+ except:
+ doc_id = "_design/testing"
+
+ if cb.couch_api_base:
+ rows = newbucket.view("_design/testing/_view/all")
+ for row in rows:
+ self.assertTrue(row is not None)
+
+ cb.delete('newbucket')

0 comments on commit 18a341d

Please sign in to comment.