From 91ec93e807667e6ac0de5dd21f63c378b41210ac Mon Sep 17 00:00:00 2001 From: Mickael Bailly Date: Wed, 14 Apr 2010 14:23:53 +0200 Subject: [PATCH] First shots of PHPUnit tests --- tests/couchClientTest.php | 287 +++++++++++++++++++ tests/couchClientViewTest.php | 500 ++++++++++++++++++++++++++++++++++ 2 files changed, 787 insertions(+) create mode 100644 tests/couchClientTest.php create mode 100644 tests/couchClientViewTest.php diff --git a/tests/couchClientTest.php b/tests/couchClientTest.php new file mode 100644 index 0000000..0e3e65f --- /dev/null +++ b/tests/couchClientTest.php @@ -0,0 +1,287 @@ +client = new couchClient($this->couch_server,"couchclienttest"); + try { + $this->client->deleteDatabase(); + } catch ( Exception $e) {} + $this->client->createDatabase(); + } + + public function tearDown() + { + $this->client = null; + } + + public function testDatabaseNameValidator () { + $matches = array ( + "Azerty"=>false, + "a-zer_ty" => true, + "a(zert)y" => true + ); + foreach ( $matches as $key => $val ) { + $this->assertEquals ( $val, couchClient::isValidDatabaseName($key) ); + } + + } + + public function testDatabaseExists () { + $exist = $this->client->databaseExists(); + $this->assertTrue($exist,"testing against an existing database"); + + $client = new couchClient($this->couch_server,"foofoofooidontexist"); + $this->assertFalse($client->databaseExists(),"testing against a non-existing database"); + + } + + public function testDatabaseInfos () { + $infos = $this->client->getDatabaseInfos(); +// print_r($infos); + $this->assertType("object", $infos); + $tsts = array( + 'db_name' => "couchclienttest", + "doc_count" => 0, + "doc_del_count" => 0, + "update_seq" => 0, + "purge_seq" => 0, + "compact_running" => false, + "disk_size" => false, + "instance_start_time" => false, + "disk_format_version" => false + ); + foreach ( $tsts as $attr => $value ) { + $this->assertObjectHasAttribute($attr,$infos); + if ( $value !== false ) { + $this->assertEquals($value,$infos->$attr); + } + } + } + + public function testDatabaseDelete () { + $back = $this->client->deleteDatabase(); + $this->assertType("object", $back); + $this->assertObjectHasAttribute("ok",$back); + $this->assertEquals(true,$back->ok); +// print_r($back); + } + + public function testGetDatabaseUri () { + $this->assertEquals ( $this->couch_server."couchclienttest", $this->client->getDatabaseUri() ); + } + + public function testGetDatabaseName () { + $this->assertEquals ( "couchclienttest", $this->client->getDatabaseName() ); + } + + public function testGetServerUri () { + $this->assertEquals ( $this->couch_server."couchclienttest", $this->client->getDatabaseUri() ); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testStoreDocException () { + $test = array ("_id"=>"great","type"=> "array"); + $this->client->storeDoc($test); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testStoreDocException2 () { + $test = new stdclass(); + $test->_id = "great"; + $test->_type = "object"; + $this->client->storeDoc($test); + } + + public function testStoreDoc () { + $infos = $this->client->getDatabaseInfos(); + $test = new stdclass(); + $test->_id = "great"; + $test->type = "object"; + $this->client->storeDoc($test); + $infos2 = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count+1, $infos2->doc_count ); + $doc = $this->client->getDoc("great"); + $this->assertType("object", $doc); + $this->assertObjectHasAttribute("type",$doc); + $this->assertEquals("object",$doc->type); + } + + public function testBulkDocsStorage () { + $data = array ( + new stdclass(), + new stdclass(), + new stdclass() + ); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 0 ); + + $stored = $this->client->storeDocs($data,false); +// print_r($stored); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 3 ); + + $data[0]->_id = "test"; + $data[0]->type = "male"; + $data[1]->_id = "test"; + $data[1]->type = "female"; + $data[2]->_id = "test"; + $data[2]->type = "both"; + $stored = $this->client->storeDocs($data,true); +// print_r($stored); + $infos = $this->client->getDatabaseInfos(); +// print_r($infos); + $this->assertEquals ( $infos->doc_count, 4 ); + + $doc = $this->client->conflicts()->getDoc("test"); + $this->assertType("object", $doc); + $this->assertObjectHasAttribute("_conflicts",$doc); + $this->assertType("array",$doc->_conflicts); + $this->assertEquals( count( $doc->_conflicts ) , 2 ); + $data[0]->_id = "test2"; + $data[1]->_id = "test2"; + $data[2]->_id = "test2"; + $stored = $this->client->storeDocs($data,false); + $this->assertType("array",$stored); + $this->assertEquals( count($stored) , 3 ); + foreach ( $stored as $s ) { + $this->assertType("object",$s); + $this->assertObjectHasAttribute("error",$s); + $this->assertEquals($s->error, "conflict"); + } + $doc = $this->client->conflicts()->getDoc("test2"); + $this->assertObjectNotHasAttribute("_conflicts",$doc); +// print_r($stored); + } + + public function testcompactAllViews () { + $cd = new couchDocument($this->client); + $cd->set ( array ( + '_id' => '_design/test', + 'language'=>'javascript' + ) ); + $this->client->compactAllViews(); + } + + public function testCouchDocumentAttachment () { + $cd = new couchDocument($this->client); + $cd->set ( array ( + '_id' => 'somedoc' + ) ); + $back = $cd->storeAsAttachment("This is the content","file.txt","text/plain"); + $fields = $cd->getFields(); + + $this->assertType("object", $back); + $this->assertObjectHasAttribute("ok",$back); + $this->assertEquals( $back->ok , true ); + $this->assertObjectHasAttribute("_attachments",$fields); + $this->assertObjectHasAttribute("file.txt",$fields->_attachments); + + $cd = new couchDocument($this->client); + $cd->set ( array ( + '_id' => 'somedoc2' + ) ); + $back = $cd->storeAttachment("lib/couch.php","text/plain","file.txt"); + $fields = $cd->getFields(); + + $this->assertType("object", $back); + $this->assertObjectHasAttribute("ok",$back); + $this->assertEquals( $back->ok , true ); + $this->assertObjectHasAttribute("_attachments",$fields); + $this->assertObjectHasAttribute("file.txt",$fields->_attachments); + + $back = $cd->deleteAttachment("file.txt"); + $fields = $cd->getFields(); + $this->assertType("object", $back); + $this->assertObjectHasAttribute("ok",$back); + $this->assertEquals( $back->ok , true ); + $test = property_exists($fields,'_attachments'); + $this->assertEquals($test,false); +// $this->assertObjectHasAttribute("file.txt",$fields->_attachments); + } + + public function testRevs () { + $cd = new couchDocument($this->client); + $cd->set ( array ( + '_id' => 'somedoc' + ) ); + $cd->property1 = "one"; + $cd->property2 = "two"; + $doc = $this->client->revs()->revs_info()->getDoc("somedoc"); + $this->assertObjectHasAttribute("_revisions",$doc); + $this->assertObjectHasAttribute("ids",$doc->_revisions); + $this->assertEquals( count($doc->_revisions->ids) , 3 ); + $this->assertObjectHasAttribute("_revs_info",$doc); + $this->assertEquals( count($doc->_revs_info) , 3 ); + } + + public function testBulkDocsStorageAllOrNothing () { + $data = array ( + new stdclass(), + new stdclass(), + new stdclass() + ); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 0 ); + + $data[0]->_id = "test"; + $data[0]->type = "male"; + $data[1]->_id = "test"; + $data[1]->type = "female"; + $data[2]->_id = "test"; + $data[2]->type = "both"; + $stored = $this->client->storeDocs($data,true); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 1 ); + $doc = $this->client->conflicts()->getDoc("test"); + $this->assertObjectHasAttribute("_conflicts",$doc); + $this->assertEquals( count($doc->_conflicts) , 2 ); + + $data[0]->_id = "test2"; + $data[0]->type = "male"; + $data[1]->_id = "test2"; + $data[1]->type = "female"; + $data[2]->_id = "test2"; + $data[2]->type = "both"; + + $stored = $this->client->storeDocs($data,false); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 2 ); + $doc = $this->client->conflicts()->getDoc("test2"); + $this->assertObjectNotHasAttribute("_conflicts",$doc); + } + + + public function testDocAsArray () { + $infos = $this->client->getDatabaseInfos(); + $test = new stdclass(); + $test->_id = "great"; + $test->type = "object"; + $this->client->storeDoc($test); + $infos2 = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count+1, $infos2->doc_count ); + $doc = $this->client->asArray()->getDoc("great"); + $this->assertType("array", $doc); + $this->assertArrayHasKey("type",$doc); + $this->assertEquals("object",$doc['type']); + } +} diff --git a/tests/couchClientViewTest.php b/tests/couchClientViewTest.php new file mode 100644 index 0000000..fde1645 --- /dev/null +++ b/tests/couchClientViewTest.php @@ -0,0 +1,500 @@ +client = new couchClient($this->couch_server,"couchclienttest"); + try { + $this->client->deleteDatabase(); + } catch ( Exception $e) {} + $this->client->createDatabase(); + } + + public function tearDown() + { + $this->client = null; + } + + protected function _makeView () { + $doc = new couchDocument($this->client); + $doc->_id = "_design/test"; + $views = array(); + $map = "function (doc) { + if ( doc.type && doc.type == 'test' ) { + emit(doc.type,1); + } + }"; + $reduce = "function (keys, values) { + return sum(values); + }"; + $map2 = "function (doc) { + if ( doc.type ) { + emit(doc.type,1); + } + }"; + $doc->views = array ( + "simple" => array ( + "map"=>$map, + "reduce"=>$reduce + ), + "complex" => array ( + "map"=>$map2, + "reduce"=>$reduce + ) + ); + } + + public function testSimpleViews () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test2","param" => null), + array( "_id"=>"five","type"=>"test","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->getView("test","simple"); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 4); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 4); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + $this->assertObjectNotHasAttribute("doc",$row); + } + } + + public function testSimpleReduceViews () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test2","param" => null), + array( "_id"=>"five","type"=>"test","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->getView("test","simple"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 1); + $row = reset($test->rows); + $this->assertType("object", $row); + $this->assertObjectHasAttribute("value",$row); + $this->assertEquals($row->value, 4); + } + + + public function testIncludeDocs () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test2","param" => null), + array( "_id"=>"five","type"=>"test","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->include_docs(true)->getView("test","simple"); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 4); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 4); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + $this->assertObjectHasAttribute("doc",$row); + } + } + + public function testViewKey () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test2","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->key("test")->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 2); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertEquals($row->key, "test"); + $this->assertObjectHasAttribute("value",$row); + } + } + + public function testViewKeys () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->keys(array("test","test3"))->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 3); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + } + + + public function testViewStartkey () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->startkey("test3")->getView("test","complex"); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 4); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 1); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + } + + + + public function testViewEndkey () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->endkey("test")->getView("test","complex"); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 2); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + + $test = $this->client->reduce(false)->endkey("test")->inclusive_end(false)->getView("test","complex"); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 0); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + + } + + public function testViewStartkeyDocid () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->startkey("test")->startkey_docid("three")->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 1); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 4); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + } + + + public function testViewEndkeyDocid () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->endkey("test2")->endkey_docid("five")->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 3); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + } + + + public function testViewLimit () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->limit(2)->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 2); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + } + + + public function testViewSkip () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->skip(2)->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 2); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 3); + foreach ( $test->rows as $row ) { + $this->assertObjectHasAttribute("id",$row); + $this->assertObjectHasAttribute("key",$row); + $this->assertObjectHasAttribute("value",$row); + } + } + + + + public function testViewDescending () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 5); + $row = reset($test->rows); + $this->assertObjectHasAttribute("key",$row); + $this->assertEquals($row->key, "test"); + + $test = $this->client->reduce(false)->descending(true)->getView("test","complex"); +// print_r($test); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("total_rows",$test); + $this->assertEquals($test->total_rows, 5); + $this->assertObjectHasAttribute("offset",$test); + $this->assertEquals($test->offset, 0); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 5); + $row = reset($test->rows); + $this->assertObjectHasAttribute("key",$row); + $this->assertEquals($row->key, "test3"); + } + + + public function testViewGroup () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => 1), + array( "_id"=>"two","type"=>"test2","param" => 2), + array( "_id"=>"three","type"=>"test","param" => 2), + array( "_id"=>"four","type"=>"test3","param" => 1), + array( "_id"=>"five","type"=>"test2","param" => 1) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + + $doc = couchDocument::getInstance($this->client, "_design/test"); + $views = $doc->views; + $views->multigroup = new stdClass(); + $views->multigroup->map = "function (doc) { + if ( doc.type && doc.param ) { + emit( [doc.type, doc.param], 1); + } + }"; + $views->multigroup->reduce = "function(keys,values) { + return sum(values); + }"; + $doc->views = $views; + + $test = $this->client->group(true)->getView("test","multigroup"); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 5); + + $test = $this->client->group(true)->group_level(1)->getView("test","multigroup"); + $this->assertType("object", $test); + $this->assertObjectHasAttribute("rows",$test); + $this->assertType("array", $test->rows); + $this->assertEquals(count($test->rows), 3); + } + + public function testViewAsArray () { + $this->_makeView(); + $docs = array ( + array( "_id"=>"one","type"=>"test","param" => null), + array( "_id"=>"two","type"=>"test2","param" => null), + array( "_id"=>"three","type"=>"test","param" => null), + array( "_id"=>"four","type"=>"test3","param" => null), + array( "_id"=>"five","type"=>"test2","param" => null) + ); + $this->client->storeDocs($docs); + $infos = $this->client->getDatabaseInfos(); + $this->assertEquals ( $infos->doc_count, 6 ); + $test = $this->client->reduce(false)->asArray()->getView("test","complex"); +// print_r($test); + $this->assertType("array", $test); + } + + + +}