Permalink
Browse files

implemented ViewResultsIterator object; tested it

Change-Id: Id3e2dcd50afd0c6565459c2c6a7b8f1c79f9ab64
Reviewed-on: http://review.couchbase.org/20347
Reviewed-by: Pavel Paulau <pavel.paulau@gmail.com>
Tested-by: Benjamin Young <benjamin@couchbase.com>
  • Loading branch information...
1 parent c85e90d commit 5e3c57dbaa0df9375e425ac74505dbef77e88947 @BigBlueHat BigBlueHat committed with Benjamin Young Aug 29, 2012
Showing with 40 additions and 8 deletions.
  1. +36 −5 couchbase/client.py
  2. +4 −3 couchbase/tests/test_client.py
View
@@ -21,6 +21,7 @@
from copy import deepcopy
from threading import Thread, Lock
import warnings
+from collections import Set
import requests
@@ -441,10 +442,40 @@ def results(self, params={}):
rest = self.ddoc.bucket.server._rest()
results = rest.view_results(self.ddoc.bucket.name, self.ddoc.name,
- self.name, params)['rows']
+ self.name, params)
- # results were reduced, so return just the reduced value
- if len(results) == 1 and results[0]['key'] is None:
- return results[0]['value']
+ return ViewResultsIterator(results)
+
+
+class ViewResultsIterator(Set):
+ def __init__(self, results):
+ self.results = results['rows']
+ if 'total_rows' in results:
+ self.total_rows = results['total_rows']
+ else:
+ # reduced values don't really have a "length" so setting to 1
+ self.total_rows = 1
+
+ def __eq__(self, other):
+ if len(self.results) == 1 and self.results[0]['key'] is None:
+ return self.results[0]['value'] == other
else:
- return results
+ return self.results == other
+
+ def __neq__(self, other):
+ return not self.__eq__(other)
+
+ def __iter__(self):
+ return self
+
+ def __len__(self):
+ return self.total_rows
+
+ def __contains__(self, item):
+ return item in self.results
+
+ def next(self):
+ try:
+ return self.results.pop(0)
+ except IndexError:
+ raise StopIteration
@@ -20,6 +20,7 @@
import uuid
import time
import json
+from collections import Set
from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest
@@ -435,13 +436,13 @@ def test_results(self):
# Retrieve reduced results from a View. PYCBC-7
# (the format is the same, but there is no associated docid)
results = view.results({'stale': False})
- self.assertIs(results, 10)
+ self.assertEqual(results, 10)
# Assemble query parameters for a View. PYCBC-7
results = view.results({'stale': False, 'reduce': False})
if "error" in results:
self.fail(results)
else:
- self.assertIsInstance(results, types.ListType)
+ self.assertIsInstance(results, Set)
self.assertIs(len(results), 10)
# test again with include_docs=true
# Retrieve non-reduced results from a View. PYCBC-7
@@ -453,7 +454,7 @@ def test_results(self):
if "error" in results:
self.fail(results)
else:
- self.assertIsInstance(results, types.ListType)
+ self.assertIsInstance(results, Set)
self.assertIs(len(results), 10)
for row in results:
self.assertIn('doc', row)

0 comments on commit 5e3c57d

Please sign in to comment.