Added tests for random queries

Improved DataObjectTest to use more helpful assertions

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60572 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-08-13 00:44:52 +00:00
parent 11ea5dd0c7
commit 26051153b8
2 changed files with 57 additions and 25 deletions

View File

@ -54,74 +54,74 @@ class DataObjectTest extends SapphireTest {
function testGet() {
// Test getting all records of a DataObject
$comments = DataObject::get('PageComment');
$this->assertTrue($comments->Count() == 4);
$this->assertEquals(8, $comments->Count());
// Test WHERE clause
$comments = DataObject::get('PageComment', 'Name="Bob"');
$this->assertTrue($comments->Count() == 2);
$this->assertEquals(2, $comments->Count());
foreach($comments as $comment) {
$this->assertTrue($comment->Name == 'Bob');
$this->assertEquals('Bob', $comment->Name);
}
// Test sorting
$comments = DataObject::get('PageComment', '', 'Name ASC');
$this->assertTrue($comments->Count() == 4);
$this->assertTrue($comments->First()->Name == 'Bob');
$this->assertEquals(8, $comments->Count());
$this->assertEquals('Bob', $comments->First()->Name);
$comments = DataObject::get('PageComment', '', 'Name DESC');
$this->assertTrue($comments->Count() == 4);
$this->assertTrue($comments->First()->Name == 'Joe');
$this->assertEquals(8, $comments->Count());
$this->assertEquals('Joe', $comments->First()->Name);
// Test join
$comments = DataObject::get('PageComment', '`SiteTree`.Title="First Page"', '', 'INNER JOIN SiteTree ON PageComment.ParentID = SiteTree.ID');
$this->assertTrue($comments->Count() == 2);
$this->assertTrue($comments->First()->Name == 'Bob');
$this->assertTrue($comments->Last()->Name == 'Bob');
$this->assertEquals(2, $comments->Count());
$this->assertEquals('Bob', $comments->First()->Name);
$this->assertEquals('Bob', $comments->Last()->Name);
// Test limit
$comments = DataObject::get('PageComment', '', 'Name ASC', '', '1,2');
$this->assertTrue($comments->Count() == 2);
$this->assertTrue($comments->First()->Name == 'Bob');
$this->assertTrue($comments->Last()->Name == 'Jane');
$this->assertEquals(2, $comments->Count());
$this->assertEquals('Bob', $comments->First()->Name);
$this->assertEquals('Dean', $comments->Last()->Name);
// Test container class
$comments = DataObject::get('PageComment', '', '', '', '', 'DataObjectSet');
$this->assertTrue(get_class($comments) == 'DataObjectSet');
$this->assertEquals('DataObjectSet', get_class($comments));
$comments = DataObject::get('PageComment', '', '', '', '', 'ComponentSet');
$this->assertTrue(get_class($comments) == 'ComponentSet');
$this->assertEquals('ComponentSet', get_class($comments));
// Test get_by_id()
$homepage = $this->fixture->objFromFixture('Page', 'home');
$page = DataObject::get_by_id('Page', $homepage->ID);
$this->assertTrue($page->Title == 'Home');
$homepageID = $this->idFromFixture('Page', 'home');
$page = DataObject::get_by_id('Page', $homepageID);
$this->assertEquals('Home', $page->Title);
// Test get_by_url()
$page = DataObject::get_by_url('home');
$this->assertTrue($page->ID == $homepage->ID);
$this->assertEquals($homepageID, $page->ID);
// Test get_one() without caching
$comment1 = DataObject::get_one('PageComment', 'Name="Joe"', false);
$comment1->Comment = "Something Else";
$comment2 = DataObject::get_one('PageComment', 'Name="Joe"', false);
$this->assertTrue($comment1->Comment != $comment2->Comment);
$this->assertNotEquals($comment1->Comment, $comment2->Comment);
// Test get_one() with caching
$comment1 = DataObject::get_one('PageComment', 'Name="Jane"', true);
$comment1->Comment = "Something Else";
$comment2 = DataObject::get_one('PageComment', 'Name="Jane"', true);
$this->assertTrue((string)$comment1->Comment == (string)$comment2->Comment);
$this->assertEquals((string)$comment1->Comment, (string)$comment2->Comment);
// Test get_one() with order by without caching
$comment = DataObject::get_one('PageComment', '', false, 'Name ASC');
$this->assertTrue($comment->Name == 'Bob');
$this->assertEquals('Bob', $comment->Name);
$comment = DataObject::get_one('PageComment', '', false, 'Name DESC');
$this->assertTrue($comment->Name == 'Joe');
$this->assertEquals('Joe', $comment->Name);
// Test get_one() with order by with caching
$comment = DataObject::get_one('PageComment', '', true, 'Name ASC');
$this->assertTrue($comment->Name == 'Bob');
$this->assertEquals('Bob', $comment->Name);
$comment = DataObject::get_one('PageComment', '', true, 'Name DESC');
$this->assertTrue($comment->Name == 'Joe');
$this->assertEquals('Joe', $comment->Name);
}
/**
@ -242,6 +242,26 @@ class DataObjectTest extends SapphireTest {
'Changed fields are correctly detected while ignoring type changes (level=2)'
);
}
function testRandomSort() {
/* If we perforn the same regularly sorted query twice, it should return the same results */
$itemsA = DataObject::get("PageComment", "", "ID");
foreach($itemsA as $item) $keysA[] = $item->ID;
$itemsB = DataObject::get("PageComment", "", "ID");
foreach($itemsB as $item) $keysB[] = $item->ID;
$this->assertEquals($keysA, $keysB);
/* If we perform the same random query twice, it shouldn't return the same results */
$itemsA = DataObject::get("PageComment", "", "RAND()");
foreach($itemsA as $item) $keysA[] = $item->ID;
$itemsB = DataObject::get("PageComment", "", "RAND()");
foreach($itemsB as $item) $keysB[] = $item->ID;
$this->assertNotEquals($keysA, $keysB);
}
}
class DataObjectTest_Player extends Member implements TestOnly {

View File

@ -11,6 +11,18 @@ PageComment:
comment4:
Name: Bob
Comment: Second comment by Bob
comment5:
Name: Ernie
Comment: This is a test comment
comment6:
Name: Jimmy
Comment: This is another test comment
comment7:
Name: Dean
Comment: Another comment
comment8:
Name: Dean
Comment: Second comment by Dean
Page:
home: