From d9ddf1dca12600cf0d32720f766ec4797b920344 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Fri, 21 May 2010 04:17:55 -0400 Subject: [PATCH] Added tests (84,19% of coverance) to release first stable ASAP - Added getIndexes() which returns a list of all indexes - Drop table if fails returns an exception - Added tests for References --- lib/ActiveMongo.php | 25 ++++++++++++---- tests/ActiveMongoSuite.php | 16 +++++++--- tests/Models.php | 5 ++++ tests/QueryTest.php | 75 +++++++++++++++++++++++++++++++++++++++++++++- tests/ReferencesTest.php | 60 +++++++++++++++++++++++++++++++++++++ tests/__SleepTest.php | 4 ++- 6 files changed, 174 insertions(+), 11 deletions(-) diff --git a/lib/ActiveMongo.php b/lib/ActiveMongo.php index dcd2585..2f5089c 100644 --- a/lib/ActiveMongo.php +++ b/lib/ActiveMongo.php @@ -213,7 +213,7 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess protected function getDatabaseName() { if (is_null(self::$_db)) { - throw new MongoException("There is no information about the default DB name"); + throw new ActiveMongo_Exception("There is no information about the default DB name"); } return self::$_db; } @@ -831,7 +831,10 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess $obj->triggerEvent('before_drop'); $result = $obj->_getCollection()->drop(); $obj->triggerEvent('after_drop'); - return $result; + if ($result['ok'] != 1) { + throw new ActiveMongo_Exception($result['errmsg']); + } + return TRUE; } // }}} @@ -932,6 +935,18 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess } // }}} + // Array getIndexes() {{{ + /** + * Return an array with all indexes + * + * @return array + */ + final static function getIndexes() + { + return self::_getCollection()->getIndexInfo(); + } + // }}} + // string __toString() {{{ /** * To String @@ -1026,7 +1041,7 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess final function next() { if ($this->_cloned) { - throw new MongoException("Cloned objects can't iterate"); + throw new ActiveMongo_Exception("Cloned objects can't iterate"); } if (!$this->_cursor_ex) { $result = $this->_cursor->next(); @@ -1138,7 +1153,7 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess final function getReference($dynamic=FALSE) { if (!$this->getID() && !$dynamic) { - return null; + return NULL; } $document = array( @@ -1208,7 +1223,7 @@ abstract class ActiveMongo implements Iterator, Countable, ArrayAccess private function _deferencingCreateObject($class) { if (!is_subclass_of($class, __CLASS__)) { - throw new MongoException("Fatal Error, imposible to create ActiveMongo object of {$class}"); + throw new ActiveMongo_Exception("Fatal Error, imposible to create ActiveMongo object of {$class}"); } return new $class; } diff --git a/tests/ActiveMongoSuite.php b/tests/ActiveMongoSuite.php index 5603ff1..e0a4e09 100644 --- a/tests/ActiveMongoSuite.php +++ b/tests/ActiveMongoSuite.php @@ -16,10 +16,18 @@ class ActiveMongoSuite extends PHPUnit_Framework_TestSuite public function __construct() { ActiveMongo::connect(DB, "localhost"); - Dummy::drop(); - Model1::drop(); - Model2::drop(); - Model3::drop(); + try { + Dummy::drop(); + } catch (Exception $e) {} + try { + Model1::drop(); + } catch (Exception $e) {} + try { + Model2::drop(); + } catch (Exception $e) {} + try { + Model3::drop(); + } catch (Exception $e) {} } public static function suite() diff --git a/tests/Models.php b/tests/Models.php index 069e607..1464f78 100644 --- a/tests/Models.php +++ b/tests/Models.php @@ -13,6 +13,11 @@ class Model1 extends ActiveMongo 'a' ); + function setup() + { + $this->addIndex(array('a' => 1)); + } + } class Model2 extends ActiveMongo diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 03a94c1..8381bfb 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -5,7 +5,9 @@ class QueryTest extends PHPUnit_Framework_TestCase function __construct() { - Model3::drop(); + try { + Model3::drop(); + } catch (Exception $e) {} $data = array(); /* Valid data */ @@ -25,6 +27,13 @@ class QueryTest extends PHPUnit_Framework_TestCase $this->assertEquals($c->count(), 5000); } + function testInstall() + { + ActiveMongo::install(); + $indexes = Model1::getIndexes(); + $this->assertTrue(isset($indexes[1]['key']['a'])); + } + function testQuery() { $c = new Model1; @@ -282,11 +291,75 @@ class QueryTest extends PHPUnit_Framework_TestCase function testDelete() { + /* Delete using a criteria */ $c = new Model3; $c->where('int < ', 100); $c->delete(); $this->assertEquals($c->count(), 4900); + + /* delete on iteration (element by element) */ + $c = new Model3; + $c->where('int', array(200, 300)); + + $i = 0; + foreach ($c as $d) { + $d->delete(); + $this->assertFalse(isset($c->int)); + $i++; + } + + $c->reset(); + + $this->assertEquals(2, $i); + $this->assertEquals($c->count(), 4898); + } + + function testDrop() + { + $c = new Dummy; + $c['foo'] = 'bar'; + $c->save(); + + $this->assertFalse(ActiveMongo::drop()); + $this->assertTrue(Dummy::drop()); + try { + $this->assertFalse(Dummy::drop()); + } catch (Exception $e) { + $this->assertTrue(TRUE); + } + } + + function testInvalidBatchInsert() + { + /* Invalid document for Model2 */ + $documents = array( + array('foo' => 'bar'), + ); + try { + /* Invalid call */ + ActiveMongo::BatchInsert($documents); + $this->assertTrue(False); + } catch (Exception $e) { + $this->assertTrue(TRUE); + } + + try { + Model2::BatchInsert($documents); + $this->assertTrue(False); + } catch (ActiveMongo_FilterException $e) { + $this->assertTrue(FALSE); + } catch (MongoException $e) { + $this->assertTrue(TRUE); + } + + try { + Model2::BatchInsert($documents, TRUE, FALSE); + $this->assertTrue(False); + } catch (ActiveMongo_FilterException $e) { + $this->assertTrue(TRUE); + } + } function testInvalidQueries() diff --git a/tests/ReferencesTest.php b/tests/ReferencesTest.php index f64aa79..9407914 100644 --- a/tests/ReferencesTest.php +++ b/tests/ReferencesTest.php @@ -4,9 +4,11 @@ class ReferencesTest extends PHPUnit_Framework_TestCase { public function testReferences() { + $c = new Model1; $c->a = "foobar"; $c->save(); + $ref = array( '$ref' => 'model1', '$id' => $c->getID(), @@ -14,6 +16,64 @@ class ReferencesTest extends PHPUnit_Framework_TestCase 'class' => 'Model1' ); $this->assertEquals($c->getReference(), $ref); + + $d = new Model1; + $d->a = "barfoo"; + /* Reference into a document */ + $d->next = $c; + /* References into sub documents */ + $d->nested = array($c, $c); + /* Get Dynamic query; AKA save the query */ + /* in this case it would be a get all */ + $query = new Model1; + $query->where('a', 'foobar'); + $query->doQuery(); + $d->query = $query->getReference(TRUE); + + $d->save(); + + } + + /** + * @depends testReferences + */ + public function testReferenceSave() + { + $d = new Model1; + $d->where('a', 'barfoo'); + + foreach ($d as $doc) { + $this->assertTrue(isset($doc->next)); + $this->assertTrue(MongoDBRef::isRef($doc->next)); + $this->assertTrue(MongoDBRef::isRef($doc->nested[0])); + $this->assertTrue(MongoDBRef::isRef($doc->nested[1])); + $this->assertTrue(MongoDBRef::isRef($doc->query)); + + /* Check dynamic references properties */ + $this->assertTrue(is_array($doc->query['dynamic'])); + $this->assertTrue(count($doc->query['dynamic']) > 0); + + /* Deference */ + $doc->doDeferencing(); + + /* Test */ + $this->assertTrue($doc->next InstanceOf Model1); + $this->assertTrue($doc->nested[0] InstanceOf Model1); + $this->assertTrue($doc->nested[1] InstanceOf Model1); + $this->assertTrue(is_array($doc->query)); + $this->assertTrue($doc->query[0] InstanceOf Model1); + + /* Testing Iteration in defered documents */ + /* They should fail because they are cloned */ + /* instances of a real document */ + try { + $doc->next->next(); + $this->assertTrue(FALSE); + } catch (ActiveMongo_Exception $e) { + $this->assertTrue(TRUE); + } + } + } } diff --git a/tests/__SleepTest.php b/tests/__SleepTest.php index 89f4007..403a0ad 100644 --- a/tests/__SleepTest.php +++ b/tests/__SleepTest.php @@ -15,7 +15,9 @@ class SleepTest extends PHPUnit_Framework_TestCase { function __construct() { - SleepModel::drop(); + try { + SleepModel::drop(); + } catch (Exception $e) {} } function testSleep() -- 2.11.4.GIT