MDL-14905 towards functional DB tests

This commit is contained in:
skodak 2008-06-10 19:54:27 +00:00
parent dfba1c3da1
commit e6b4f00ed7
7 changed files with 200 additions and 86 deletions

View file

@ -8,60 +8,55 @@ if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir . '/simpletestlib/web_tester.php');
require_once($CFG->libdir . '/dmllib.php');
require_once($CFG->libdir . '/dml/mysql_adodb_moodle_database.php');
class dmllib_test extends UnitTestCase {
private $tables = array();
private $dbmanager;
private $db;
function setUp() {
global $CFG;
global $CFG, $DB, $EXT_TEST_DB;
$this->db = new mysqli_adodb_moodle_database();
$this->db->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->prefix);
$this->dbmanager = $this->db->get_manager();
if (isset($EXT_TEST_DB)) {
$this->db = $EXT_TEST_DB;
} else {
$this->db = $DB;
}
$dbmanager = $this->db->get_manager();
$table = new xmldb_table("testtable");
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
$table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
$table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
$table->addFieldInfo('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
$table->addFieldInfo('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
$table->addFieldInfo('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
$table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
$table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
$table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
$table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
$table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
$table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->dbmanager->create_table($table);
$this->tables[] = $table;
// insert records
$datafile = $CFG->libdir . '/dml/simpletest/fixtures/testdata.xml';
$xml = simplexml_load_file($datafile);
foreach ($xml->record as $record) {
$this->db->insert_record('testtable', $record);
if ($dbmanager->table_exists($table)) {
$dbmanager->drop_table($table, true, false);
}
$dbmanager->create_table($table);
$tables[$table->getName()] = $table;
}
function tearDown() {
@ -70,41 +65,33 @@ class dmllib_test extends UnitTestCase {
$this->dbmanager->drop_table($table, true, false);
}
}
unset($this->tables);
setup_DB();
}
function test_insert_record() {
}
function test_get_record_select() {
$record = $this->db->get_record_select('testtable', 'id = 1');
$this->tables = array();
}
function test_fix_sql_params() {
$DB = $this->db; // do not use global $DB!
// Malformed table placeholder
$sql = "SELECT * FROM [testtable]";
$sqlarray = $this->db->fix_sql_params($sql);
$sqlarray = $DB->fix_sql_params($sql);
$this->assertEqual($sql, $sqlarray[0]);
// Correct table placeholder substitution
$sql = "SELECT * FROM {testtable}";
$sqlarray = $this->db->fix_sql_params($sql);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable", $sqlarray[0]);
// Malformed param placeholders
$sql = "SELECT * FROM {testtable} WHERE name = ?param1";
$params = array('param1' => 'first record');
$sqlarray = $this->db->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
// Mixed param types (colon and dollar)
$sql = "SELECT * FROM {testtable} WHERE name = :param1, rsstype = \$1";
$params = array('param1' => 'first record', 'param2' => 1);
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$this->assertEqual('error() call: ERROR: Mixed types of sql query parameters!!', $e->getMessage());
}
@ -114,7 +101,7 @@ class dmllib_test extends UnitTestCase {
$params = array('param1' => 'first record', 'param2' => 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -125,7 +112,7 @@ class dmllib_test extends UnitTestCase {
$params = array('first record', 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -138,7 +125,7 @@ class dmllib_test extends UnitTestCase {
$sqlarray = null;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -150,7 +137,7 @@ class dmllib_test extends UnitTestCase {
$params = array('wrongname' => 'first record', 'rsstype' => 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -161,7 +148,7 @@ class dmllib_test extends UnitTestCase {
$params = array('name' => 'first record', 'rsstype' => 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -172,7 +159,7 @@ class dmllib_test extends UnitTestCase {
$params = array('first record', 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -181,15 +168,15 @@ class dmllib_test extends UnitTestCase {
// Correct named param placeholders
$sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
$params = array('name' => 'first record', 'rsstype' => 1);
$sqlarray = $this->db->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$this->assertEqual(2, count($sqlarray[1]));
// Correct ? params
$sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?";
$params = array('first record', 1);
$sqlarray = $this->db->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$this->assertEqual(2, count($sqlarray[1]));
}