Skip to content

Commit

Permalink
[DB MIGRATION REQ'D] Insights and baseline DAOs
Browse files Browse the repository at this point in the history
  • Loading branch information
ginatrapani committed Sep 6, 2012
1 parent 7a91800 commit 2246c55
Show file tree
Hide file tree
Showing 18 changed files with 709 additions and 50 deletions.
8 changes: 7 additions & 1 deletion tests/TestOfDAOFactory.php
Expand Up @@ -376,6 +376,12 @@ public function testGetShortLinkDAO() {
$this->assertIsA($dao, 'ShortLinkMySQLDAO');
}

public function testInsightBaselineDAO() {
$dao = DAOFactory::getDAO('InsightBaselineDAO');
$this->assertNotNull($dao);
$this->assertIsA($dao, 'InsightBaselineMySQLDAO');
}

public function testInsightDAO() {
$dao = DAOFactory::getDAO('InsightDAO');
$this->assertNotNull($dao);
Expand All @@ -393,7 +399,7 @@ public function testGetInstallerDAONoConfigFile(){
$this->assertTrue(isset($dao));
$this->assertIsA($dao, 'InstallerMySQLDAO');
$result = $dao->getTables();
$this->assertEqual(sizeof($result), 30);
$this->assertEqual(sizeof($result), 31);
$this->assertEqual($result[0], $cfg_values["table_prefix"].'encoded_locations');
$this->restoreConfigFile();
}
Expand Down
104 changes: 104 additions & 0 deletions tests/TestOfInsightBaselineMySQLDAO.php
@@ -0,0 +1,104 @@
<?php
/**
*
* ThinkUp/tests/TestOfInsightBaselineMySQLDAO.php
*
* Copyright (c) 2012 Gina Trapani
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
* later version.
*
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani
*/
require_once dirname(__FILE__).'/init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'config.inc.php';

class TestOfInsightBaselineMySQLDAO extends ThinkUpUnitTestCase {
public function setUp() {
parent::setUp();
$this->builders = self::buildData();
}

protected function buildData() {
$builders = array();
$builders[] = FixtureBuilder::build('insight_baselines', array('date'=>'2012-05-01',
'slug'=>'avg_replies_per_week', 'instance_id'=>1, 'value'=>51));
return $builders;
}

public function tearDown() {
$this->builders = null;
parent::tearDown();
}

public function testInsertInsightBaseline() {
$dao = new InsightBaselineMySQLDAO();
//date specified
$result = $dao->insertInsightBaseline('avg_replies_per_week', 1, 51, '2012-05-05');
$this->assertTrue($result);

$result = $dao->getInsightBaseline('avg_replies_per_week', 1, '2012-05-05');
$this->assertEqual($result->value, 51);

//inserting existing baseline should update
$result = $dao->insertInsightBaseline('avg_replies_per_week', 1, 50, '2012-05-05');
$this->assertTrue($result);

//assert update was successful
$result = $dao->getInsightBaseline('avg_replies_per_week', 1, '2012-05-05');
$this->assertEqual($result->value, 50);

//no date specified
$result = $dao->insertInsightBaseline('avg_replies_per_week', 1, 4551);
$this->assertTrue($result);
$result = $dao->getInsightBaseline('avg_replies_per_week', 1);
$this->assertEqual($result->value, 4551);
}

public function testGetInsightBaseline() {
$dao = new InsightBaselineMySQLDAO();
$result = $dao->getInsightBaseline('avg_replies_per_week', 1, '2012-05-01');
$this->assertIsA($result, 'InsightBaseline');
$this->assertEqual($result->slug, 'avg_replies_per_week');
$this->assertEqual($result->date, '2012-05-01');
$this->assertEqual($result->instance_id, 1);
$this->assertEqual($result->value, 51);

$result = $dao->getInsightBaseline('avg_replies_per_week', 1, '2012-05-02');
$this->assertNull($result);
}

public function testUpdateInsightBaseline() {
$dao = new InsightBaselineMySQLDAO();

//update existing baseline
$result = $dao->updateInsightBaseline('avg_replies_per_week', 1, 101, '2012-05-01');
$this->assertTrue($result);
//check that value was updated
$result = $dao->getInsightBaseline('avg_replies_per_week', 1, '2012-05-01');
$this->assertEqual($result->value, 101);

//update nonexistent baseline
$result = $dao->updateInsightBaseline('avg_replies_per_week', 1, 101, '2012-05-10');
$this->assertFalse($result);

//no date specified
$result = $dao->updateInsightBaseline('avg_replies_per_week', 1, 101);
$this->assertFalse($result);
}
}
164 changes: 156 additions & 8 deletions tests/TestOfInsightMySQLDAO.php
Expand Up @@ -37,7 +37,7 @@ public function setUp() {
protected function buildData() {
$builders = array();
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'avg_replies_per_week',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'instance_id'=>'1', 'prefix'=>'Booyah!', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));

//Set up array of owner objects
Expand Down Expand Up @@ -78,7 +78,7 @@ protected function buildData() {
$owners[] = $owner_3;

$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-06-15', 'slug'=>'a_bunch_of_owners',
'instance_id'=>'1', 'text'=>'Here are owners', 'related_data'=>serialize($owners),
'instance_id'=>'1', 'prefix'=>'Hooray!', 'text'=>'Here are owners', 'related_data'=>serialize($owners),
'emphasis'=>Insight::EMPHASIS_HIGH));

return $builders;
Expand All @@ -88,14 +88,14 @@ public function tearDown() {
$this->builders = null;
parent::tearDown();
}

public function testGetInsight() {
$dao = new InsightMySQLDAO();
$result = $dao->getInsight('avg_replies_per_week', 1, '2012-05-01');
$this->assertIsA($result, 'Insight');
$this->assertEqual($result->slug, 'avg_replies_per_week');
$this->assertEqual($result->date, '2012-05-01');
$this->assertEqual($result->instance_id, 1);
$this->assertEqual($result->prefix, 'Booyah!');
$this->assertEqual($result->text, 'Retweet spike! Your post got retweeted 110 times');
$this->assertEqual($result->emphasis, Insight::EMPHASIS_HIGH);

Expand Down Expand Up @@ -123,33 +123,37 @@ public function testGetPreCachedInsightData() {
public function testInsertInsight() {
$dao = new InsightMySQLDAO();
//date specified
$result = $dao->insertInsight('avg_replies_per_week', 1, '2012-05-05', 'Oh hai! You rock');
$result = $dao->insertInsight('avg_replies_per_week', 1, '2012-05-05', 'Oh hai!', 'You rock');
$this->assertTrue($result);

$result = $dao->getInsight('avg_replies_per_week', 1, '2012-05-05');
$this->assertEqual($result->text, 'Oh hai! You rock');
$this->assertEqual($result->prefix, 'Oh hai!');
$this->assertEqual($result->text, 'You rock');
$this->assertNull($result->related_data);
$this->assertEqual($result->emphasis, Insight::EMPHASIS_LOW);

//inserting existing insight should update
$result = $dao->insertInsight('avg_replies_per_week', 1, '2012-05-05', 'Oh hai! Updated: You rock',
$result = $dao->insertInsight('avg_replies_per_week', 1, '2012-05-05', 'Ohai!', 'Updated: You rock',
Insight::EMPHASIS_HIGH);
$this->assertTrue($result);

//assert update was successful
$result = $dao->getInsight('avg_replies_per_week', 1, '2012-05-05');
$this->assertEqual($result->text, 'Oh hai! Updated: You rock');
$this->assertEqual($result->prefix, 'Ohai!' );
$this->assertEqual($result->text, 'Updated: You rock');
$this->assertEqual($result->emphasis, Insight::EMPHASIS_HIGH);
}

public function testUpdateInsight() {
$dao = new InsightMySQLDAO();

//update existing baseline
$result = $dao->updateInsight('avg_replies_per_week', 1, '2012-05-01', 'LOLlerskates', Insight::EMPHASIS_MED);
$result = $dao->updateInsight('avg_replies_per_week', 1, '2012-05-01', "Yay", 'LOLlerskates',
Insight::EMPHASIS_MED);
$this->assertTrue($result);
//check that value was updated
$result = $dao->getInsight('avg_replies_per_week', 1, '2012-05-01');
$this->assertEqual($result->prefix, 'Yay');
$this->assertEqual($result->text, 'LOLlerskates');
$this->assertEqual($result->emphasis, Insight::EMPHASIS_MED);

Expand Down Expand Up @@ -203,4 +207,148 @@ public function testDeleteInsightsBySlug() {
$result = $dao->deleteInsightsBySlug('avg_replies_per_week_another_slug', 1);
$this->assertFalse($result);
}

public function testGetPublicInsights() {
$builders = array();
//insert a public instance
$builders[] = FixtureBuilder::build('instances', array('id'=>1, 'network_user_id'=>10,
'network_username'=>'jack', 'network'=>'twitter', 'network_viewer_id'=>10,
'crawler_last_run'=>'1988-01-20 12:00:00', 'is_active'=>1, 'is_public'=>0));
//insert a private instance
$builders[] = FixtureBuilder::build('instances', array('id'=>2, 'network_user_id'=>12,
'network_username'=>'jill', 'network'=>'twitter', 'network_viewer_id'=>12,
'crawler_last_run'=>'2010-01-20 12:00:00', 'is_active'=>1, 'is_public'=>1));

//insert 2 insights for a private instance and 3 for a public instance
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-03', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));

//assert that page of insights is only 3 long for public instane
$dao = new InsightMySQLDAO();
$results = $dao->getPublicInsights($page_count=10, $page_number=1);
$this->assertEqual(sizeof($results), 3);
foreach ($results as $result) {
$this->assertTrue(isset($result->instance));
}
}

public function testGetAllInsights() {
$builders = array();
//insert a public instance
$builders[] = FixtureBuilder::build('instances', array('id'=>1, 'network_user_id'=>10,
'network_username'=>'jack', 'network'=>'twitter', 'network_viewer_id'=>10,
'crawler_last_run'=>'1988-01-20 12:00:00', 'is_active'=>1, 'is_public'=>0));
//insert a private instance
$builders[] = FixtureBuilder::build('instances', array('id'=>2, 'network_user_id'=>12,
'network_username'=>'jill', 'network'=>'twitter', 'network_viewer_id'=>12,
'crawler_last_run'=>'2010-01-20 12:00:00', 'is_active'=>1, 'is_public'=>1));
//insert a non-active instance
$builders[] = FixtureBuilder::build('instances', array('id'=>3, 'network_user_id'=>12,
'network_username'=>'jane', 'network'=>'twitter', 'network_viewer_id'=>12,
'crawler_last_run'=>'2010-01-20 12:00:00', 'is_active'=>0, 'is_public'=>1));

//insert 2 insights for a private instance and 3 for a public instance
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-03', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
//insight with no text shouldn't be returned
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'',
'emphasis'=>Insight::EMPHASIS_HIGH));

//assert that page of insights includes from both private and public
$dao = new InsightMySQLDAO();
$results = $dao->getAllInstanceInsights($page_count=10, $page_number=1);
$this->assertEqual(sizeof($results), 7);
foreach ($results as $result) {
$this->assertTrue(isset($result->instance));
}
}

public function testGetAllOwnerInstanceInsights() {
$builders = array();
//insert a public instance
$builders[] = FixtureBuilder::build('instances', array('id'=>1, 'network_user_id'=>10,
'network_username'=>'jack', 'network'=>'twitter', 'network_viewer_id'=>10,
'crawler_last_run'=>'1988-01-20 12:00:00', 'is_active'=>1, 'is_public'=>0));
//insert a private instance
$builders[] = FixtureBuilder::build('instances', array('id'=>2, 'network_user_id'=>12,
'network_username'=>'jill', 'network'=>'twitter', 'network_viewer_id'=>12,
'crawler_last_run'=>'2010-01-20 12:00:00', 'is_active'=>1, 'is_public'=>1));
//insert a non-active instance
$builders[] = FixtureBuilder::build('instances', array('id'=>3, 'network_user_id'=>12,
'network_username'=>'jane', 'network'=>'twitter', 'network_viewer_id'=>12,
'crawler_last_run'=>'2010-01-20 12:00:00', 'is_active'=>0, 'is_public'=>1));

//insert instance owner
$builders[] = FixtureBuilder::build('owners', array('id'=>1, 'full_name'=>'Owner 1',
'email'=>'owner@example.com'));
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>1));
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>2));
$builders[] = FixtureBuilder::build('owner_instances', array('owner_id'=>1, 'instance_id'=>3));

//insert 2 insights for a private instance and 3 for a public instance
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-02', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-03', 'slug'=>'avg_replies_per_week',
'instance_id'=>'2', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'Retweet spike! Your post got retweeted 110 times',
'emphasis'=>Insight::EMPHASIS_HIGH));
//insight with no text shouldn't be returned
$builders[] = FixtureBuilder::build('insights', array('date'=>'2012-05-01', 'slug'=>'another_slug',
'instance_id'=>'1', 'text'=>'',
'emphasis'=>Insight::EMPHASIS_HIGH));

//assert that page of insights includes from both private and public
$dao = new InsightMySQLDAO();
$results = $dao->getAllOwnerInstanceInsights(1, $page_count=10, $page_number=1);
$this->assertEqual(sizeof($results), 7);
foreach ($results as $result) {
$this->assertTrue(isset($result->instance));
}
}

public function testDoesInsightExist() {
$dao = new InsightMySQLDAO();
$result = $dao->doesInsightExist("avg_replies_per_week", 1);
$this->assertTrue($result);
$result = $dao->doesInsightExist("avg_replies_per_week", 10);
$this->assertFalse($result);
$result = $dao->doesInsightExist("yo_yo_yooo", 1);
$this->assertFalse($result);
}
}

0 comments on commit 2246c55

Please sign in to comment.