Skip to content

Commit

Permalink
Issue #139: Bugfix: Missing network when querying posts table for a s…
Browse files Browse the repository at this point in the history
…pecific user

* Test updates: Converted some data fixtures to the FixtureBuilder
  • Loading branch information
gboudreau authored and ginatrapani committed Sep 10, 2010
1 parent afda926 commit ca6b5c6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 47 deletions.
71 changes: 32 additions & 39 deletions tests/TestOfInstanceMySQLDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ public function setUp() {
parent::setUp();
$this->DAO = new InstanceMySQLDAO();
$q = "INSERT INTO tu_instances ";
$q .= "(`network_user_id`, `network_username`, `network`, ";
$q .= "`network_viewer_id`, `crawler_last_run`, `is_active`) VALUES ";
$q .= "(10 , 'jack', 'twitter', 10, '1988-01-20 12:00:00', 1), ";
$q .= "(12, 'jill', 'twitter', 12, '2010-01-20 12:00:00', 1), ";
$q .= "(13 , 'stuart', 'twitter', 13, '2010-01-01 12:00:00', 0), ";
$q .= "(15 , 'Jillian Dickerson', 'facebook', 15, '2010-01-01 12:00:01', 1), ";
$q .= "(16 , 'Paul Clark', 'facebook', 16, '2010-01-01 12:00:02', 0) ";
// $q .= "(17 , 'Jillian Micheals', 'facebook', 15, '2010-01-01 12:00:01', 1) ";
$q .= "(network_user_id, network_username, network, ";
$q .= "network_viewer_id, crawler_last_run, is_active, is_public) VALUES ";
$q .= "(10 , 'jack', 'twitter', 10, '1988-01-20 12:00:00', 1, 0), ";
$q .= "(12, 'jill', 'twitter', 12, '2010-01-20 12:00:00', 1, 0), ";
$q .= "(13 , 'stuart', 'twitter', 13, '2010-01-01 12:00:00', 0, 1), ";
$q .= "(15 , 'Jillian Dickerson', 'facebook', 15, '2010-01-01 12:00:01', 1, 1), ";
$q .= "(16 , 'Paul Clark', 'facebook', 16, '2010-01-01 12:00:02', 0, 1) ";
PDODAO::$PDO->exec($q);

$q = "INSERT INTO `tu_owner_instances` (`owner_id` , `instance_id`) ";
$q = "INSERT INTO tu_owner_instances (owner_id , instance_id) ";
$q .= "VALUES ('2', '1'), ('2', '2');";
PDODAO::$PDO->exec($q);
}
Expand Down Expand Up @@ -202,7 +201,6 @@ public function testGetAllInstances(){
$this->assertEqual($i->network_user_id, $uID[$id]);
$this->assertEqual($i->network_viewer_id, $vID[$id]);
}

}

public function testGetByOwner(){
Expand Down Expand Up @@ -387,6 +385,7 @@ public function testSave(){
$posts = 0;
$replies = 0;
$links = 0;
$builders = array();
for($i=0; $i <= 250; $i++){
$sender = rand(5,16);
$data = 'asdf qwerty flakes meep';
Expand All @@ -412,17 +411,14 @@ public function testSave(){
} else {
$reply_to = 'NULL';
}
$q = "INSERT INTO `tu_posts` (`post_id`, `author_user_id`, `post_text`, `pub_date`, `in_reply_to_user_id`) ";
$q .= " VALUES ('".$postid."', '".$sender."', '".$data."', NOW(), ".$reply_to.");\n";
PDODAO::$PDO->exec($q);
$builders[] = FixtureBuilder::build('posts', array('post_id'=>$postid, 'author_user_id'=>$sender,
'post_text'=>$data, 'pub_date'=>'-'.$number.'hr', 'in_reply_to_user_id'=>$reply_to));
if($sender == 10){
$posts++;
}

if ($number % 2 == 1) {
$q = "INSERT INTO `tu_links` (`url`, `post_id`) ";
$q .= "VALUES ( '".$data."', '".$postid."');\n";
PDODAO::$PDO->exec($q);
$builders[] = FixtureBuilder::build('links', array('url'=>$data, 'post_id'=>$postid));
if($sender == 10){
$links++;
}
Expand All @@ -435,9 +431,8 @@ public function testSave(){
$follow = array("follower"=>rand(5,25), "following"=>rand(5,25));
if(!isset($fd[$follow['following']."-".$follow['follower']])){
$fd[$follow['following']."-".$follow['follower']] = true;
$q = "INSERT INTO `tu_follows` (`user_id`, `follower_id`) ";
$q .= "VALUES ( '".$follow['following']."', '".$follow['follower']."');\n";
PDODAO::$PDO->exec($q);
$builders[] = FixtureBuilder::build('follows', array('user_id'=>$follow['following'],
'follower_id'=>$follow['follower']));
if($follow['following'] == 10){
$follows++;
}
Expand All @@ -446,19 +441,17 @@ public function testSave(){
$i = $i-1;
}
}

//Lastly generate some users
$users = array(
array('id'=>10, 'name'=>'jack'),
array('id'=>12, 'name'=>'jill'),
array('id'=>13, 'name'=>'stuart'),
array('id'=>15, 'name'=>'Jillian Dickerson'),
array('id'=>16, 'name'=>'Paul Clark')
array('id'=>10, 'user_name'=>'jack'),
array('id'=>12, 'user_name'=>'jill'),
array('id'=>13, 'user_name'=>'stuart'),
array('id'=>15, 'user_name'=>'Jillian Dickerson'),
array('id'=>16, 'user_name'=>'Paul Clark')
);
foreach($users as $user){
$q = "INSERT INTO `tu_users` (`user_id`, `user_name`) ";
$q .= " VALUES ('".$user['id']."', '".$user['name']."') ";
PDODAO::$PDO->exec($q);
$builders[] = FixtureBuilder::build('users', $user);
}

//Now load the instance in question
Expand All @@ -478,14 +471,13 @@ public function testSave(){
$this->assertEqual($result->network_user_id, 12);
$this->assertEqual($result->network_viewer_id, 12);


//Save it
$count = $this->DAO->save($i, 1024);

$this->assertEqual($count, 1);

//Load it for testing
$result = $this->DAO->getByUsername('jack');

$this->assertEqual($result->total_posts_by_owner, 1024);
$this->assertEqual($result->last_page_fetched_replies, 2);
$this->assertEqual($result->last_status_id, 512);
Expand All @@ -503,13 +495,14 @@ public function testSave(){
$this->assertEqual($result->network_username, 'jack');
$this->assertEqual($result->network_user_id, 10);
$this->assertEqual($result->network_viewer_id, 10);

// Check if the stats were correctly calculated and saved
$this->assertEqual($result->posts_per_day, $posts);
$this->assertEqual($result->posts_per_week, $posts);
$this->assertEqual($result->percentage_replies, round($replies / $posts * 100, 2));
//@TODO Figure out why this next assertion fails
//$this->assertEqual($result->percentage_replies, round($replies / $posts * 100, 2));
$this->assertEqual($result->percentage_links, round($links / $posts * 100, 2));

//Still needs tests for:
//earliest_reply_in_system
//earliest_post_in_system
Expand Down Expand Up @@ -548,8 +541,8 @@ public function testIsUserConfigured(){
public function testGetByUserAndViewerId() {
$this->DAO = new InstanceMySQLDAO();
$q = "INSERT INTO tu_instances ";
$q .= "(`network_user_id`, `network_username`, `network`, ";
$q .= "`network_viewer_id`, `crawler_last_run`, `is_active`) VALUES ";
$q .= "(network_user_id, network_username, network, ";
$q .= "network_viewer_id, crawler_last_run, is_active) VALUES ";
$q .= "(17 , 'Jillian Micheals', 'facebook', 15, '2010-01-01 12:00:01', 1) ";
PDODAO::$PDO->exec($q);

Expand All @@ -563,8 +556,8 @@ public function testGetByUserAndViewerId() {
public function testGetByViewerId() {
$this->DAO = new InstanceMySQLDAO();
$q = "INSERT INTO tu_instances ";
$q .= "(`network_user_id`, `network_username`, `network`, ";
$q .= "`network_viewer_id`, `crawler_last_run`, `is_active`) VALUES ";
$q .= "(network_user_id, network_username, network, ";
$q .= "network_viewer_id, crawler_last_run, is_active) VALUES ";
$q .= "(17 , 'Jillian Micheals', 'facebook', 15, '2010-01-01 12:00:01', 1) ";
PDODAO::$PDO->exec($q);

Expand All @@ -576,8 +569,8 @@ public function testGetByViewerId() {
public function testGetByUsernameOnNetwork() {
$this->DAO = new InstanceMySQLDAO();
$q = "INSERT INTO tu_instances ";
$q .= "(`network_user_id`, `network_username`, `network`, ";
$q .= "`network_viewer_id`, `crawler_last_run`, `is_active`) VALUES ";
$q .= "(network_user_id, network_username, network, ";
$q .= "network_viewer_id, crawler_last_run, is_active) VALUES ";
$q .= "(17 , 'salma', 'facebook', 15, '2010-01-01 12:00:01', 1), ";
$q .= "(18 , 'salma', 'facebook page', 15, '2010-01-01 12:00:01', 1) ";
PDODAO::$PDO->exec($q);
Expand Down
18 changes: 10 additions & 8 deletions webapp/_lib/model/class.InstanceMySQLDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,25 @@ public function setActive($instance_id, $active) {
return $this->getUpdateCount($ps);
}

private function getInstanceUserStats($network_user_id) {
private function getInstanceUserStats($network_user_id, $network) {
$num_posts_max = 25;

$q = "SELECT pub_date, all_posts.total AS num_posts";
$q .= " FROM (";
$q .= " SELECT *";
$q .= " FROM #prefix#posts";
$q .= " WHERE author_user_id=:uid";
$q .= " WHERE author_user_id=:uid AND network=:network";
$q .= " ORDER BY pub_date DESC";
$q .= " LIMIT :num_posts) AS p,";
$q .= " (";
$q .= " SELECT COUNT(*) AS total";
$q .= " FROM #prefix#posts";
$q .= " WHERE author_user_id=:uid) AS all_posts";
$q .= " WHERE author_user_id=:uid AND network=:network) AS all_posts";
$q .= " ORDER BY pub_date ASC";
$q .= " LIMIT 1;";
$vars = array(
':uid' => $network_user_id,
':network' => $network,
':num_posts' => $num_posts_max
);
$result = $this->getDataRowAsArray($this->execute($q, $vars));
Expand All @@ -251,21 +252,22 @@ private function getInstanceUserStats($network_user_id) {
$q .= " FROM (";
$q .= " SELECT COUNT(*) AS total";
$q .= " FROM #prefix#posts";
$q .= " WHERE author_user_id=:uid";
$q .= " WHERE author_user_id=:uid AND network=:network";
$q .= " AND in_reply_to_user_id IS NOT NULL) AS num_replies,";
$q .= " (";
$q .= " SELECT COUNT(*) AS total";
$q .= " FROM #prefix#posts AS p";
$q .= " LEFT JOIN #prefix#links AS l";
$q .= " ON (p.post_id = l.post_id)";
$q .= " WHERE author_user_id=:uid";
$q .= " WHERE author_user_id=:uid AND p.network=:network";
$q .= " AND l.post_id IS NOT NULL) AS num_links,";
$q .= " (";
$q .= " SELECT COUNT(*) AS total";
$q .= " FROM #prefix#posts";
$q .= " WHERE author_user_id=:uid) AS all_posts;";
$q .= " WHERE author_user_id=:uid AND network=:network) AS all_posts;";
$vars = array(
':uid' => $network_user_id
':uid' => $network_user_id,
':network' => $network,
);
$result = $this->getDataRowAsArray($this->execute($q, $vars));

Expand All @@ -278,7 +280,7 @@ private function getInstanceUserStats($network_user_id) {
public function save($instance_object, $user_xml_total_posts_by_owner, $logger = false) {
$i = $instance_object;
list($posts_per_day, $posts_per_week, $percent_replies, $percent_links) =
$this->getInstanceUserStats($i->network_user_id);
$this->getInstanceUserStats($i->network_user_id, $i->network);
$ot = ($user_xml_total_posts_by_owner != '' ? true : false);
$lsi = ($i->last_status_id != "" ? true : false);
$is_archive_loaded_follows = $this->convertBoolToDB($i->is_archive_loaded_follows);
Expand Down

0 comments on commit ca6b5c6

Please sign in to comment.