1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | <?php require_once (realpath(dirname(__FILE__) . '/../src/settee.php')); require_once (dirname(__FILE__) . '/SetteeTestCase.class.php'); class SetteeDatabaseTest extends SetteeTestCase { private $db; public function setUp() { parent::setUp(); $dbname = "settee_tests_" . md5(microtime(true)); $this->db = $this->server->get_db($dbname); $this->server->create_db($this->db); } public function test_document_lifecycle_objectbased() { $doc = new StdClass(); $doc->firstName = "Irakli"; $doc->lastName = "Nadareishvili"; $doc->IQ = 200; $doc->hobbies = array("skiing", "swimming"); $doc->pets = array ("whitey" => "labrador", "mikey" => "pug"); $doc = $this->db->save($doc); $this->assertTrue(!empty($doc->_id) && !empty($doc->_rev), "Document creation success [object-based]"); $_rev = $doc->_rev; $doc = $this->db->get($doc->_id); $this->assertEquals($_rev, $doc->_rev, "Document retrieval success [object-based] test"); $doc->firstName = "Ika"; $db_doc = $this->db->save($doc); $this->assertEquals($doc->firstName, $db_doc->firstName, "Document update success [object-based]"); $this->db->delete($doc); try { $doc = $this->db->get($doc->_id); } catch (SetteeRestClientException $e) { // we expect exception to fire, so this is good. return; } $this->fail('Document still available for retrieval after being deleted. [object-based]'); } // Should work with json string as well: // public function test_document_lifecycle_jsonbased() { $doc = '{"firstName":"Irakli","lastName":"Nadareishvili","IQ":200,"hobbies":["skiing","swimming"],"pets":{"whitey":"labrador","mikey":"pug"}}'; $doc = $this->db->save($doc); $this->assertTrue(!empty($doc->_id) && !empty($doc->_rev), "Document creation success [json-based]"); $_rev = $doc->_rev; $db_doc = $this->db->get($doc->_id); $this->assertEquals($_rev, $db_doc->_rev, "Document retrieval success [json-based] test"); $doc = '{'; $doc .= '"_id":"' . $db_doc->_id . '",'; $doc .= '"_rev":"' . $db_doc->_rev . '",'; $doc .= '"firstName":"Ika","lastName":"Nadareishvili","IQ":200,"hobbies":["skiing","swimming"],"pets":{"whitey":"labrador","mikey":"pug"}}'; $orig_doc = json_decode($doc); $db_doc = $this->db->save($doc); $this->assertEquals($orig_doc->firstName, $db_doc->firstName, "Document update success [json-based]"); $doc = '{'; $doc .= '"_id":"' . $db_doc->_id . '",'; $doc .= '"_rev":"' . $db_doc->_rev . '",'; $doc .= '"firstName":"Ika","lastName":"Nadareishvili","IQ":200,"hobbies":["skiing","swimming"],"pets":{"whitey":"labrador","mikey":"pug"}}'; $this->db->delete($doc); try { $doc = $this->db->get($db_doc->_id); } catch (SetteeRestClientException $e) { // we expect exception to fire, so this is good. return; } $this->fail('Document still available for retrieval after being deleted. [object-based]'); } public function test_invalid_document() { $doc = 12345; try { $doc = $this->db->save($doc); } catch (SetteeRestClientException $e) { // we expect exception to fire, so this is good. return; } $this->fail('Document saved with invalid format'); } public function test_get_rev() { $doc = new stdClass(); $doc->_id = "some_fixed_id"; $doc = $this->db->save($doc); $_rev = $doc->_rev; $db_rev = $this->db->get_rev($doc->_id); $this->assertEquals($_rev, $db_rev, "Document Revision retrieval success"); // _rev is now attached to this object due to last ->save() call $doc->_id = "some_fixed_id"; $doc->title = "Some Fixed ID"; $doc = $this->db->save($doc); $_rev = $doc->_rev; $db_rev = $this->db->get_rev($doc->_id); $this->assertEquals($_rev, $db_rev, "Document Revision retrieval success after re-save"); } public function test_save_auto_revision_detection() { $doc = new stdClass(); $doc->_id = "some_fixed_id"; $this->db->save($doc); $doc = new stdClass(); $doc->_id = "some_fixed_id"; $doc->extra_field = "some other value"; $new_doc = $this->db->save($doc, true); $this->assertEquals ($new_doc->extra_field, "some other value", "Testing auto-rev detection by save method"); } public function test_inline_attachment_json() { $doc = '{ "_id":"attachment_doc", "_attachments": { "foo.txt": { "content_type":"text\/plain", "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=" } } }'; $db_doc = $this->db->save($doc); $this->assertTrue(is_object($db_doc->_attachments), "Inline attachment save successful [json-based]"); } public function test_inline_attachment_obj_content() { $doc = new stdClass(); $doc->_id = "attachment_doc"; $this->db->add_attachment($doc, "foo.txt", "This is some text to be encoded", "text/plain"); $db_doc = $this->db->save($doc); $this->assertTrue(is_object($db_doc->_attachments), "Inline attachment save successful [object-based]"); $doc = new stdClass(); $doc->_id = "attachment_doc_autodetect"; $this->db->add_attachment($doc, "foo.txt", "This is some other text to be encoded"); $db_doc = $this->db->save($doc); $this->assertTrue(is_object($db_doc->_attachments), "Inline attachment save successful [object-based, mime auto-detection]"); } public function test_inline_attachment_obj_file() { $doc = new stdClass(); $doc->_id = "attachment_doc"; $file_path = dirname(__FILE__) . "/resources/couch-logo.pdf"; $this->db->add_attachment_file($doc, "foo.pdf", $file_path, "application/pdf"); $db_doc = $this->db->save($doc); $this->assertTrue(is_object($db_doc->_attachments), "Inline attachment of file successful"); $doc = new stdClass(); $doc->_id = "attachment_doc_autodetect"; $file_path = dirname(__FILE__) . "/resources/couch-logo.pdf"; $this->db->add_attachment_file($doc, "foo.pdf", $file_path); $db_doc = $this->db->save($doc); $this->assertTrue(is_object($db_doc->_attachments), "Inline attachment of file successful w/ mime type auto-detection"); } public function test_view_lifecycle() { $this->_create_some_sample_docs(); $map_src = <<<VIEW function(doc) { if(doc.date && doc.title) { emit(doc.date, doc.title); } } VIEW; $view = $this->db->save_view("foo_views", "bar_view", $map_src); $this->assertEquals("_design/foo_views", $view->_id, "View Creation Success"); $view = $this->db->get_view("foo_views", "bar_view"); $this->assertEquals(3, $view->total_rows, "Running a View Success"); $map_src = <<<VIEW function(doc) { if(doc.date) { emit(doc.date, doc); } } VIEW; $view = $this->db->save_view("foo_views", "bar_view", $map_src); $this->assertEquals("_design/foo_views", $view->_id, "View Update Success"); $view = $this->db->get_view("foo_views", "bar_view"); $this->assertEquals("Well hello and welcome to my new blog...", $view->rows[0]->value->body, "Running a View Success (after update)"); $view = $this->db->get_view("foo_views", "bar_view", "2009/02/17 21:13:39"); $this->assertEquals("Bought a Cat", $view->rows[0]->value->title, "Running a Parametrized View"); $view = $this->db->get_view("foo_views", "bar_view", array("2009/01/30 18:04:11", "2009/02/17 21:13:39")); $this->assertEquals("Biking", $view->rows[0]->value->title, "Running a Parametrized View with range"); $view = $this->db->get_view("foo_views", "bar_view", array("2009/02/17 21:13:39", "2009/01/30 18:04:11"), true); $this->assertEquals("Bought a Cat", $view->rows[0]->value->title, "Running a Parametrized View with range, descending"); $this->assertEquals(2, count($view->rows), "Running a Parametrized View with range, descending [count]"); } function test_two_views_in_a_design_doc() { $map_src = <<<VIEW function(doc) { if(doc.date && doc.title) { emit(doc.date, doc.title); } } VIEW; $view = $this->db->save_view("a_settee_design_doc", "foo_view", $map_src); $this->assertTrue(isset($view->views->foo_view), "View1 Creation Success"); $view = $this->db->save_view("a_settee_design_doc", "bar_view", $map_src); $this->assertTrue(isset($view->views->bar_view), "View2 Creation Success"); } /** * Create some sample docs for running tests on them. * * <p>This sample was taken from a wonderful book: * CouchDB: The Definitive Guide (Animal Guide) by J. Chris Anderson, Jan Lehnardt and Noah Slater * http://www.amazon.com/CouchDB-Definitive-Guide-Relax-Animal/dp/0596155891/ref=sr_1_1?ie=UTF8&qid=1311533443&sr=8-1 * * @return void */ private function _create_some_sample_docs() { $doc = new stdClass(); $doc->_id = "biking"; $doc->title = "Biking"; $doc->body = "My biggest hobby is mountainbiking"; $doc->date = "2009/01/30 18:04:11"; $this->db->save($doc); $doc = new stdClass(); $doc->_id = "bought-a-cat"; $doc->title = "Bought a Cat"; $doc->body = "I went to the the pet store earlier and brought home a little kitty..."; $doc->date = "2009/02/17 21:13:39"; $this->db->save($doc); $doc = new stdClass(); $doc->_id = "hello-world"; $doc->title = "Hello World"; $doc->body = "Well hello and welcome to my new blog..."; $doc->date = "2009/01/15 15:52:20"; $this->db->save($doc); } public function tearDown() {   |