Просмотр исходного кода

[tests] Add more tests for SCAN-based iterators.

Daniele Alessandri 11 лет назад
Родитель
Сommit
4d8348158f

+ 59 - 1
tests/Predis/Iterator/Scan/HashIteratorTest.php

@@ -37,6 +37,27 @@ class HashIteratorTest extends StandardTestCase
         $iterator = new HashIterator($client, 'key:hash');
         $iterator = new HashIterator($client, 'key:hash');
     }
     }
 
 
+    /**
+     * @group disconnected
+     */
+    public function testIterationWithNoResults()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'hscan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->once())
+               ->method('hscan')
+               ->with('key:hash', 0, array())
+               ->will($this->returnValue(array(0, array())));
+
+        $iterator = new HashIterator($client, 'key:hash');
+
+        $iterator->rewind();
+        $this->assertFalse($iterator->valid());
+    }
+
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
@@ -122,7 +143,44 @@ class HashIteratorTest extends StandardTestCase
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
-    public function testIterationWithMultipleFetchesAndHoles()
+    public function testIterationOnMultipleFetchesAndHoleInFirstFetch()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'hscan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->at(1))
+               ->method('hscan')
+               ->with('key:hash', 0, array())
+               ->will($this->returnValue(array(4, array())));
+        $client->expects($this->at(2))
+               ->method('hscan')
+               ->with('key:hash', 4, array())
+               ->will($this->returnValue(array(0, array(
+                    'field:1st' => 'value:1st', 'field:2nd' => 'value:2nd',
+               ))));
+
+        $iterator = new HashIterator($client, 'key:hash');
+
+        $iterator->rewind();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame('value:1st', $iterator->current());
+        $this->assertSame('field:1st', $iterator->key());
+
+        $iterator->next();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame('value:2nd', $iterator->current());
+        $this->assertSame('field:2nd', $iterator->key());
+
+        $iterator->next();
+        $this->assertFalse($iterator->valid());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testIterationOnMultipleFetchesAndHoleInMidFetch()
     {
     {
         $client = $this->getMock('Predis\Client', array('getProfile', 'hscan'));
         $client = $this->getMock('Predis\Client', array('getProfile', 'hscan'));
 
 

+ 57 - 1
tests/Predis/Iterator/Scan/KeyspaceIteratorTest.php

@@ -37,6 +37,27 @@ class KeyspaceIteratorTest extends StandardTestCase
         $iterator = new KeyspaceIterator($client);
         $iterator = new KeyspaceIterator($client);
     }
     }
 
 
+    /**
+     * @group disconnected
+     */
+    public function testIterationWithNoResults()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'scan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->once())
+               ->method('scan')
+               ->with(0, array())
+               ->will($this->returnValue(array(0, array())));
+
+        $iterator = new KeyspaceIterator($client);
+
+        $iterator->rewind();
+        $this->assertFalse($iterator->valid());
+    }
+
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
@@ -116,7 +137,42 @@ class KeyspaceIteratorTest extends StandardTestCase
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
-    public function testIterationWithMultipleFetchesAndHoles()
+    public function testIterationOnMultipleFetchesAndHoleInFirstFetch()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'scan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->at(1))
+               ->method('scan')
+               ->with(0, array())
+               ->will($this->returnValue(array(4, array())));
+        $client->expects($this->at(2))
+               ->method('scan')
+               ->with(4, array())
+               ->will($this->returnValue(array(0, array('key:1st', 'key:2nd'))));
+
+        $iterator = new KeyspaceIterator($client);
+
+        $iterator->rewind();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame('key:1st', $iterator->current());
+        $this->assertSame(0, $iterator->key());
+
+        $iterator->next();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame('key:2nd', $iterator->current());
+        $this->assertSame(1, $iterator->key());
+
+        $iterator->next();
+        $this->assertFalse($iterator->valid());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testIterationOnMultipleFetchesAndHoleInMidFetch()
     {
     {
         $client = $this->getMock('Predis\Client', array('getProfile', 'scan'));
         $client = $this->getMock('Predis\Client', array('getProfile', 'scan'));
 
 

+ 57 - 1
tests/Predis/Iterator/Scan/SetIteratorTest.php

@@ -37,6 +37,27 @@ class SetIteratorTest extends StandardTestCase
         $iterator = new SetIterator($client, 'key:set');
         $iterator = new SetIterator($client, 'key:set');
     }
     }
 
 
+    /**
+     * @group disconnected
+     */
+    public function testIterationWithNoResults()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'sscan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->once())
+               ->method('sscan')
+               ->with('key:set', 0, array())
+               ->will($this->returnValue(array(0, array())));
+
+        $iterator = new SetIterator($client, 'key:set');
+
+        $iterator->rewind();
+        $this->assertFalse($iterator->valid());
+    }
+
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
@@ -116,7 +137,42 @@ class SetIteratorTest extends StandardTestCase
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
-    public function testIterationWithMultipleFetchesAndHoles()
+    public function testIterationOnMultipleFetchesAndHoleInFirstFetch()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'sscan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->at(1))
+               ->method('sscan')
+               ->with('key:set', 0, array())
+               ->will($this->returnValue(array(4, array())));
+        $client->expects($this->at(2))
+               ->method('sscan')
+               ->with('key:set', 4, array())
+               ->will($this->returnValue(array(0, array('member:1st', 'member:2nd'))));
+
+        $iterator = new SetIterator($client, 'key:set');
+
+        $iterator->rewind();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame('member:1st', $iterator->current());
+        $this->assertSame(0, $iterator->key());
+
+        $iterator->next();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame('member:2nd', $iterator->current());
+        $this->assertSame(1, $iterator->key());
+
+        $iterator->next();
+        $this->assertFalse($iterator->valid());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testIterationOnMultipleFetchesAndHoleInMidFetch()
     {
     {
         $client = $this->getMock('Predis\Client', array('getProfile', 'sscan'));
         $client = $this->getMock('Predis\Client', array('getProfile', 'sscan'));
 
 

+ 59 - 1
tests/Predis/Iterator/Scan/SortedSetIteratorTest.php

@@ -37,6 +37,27 @@ class SortedSetIteratorTest extends StandardTestCase
         $iterator = new SortedSetIterator($client, 'key:zset');
         $iterator = new SortedSetIterator($client, 'key:zset');
     }
     }
 
 
+    /**
+     * @group disconnected
+     */
+    public function testIterationWithNoResults()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->once())
+               ->method('zscan')
+               ->with('key:zset', 0, array())
+               ->will($this->returnValue(array(0, array())));
+
+        $iterator = new SortedSetIterator($client, 'key:zset');
+
+        $iterator->rewind();
+        $this->assertFalse($iterator->valid());
+    }
+
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
@@ -122,7 +143,44 @@ class SortedSetIteratorTest extends StandardTestCase
     /**
     /**
      * @group disconnected
      * @group disconnected
      */
      */
-    public function testIterationWithMultipleFetchesAndHoles()
+    public function testIterationOnMultipleFetchesAndHoleInFirstFetch()
+    {
+        $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
+
+        $client->expects($this->any())
+               ->method('getProfile')
+               ->will($this->returnValue(ServerProfile::get('2.8')));
+        $client->expects($this->at(1))
+               ->method('zscan')
+               ->with('key:zset', 0, array())
+               ->will($this->returnValue(array(4, array())));
+        $client->expects($this->at(2))
+               ->method('zscan')
+               ->with('key:zset', 4, array())
+               ->will($this->returnValue(array(0, array(
+                    array('member:1st', 1.0), array('member:2nd', 2.0),
+               ))));
+
+        $iterator = new SortedSetIterator($client, 'key:zset');
+
+        $iterator->rewind();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame(1.0, $iterator->current());
+        $this->assertSame('member:1st', $iterator->key());
+
+        $iterator->next();
+        $this->assertTrue($iterator->valid());
+        $this->assertSame(2.0, $iterator->current());
+        $this->assertSame('member:2nd', $iterator->key());
+
+        $iterator->next();
+        $this->assertFalse($iterator->valid());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testIterationOnMultipleFetchesAndHoleInMidFetch()
     {
     {
         $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));
         $client = $this->getMock('Predis\Client', array('getProfile', 'zscan'));