From 1ddfd4ec23043e29d1737e6c3cea5c65f486df56 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Thu, 7 Aug 2008 14:55:03 +0000 Subject: [PATCH] MDL-15837 Started sql_ helper function tests for mysqli, mysql and postgres7 --- admin/report/simpletest/dbtest.php | 7 +++ lib/dml/simpletest/dbspecific.php | 30 ++++++++++ .../test_mysqli_adodb_moodle_database.php | 32 ++++++++++ .../test_postgres7_adodb_moodle_database.php | 58 +++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 lib/dml/simpletest/dbspecific.php create mode 100644 lib/dml/simpletest/test_mysqli_adodb_moodle_database.php create mode 100644 lib/dml/simpletest/test_postgres7_adodb_moodle_database.php diff --git a/admin/report/simpletest/dbtest.php b/admin/report/simpletest/dbtest.php index e5ca21a212..4d8c4246ef 100644 --- a/admin/report/simpletest/dbtest.php +++ b/admin/report/simpletest/dbtest.php @@ -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 index 0000000000..e403d44705 --- /dev/null +++ b/lib/dml/simpletest/dbspecific.php @@ -0,0 +1,30 @@ +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 index 0000000000..e688d4b22f --- /dev/null +++ b/lib/dml/simpletest/test_mysqli_adodb_moodle_database.php @@ -0,0 +1,32 @@ +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 index 0000000000..2213e79d80 --- /dev/null +++ b/lib/dml/simpletest/test_postgres7_adodb_moodle_database.php @@ -0,0 +1,58 @@ +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); + } +} +?> -- 2.39.5