function testFindFirstBy()
 {
     $user = new User();
     $newuser = $user->findFirstBy('id', 1);
     $this->assertEqual($newuser->username, 'haakon');
 }
 public function test_1_n_m()
 {
     $this->installAndIncludeModels(array('User', 'Post', 'Comment'));
     $this->User = new User();
     if ($this->User->_db->type() == 'postgre') {
         //from postgres docs:
         //A value of type name is a string of 63 or fewer characters.
         //A name must start with a letter or an underscore;
         //the rest of the string can contain letters, digits, and underscores.
         //IF a column name here is over 63 characters long, the assoc finder will fail
         $this->assertTrue(true);
         return;
     }
     $User = new User(array('name' => 'Arno', 'email' => '*****@*****.**'));
     $Post1 = new Post(array('title' => 'Test1'));
     $Post2 = new Post(array('title' => 'Test2'));
     $Comment1_1 = new Comment(array('name' => 'Comment1_1'));
     $Comment1_2 = new Comment(array('name' => 'Comment1_2'));
     $Comment2_1 = new Comment(array('name' => 'Comment2_1'));
     $Comment2_2 = new Comment(array('name' => 'Comment2_2'));
     $User->post->add($Post1);
     $User->post->add($Post2);
     $Post1->comment->add($Comment1_1);
     $Post1->comment->add($Comment1_2);
     $Post2->comment->add($Comment2_1);
     $Post2->comment->add($Comment2_2);
     $User->save();
     //$Post1->save();
     //$Post2->save();
     $Comment1_1->save();
     $Comment1_2->save();
     $Comment2_1->save();
     $Comment2_2->save();
     $Test = $User->findFirstBy('name', 'Arno', array('include' => array('posts' => array('order' => 'id ASC', 'include' => array('comments' => array('order' => 'id ASC'))))));
     $this->assertEqual($Test->name, 'Arno');
     $this->assertEqual($Test->posts[0]->title, 'Test1');
     $this->assertEqual($Test->posts[1]->title, 'Test2');
     $this->assertEqual($Test->posts[0]->comments[0]->name, 'Comment1_1');
     $this->assertEqual($Test->posts[0]->comments[1]->name, 'Comment1_2');
     $this->assertEqual($Test->posts[1]->comments[0]->name, 'Comment2_1');
     $this->assertEqual($Test->posts[1]->comments[1]->name, 'Comment2_2');
     // singular in "post", plural in "comments"
     $Test = $User->findFirstBy('name', 'Arno', array('include' => array('post' => array('order' => 'id ASC', 'include' => array('comments' => array('order' => 'id ASC'))))));
     $this->assertEqual($Test->name, 'Arno');
     $this->assertEqual($Test->posts[0]->title, 'Test1');
     $this->assertEqual($Test->posts[1]->title, 'Test2');
     $this->assertEqual($Test->posts[0]->comments[0]->name, 'Comment1_1');
     $this->assertEqual($Test->posts[0]->comments[1]->name, 'Comment1_2');
     $this->assertEqual($Test->posts[1]->comments[0]->name, 'Comment2_1');
     $this->assertEqual($Test->posts[1]->comments[1]->name, 'Comment2_2');
     // plural in "posts", singular in "comment"
     $Test = $User->findFirstBy('name', 'Arno', array('include' => array('posts' => array('order' => 'id ASC', 'include' => array('comments' => array('order' => 'id ASC'))))));
     $this->assertEqual($Test->name, 'Arno');
     $this->assertEqual($Test->posts[0]->title, 'Test1');
     $this->assertEqual($Test->posts[1]->title, 'Test2');
     $this->assertEqual($Test->posts[0]->comments[0]->name, 'Comment1_1');
     $this->assertEqual($Test->posts[0]->comments[1]->name, 'Comment1_2');
     $this->assertEqual($Test->posts[1]->comments[0]->name, 'Comment2_1');
     $this->assertEqual($Test->posts[1]->comments[1]->name, 'Comment2_2');
     // singular in "post", singular in "comment"
     $Test = $User->findFirstBy('name', 'Arno', array('order' => 'id ASC', 'include' => array('post' => array('order' => 'id ASC', 'include' => array('comment' => array('order' => 'id ASC'))))));
     $this->assertEqual($Test->name, 'Arno');
     $this->assertEqual($Test->posts[0]->title, 'Test1');
     $this->assertEqual($Test->posts[1]->title, 'Test2');
     $this->assertEqual($Test->posts[0]->comments[0]->name, 'Comment1_1');
     $this->assertEqual($Test->posts[0]->comments[1]->name, 'Comment1_2');
     $this->assertEqual($Test->posts[1]->comments[0]->name, 'Comment2_1');
     $this->assertEqual($Test->posts[1]->comments[1]->name, 'Comment2_2');
     // singular in "post", singular in "comment" + test order_statements in parent condition
     $Test = $User->findFirstBy('name', 'Arno', array('order' => 'id , _posts.id, _comments.id ASC', 'include' => array('post' => array('include' => array('comment')))));
     $this->assertEqual($Test->name, 'Arno');
     $this->assertEqual($Test->posts[0]->title, 'Test1');
     $this->assertEqual($Test->posts[1]->title, 'Test2');
     $this->assertEqual($Test->posts[0]->comments[0]->name, 'Comment1_1');
     $this->assertEqual($Test->posts[0]->comments[1]->name, 'Comment1_2');
     $this->assertEqual($Test->posts[1]->comments[0]->name, 'Comment2_1');
     $this->assertEqual($Test->posts[1]->comments[1]->name, 'Comment2_2');
     // singular in "post", singular in "comment" + test order_statements in parent condition using the handlername
     $Test = $User->findFirstBy('name', 'Arno', array('order' => 'id , _post.id, _comment.id ASC', 'include' => array('post' => array('include' => array('comment')))));
     $this->assertEqual($Test->name, 'Arno');
     $this->assertEqual($Test->posts[0]->title, 'Test1');
     $this->assertEqual($Test->posts[1]->title, 'Test2');
     $this->assertEqual($Test->posts[0]->comments[0]->name, 'Comment1_1');
     $this->assertEqual($Test->posts[0]->comments[1]->name, 'Comment1_2');
     $this->assertEqual($Test->posts[1]->comments[0]->name, 'Comment2_1');
     $this->assertEqual($Test->posts[1]->comments[1]->name, 'Comment2_2');
 }