mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
11ea5dd0c7
commit
26051153b8
@ -54,74 +54,74 @@ class DataObjectTest extends SapphireTest {
|
|||||||
function testGet() {
|
function testGet() {
|
||||||
// Test getting all records of a DataObject
|
// Test getting all records of a DataObject
|
||||||
$comments = DataObject::get('PageComment');
|
$comments = DataObject::get('PageComment');
|
||||||
$this->assertTrue($comments->Count() == 4);
|
$this->assertEquals(8, $comments->Count());
|
||||||
|
|
||||||
// Test WHERE clause
|
// Test WHERE clause
|
||||||
$comments = DataObject::get('PageComment', 'Name="Bob"');
|
$comments = DataObject::get('PageComment', 'Name="Bob"');
|
||||||
$this->assertTrue($comments->Count() == 2);
|
$this->assertEquals(2, $comments->Count());
|
||||||
foreach($comments as $comment) {
|
foreach($comments as $comment) {
|
||||||
$this->assertTrue($comment->Name == 'Bob');
|
$this->assertEquals('Bob', $comment->Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test sorting
|
// Test sorting
|
||||||
$comments = DataObject::get('PageComment', '', 'Name ASC');
|
$comments = DataObject::get('PageComment', '', 'Name ASC');
|
||||||
$this->assertTrue($comments->Count() == 4);
|
$this->assertEquals(8, $comments->Count());
|
||||||
$this->assertTrue($comments->First()->Name == 'Bob');
|
$this->assertEquals('Bob', $comments->First()->Name);
|
||||||
$comments = DataObject::get('PageComment', '', 'Name DESC');
|
$comments = DataObject::get('PageComment', '', 'Name DESC');
|
||||||
$this->assertTrue($comments->Count() == 4);
|
$this->assertEquals(8, $comments->Count());
|
||||||
$this->assertTrue($comments->First()->Name == 'Joe');
|
$this->assertEquals('Joe', $comments->First()->Name);
|
||||||
|
|
||||||
// Test join
|
// Test join
|
||||||
$comments = DataObject::get('PageComment', '`SiteTree`.Title="First Page"', '', 'INNER JOIN SiteTree ON PageComment.ParentID = SiteTree.ID');
|
$comments = DataObject::get('PageComment', '`SiteTree`.Title="First Page"', '', 'INNER JOIN SiteTree ON PageComment.ParentID = SiteTree.ID');
|
||||||
$this->assertTrue($comments->Count() == 2);
|
$this->assertEquals(2, $comments->Count());
|
||||||
$this->assertTrue($comments->First()->Name == 'Bob');
|
$this->assertEquals('Bob', $comments->First()->Name);
|
||||||
$this->assertTrue($comments->Last()->Name == 'Bob');
|
$this->assertEquals('Bob', $comments->Last()->Name);
|
||||||
|
|
||||||
// Test limit
|
// Test limit
|
||||||
$comments = DataObject::get('PageComment', '', 'Name ASC', '', '1,2');
|
$comments = DataObject::get('PageComment', '', 'Name ASC', '', '1,2');
|
||||||
$this->assertTrue($comments->Count() == 2);
|
$this->assertEquals(2, $comments->Count());
|
||||||
$this->assertTrue($comments->First()->Name == 'Bob');
|
$this->assertEquals('Bob', $comments->First()->Name);
|
||||||
$this->assertTrue($comments->Last()->Name == 'Jane');
|
$this->assertEquals('Dean', $comments->Last()->Name);
|
||||||
|
|
||||||
// Test container class
|
// Test container class
|
||||||
$comments = DataObject::get('PageComment', '', '', '', '', 'DataObjectSet');
|
$comments = DataObject::get('PageComment', '', '', '', '', 'DataObjectSet');
|
||||||
$this->assertTrue(get_class($comments) == 'DataObjectSet');
|
$this->assertEquals('DataObjectSet', get_class($comments));
|
||||||
$comments = DataObject::get('PageComment', '', '', '', '', 'ComponentSet');
|
$comments = DataObject::get('PageComment', '', '', '', '', 'ComponentSet');
|
||||||
$this->assertTrue(get_class($comments) == 'ComponentSet');
|
$this->assertEquals('ComponentSet', get_class($comments));
|
||||||
|
|
||||||
|
|
||||||
// Test get_by_id()
|
// Test get_by_id()
|
||||||
$homepage = $this->fixture->objFromFixture('Page', 'home');
|
$homepageID = $this->idFromFixture('Page', 'home');
|
||||||
$page = DataObject::get_by_id('Page', $homepage->ID);
|
$page = DataObject::get_by_id('Page', $homepageID);
|
||||||
$this->assertTrue($page->Title == 'Home');
|
$this->assertEquals('Home', $page->Title);
|
||||||
|
|
||||||
// Test get_by_url()
|
// Test get_by_url()
|
||||||
$page = DataObject::get_by_url('home');
|
$page = DataObject::get_by_url('home');
|
||||||
$this->assertTrue($page->ID == $homepage->ID);
|
$this->assertEquals($homepageID, $page->ID);
|
||||||
|
|
||||||
// Test get_one() without caching
|
// Test get_one() without caching
|
||||||
$comment1 = DataObject::get_one('PageComment', 'Name="Joe"', false);
|
$comment1 = DataObject::get_one('PageComment', 'Name="Joe"', false);
|
||||||
$comment1->Comment = "Something Else";
|
$comment1->Comment = "Something Else";
|
||||||
$comment2 = DataObject::get_one('PageComment', 'Name="Joe"', false);
|
$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
|
// Test get_one() with caching
|
||||||
$comment1 = DataObject::get_one('PageComment', 'Name="Jane"', true);
|
$comment1 = DataObject::get_one('PageComment', 'Name="Jane"', true);
|
||||||
$comment1->Comment = "Something Else";
|
$comment1->Comment = "Something Else";
|
||||||
$comment2 = DataObject::get_one('PageComment', 'Name="Jane"', true);
|
$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
|
// Test get_one() with order by without caching
|
||||||
$comment = DataObject::get_one('PageComment', '', false, 'Name ASC');
|
$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');
|
$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
|
// Test get_one() with order by with caching
|
||||||
$comment = DataObject::get_one('PageComment', '', true, 'Name ASC');
|
$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');
|
$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)'
|
'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 {
|
class DataObjectTest_Player extends Member implements TestOnly {
|
||||||
|
@ -11,6 +11,18 @@ PageComment:
|
|||||||
comment4:
|
comment4:
|
||||||
Name: Bob
|
Name: Bob
|
||||||
Comment: Second comment by 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:
|
Page:
|
||||||
home:
|
home:
|
||||||
|
Loading…
Reference in New Issue
Block a user