From 0a2553cce596c7f70e273accb604cb10d3581fd2 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Mon, 3 Oct 2011 14:51:07 -0430 Subject: [PATCH] Making model tests less time dependent --- .../Component/Auth/FormAuthenticateTest.php | 5 +- .../Test/Case/Model/ModelIntegrationTest.php | 85 ++++---- lib/Cake/Test/Case/Model/ModelWriteTest.php | 197 ++++++++++-------- 3 files changed, 158 insertions(+), 129 deletions(-) diff --git a/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php b/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php index 52b0683faa9..bc9a22beb5a 100644 --- a/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php +++ b/lib/Cake/Test/Case/Controller/Component/Auth/FormAuthenticateTest.php @@ -179,9 +179,10 @@ public function testPluginModel() { $expected = array( 'id' => 1, 'username' => 'gwoo', - 'created' => '2007-03-17 01:16:23', - 'updated' => date('Y-m-d H:i:s') + 'created' => '2007-03-17 01:16:23' ); + $this->assertTrue($result['updated'] >= date('Y-m-d H:i:s')); + unset($result['updated']); $this->assertEquals($expected, $result); CakePlugin::unload(); } diff --git a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php index ab7076c21e1..10bd8576c6e 100644 --- a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php +++ b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php @@ -1639,46 +1639,51 @@ public function testWithAssociation() { 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => $ts), - 'SomethingElse' => array( - array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31', - 'JoinThing' => array( - 'doomed' => true, - 'something_id' => '1', - 'something_else_id' => '1' - )), - array( - 'id' => '2', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31', - 'JoinThing' => array( - 'doomed' => true, - 'something_id' => '1', - 'something_else_id' => '2' - )), - array( - 'id' => '3', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31', - 'JoinThing' => array( - 'doomed' => false, - 'something_id' => '1', - 'something_else_id' => '3' - )))); - + 'created' => '2007-03-18 10:39:23' + ), + 'SomethingElse' => array( + array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'JoinThing' => array( + 'doomed' => true, + 'something_id' => '1', + 'something_else_id' => '1' + ) + ), + array( + 'id' => '2', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31', + 'JoinThing' => array( + 'doomed' => true, + 'something_id' => '1', + 'something_else_id' => '2' + ) + ), + array( + 'id' => '3', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31', + 'JoinThing' => array( + 'doomed' => false, + 'something_id' => '1', + 'something_else_id' => '3') + ) + ) + ); + $this->assertTrue($result['Something']['updated'] >= $ts); + unset($result['Something']['updated']); $this->assertEqual($expected, $result); } diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index d812f25816b..27a87196aa5 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -2496,18 +2496,20 @@ public function testSaveAll() { 'author_id' => '5', 'title' => 'Post with Author', 'body' => 'This post will be saved with an author', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'published' => 'N' ), 'Author' => array( 'id' => '5', 'user' => 'bob', 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', - 'created' => $ts, - 'updated' => $ts, 'test' => 'working' )); + $this->assertTrue($result[3]['Post']['created'] >= $ts); + $this->assertTrue($result[3]['Post']['updated'] >= $ts); + $this->assertTrue($result[3]['Author']['created'] >= $ts); + $this->assertTrue($result[3]['Author']['updated'] >= $ts); + unset($result[3]['Post']['created'], $result[3]['Post']['updated']); + unset($result[3]['Author']['created'], $result[3]['Author']['updated']); $this->assertEqual($result[3], $expected); $this->assertEqual(count($result), 4); @@ -2541,9 +2543,7 @@ public function testSaveAll() { 'author_id' => '2', 'title' => 'Multi-record post 1', 'body' => 'First multi-record post', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'published' => 'N' )), array( 'Post' => array( @@ -2551,10 +2551,14 @@ public function testSaveAll() { 'author_id' => '2', 'title' => 'Multi-record post 2', 'body' => 'Second multi-record post', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'published' => 'N' ))); + $this->assertTrue($result[0]['Post']['created'] >= $ts); + $this->assertTrue($result[0]['Post']['updated'] >= $ts); + $this->assertTrue($result[1]['Post']['created'] >= $ts); + $this->assertTrue($result[1]['Post']['updated'] >= $ts); + unset($result[0]['Post']['created'], $result[0]['Post']['updated']); + unset($result[1]['Post']['created'], $result[1]['Post']['updated']); $this->assertEqual($expected, $result); $TestModel = new Comment(); @@ -2577,30 +2581,22 @@ public function testSaveAll() { 'article_id' => '2', 'user_id' => '2', 'comment' => 'New comment with attachment', - 'published' => 'Y', - 'created' => $ts, - 'updated' => $ts + 'published' => 'Y' ); + $this->assertTrue($result[6]['Comment']['created'] >= $ts); + $this->assertTrue($result[6]['Comment']['updated'] >= $ts); + unset($result[6]['Comment']['created'], $result[6]['Comment']['updated']); $this->assertEqual($result[6]['Comment'], $expected); - $expected = array( - 'id' => '7', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'New comment with attachment', - 'published' => 'Y', - 'created' => $ts, - 'updated' => $ts - ); - $this->assertEqual($result[6]['Comment'], $expected); $expected = array( 'id' => '2', 'comment_id' => '7', - 'attachment' => 'some_file.tgz', - 'created' => $ts, - 'updated' => $ts + 'attachment' => 'some_file.tgz' ); + $this->assertTrue($result[6]['Attachment']['created'] >= $ts); + $this->assertTrue($result[6]['Attachment']['updated'] >= $ts); + unset($result[6]['Attachment']['created'], $result[6]['Attachment']['updated']); $this->assertEqual($result[6]['Attachment'], $expected); } @@ -3424,9 +3420,9 @@ public function testSaveAllValidation() { 'title' => 'Un-Baleeted First Post', 'body' => 'Not Baleeted!', 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => $newTs - )), + 'created' => '2007-03-18 10:39:23' + ) + ), array( 'Post' => array( 'id' => '2', @@ -3434,9 +3430,9 @@ public function testSaveAllValidation() { 'title' => 'Just update the title', 'body' => 'Second Post Body', 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => $ts - )), + 'created' => '2007-03-18 10:41:23' + ) + ), array( 'Post' => array( 'id' => '3', @@ -3446,17 +3442,27 @@ public function testSaveAllValidation() { 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31' - )), + ) + ), array( 'Post' => array( 'id' => '4', 'author_id' => '2', 'title' => 'Creating a fourth post', 'body' => 'Fourth post body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - ))); + 'published' => 'N' + ) + ) + ); + + $this->assertTrue($result[0]['Post']['updated'] >= $newTs); + $this->assertTrue($result[1]['Post']['updated'] >= $newTs); + $this->assertTrue($result[3]['Post']['updated'] >= $newTs); + $this->assertTrue($result[3]['Post']['created'] >= $newTs); + unset( + $result[0]['Post']['updated'], $result[1]['Post']['updated'], + $result[3]['Post']['updated'], $result[3]['Post']['created'] + ); $this->assertEqual($expected, $result); $this->assertEqual($TestModel->validationErrors, $errors); @@ -3475,6 +3481,10 @@ public function testSaveAllValidation() { $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first'))); $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); + unset( + $result[0]['Post']['updated'], $result[1]['Post']['updated'], + $result[3]['Post']['updated'], $result[3]['Post']['created'] + ); $this->assertEqual($expected, $result); $this->assertEqual($TestModel->validationErrors, $errors); } @@ -3796,18 +3806,22 @@ public function testSaveAssociated() { 'author_id' => '5', 'title' => 'Post with Author', 'body' => 'This post will be saved with an author', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'published' => 'N' ), 'Author' => array( 'id' => '5', 'user' => 'bob', 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', - 'created' => $ts, - 'updated' => $ts, 'test' => 'working' )); + $this->assertTrue($result[3]['Post']['updated'] >= $ts); + $this->assertTrue($result[3]['Post']['created'] >= $ts); + $this->assertTrue($result[3]['Author']['created'] >= $ts); + $this->assertTrue($result[3]['Author']['updated'] >= $ts); + unset( + $result[3]['Post']['updated'], $result[3]['Post']['created'], + $result[3]['Author']['updated'], $result[3]['Author']['created'] + ); $this->assertEqual($result[3], $expected); $this->assertEqual(count($result), 4); @@ -3833,30 +3847,22 @@ public function testSaveAssociated() { 'article_id' => '2', 'user_id' => '2', 'comment' => 'New comment with attachment', - 'published' => 'Y', - 'created' => $ts, - 'updated' => $ts + 'published' => 'Y' ); + $this->assertTrue($result[6]['Comment']['updated'] >= $ts); + $this->assertTrue($result[6]['Comment']['created'] >= $ts); + unset($result[6]['Comment']['updated'], $result[6]['Comment']['created']); $this->assertEqual($result[6]['Comment'], $expected); - $expected = array( - 'id' => '7', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'New comment with attachment', - 'published' => 'Y', - 'created' => $ts, - 'updated' => $ts - ); - $this->assertEqual($result[6]['Comment'], $expected); $expected = array( 'id' => '2', 'comment_id' => '7', - 'attachment' => 'some_file.tgz', - 'created' => $ts, - 'updated' => $ts + 'attachment' => 'some_file.tgz' ); + $this->assertTrue($result[6]['Attachment']['updated'] >= $ts); + $this->assertTrue($result[6]['Attachment']['created'] >= $ts); + unset($result[6]['Attachment']['updated'], $result[6]['Attachment']['created']); $this->assertEqual($result[6]['Attachment'], $expected); } @@ -3898,20 +3904,25 @@ public function testSaveMany() { 'author_id' => '2', 'title' => 'Multi-record post 1', 'body' => 'First multi-record post', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - )), + 'published' => 'N' + ) + ), array( 'Post' => array( 'id' => '2', 'author_id' => '2', 'title' => 'Multi-record post 2', 'body' => 'Second multi-record post', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - ))); + 'published' => 'N' + ) + ) + ); + $this->assertTrue($result[0]['Post']['updated'] >= $ts); + $this->assertTrue($result[0]['Post']['created'] >= $ts); + $this->assertTrue($result[1]['Post']['updated'] >= $ts); + $this->assertTrue($result[1]['Post']['created'] >= $ts); + unset($result[0]['Post']['updated'], $result[0]['Post']['created']); + unset($result[1]['Post']['updated'], $result[1]['Post']['created']); $this->assertEqual($expected, $result); } @@ -4472,9 +4483,7 @@ public function testSaveManyTransaction() { 'author_id' => 1, 'title' => 'New Fourth Post', 'body' => null, - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'published' => 'N' )); $expected[] = array( @@ -4484,10 +4493,14 @@ public function testSaveManyTransaction() { 'title' => 'New Fifth Post', 'body' => null, 'published' => 'N', - 'created' => $ts, - 'updated' => $ts )); + $this->assertTrue($result[3]['Post']['created'] >= $ts); + $this->assertTrue($result[3]['Post']['updated'] >= $ts); + $this->assertTrue($result[4]['Post']['created'] >= $ts); + $this->assertTrue($result[4]['Post']['updated'] >= $ts); + unset($result[3]['Post']['created'], $result[3]['Post']['updated']); + unset($result[4]['Post']['created'], $result[4]['Post']['updated']); $this->assertEqual($expected, $result); // Skip the rest of the transactional tests return; @@ -4541,9 +4554,7 @@ public function testSaveManyTransaction() { 'author_id' => 1, 'title' => 'New Fourth Post', 'body' => 'Third Post Body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'published' => 'N' )); $expected[] = array( @@ -4552,10 +4563,14 @@ public function testSaveManyTransaction() { 'author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts + 'published' => 'N' )); + $this->assertTrue($result[3]['Post']['created'] >= $ts); + $this->assertTrue($result[3]['Post']['updated'] >= $ts); + $this->assertTrue($result[4]['Post']['created'] >= $ts); + $this->assertTrue($result[4]['Post']['updated'] >= $ts); + unset($result[3]['Post']['created'], $result[3]['Post']['updated']); + unset($result[4]['Post']['created'], $result[4]['Post']['updated']); } $this->assertEqual($expected, $result); @@ -4650,9 +4665,9 @@ public function testSaveManyValidation() { 'title' => 'Baleeted First Post', 'body' => 'Baleeted!', 'published' => 'N', - 'created' => '2007-03-18 10:39:23', - 'updated' => $ts - )), + 'created' => '2007-03-18 10:39:23' + ) + ), array( 'Post' => array( 'id' => '2', @@ -4660,8 +4675,9 @@ public function testSaveManyValidation() { 'title' => 'Just update the title', 'body' => 'Second Post Body', 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', 'updated' => $ts - )), + 'created' => '2007-03-18 10:41:23' + ) + ), array( 'Post' => array( 'id' => '3', @@ -4678,10 +4694,17 @@ public function testSaveManyValidation() { 'author_id' => '2', 'title' => 'Creating a fourth post', 'body' => 'Fourth post body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - ))); + 'published' => 'N' + ) + ) + ); + + $this->assertTrue($result[0]['Post']['updated'] >= $ts); + $this->assertTrue($result[1]['Post']['updated'] >= $ts); + $this->assertTrue($result[3]['Post']['created'] >= $ts); + $this->assertTrue($result[3]['Post']['updated'] >= $ts); + unset($result[0]['Post']['updated'], $result[1]['Post']['updated']); + unset($result[3]['Post']['created'], $result[3]['Post']['updated']); $this->assertEqual($expected, $result); $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');