From 562979c4a8fe47952edf7986d4144624e41630f7 Mon Sep 17 00:00:00 2001 From: wei <> Date: Mon, 4 Dec 2006 02:42:57 +0000 Subject: add unit tests for active record and sqlmap --- .../ActiveRecordDynamicCallTestCase.php | 52 +++++++ .../ActiveRecord/ActiveRecordFinderTestCase.php | 45 ++++++ .../ActiveRecord/ActiveRecordMySql5TestCase.php | 47 ++++++ .../ActiveRecord/ActiveRecordRegistryTestCase.php | 168 +++++++++++++++++++++ .../ActiveRecord/BaseActiveRecordTestCase.php | 34 +++++ .../ActiveRecord/CountRecordsTestCase.php | 29 ++++ .../ActiveRecord/DeleteByPkTestCase.php | 31 ++++ .../simple_unit/ActiveRecord/FindBySqlTestCase.php | 21 +++ tests/simple_unit/ActiveRecord/SqliteTestCase.php | 22 +++ .../ActiveRecord/UserRecordTestCase.php | 67 ++++++++ .../ActiveRecord/ViewRecordTestCase.php | 76 ++++++++++ tests/simple_unit/ActiveRecord/ar_test.db | Bin 0 -> 5120 bytes tests/simple_unit/ActiveRecord/records/Blogs.php | 15 ++ .../ActiveRecord/records/DepSections.php | 17 +++ .../ActiveRecord/records/DepartmentRecord.php | 19 +++ .../ActiveRecord/records/SimpleUser.php | 15 ++ .../ActiveRecord/records/SqliteUsers.php | 17 +++ .../ActiveRecord/records/UserRecord.php | 39 +++++ 18 files changed, 714 insertions(+) create mode 100644 tests/simple_unit/ActiveRecord/ActiveRecordDynamicCallTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/ActiveRecordFinderTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php create mode 100644 tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/BaseActiveRecordTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/CountRecordsTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/DeleteByPkTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/FindBySqlTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/SqliteTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/UserRecordTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/ViewRecordTestCase.php create mode 100644 tests/simple_unit/ActiveRecord/ar_test.db create mode 100644 tests/simple_unit/ActiveRecord/records/Blogs.php create mode 100644 tests/simple_unit/ActiveRecord/records/DepSections.php create mode 100644 tests/simple_unit/ActiveRecord/records/DepartmentRecord.php create mode 100644 tests/simple_unit/ActiveRecord/records/SimpleUser.php create mode 100644 tests/simple_unit/ActiveRecord/records/SqliteUsers.php create mode 100644 tests/simple_unit/ActiveRecord/records/UserRecord.php (limited to 'tests/simple_unit/ActiveRecord') diff --git a/tests/simple_unit/ActiveRecord/ActiveRecordDynamicCallTestCase.php b/tests/simple_unit/ActiveRecord/ActiveRecordDynamicCallTestCase.php new file mode 100644 index 00000000..8f8868e9 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/ActiveRecordDynamicCallTestCase.php @@ -0,0 +1,52 @@ +setDbConnection($conn); + } + + function test_dynamic_call() + { + $finder = DepartmentRecord::finder(); + $r2 = $finder->findByName('Facilities'); + $this->assertNotNull($r2); + } + + function test_dynamic_multiple_field_call() + { + $finder = DepartmentRecord::finder(); + $rs = $finder->findByNameAndActive('Marketing',true); + $this->assertNotNull($rs); + } + + function test_dynamic_call_missing_parameters_throws_exception() + { + $finder = DepartmentRecord::finder(); + try + { + $rs = $finder->findByNameAndActive('Marketing'); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_dynamic_call_extras_parameters_ok() + { + $finder = DepartmentRecord::finder(); + $rs = $finder->findByNameAndActive('Marketing',true,true); + $this->assertNotNull($rs); + } + +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/ActiveRecordFinderTestCase.php b/tests/simple_unit/ActiveRecord/ActiveRecordFinderTestCase.php new file mode 100644 index 00000000..597f8a6b --- /dev/null +++ b/tests/simple_unit/ActiveRecord/ActiveRecordFinderTestCase.php @@ -0,0 +1,45 @@ +setDbConnection($conn); + } + + function testFindBySQL_returns_one_record() + { + $department = DepartmentRecord::finder()->find('department_id < ?', 5); + $this->assertNotNull($department); + } + + function testFindBySQL_returns_zero_records() + { + $department = DepartmentRecord::finder()->find('department_id > ?', 15); + $this->assertNull($department); + } + + function test_find_by_sql_returns_iterator() + { + $deps = DepartmentRecord::finder()->findAll('department_id < :id', array('id'=>5)); + $this->assertEqual(count($deps),4); + } + + function test_find_by_multiple_parameters() + { + $department = DepartmentRecord::finder()->find('department_id < ? AND "order" > ?', 5,2); + $this->assertNotNull($department); + } + + function test_find_by_array_parameter() + { + $department = DepartmentRecord::finder()->find('department_id < ? AND "order" > ?', array(5,2)); + $this->assertNotNull($department); + } + +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php b/tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php new file mode 100644 index 00000000..b02de3de --- /dev/null +++ b/tests/simple_unit/ActiveRecord/ActiveRecordMySql5TestCase.php @@ -0,0 +1,47 @@ +setDbConnection($conn); + } + + function test_find_first_blog() + { + $blog = Blogs::finder()->findByPk(1); + $this->assertNotNull($blog); + } + + function test_insert_new_blog() + { + $blog = new Blogs(); + $blog->blog_name = 'test1'; + $blog->blog_author = 'wei'; + + $this->assertTrue($blog->save()); + + $blog->blog_name = 'test2'; + + $this->assertTrue($blog->save()); + + $check = Blogs::finder()->findByPk($blog->blog_id); + + $this->assertSameBlog($check,$blog); + + $this->assertTrue($blog->delete()); + } + + function assertSameBlog($check, $blog) + { + $props = array('blog_id', 'blog_name', 'blog_author'); + foreach($props as $prop) + $this->assertEqual($check->{$prop}, $blog->{$prop}); + } + +} +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php b/tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php new file mode 100644 index 00000000..5ab89206 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/ActiveRecordRegistryTestCase.php @@ -0,0 +1,168 @@ +assertTrue($registry->getIsNewObject($obj)); + $this->assertFalse($registry->getIsDirtyObject($obj)); + $this->assertFalse($registry->getIsRemovedObject($obj)); + $this->assertFalse($registry->getIsCleanObject($obj)); + } + + function test_clean_object_registers_without_error() + { + $obj = new StateTestObject(); + $registry = new TActiveRecordStateRegistry(); + $registry->registerClean($obj); + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertFalse($registry->getIsDirtyObject($obj)); + $this->assertFalse($registry->getIsRemovedObject($obj)); + $this->assertTrue($registry->getIsCleanObject($obj)); + } + + function test_clean_object_becomes_dirty_when_changed() + { + $obj = new StateTestObject(); + $registry = new TActiveRecordStateRegistry(); + + $registry->registerClean($obj); + + $obj->propB='b'; + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertTrue($registry->getIsDirtyObject($obj)); + $this->assertFalse($registry->getIsRemovedObject($obj)); + $this->assertFalse($registry->getIsCleanObject($obj)); + } + + function test_removed_object_must_register_as_clean_first() + { + $obj = new StateTestObject(); + $registry = new TActiveRecordStateRegistry(); + + try + { + $registry->registerRemoved($obj); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_removed_object_registers_without_error() + { + $obj = new StateTestObject(); + $registry = new TActiveRecordStateRegistry(); + $registry->registerClean($obj); + + $registry->registerRemoved($obj); + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertFalse($registry->getIsDirtyObject($obj)); + $this->assertTrue($registry->getIsRemovedObject($obj)); + $this->assertFalse($registry->getIsCleanObject($obj)); + } + + + function test_removed_object_can_not_become_clean() + { + $obj = new StateTestObject(); + $registry = new TActiveRecordStateRegistry(); + $registry->registerClean($obj); + + $registry->registerRemoved($obj); + + try + { + $registry->registerClean($obj); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertFalse($registry->getIsDirtyObject($obj)); + $this->assertTrue($registry->getIsRemovedObject($obj)); + $this->assertFalse($registry->getIsCleanObject($obj)); + } + + function test_remove_dirty_object() + { + $obj = new StateTestObject(); + $registry = new TActiveRecordStateRegistry(); + + $registry->registerClean($obj); + + $obj->propB='b'; + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertTrue($registry->getIsDirtyObject($obj)); + $this->assertFalse($registry->getIsRemovedObject($obj)); + $this->assertFalse($registry->getIsCleanObject($obj)); + + $registry->registerRemoved($obj); + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertFalse($registry->getIsDirtyObject($obj)); + $this->assertTrue($registry->getIsRemovedObject($obj)); + $this->assertFalse($registry->getIsCleanObject($obj)); + + try + { + $registry->registerClean($obj); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_clean_dirty_clean_object() + { + $obj = new StateTestObject(); + $registry = new TActiveRecordStateRegistry(); + + $registry->registerClean($obj); + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertFalse($registry->getIsDirtyObject($obj)); + $this->assertFalse($registry->getIsRemovedObject($obj)); + $this->assertTrue($registry->getIsCleanObject($obj)); + + $obj->propB='b'; + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertTrue($registry->getIsDirtyObject($obj)); + $this->assertFalse($registry->getIsRemovedObject($obj)); + $this->assertFalse($registry->getIsCleanObject($obj)); + + $registry->registerClean($obj); + + $this->assertFalse($registry->getIsNewObject($obj)); + $this->assertFalse($registry->getIsDirtyObject($obj)); + $this->assertFalse($registry->getIsRemovedObject($obj)); + $this->assertTrue($registry->getIsCleanObject($obj)); + } + +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/BaseActiveRecordTestCase.php b/tests/simple_unit/ActiveRecord/BaseActiveRecordTestCase.php new file mode 100644 index 00000000..9e48fe5f --- /dev/null +++ b/tests/simple_unit/ActiveRecord/BaseActiveRecordTestCase.php @@ -0,0 +1,34 @@ +assertIdentical($obj1,$obj2); + } + + function test_finder_throw_exception_when_save() + { + $obj = TActiveRecord::getRecordFinder('BaseRecordTest'); + try + { + $obj->save(); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/CountRecordsTestCase.php b/tests/simple_unit/ActiveRecord/CountRecordsTestCase.php new file mode 100644 index 00000000..d9a98622 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/CountRecordsTestCase.php @@ -0,0 +1,29 @@ +setDbConnection($conn); + } + + function test_count() + { + $finder = DepartmentRecord::finder(); + $count = $finder->count('"order" > ?', 2); + $this->assertTrue($count > 0); + } + + function test_count_zero() + { + $finder = DepartmentRecord::finder(); + $count = $finder->count('"order" > ?', 11); + $this->assertEqual($count,0); + } +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/DeleteByPkTestCase.php b/tests/simple_unit/ActiveRecord/DeleteByPkTestCase.php new file mode 100644 index 00000000..45415b90 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/DeleteByPkTestCase.php @@ -0,0 +1,31 @@ +setDbConnection($conn); + } + + function test_delete_by_pks() + { + $finder = DepartmentRecord::finder(); + $this->assertEqual($finder->deleteByPk(100),0); + $this->assertEqual($finder->deleteByPk(100, 101),0); + $this->assertEqual($finder->deleteByPk(array(100, 101)),0); + } + + function test_delete_by_composite_pks() + { + $finder = DepSections::finder(); + $this->assertEqual($finder->deleteByPk(array(100,101)),0); + $this->assertEqual($finder->deleteByPk(array(100, 101), array(102, 103)),0); + $this->assertEqual($finder->deleteByPk(array(array(100, 101), array(102, 103))),0); + } +} +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/FindBySqlTestCase.php b/tests/simple_unit/ActiveRecord/FindBySqlTestCase.php new file mode 100644 index 00000000..953f83e6 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/FindBySqlTestCase.php @@ -0,0 +1,21 @@ +setDbConnection($conn); + } + + function test_find_by_sql() + { + $deps = DepartmentRecord::finder()->findBySql('SELECT * FROM departments'); + $this->assertTrue(count($deps) > 0); + } +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/SqliteTestCase.php b/tests/simple_unit/ActiveRecord/SqliteTestCase.php new file mode 100644 index 00000000..01026c91 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/SqliteTestCase.php @@ -0,0 +1,22 @@ +setDbConnection($conn); + } + + function test_finder() + { + $finder = SqliteUsers::finder(); + $user = $finder->findByPk('test'); + $this->assertNotNull($user); + } +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/UserRecordTestCase.php b/tests/simple_unit/ActiveRecord/UserRecordTestCase.php new file mode 100644 index 00000000..483dc42f --- /dev/null +++ b/tests/simple_unit/ActiveRecord/UserRecordTestCase.php @@ -0,0 +1,67 @@ +setDbConnection($conn); + } + + function testFindByPk() + { + $user1 = UserRecord::finder()->findByPk('admin'); + $this->assertNotNull($user1); + } + + function test_same_data_returns_same_object() + { + $user1 = UserRecord::finder()->findByPk('admin'); + $this->assertNotNull($user1); + + $user2 = UserRecord::finder()->findByPk('admin'); + $this->assertTrue($user1===$user2); + } + + function testFindByPk_returns_null() + { + $user = UserRecord::finder()->findByPk('me'); + $this->assertNull($user); + } + + function test_Create_new_user_returns_true() + { + $user = new UserRecord; + $user->username = 'hello'; + $user->password = md5('asd'); + $user->email = 'asdasd'; + $user->first_name = 'wei'; + $user->last_name = 'zhuo'; + + $this->assertTrue($user->save()); + + $user->password = md5('more'); + + $this->assertTrue($user->save()); + + $check = UserRecord::finder()->findByPk('hello'); + + $this->assertSameUser($user, $check); + + $this->assertTrue($user->delete()); + } + + function assertSameUser($user,$check) + { + $props = array('username', 'password', 'email', 'first_name', 'last_name', 'job_title', + 'work_phone', 'work_fax', 'active', 'department_id', 'salutation', + 'hint_question', 'hint_answer'); + foreach($props as $prop) + $this->assertEqual($user->$prop,$check->$prop); + } +} + +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/ViewRecordTestCase.php b/tests/simple_unit/ActiveRecord/ViewRecordTestCase.php new file mode 100644 index 00000000..29fdee49 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/ViewRecordTestCase.php @@ -0,0 +1,76 @@ +setDbConnection($conn); + } + + function test_view_record() + { + $users = SimpleUser::finder()->findAll(); + $this->assertTrue(count($users) > 0); + } + + function test_save_view_record_throws_exception() + { + $user = new SimpleUser(); + try + { + $user->save(); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_update_view_record_throws_exception() + { + $user = SimpleUser::finder()->findByUsername('admin'); + $user->username = 'ads'; + try + { + $user->save(); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_find_by_pk_throws_exception() + { + try + { + $user = SimpleUser::finder()->findByPk('admin'); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } + + function test_delete_by_pk_throws_exception() + { + try + { + SimpleUser::finder()->deleteByPk('admin'); + $this->fail(); + } + catch(TActiveRecordException $e) + { + $this->pass(); + } + } +} +?> \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/ar_test.db b/tests/simple_unit/ActiveRecord/ar_test.db new file mode 100644 index 00000000..7549bb66 Binary files /dev/null and b/tests/simple_unit/ActiveRecord/ar_test.db differ diff --git a/tests/simple_unit/ActiveRecord/records/Blogs.php b/tests/simple_unit/ActiveRecord/records/Blogs.php new file mode 100644 index 00000000..69bdecd9 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/records/Blogs.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/records/DepSections.php b/tests/simple_unit/ActiveRecord/records/DepSections.php new file mode 100644 index 00000000..9563dda6 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/records/DepSections.php @@ -0,0 +1,17 @@ + \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php b/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php new file mode 100644 index 00000000..62e5c3e4 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/records/DepartmentRecord.php @@ -0,0 +1,19 @@ + \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/records/SimpleUser.php b/tests/simple_unit/ActiveRecord/records/SimpleUser.php new file mode 100644 index 00000000..a6eb2f81 --- /dev/null +++ b/tests/simple_unit/ActiveRecord/records/SimpleUser.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/records/SqliteUsers.php b/tests/simple_unit/ActiveRecord/records/SqliteUsers.php new file mode 100644 index 00000000..7a8f3cbb --- /dev/null +++ b/tests/simple_unit/ActiveRecord/records/SqliteUsers.php @@ -0,0 +1,17 @@ + \ No newline at end of file diff --git a/tests/simple_unit/ActiveRecord/records/UserRecord.php b/tests/simple_unit/ActiveRecord/records/UserRecord.php new file mode 100644 index 00000000..45b74d3a --- /dev/null +++ b/tests/simple_unit/ActiveRecord/records/UserRecord.php @@ -0,0 +1,39 @@ +_level; + } + + public function setLevel($level) + { + $this->_level=TPropertyValue::ensureInteger($level); + } + + public static function finder() + { + return self::getRecordFinder('UserRecord'); + } +} + +?> \ No newline at end of file -- cgit v1.2.3