From 162a873f9dab13c39041b08e58fb9bfbe3d49b3e Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Thu, 23 Jan 2014 17:04:29 -0500 Subject: [PATCH] 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. :-) --- airtime_mvc/tests/README.txt | 5 ++ airtime_mvc/tests/TODO.txt | 2 + airtime_mvc/tests/airtime.conf | 2 +- airtime_mvc/tests/application/bootstrap.php | 3 + .../tests/application/helpers/TestHelper.php | 67 ++++++++++++++++++- .../models/unit/PreferenceUnitTest.php | 29 ++++++++ .../services/database/ShowServiceDbTest.php | 12 +--- install_minimal/include/AirtimeInstall.php | 7 +- 8 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 airtime_mvc/tests/TODO.txt create mode 100644 airtime_mvc/tests/application/models/unit/PreferenceUnitTest.php diff --git a/airtime_mvc/tests/README.txt b/airtime_mvc/tests/README.txt index 4dac4e9f2..adc5beb83 100644 --- a/airtime_mvc/tests/README.txt +++ b/airtime_mvc/tests/README.txt @@ -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.) diff --git a/airtime_mvc/tests/TODO.txt b/airtime_mvc/tests/TODO.txt new file mode 100644 index 000000000..751b596d8 --- /dev/null +++ b/airtime_mvc/tests/TODO.txt @@ -0,0 +1,2 @@ +TODO: +- Set up a RabbitMQ airtime_test user that is hardcoded diff --git a/airtime_mvc/tests/airtime.conf b/airtime_mvc/tests/airtime.conf index 21275e80a..703880ba0 100755 --- a/airtime_mvc/tests/airtime.conf +++ b/airtime_mvc/tests/airtime.conf @@ -8,7 +8,7 @@ dbpass = airtime host = 127.0.0.1 port = 5672 user = airtime -password = GTUR1HVMU7GPIO3FETKY +password = GEN7GWIOB66FFKY30ERF vhost = /airtime [general] diff --git a/airtime_mvc/tests/application/bootstrap.php b/airtime_mvc/tests/application/bootstrap.php index 66f008f1b..55b1408da 100644 --- a/airtime_mvc/tests/application/bootstrap.php +++ b/airtime_mvc/tests/application/bootstrap.php @@ -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(); diff --git a/airtime_mvc/tests/application/helpers/TestHelper.php b/airtime_mvc/tests/application/helpers/TestHelper.php index b5880c1ba..283f8b895 100644 --- a/airtime_mvc/tests/application/helpers/TestHelper.php +++ b/airtime_mvc/tests/application/helpers/TestHelper.php @@ -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(); } -} \ No newline at end of file + + public static function setupZendBootstrap() + { + $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH .'/configs/application.ini'); + $application->bootstrap(); + return $application; + } +} diff --git a/airtime_mvc/tests/application/models/unit/PreferenceUnitTest.php b/airtime_mvc/tests/application/models/unit/PreferenceUnitTest.php new file mode 100644 index 000000000..94322af03 --- /dev/null +++ b/airtime_mvc/tests/application/models/unit/PreferenceUnitTest.php @@ -0,0 +1,29 @@ +assertEquals(Application_Model_Preference::GetHeadTitle(), $title); + } + + public function testSetShowsPopulatedUntil() + { + $date = new DateTime(); + Application_Model_Preference::SetShowsPopulatedUntil($date); + $this->assertEquals(Application_Model_Preference::GetShowsPopulatedUntil(), $date); + } + +} diff --git a/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php b/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php index 551bf4b5d..50ad7e5be 100644 --- a/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php +++ b/airtime_mvc/tests/application/services/database/ShowServiceDbTest.php @@ -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) { diff --git a/install_minimal/include/AirtimeInstall.php b/install_minimal/include/AirtimeInstall.php index fccec52b6..7f1523137 100644 --- a/install_minimal/include/AirtimeInstall.php +++ b/install_minimal/include/AirtimeInstall.php @@ -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);