/tests/couchClientViewTest.php

https://github.com/rwaldron/PHP-on-Couch · PHP · 500 lines · 447 code · 42 blank · 11 comment · 6 complexity · 3123fb059b18ca67cd28f913d355c0ad MD5 · raw file

  1. <?php
  2. // error_reporting(E_STRICT);
  3. error_reporting(E_ALL);
  4. require_once 'PHPUnit/Framework.php';
  5. require_once "lib/couch.php";
  6. require_once "lib/couchClient.php";
  7. require_once "lib/couchDocument.php";
  8. require_once "lib/couchReplicator.php";
  9. class couchClientViewTest extends PHPUnit_Framework_TestCase
  10. {
  11. private $couch_server = "http://localhost:5984/";
  12. public function setUp()
  13. {
  14. $this->client = new couchClient($this->couch_server,"couchclienttest");
  15. try {
  16. $this->client->deleteDatabase();
  17. } catch ( Exception $e) {}
  18. $this->client->createDatabase();
  19. }
  20. public function tearDown()
  21. {
  22. $this->client = null;
  23. }
  24. protected function _makeView () {
  25. $doc = new couchDocument($this->client);
  26. $doc->_id = "_design/test";
  27. $views = array();
  28. $map = "function (doc) {
  29. if ( doc.type && doc.type == 'test' ) {
  30. emit(doc.type,1);
  31. }
  32. }";
  33. $reduce = "function (keys, values) {
  34. return sum(values);
  35. }";
  36. $map2 = "function (doc) {
  37. if ( doc.type ) {
  38. emit(doc.type,1);
  39. }
  40. }";
  41. $doc->views = array (
  42. "simple" => array (
  43. "map"=>$map,
  44. "reduce"=>$reduce
  45. ),
  46. "complex" => array (
  47. "map"=>$map2,
  48. "reduce"=>$reduce
  49. )
  50. );
  51. }
  52. public function testSimpleViews () {
  53. $this->_makeView();
  54. $docs = array (
  55. array( "_id"=>"one","type"=>"test","param" => null),
  56. array( "_id"=>"two","type"=>"test","param" => null),
  57. array( "_id"=>"three","type"=>"test","param" => null),
  58. array( "_id"=>"four","type"=>"test2","param" => null),
  59. array( "_id"=>"five","type"=>"test","param" => null)
  60. );
  61. $this->client->storeDocs($docs);
  62. $infos = $this->client->getDatabaseInfos();
  63. $this->assertEquals ( $infos->doc_count, 6 );
  64. $test = $this->client->reduce(false)->getView("test","simple");
  65. $this->assertType("object", $test);
  66. $this->assertObjectHasAttribute("total_rows",$test);
  67. $this->assertEquals($test->total_rows, 4);
  68. $this->assertObjectHasAttribute("offset",$test);
  69. $this->assertEquals($test->offset, 0);
  70. $this->assertObjectHasAttribute("rows",$test);
  71. $this->assertType("array", $test->rows);
  72. $this->assertEquals(count($test->rows), 4);
  73. foreach ( $test->rows as $row ) {
  74. $this->assertObjectHasAttribute("id",$row);
  75. $this->assertObjectHasAttribute("key",$row);
  76. $this->assertObjectHasAttribute("value",$row);
  77. $this->assertObjectNotHasAttribute("doc",$row);
  78. }
  79. }
  80. public function testSimpleReduceViews () {
  81. $this->_makeView();
  82. $docs = array (
  83. array( "_id"=>"one","type"=>"test","param" => null),
  84. array( "_id"=>"two","type"=>"test","param" => null),
  85. array( "_id"=>"three","type"=>"test","param" => null),
  86. array( "_id"=>"four","type"=>"test2","param" => null),
  87. array( "_id"=>"five","type"=>"test","param" => null)
  88. );
  89. $this->client->storeDocs($docs);
  90. $infos = $this->client->getDatabaseInfos();
  91. $this->assertEquals ( $infos->doc_count, 6 );
  92. $test = $this->client->getView("test","simple");
  93. // print_r($test);
  94. $this->assertType("object", $test);
  95. $this->assertObjectHasAttribute("rows",$test);
  96. $this->assertType("array", $test->rows);
  97. $this->assertEquals(count($test->rows), 1);
  98. $row = reset($test->rows);
  99. $this->assertType("object", $row);
  100. $this->assertObjectHasAttribute("value",$row);
  101. $this->assertEquals($row->value, 4);
  102. }
  103. public function testIncludeDocs () {
  104. $this->_makeView();
  105. $docs = array (
  106. array( "_id"=>"one","type"=>"test","param" => null),
  107. array( "_id"=>"two","type"=>"test","param" => null),
  108. array( "_id"=>"three","type"=>"test","param" => null),
  109. array( "_id"=>"four","type"=>"test2","param" => null),
  110. array( "_id"=>"five","type"=>"test","param" => null)
  111. );
  112. $this->client->storeDocs($docs);
  113. $infos = $this->client->getDatabaseInfos();
  114. $this->assertEquals ( $infos->doc_count, 6 );
  115. $test = $this->client->reduce(false)->include_docs(true)->getView("test","simple");
  116. $this->assertType("object", $test);
  117. $this->assertObjectHasAttribute("total_rows",$test);
  118. $this->assertEquals($test->total_rows, 4);
  119. $this->assertObjectHasAttribute("offset",$test);
  120. $this->assertEquals($test->offset, 0);
  121. $this->assertObjectHasAttribute("rows",$test);
  122. $this->assertType("array", $test->rows);
  123. $this->assertEquals(count($test->rows), 4);
  124. foreach ( $test->rows as $row ) {
  125. $this->assertObjectHasAttribute("id",$row);
  126. $this->assertObjectHasAttribute("key",$row);
  127. $this->assertObjectHasAttribute("value",$row);
  128. $this->assertObjectHasAttribute("doc",$row);
  129. }
  130. }
  131. public function testViewKey () {
  132. $this->_makeView();
  133. $docs = array (
  134. array( "_id"=>"one","type"=>"test","param" => null),
  135. array( "_id"=>"two","type"=>"test2","param" => null),
  136. array( "_id"=>"three","type"=>"test","param" => null),
  137. array( "_id"=>"four","type"=>"test2","param" => null),
  138. array( "_id"=>"five","type"=>"test2","param" => null)
  139. );
  140. $this->client->storeDocs($docs);
  141. $infos = $this->client->getDatabaseInfos();
  142. $this->assertEquals ( $infos->doc_count, 6 );
  143. $test = $this->client->reduce(false)->key("test")->getView("test","complex");
  144. // print_r($test);
  145. $this->assertType("object", $test);
  146. $this->assertObjectHasAttribute("total_rows",$test);
  147. $this->assertEquals($test->total_rows, 5);
  148. $this->assertObjectHasAttribute("offset",$test);
  149. $this->assertEquals($test->offset, 0);
  150. $this->assertObjectHasAttribute("rows",$test);
  151. $this->assertType("array", $test->rows);
  152. $this->assertEquals(count($test->rows), 2);
  153. foreach ( $test->rows as $row ) {
  154. $this->assertObjectHasAttribute("id",$row);
  155. $this->assertObjectHasAttribute("key",$row);
  156. $this->assertEquals($row->key, "test");
  157. $this->assertObjectHasAttribute("value",$row);
  158. }
  159. }
  160. public function testViewKeys () {
  161. $this->_makeView();
  162. $docs = array (
  163. array( "_id"=>"one","type"=>"test","param" => null),
  164. array( "_id"=>"two","type"=>"test2","param" => null),
  165. array( "_id"=>"three","type"=>"test","param" => null),
  166. array( "_id"=>"four","type"=>"test3","param" => null),
  167. array( "_id"=>"five","type"=>"test2","param" => null)
  168. );
  169. $this->client->storeDocs($docs);
  170. $infos = $this->client->getDatabaseInfos();
  171. $this->assertEquals ( $infos->doc_count, 6 );
  172. $test = $this->client->reduce(false)->keys(array("test","test3"))->getView("test","complex");
  173. // print_r($test);
  174. $this->assertType("object", $test);
  175. $this->assertObjectHasAttribute("total_rows",$test);
  176. $this->assertEquals($test->total_rows, 5);
  177. $this->assertObjectHasAttribute("offset",$test);
  178. $this->assertEquals($test->offset, 0);
  179. $this->assertObjectHasAttribute("rows",$test);
  180. $this->assertType("array", $test->rows);
  181. $this->assertEquals(count($test->rows), 3);
  182. foreach ( $test->rows as $row ) {
  183. $this->assertObjectHasAttribute("id",$row);
  184. $this->assertObjectHasAttribute("key",$row);
  185. $this->assertObjectHasAttribute("value",$row);
  186. }
  187. }
  188. public function testViewStartkey () {
  189. $this->_makeView();
  190. $docs = array (
  191. array( "_id"=>"one","type"=>"test","param" => null),
  192. array( "_id"=>"two","type"=>"test2","param" => null),
  193. array( "_id"=>"three","type"=>"test","param" => null),
  194. array( "_id"=>"four","type"=>"test3","param" => null),
  195. array( "_id"=>"five","type"=>"test2","param" => null)
  196. );
  197. $this->client->storeDocs($docs);
  198. $infos = $this->client->getDatabaseInfos();
  199. $this->assertEquals ( $infos->doc_count, 6 );
  200. $test = $this->client->reduce(false)->startkey("test3")->getView("test","complex");
  201. $this->assertType("object", $test);
  202. $this->assertObjectHasAttribute("total_rows",$test);
  203. $this->assertEquals($test->total_rows, 5);
  204. $this->assertObjectHasAttribute("offset",$test);
  205. $this->assertEquals($test->offset, 4);
  206. $this->assertObjectHasAttribute("rows",$test);
  207. $this->assertType("array", $test->rows);
  208. $this->assertEquals(count($test->rows), 1);
  209. foreach ( $test->rows as $row ) {
  210. $this->assertObjectHasAttribute("id",$row);
  211. $this->assertObjectHasAttribute("key",$row);
  212. $this->assertObjectHasAttribute("value",$row);
  213. }
  214. }
  215. public function testViewEndkey () {
  216. $this->_makeView();
  217. $docs = array (
  218. array( "_id"=>"one","type"=>"test","param" => null),
  219. array( "_id"=>"two","type"=>"test2","param" => null),
  220. array( "_id"=>"three","type"=>"test","param" => null),
  221. array( "_id"=>"four","type"=>"test3","param" => null),
  222. array( "_id"=>"five","type"=>"test2","param" => null)
  223. );
  224. $this->client->storeDocs($docs);
  225. $infos = $this->client->getDatabaseInfos();
  226. $this->assertEquals ( $infos->doc_count, 6 );
  227. $test = $this->client->reduce(false)->endkey("test")->getView("test","complex");
  228. $this->assertType("object", $test);
  229. $this->assertObjectHasAttribute("total_rows",$test);
  230. $this->assertEquals($test->total_rows, 5);
  231. $this->assertObjectHasAttribute("offset",$test);
  232. $this->assertEquals($test->offset, 0);
  233. $this->assertObjectHasAttribute("rows",$test);
  234. $this->assertType("array", $test->rows);
  235. $this->assertEquals(count($test->rows), 2);
  236. foreach ( $test->rows as $row ) {
  237. $this->assertObjectHasAttribute("id",$row);
  238. $this->assertObjectHasAttribute("key",$row);
  239. $this->assertObjectHasAttribute("value",$row);
  240. }
  241. $test = $this->client->reduce(false)->endkey("test")->inclusive_end(false)->getView("test","complex");
  242. $this->assertType("object", $test);
  243. $this->assertObjectHasAttribute("total_rows",$test);
  244. $this->assertEquals($test->total_rows, 5);
  245. $this->assertObjectHasAttribute("offset",$test);
  246. $this->assertEquals($test->offset, 0);
  247. $this->assertObjectHasAttribute("rows",$test);
  248. $this->assertType("array", $test->rows);
  249. $this->assertEquals(count($test->rows), 0);
  250. foreach ( $test->rows as $row ) {
  251. $this->assertObjectHasAttribute("id",$row);
  252. $this->assertObjectHasAttribute("key",$row);
  253. $this->assertObjectHasAttribute("value",$row);
  254. }
  255. }
  256. public function testViewStartkeyDocid () {
  257. $this->_makeView();
  258. $docs = array (
  259. array( "_id"=>"one","type"=>"test","param" => null),
  260. array( "_id"=>"two","type"=>"test2","param" => null),
  261. array( "_id"=>"three","type"=>"test","param" => null),
  262. array( "_id"=>"four","type"=>"test3","param" => null),
  263. array( "_id"=>"five","type"=>"test2","param" => null)
  264. );
  265. $this->client->storeDocs($docs);
  266. $infos = $this->client->getDatabaseInfos();
  267. $this->assertEquals ( $infos->doc_count, 6 );
  268. $test = $this->client->reduce(false)->startkey("test")->startkey_docid("three")->getView("test","complex");
  269. // print_r($test);
  270. $this->assertType("object", $test);
  271. $this->assertObjectHasAttribute("total_rows",$test);
  272. $this->assertEquals($test->total_rows, 5);
  273. $this->assertObjectHasAttribute("offset",$test);
  274. $this->assertEquals($test->offset, 1);
  275. $this->assertObjectHasAttribute("rows",$test);
  276. $this->assertType("array", $test->rows);
  277. $this->assertEquals(count($test->rows), 4);
  278. foreach ( $test->rows as $row ) {
  279. $this->assertObjectHasAttribute("id",$row);
  280. $this->assertObjectHasAttribute("key",$row);
  281. $this->assertObjectHasAttribute("value",$row);
  282. }
  283. }
  284. public function testViewEndkeyDocid () {
  285. $this->_makeView();
  286. $docs = array (
  287. array( "_id"=>"one","type"=>"test","param" => null),
  288. array( "_id"=>"two","type"=>"test2","param" => null),
  289. array( "_id"=>"three","type"=>"test","param" => null),
  290. array( "_id"=>"four","type"=>"test3","param" => null),
  291. array( "_id"=>"five","type"=>"test2","param" => null)
  292. );
  293. $this->client->storeDocs($docs);
  294. $infos = $this->client->getDatabaseInfos();
  295. $this->assertEquals ( $infos->doc_count, 6 );
  296. $test = $this->client->reduce(false)->endkey("test2")->endkey_docid("five")->getView("test","complex");
  297. // print_r($test);
  298. $this->assertType("object", $test);
  299. $this->assertObjectHasAttribute("total_rows",$test);
  300. $this->assertEquals($test->total_rows, 5);
  301. $this->assertObjectHasAttribute("offset",$test);
  302. $this->assertEquals($test->offset, 0);
  303. $this->assertObjectHasAttribute("rows",$test);
  304. $this->assertType("array", $test->rows);
  305. $this->assertEquals(count($test->rows), 3);
  306. foreach ( $test->rows as $row ) {
  307. $this->assertObjectHasAttribute("id",$row);
  308. $this->assertObjectHasAttribute("key",$row);
  309. $this->assertObjectHasAttribute("value",$row);
  310. }
  311. }
  312. public function testViewLimit () {
  313. $this->_makeView();
  314. $docs = array (
  315. array( "_id"=>"one","type"=>"test","param" => null),
  316. array( "_id"=>"two","type"=>"test2","param" => null),
  317. array( "_id"=>"three","type"=>"test","param" => null),
  318. array( "_id"=>"four","type"=>"test3","param" => null),
  319. array( "_id"=>"five","type"=>"test2","param" => null)
  320. );
  321. $this->client->storeDocs($docs);
  322. $infos = $this->client->getDatabaseInfos();
  323. $this->assertEquals ( $infos->doc_count, 6 );
  324. $test = $this->client->reduce(false)->limit(2)->getView("test","complex");
  325. // print_r($test);
  326. $this->assertType("object", $test);
  327. $this->assertObjectHasAttribute("total_rows",$test);
  328. $this->assertEquals($test->total_rows, 5);
  329. $this->assertObjectHasAttribute("offset",$test);
  330. $this->assertEquals($test->offset, 0);
  331. $this->assertObjectHasAttribute("rows",$test);
  332. $this->assertType("array", $test->rows);
  333. $this->assertEquals(count($test->rows), 2);
  334. foreach ( $test->rows as $row ) {
  335. $this->assertObjectHasAttribute("id",$row);
  336. $this->assertObjectHasAttribute("key",$row);
  337. $this->assertObjectHasAttribute("value",$row);
  338. }
  339. }
  340. public function testViewSkip () {
  341. $this->_makeView();
  342. $docs = array (
  343. array( "_id"=>"one","type"=>"test","param" => null),
  344. array( "_id"=>"two","type"=>"test2","param" => null),
  345. array( "_id"=>"three","type"=>"test","param" => null),
  346. array( "_id"=>"four","type"=>"test3","param" => null),
  347. array( "_id"=>"five","type"=>"test2","param" => null)
  348. );
  349. $this->client->storeDocs($docs);
  350. $infos = $this->client->getDatabaseInfos();
  351. $this->assertEquals ( $infos->doc_count, 6 );
  352. $test = $this->client->reduce(false)->skip(2)->getView("test","complex");
  353. // print_r($test);
  354. $this->assertType("object", $test);
  355. $this->assertObjectHasAttribute("total_rows",$test);
  356. $this->assertEquals($test->total_rows, 5);
  357. $this->assertObjectHasAttribute("offset",$test);
  358. $this->assertEquals($test->offset, 2);
  359. $this->assertObjectHasAttribute("rows",$test);
  360. $this->assertType("array", $test->rows);
  361. $this->assertEquals(count($test->rows), 3);
  362. foreach ( $test->rows as $row ) {
  363. $this->assertObjectHasAttribute("id",$row);
  364. $this->assertObjectHasAttribute("key",$row);
  365. $this->assertObjectHasAttribute("value",$row);
  366. }
  367. }
  368. public function testViewDescending () {
  369. $this->_makeView();
  370. $docs = array (
  371. array( "_id"=>"one","type"=>"test","param" => null),
  372. array( "_id"=>"two","type"=>"test2","param" => null),
  373. array( "_id"=>"three","type"=>"test","param" => null),
  374. array( "_id"=>"four","type"=>"test3","param" => null),
  375. array( "_id"=>"five","type"=>"test2","param" => null)
  376. );
  377. $this->client->storeDocs($docs);
  378. $infos = $this->client->getDatabaseInfos();
  379. $this->assertEquals ( $infos->doc_count, 6 );
  380. $test = $this->client->reduce(false)->getView("test","complex");
  381. // print_r($test);
  382. $this->assertType("object", $test);
  383. $this->assertObjectHasAttribute("total_rows",$test);
  384. $this->assertEquals($test->total_rows, 5);
  385. $this->assertObjectHasAttribute("offset",$test);
  386. $this->assertEquals($test->offset, 0);
  387. $this->assertObjectHasAttribute("rows",$test);
  388. $this->assertType("array", $test->rows);
  389. $this->assertEquals(count($test->rows), 5);
  390. $row = reset($test->rows);
  391. $this->assertObjectHasAttribute("key",$row);
  392. $this->assertEquals($row->key, "test");
  393. $test = $this->client->reduce(false)->descending(true)->getView("test","complex");
  394. // print_r($test);
  395. $this->assertType("object", $test);
  396. $this->assertObjectHasAttribute("total_rows",$test);
  397. $this->assertEquals($test->total_rows, 5);
  398. $this->assertObjectHasAttribute("offset",$test);
  399. $this->assertEquals($test->offset, 0);
  400. $this->assertObjectHasAttribute("rows",$test);
  401. $this->assertType("array", $test->rows);
  402. $this->assertEquals(count($test->rows), 5);
  403. $row = reset($test->rows);
  404. $this->assertObjectHasAttribute("key",$row);
  405. $this->assertEquals($row->key, "test3");
  406. }
  407. public function testViewGroup () {
  408. $this->_makeView();
  409. $docs = array (
  410. array( "_id"=>"one","type"=>"test","param" => 1),
  411. array( "_id"=>"two","type"=>"test2","param" => 2),
  412. array( "_id"=>"three","type"=>"test","param" => 2),
  413. array( "_id"=>"four","type"=>"test3","param" => 1),
  414. array( "_id"=>"five","type"=>"test2","param" => 1)
  415. );
  416. $this->client->storeDocs($docs);
  417. $infos = $this->client->getDatabaseInfos();
  418. $this->assertEquals ( $infos->doc_count, 6 );
  419. $doc = couchDocument::getInstance($this->client, "_design/test");
  420. $views = $doc->views;
  421. $views->multigroup = new stdClass();
  422. $views->multigroup->map = "function (doc) {
  423. if ( doc.type && doc.param ) {
  424. emit( [doc.type, doc.param], 1);
  425. }
  426. }";
  427. $views->multigroup->reduce = "function(keys,values) {
  428. return sum(values);
  429. }";
  430. $doc->views = $views;
  431. $test = $this->client->group(true)->getView("test","multigroup");
  432. $this->assertType("object", $test);
  433. $this->assertObjectHasAttribute("rows",$test);
  434. $this->assertType("array", $test->rows);
  435. $this->assertEquals(count($test->rows), 5);
  436. $test = $this->client->group(true)->group_level(1)->getView("test","multigroup");
  437. $this->assertType("object", $test);
  438. $this->assertObjectHasAttribute("rows",$test);
  439. $this->assertType("array", $test->rows);
  440. $this->assertEquals(count($test->rows), 3);
  441. }
  442. public function testViewAsArray () {
  443. $this->_makeView();
  444. $docs = array (
  445. array( "_id"=>"one","type"=>"test","param" => null),
  446. array( "_id"=>"two","type"=>"test2","param" => null),
  447. array( "_id"=>"three","type"=>"test","param" => null),
  448. array( "_id"=>"four","type"=>"test3","param" => null),
  449. array( "_id"=>"five","type"=>"test2","param" => null)
  450. );
  451. $this->client->storeDocs($docs);
  452. $infos = $this->client->getDatabaseInfos();
  453. $this->assertEquals ( $infos->doc_count, 6 );
  454. $test = $this->client->reduce(false)->asArray()->getView("test","complex");
  455. // print_r($test);
  456. $this->assertType("array", $test);
  457. }
  458. }