1
0
mirror of synced 2024-12-13 14:56:01 +03:00

Added unit test for queries on multiple left join branches [ticket #139]

Ticket: 139
This commit is contained in:
Tanken 2006-09-30 11:36:14 +00:00
parent b55ae1fd5e
commit cba5846b6a
2 changed files with 167 additions and 2 deletions

View File

@ -15,12 +15,65 @@ class Doctrine_RelationAccessTestCase extends Doctrine_UnitTestCase {
$o1->Data_File->filename = 'file4';
// multiple left join branches test
$us = array();
$us[1] = new MyUser();
$us[1]->name = "user1";
$this->connection->flush();
// OneThings
$onethings_gs = array(
array(6,1)
);
$count = 1;
foreach($onethings_gs as $onething_g) {
for($i=$count;$i<$count+$onething_g[0];$i++) {
$d = new MyOneThing();
$d->name = "onething".$i;
if($onething_g[1]) {
$us[$onething_g[1]]->MyOneThing->add($d);
}
}
$count += $onething_g[0];
}
// OtherThings
for($i=0;$i<6;$i++) {
$o = new MyOtherThing();
$o->name = "otherthing".$i;
$us[1]->MyOtherThing->add($o);
}
// UserOneThings
$one_id_gs = array(
array(array(2,3,6,5,1), 1)
);
foreach($one_id_gs as $one_ids) {
foreach($one_ids[0] as $oid) {
$od = new MyUserOneThing();
$od->one_thing_id = $oid;
$od->user_id = $one_ids[1];
}
}
// UserOtherThings
$oth_id_gs = array(
array(array(5,4), 1)
);
foreach($oth_id_gs as $oth_ids) {
foreach($oth_ids[0] as $oid) {
$uo = new MyUserOtherThing();
$uo->other_thing_id = $oid;
$uo->user_id = $oth_ids[1];
}
}
$this->connection->flush();
$this->connection->clear();
}
public function prepareTables() {
$this->tables = array("File_Owner", "Data_File");
$this->tables = array("MyUser",
"MyOneThing",
"MyUserOneThing",
"MyOtherThing",
"MyUserOtherThing");
parent::prepareTables();
}
public function testOneToOneAggregateRelationFetching() {
@ -85,5 +138,78 @@ class Doctrine_RelationAccessTestCase extends Doctrine_UnitTestCase {
}
public function testMultipleLeftJoinBranches() {
$query = "FROM MyUserOtherThing";
$other = $this->connection->query($query);
$check1 = array();
foreach($other as $oth) {
if(!isset($check1[$oth->other_thing_id])) {
$check1[$oth->other_thing_id] = array();
}
$check1[$oth->other_thing_id][$oth->id] = $oth;
}
$query = "FROM MyUserOneThing";
$ones = $this->connection->query($query);
$check2 = array();
foreach($ones as $one) {
if(!isset($check2[$one->one_thing_id])) {
$check2[$one->one_thing_id] = array();
}
$check2[$one->one_thing_id][$one->id] = $one;
}
$query = "FROM MyUser,
MyUser.MyOneThing,
MyUser.MyOneThing.MyUserOneThing,
MyUser.MyOtherThing,
MyUser.MyOtherThing.MyUserOtherThing";
$users = $this->connection->query($query);
foreach($users as $u) {
$this->assertEqual($u->MyOtherThing->count(), 6, "incorrect count of MyOtherThing");
foreach($u->MyOtherThing as $o) {
$in_check = array_key_exists($o->id, $check1);
$wanted_user_thing_count = $in_check ? count($check1[$o->id]) : 0;
$this->assertEqual($o->MyUserOtherThing->count(), $wanted_user_thing_count, "incorrect count of MyUserOtherThing on MyOtherThing");
foreach($o->MyUserOtherThing as $uo) {
$this->assertEqual($uo->other_thing_id, $o->id, "incorrectly assigned MyOtherThing.id on MyUserOtherThing");
if($in_check) {
$wanted_user_thing_exists = array_key_exists($uo->id, $check1[$o->id]);
$this->assertTrue($wanted_user_thing_exists, "MyUserOtherThing incorrectly assigned to MyOtherThing.");
if($wanted_user_thing_exists) {
$this->assertEqual($uo->other_thing_id, $check1[$o->id][$uo->id]->user_id, "incorrect value of MyUserOtherThing.user_id");
$this->assertEqual($uo->other_thing_id, $check1[$o->id][$uo->id]->other_thing_id, "incorrect value of MyUserOtherThing.other_thing_id");
}
}
}
}
}
$query = "FROM MyUser,
MyUser.MyOtherThing,
MyUser.MyOtherThing.MyUserOtherThing,
MyUser.MyOneThing,
MyUser.MyOneThing.MyUserOneThing";
$users = $this->connection->query($query);
foreach($users as $u) {
$this->assertEqual($u->MyOneThing->count(), 6, "incorrect count of MyOneThing");
foreach($u->MyOneThing as $o) {
$in_check = array_key_exists($o->id, $check2);
$wanted_user_thing_count = $in_check ? count($check2[$o->id]) : 0;
$this->assertEqual($o->MyUserOneThing->count(), $wanted_user_thing_count, "incorrect count of MyUserOneThing on MyOneThing");
foreach($o->MyUserOneThing as $uo) {
$this->assertEqual($uo->one_thing_id, $o->id, "incorrectly assigned MyOneThing.id on MyUserOneThing");
if($in_check) {
$wanted_user_thing_exists = array_key_exists($uo->id, $check2[$o->id]);
$this->assertTrue($wanted_user_thing_exists, "MyUserOneThing incorrectly assigned to MyOneThing.");
if($wanted_user_thing_exists) {
$this->assertEqual($uo->one_thing_id, $check2[$o->id][$uo->id]->user_id, "incorrect value of MyUserOneThing.user_id");
$this->assertEqual($uo->one_thing_id, $check2[$o->id][$uo->id]->one_thing_id, "incorrect value of MyUserOneThing.one_thing_id");
}
}
}
}
}
}
}
?>

View File

@ -467,4 +467,43 @@ class File_Owner extends Doctrine_Record {
$this->hasOne("Data_File", "Data_File.file_owner_id");
}
}
class MyUser extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name", "string");
}
public function setUp() {
$this->hasMany("MyOneThing", "MyOneThing.user_id");
$this->hasMany("MyOtherThing", "MyOtherThing.user_id");
}
}
class MyOneThing extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name", "string");
$this->hasColumn("user_id", "integer");
}
public function setUp() {
$this->hasMany("MyUserOneThing", "MyUserOneThing.one_thing_id");
}
}
class MyOtherThing extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name", "string");
$this->hasColumn("user_id", "integer");
}
public function setUp() {
$this->hasMany("MyUserOtherThing", "MyUserOtherThing.other_thing_id");
}
}
class MyUserOneThing extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("user_id", "integer");
$this->hasColumn("one_thing_id", "integer");
}
}
class MyUserOtherThing extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("user_id", "integer");
$this->hasColumn("other_thing_id", "integer");
}
}
?>