Skip to content

Commit

Permalink
Create insight baselines and insight database tables, model objects. …
Browse files Browse the repository at this point in the history
…Create InsightBaseline data access object interface and MySQL implementation and tests
  • Loading branch information
ginatrapani committed May 8, 2012
1 parent 24440cd commit 44e4a4a
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 2 deletions.
87 changes: 87 additions & 0 deletions tests/TestOfInsightBaselineMySQLDAO.php
@@ -0,0 +1,87 @@
<?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();
$result = $dao->insertInsightBaseline('avg_replies_per_week', '2012-05-05', 1, 51);
$this->assertTrue($result);

$result = $dao->insertInsightBaseline('avg_replies_per_week', '2012-05-05', 1, 50);
$this->assertTrue($result);
$result = $dao->getInsightBaseline('avg_replies_per_week', '2012-05-05', 1);
$this->assertEqual($result->value, 50);
}

public function testGetInsightBaseline() {
$dao = new InsightBaselineMySQLDAO();
$result = $dao->getInsightBaseline('avg_replies_per_week', '2012-05-01', 1);
$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', '2012-05-02', 1);
$this->assertNull($result);
}

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

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

//update nonexistent baseline
$result = $dao->updateInsightBaseline('avg_replies_per_week', '2012-05-10', 1, 101);
$this->assertFalse($result);
}
}
1 change: 1 addition & 0 deletions tests/all_model_tests.php
Expand Up @@ -43,6 +43,7 @@
$model_tests->add(new TestOfGroupMySQLDAO());
$model_tests->add(new TestOfGroupMemberMySQLDAO());
$model_tests->add(new TestOfGroupMembershipCountMySQLDAO());
$model_tests->add(new TestOfInsightBaselineMySQLDAO());
$model_tests->add(new TestOfInstanceMySQLDAO());
$model_tests->add(new TestOfInstaller());
$model_tests->add(new TestOfInstallerMySQLDAO());
Expand Down
64 changes: 64 additions & 0 deletions webapp/_lib/model/class.Insight.php
@@ -0,0 +1,64 @@
<?php
/**
*
* ThinkUp/webapp/_lib/model/class.Insight.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/>.
*
* Insight
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
class Insight {
/**
* @var int Internal unique ID.
*/
var $id;
/**
* @var int Instance ID.
*/
var $instance_id;
/**
* @var str Identifier for a type of statistic.
*/
var $slug;
/**
* @var str Text content of the alert.
*/
var $text;
/**
* @var int Internal post ID.
*/
var $post_key;
/**
* @var date Date of insight.
*/
var $date;
public function __construct($row = false) {
if ($row) {
$this->id = $row['id'];
$this->instance_id = $row['instance_id'];
$this->slug = $row['slug'];
$this->text = $row['text'];
$this->post_key = $row['post_key'];
$this->date = $row['date'];
}
}
}
54 changes: 54 additions & 0 deletions webapp/_lib/model/class.InsightBaseline.php
@@ -0,0 +1,54 @@
<?php
/**
*
* ThinkUp/webapp/_lib/model/class.InsightBaseline.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/>.
*
* InsightBaseline
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
class InsightBaseline {
/**
* @var date Date of baseline statistic.
*/
var $date;
/**
* @var int Instance ID.
*/
var $instance_id;
/**
* @var str Unique identifier for a type of statistic.
*/
var $slug;
/**
* @var int The numeric value of this stat/total/average.
*/
var $value;
public function __construct($row = false) {
if ($row) {
$this->date = $row['date'];
$this->instance_id = $row['instance_id'];
$this->slug = $row['slug'];
$this->value = $row['value'];
}
}
}
80 changes: 80 additions & 0 deletions webapp/_lib/model/class.InsightBaselineMySQLDAO.php
@@ -0,0 +1,80 @@
<?php
/**
*
* ThinkUp/webapp/_lib/model/class.InsightBaselineMySQLDAO.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/>.
*
* Insight Baseline Data Access Object MySQL Implementation
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
class InsightBaselineMySQLDAO extends PDODAO implements InsightBaselineDAO {

public function insertInsightBaseline($slug, $date, $instance_id, $value) {
$baseline = self::getInsightBaseline($slug, $date, $instance_id);
if ($baseline == null) {
$q = "INSERT INTO #prefix#insight_baselines SET slug=:slug, date=:date, instance_id=:instance_id, ";
$q .= "value=:value";
$vars = array(
':slug'=>$slug,
':date'=>$date,
':instance_id'=>$instance_id,
':value'=>$value
);
$ps = $this->execute($q, $vars);
$result = $this->getUpdateCount($ps);
return ($result > 0);
} else {
return self::updateInsightBaseline($slug, $date, $instance_id, $value);
}
}

public function getInsightBaseline($slug, $date, $instance_id) {
$q = "SELECT date, instance_id, slug, value FROM #prefix#insight_baselines WHERE slug=:slug AND date=:date ";
$q .= "AND instance_id=:instance_id";
$vars = array(
':slug'=>$slug,
':date'=>$date,
':instance_id'=>$instance_id
);
$ps = $this->execute($q, $vars);
$row = $this->getDataRowAsArray($ps);
if ($row) {
return new InsightBaseline($row);
} else {
return null;
}
}

public function updateInsightBaseline($slug, $date, $instance_id, $value) {
$q = "UPDATE #prefix#insight_baselines SET value=:value ";
$q .= "WHERE slug=:slug AND date=:date AND instance_id=:instance_id";
$vars = array(
':slug'=>$slug,
':date'=>$date,
':instance_id'=>$instance_id,
':value'=>$value
);
$ps = $this->execute($q, $vars);
$result = $this->getUpdateCount($ps);
return ($result > 0);
}
}
54 changes: 54 additions & 0 deletions webapp/_lib/model/interface.InsightBaselineDAO.php
@@ -0,0 +1,54 @@
<?php
/**
*
* ThinkUp/webapp/_lib/model/interface.InsightBaselineDAO.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/>.
*
* Insight Baseline Data Access Object
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2012 Gina Trapani
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
interface InsightBaselineDAO {
/**
* Insert insight baseline into storage.
* @param str $slug
* @param str $date
* @param int $instance_id
* @param int $value
* @return bool
*/
public function insertInsightBaseline($slug, $date, $instance_id, $value);
/**
* Retrieve insight baseline from storage.
* @param str $slug
* @param str $date
* @param int $instance_id
* @return InsightBaseline
*/
public function getInsightBaseline($slug, $date, $instance_id);
/**
* Update insight baseline in storage.
* @param str $slug
* @param str $date
* @return bool
*/
public function updateInsightBaseline($slug, $date, $instance_id, $value);
}

0 comments on commit 44e4a4a

Please sign in to comment.