]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15837 Started sql_ helper function tests for mysqli, mysql and postgres7
authornicolasconnault <nicolasconnault>
Thu, 7 Aug 2008 14:55:03 +0000 (14:55 +0000)
committernicolasconnault <nicolasconnault>
Thu, 7 Aug 2008 14:55:03 +0000 (14:55 +0000)
admin/report/simpletest/dbtest.php
lib/dml/simpletest/dbspecific.php [new file with mode: 0644]
lib/dml/simpletest/test_mysqli_adodb_moodle_database.php [new file with mode: 0644]
lib/dml/simpletest/test_postgres7_adodb_moodle_database.php [new file with mode: 0644]

index e5ca21a212b55f559c6ec5c408d828597354f0c4..4d8c4246ef3252e0e572f08ad550d40720f18f73 100644 (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);
 
diff --git a/lib/dml/simpletest/dbspecific.php b/lib/dml/simpletest/dbspecific.php
new file mode 100644 (file)
index 0000000..e403d44
--- /dev/null
@@ -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();
+    }
+
+}
+?>
diff --git a/lib/dml/simpletest/test_mysqli_adodb_moodle_database.php b/lib/dml/simpletest/test_mysqli_adodb_moodle_database.php
new file mode 100644 (file)
index 0000000..e688d4b
--- /dev/null
@@ -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));
+    }
+}
+?>
diff --git a/lib/dml/simpletest/test_postgres7_adodb_moodle_database.php b/lib/dml/simpletest/test_postgres7_adodb_moodle_database.php
new file mode 100644 (file)
index 0000000..2213e79
--- /dev/null
@@ -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);
+    }
+}
+?>