MDL-15837 Started sql_ helper function tests for mysqli, mysql and postgres7

This commit is contained in:
nicolasconnault
2008-08-07 14:55:03 +00:00
parent a0df358a47
commit 1ddfd4ec23
4 changed files with 127 additions and 0 deletions
+7
View File
@@ -71,6 +71,7 @@ if (!empty($tests)) {
foreach ($tests as $i=>$database) {
$dbinfo = $dbinfos[$i];
$UNITTEST->func_test_db = $database; // pass the db to the tests through global
print_heading('Running tests on: '.$dbinfo['name'], '', 3); // TODO: localise
@@ -81,6 +82,12 @@ if (!empty($tests)) {
$test->addTestFile($CFG->libdir.'/dml/simpletest/testdml.php');
$test->addTestFile($CFG->libdir.'/ddl/simpletest/testddl.php');
// Look for DB-specific tests (testing sql_ helper functions)
$dbfilename = $CFG->libdir.'/dml/simpletest/test_'.get_class($database).'.php';
if (file_exists($dbfilename)) {
$test->addTestFile($dbfilename);
}
// Make the reporter, which is what displays the results.
$reporter = new ExHtmlReporter($showpasses);
+30
View File
@@ -0,0 +1,30 @@
<?php
class dbspecific_test extends UnitTestCase {
protected $tables = array();
protected $tdb;
protected $data;
function setUp() {
global $CFG, $DB, $UNITTEST;
if (isset($UNITTEST->func_test_db)) {
$this->tdb = $UNITTEST->func_test_db;
} else {
$this->tdb = $DB;
}
}
function tearDown() {
$dbman = $this->tdb->get_manager();
foreach ($this->tables as $table) {
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
}
$this->tables = array();
}
}
?>
@@ -0,0 +1,32 @@
<?php
/**
* Unit tests for helper functions of mysqli class
* @package dml
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once('dbspecific.php');
class mysqli_adodb_moodle_database_test extends dbspecific_test {
function test_cast_char2int() {
$DB = $this->tdb;
$field = $DB->get_field_sql("SELECT " . $DB->sql_cast_char2int("'two'") . " AS name_int");
$this->assertEqual(0, $field);
$field = $DB->get_field_sql("SELECT " . $DB->sql_cast_char2int("'74971.4901'") . " AS name_int");
$this->assertEqual(74971, $field);
}
function test_regex() {
$DB = $this->tdb;
$name = 'something or another';
$sql = "SELECT '$name' ".$DB->sql_regex()." 'th'";
$this->assertTrue($DB->get_field_sql($sql));
$sql = "SELECT '$name' ".$DB->sql_regex(false)." 'th'";
$this->assertFalse($DB->get_field_sql($sql));
}
}
?>
@@ -0,0 +1,58 @@
<?php
/**
* Unit tests for helper functions of mysqli class
* @package dml
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once('dbspecific.php');
class postgres7_adodb_moodle_database_test extends dbspecific_test {
function test_ilike() {
$DB = $this->tdb;
$dbman = $DB->get_manager();
$table = new xmldb_table("testtable");
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$this->tables[$table->getName()] = $table;
$id = $DB->insert_record('testtable', array('name' => 'SuperDuperREcord'));
$wheresql = "name " . $DB->sql_ilike() . " '%per%'";
$record = $DB->get_record_select('testtable', $wheresql);
$this->assertEqual('SuperDuperREcord', $record->name);
$wheresql = "name " . $DB->sql_ilike() . " 'per'";
$record = $DB->get_record_select('testtable', $wheresql);
$this->assertFalse($record);
}
function test_concat() {
$DB = $this->tdb;
$sql = "SELECT " . $DB->sql_concat("'name'", "'name2'", "'name3'") . " AS fullname";
$this->assertEqual("namename2name3", $DB->get_field_sql($sql));
}
function test_bitxor() {
$DB = $this->tdb;
$sql = "SELECT " . $DB->sql_bitxor(23,53);
$this->assertEqual(34, $DB->get_field_sql($sql));
}
function test_cast_char2int() {
$DB = $this->tdb;
$field = $DB->get_field_sql("SELECT " . $DB->sql_cast_char2int("'two'"));
$this->assertFalse($field);
$field = $DB->get_field_sql("SELECT " . $DB->sql_cast_char2int("'74971.4901'"));
$this->assertFalse($field);
$field = $DB->get_field_sql("SELECT " . $DB->sql_cast_char2int("'74971'"));
$this->assertEqual(74971, $field);
}
}
?>