Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added views() to DesignDoc; tested it

Also deleting created design doc in DesignDoc tests

Change-Id: I4f52202ed62a30d5d04932fea550ad3a9bc1de60
Reviewed-on: http://review.couchbase.org/19889
Reviewed-by: Benjamin Young <benjamin@couchbase.com>
Tested-by: Benjamin Young <benjamin@couchbase.com>
  • Loading branch information...
commit dc82234a4ca9d453e651b85b4bc30097db8e05d0 1 parent bba0851
@BigBlueHat BigBlueHat authored BigBlueHat committed
Showing with 75 additions and 0 deletions.
  1. +43 −0 couchbase/client.py
  2. +32 −0 couchbase/tests/test_client.py
View
43 couchbase/client.py
@@ -379,3 +379,46 @@ def __eq__(self, other):
def __neq__(self, other):
return not self.__eq__(other)
+
+ def views(self):
+ return [View(view, self.ddoc['views'][view], self)
+ for view in self.ddoc['views']]
+
+
+class View(object):
+ def __init__(self, name, view, ddoc=None):
+ """Object for holding View information.
+
+ Keyword arguments:
+ name (str): Name of the View within the Design Doc
+ view (dict): View dictionary containing a 'map' and/or 'reduce' key
+ who's value should be of type string or unicode and
+ contain the JS MapReduce function
+ """
+ assert isinstance(name, (str, unicode)), \
+ "name parameter must be of type string or unicode"
+ assert isinstance(view, dict), \
+ "view parameter must be of type dictionary"
+ assert 'map' in view, \
+ "name parameter must be of type string or unicode"
+ assert isinstance(view['map'], (str, unicode)), \
+ "name parameter must be of type string or unicode"
+ self.name = name
+ self.view = view
+ self.ddoc = ddoc
+
+ def __str__(self):
+ """Return the name of the View 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 == {self.name: self.view}
+
+ def __neq__(self, other):
+ return not self.__eq__(other)
View
32 couchbase/tests/test_client.py
@@ -264,6 +264,38 @@ def test_design_docs(self):
self.assertIsInstance(ddocs, types.ListType)
self.assertIn('test_ddoc', [ddoc for ddoc in ddocs])
self.assertIn(design_doc, [ddoc for ddoc in ddocs])
+ rest.delete_design_doc(self.client.name, 'test_ddoc')
+
+
+class DesignDocTest(Base):
+ def setUp(self):
+ super(DesignDocTest, self).setUp()
+ self.cb = Couchbase(self.host + ':' + self.port, self.username,
+ self.password)
+ self.client = self.cb[self.bucket_name]
+ self.rest = self.client.server._rest()
+ if self.rest.couch_api_base is None:
+ raise SkipTest
+
+ self.ddoc = {"views":
+ {"testing":
+ {"map":
+ "function(doc) { emit(doc.name, doc.num); }"
+ }
+ }
+ }
+ self.rest.create_design_doc(self.client.name, 'test_ddoc',
+ json.dumps(self.ddoc))
+ self.design_docs = self.client.design_docs()
+
+ def tearDown(self):
+ self.rest.delete_design_doc(self.client.name, 'test_ddoc')
+
+ def test_views(self):
+ views = self.design_docs[0].views()
+ self.assertIsInstance(views, types.ListType)
+ self.assertIn('testing', views)
+ self.assertIn(self.ddoc['views'], views)
if __name__ == "__main__":
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.