CC-5651: Unit Test the Scheduler

* Continued refactoring of the database creation.
* Database now persists after running tests but most of the tables are
  cleared.
* The unit tests run WAY faster now. :-)
This commit is contained in:
Albert Santoni 2014-01-23 17:04:29 -05:00
parent aa2e084a09
commit 162a873f9d
8 changed files with 109 additions and 18 deletions

View File

@ -35,3 +35,8 @@ with a version that's incompatible and gives an error for us.
IMPORTANT: Make sure you use "sudo" with the "-E" flag so it preserves the environment variable we set before that.
FAQ
====
- If you get errors about an AMPQ fwrite failing, it means your RabbitMQ credentials are wrong in airtime.conf.
(That's the airtime.conf in this directory.)

View File

@ -0,0 +1,2 @@
TODO:
- Set up a RabbitMQ airtime_test user that is hardcoded

View File

@ -8,7 +8,7 @@ dbpass = airtime
host = 127.0.0.1
port = 5672
user = airtime
password = GTUR1HVMU7GPIO3FETKY
password = GEN7GWIOB66FFKY30ERF
vhost = /airtime
[general]

View File

@ -59,6 +59,7 @@ if (file_exists('/usr/share/php/libzend-framework-php')) {
require_once 'Zend/Application.php';
require_once 'Zend/Config.php';
//require_once 'helpers/TestHelper.php';
require_once APPLICATION_PATH.'/configs/conf.php';
require_once 'propel/runtime/lib/Propel.php';
@ -67,3 +68,5 @@ Propel::init("../application/configs/airtime-conf-production.php");
#require_once 'DatabaseTestCase.php';
require_once 'Zend/Session.php';
Zend_Session::start();
//TestHelper::installTestDatabase();

View File

@ -45,9 +45,70 @@ class TestHelper
$dbpasswd = $CC_CONFIG['dsn']['password'];
$dbname = $CC_CONFIG['dsn']['database'];
$dbhost = $CC_CONFIG['dsn']['hostspec'];
$databaseAlreadyExists = AirtimeInstall::createDatabase();
if ($databaseAlreadyExists)
{
//Truncate all the tables
$con = Propel::getConnection();
$sql = "select * from pg_tables where tableowner = 'airtime'";
try {
$rows = $con->query($sql)->fetchAll();
} catch (Exception $e) {
$rows = array();
}
//Add any tables that shouldn't be cleared here.
// cc_subjs - Most of Airtime requires an admin account to work, which has id=1,
// so don't clear it.
$tablesToNotClear = array("cc_subjs");
AirtimeInstall::createDatabase();
AirtimeInstall::createDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost);
$con->beginTransaction();
foreach ($rows as $row) {
$tablename = $row["tablename"];
if (in_array($tablename, $tablesToNotClear))
{
continue;
}
//echo " * Clearing database table $tablename...";
//TRUNCATE is actually slower than DELETE in many cases:
//http://stackoverflow.com/questions/11419536/postgresql-truncation-speed
//$sql = "TRUNCATE TABLE $tablename CASCADE";
$sql = "DELETE FROM $tablename";
AirtimeInstall::InstallQuery($sql, false);
}
$con->commit();
//Because we're DELETEing all the rows instead of using TRUNCATE (for speed),
//we have to reset the sequences so the auto-increment columns (like primary keys)
//all start at 1 again. This is hacky but it still lets all of this execute fast.
$sql = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'";
try {
$rows = $con->query($sql)->fetchAll();
} catch (Exception $e) {
$rows = array();
}
$con->beginTransaction();
foreach ($rows as $row) {
$seqrelname= $row["relname"];
$sql = "ALTER SEQUENCE ${seqrelname} RESTART WITH 1";
AirtimeInstall::InstallQuery($sql, false);
}
$con->commit();
}
else
{
//Create all the database tables
AirtimeInstall::createDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost);
}
AirtimeInstall::SetDefaultTimezone();
}
}
public static function setupZendBootstrap()
{
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH .'/configs/application.ini');
$application->bootstrap();
return $application;
}
}

View File

@ -0,0 +1,29 @@
<?php
require_once "../application/configs/conf.php";
require_once "TestHelper.php";
require_once "Preference.php";
class PreferenceUnitTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
TestHelper::installTestDatabase();
TestHelper::setupZendBootstrap();
}
public function testSetHeadTitle()
{
$title = "unit test";
Application_Model_Preference::SetHeadTitle($title);
$this->assertEquals(Application_Model_Preference::GetHeadTitle(), $title);
}
public function testSetShowsPopulatedUntil()
{
$date = new DateTime();
Application_Model_Preference::SetShowsPopulatedUntil($date);
$this->assertEquals(Application_Model_Preference::GetShowsPopulatedUntil(), $date);
}
}

View File

@ -22,23 +22,13 @@ class ShowServiceDbTest extends Zend_Test_PHPUnit_DatabaseTestCase
public function setUp()
{
TestHelper::installTestDatabase();
//XXX: Zend_Test_PHPUnit_DatabaseTestCase doesn't use this for whatever reason:
//$this->bootstrap = array($this, 'appBootstrap');
//So instead we just manually call the appBootstrap here:
$this->appBootstrap();
TestHelper::setupZendBootstrap();
//$this->_nowDT = new DateTime("now", new DateTimeZone("UTC"));
parent::setUp();
}
public function appBootstrap()
{
$this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH .'/configs/application.ini');
$this->application->bootstrap();
}
public function getConnection()
{
if ($this->_connectionMock == null) {

View File

@ -211,13 +211,14 @@ class AirtimeInstall
{
$CC_CONFIG = Config::getConfig();
echo " * Creating Airtime database".PHP_EOL;
$database = $CC_CONFIG['dsn']['database'];
$username = $CC_CONFIG['dsn']['username'];
#$command = "echo \"CREATE DATABASE $database OWNER $username\" | su postgres -c psql 2>/dev/null";
echo $database . PHP_EOL;
echo " * Creating Airtime database: " . $database . PHP_EOL;
putenv("LC_ALL=en_CA.UTF-8"); //Squash warnings when running unit tests
$command = "su postgres -c \"psql -l | cut -f2 -d' ' | grep -w '{$database}'\";";
exec($command, $output, $rv);