Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 5e3c57dbaa0df9375e425ac74505dbef77e88947 1 parent c85e90d
BigBlueHat authored BigBlueHat committed
41  couchbase/client.py
@@ -21,6 +21,7 @@
21 21
 from copy import deepcopy
22 22
 from threading import Thread, Lock
23 23
 import warnings
  24
+from collections import Set
24 25
 
25 26
 import requests
26 27
 
@@ -441,10 +442,40 @@ def results(self, params={}):
441 442
 
442 443
         rest = self.ddoc.bucket.server._rest()
443 444
         results = rest.view_results(self.ddoc.bucket.name, self.ddoc.name,
444  
-                                    self.name, params)['rows']
  445
+                                    self.name, params)
445 446
 
446  
-        # results were reduced, so return just the reduced value
447  
-        if len(results) == 1 and results[0]['key'] is None:
448  
-            return results[0]['value']
  447
+        return ViewResultsIterator(results)
  448
+
  449
+
  450
+class ViewResultsIterator(Set):
  451
+    def __init__(self, results):
  452
+        self.results = results['rows']
  453
+        if 'total_rows' in results:
  454
+            self.total_rows = results['total_rows']
  455
+        else:
  456
+            # reduced values don't really have a "length" so setting to 1
  457
+            self.total_rows = 1
  458
+
  459
+    def __eq__(self, other):
  460
+        if len(self.results) == 1 and self.results[0]['key'] is None:
  461
+            return self.results[0]['value'] == other
449 462
         else:
450  
-            return results
  463
+            return self.results == other
  464
+
  465
+    def __neq__(self, other):
  466
+        return not self.__eq__(other)
  467
+
  468
+    def __iter__(self):
  469
+        return self
  470
+
  471
+    def __len__(self):
  472
+        return self.total_rows
  473
+
  474
+    def __contains__(self, item):
  475
+        return item in self.results
  476
+
  477
+    def next(self):
  478
+        try:
  479
+            return self.results.pop(0)
  480
+        except IndexError:
  481
+            raise StopIteration
7  couchbase/tests/test_client.py
@@ -20,6 +20,7 @@
20 20
 import uuid
21 21
 import time
22 22
 import json
  23
+from collections import Set
23 24
 
24 25
 from nose.plugins.attrib import attr
25 26
 from nose.plugins.skip import SkipTest
@@ -435,13 +436,13 @@ def test_results(self):
435 436
         # Retrieve reduced results from a View. PYCBC-7
436 437
         #   (the format is the same, but there is no associated docid)
437 438
         results = view.results({'stale': False})
438  
-        self.assertIs(results, 10)
  439
+        self.assertEqual(results, 10)
439 440
         # Assemble query parameters for a View. PYCBC-7
440 441
         results = view.results({'stale': False, 'reduce': False})
441 442
         if "error" in results:
442 443
             self.fail(results)
443 444
         else:
444  
-            self.assertIsInstance(results, types.ListType)
  445
+            self.assertIsInstance(results, Set)
445 446
             self.assertIs(len(results), 10)
446 447
         # test again with include_docs=true
447 448
         # Retrieve non-reduced results from a View. PYCBC-7
@@ -453,7 +454,7 @@ def test_results(self):
453 454
         if "error" in results:
454 455
             self.fail(results)
455 456
         else:
456  
-            self.assertIsInstance(results, types.ListType)
  457
+            self.assertIsInstance(results, Set)
457 458
             self.assertIs(len(results), 10)
458 459
             for row in results:
459 460
                 self.assertIn('doc', row)

0 notes on commit 5e3c57d

Please sign in to comment.
Something went wrong with that request. Please try again.