Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

design_docs() now returns DesignDoc objects

These DesignDoc objects have (n)eq comparison functions
to simplify finding out if the design doc you made is
uploaded or not yet.

Change-Id: I1fe01ab76e6c1b5d92fc477b2d2ad9780c678208
Reviewed-on: http://review.couchbase.org/19888
Reviewed-by: Pavel Paulau <pavel.paulau@gmail.com>
Tested-by: Benjamin Young <benjamin@couchbase.com>
  • Loading branch information...
commit bba0851ce1c2b436f15964871023fd36c2a89fc3 1 parent 09f5b6c
BigBlueHat BigBlueHat authored BigBlueHat committed
Showing with 55 additions and 10 deletions.
  1. +44 −1 couchbase/client.py
  2. +11 −9 couchbase/tests/test_client.py
45 couchbase/client.py
View
@@ -333,6 +333,49 @@ def design_docs(self):
self.server.rest_password))
ddocs = []
for ddoc in r.json.get('rows'):
- ddocs.append({ddoc['doc']['meta']['id']: ddoc['doc']['json']})
+ ddocs.append(DesignDoc(ddoc['doc']['meta']['id'],
+ ddoc['doc']['json'], bucket=self))
return ddocs
+
+
+class DesignDoc(object):
+ """Object representation of a Couchbase Server Design Document--the thing
+ that holds the MapReduce Views.
+
+ This Object handles the core logic behind creating and updating views to
+ a specific design doc. It also handles copying/publishing a design doc into
+ (or out of) production."""
+ def __init__(self, name, ddoc=None, bucket=None):
+ assert isinstance(name, (str, unicode)), \
+ "name parameter must be of type string or unicode"
+ assert isinstance(ddoc, (str, unicode, dict)), \
+ "ddoc parameter must be of type string, unicode, or dictionary"
+
+ if name.startswith('_design/'):
+ name = name[8:]
+ self.name = name
+ self.bucket = bucket
+
+ if ddoc is None:
+ self.ddoc = {'type': 'javascript', 'views': {}}
+ elif isinstance(ddoc, unicode):
+ self.ddoc = json.loads(ddoc)
+ else:
+ self.ddoc = ddoc
+
+ def __str__(self):
+ """Return the name of the Design Doc when using print"""
+ return self.name
+
+ def __eq__(self, other):
+ """Compare name or "views" section of the Design Doc. This allows the
+ use of "for ddoc in" style syntax when used with
+ Bucket().design_docs()"""
+ if isinstance(other, str) and "{" not in other and "}" not in other:
+ return other == self.name
+ elif isinstance(other, dict):
+ return other['views'] == self.ddoc['views']
+
+ def __neq__(self, other):
+ return not self.__eq__(other)
20 couchbase/tests/test_client.py
View
@@ -248,20 +248,22 @@ def test_design_docs(self):
for i in range(0, 10):
self.client['doc' + str(i)] = {'name': 'doc' + str(i), 'num': i}
- design_doc = json.dumps({"views":
- {"testing":
- {"map":
- "function(doc) { emit(doc.name, doc.num); }"
- }
- }
- })
+ design_doc = {"views":
+ {"testing":
+ {"map":
+ "function(doc) { emit(doc.name, doc.num); }"
+ }
+ }
+ }
rest = self.client.server._rest()
if rest.couch_api_base is None:
raise SkipTest
- rest.create_design_doc(self.client.name, 'test_ddoc', design_doc)
+ rest.create_design_doc(self.client.name, 'test_ddoc',
+ json.dumps(design_doc))
ddocs = self.client.design_docs()
self.assertIsInstance(ddocs, types.ListType)
- self.assertEqual(ddocs[0].keys()[0], '_design/test_ddoc')
+ self.assertIn('test_ddoc', [ddoc for ddoc in ddocs])
+ self.assertIn(design_doc, [ddoc for ddoc in ddocs])
if __name__ == "__main__":
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.