fix minor db bugs, add preliminary unit testing

This commit is contained in:
Sergey Tsalkov
2011-02-21 01:14:36 -05:00
parent db0feccfd4
commit 2e07278d35
3 changed files with 150 additions and 1 deletions

View File

@@ -42,7 +42,6 @@ class DB
public static function useDB() { return call_user_func_array('DB::setDB', func_get_args()); } public static function useDB() { return call_user_func_array('DB::setDB', func_get_args()); }
public static function setDB($dbName, $limit=0) { public static function setDB($dbName, $limit=0) {
if (DB::$current_db == $dbName) return true;
$db = DB::get(); $db = DB::get();
DB::$old_db = DB::$current_db; DB::$old_db = DB::$current_db;
if (! $db->select_db($dbName)) die("unable to set db to $dbName"); if (! $db->select_db($dbName)) die("unable to set db to $dbName");
@@ -74,6 +73,7 @@ class DB
} }
private static function formatTableName($table) { private static function formatTableName($table) {
$table = str_replace('`', '', $table);
if (strpos($table, '.')) { if (strpos($table, '.')) {
list($table_db, $table_table) = explode('.', $table, 2); list($table_db, $table_table) = explode('.', $table, 2);
$table = "`$table_db`.`$table_table`"; $table = "`$table_db`.`$table_table`";

89
simpletest/BasicTest.php Normal file
View File

@@ -0,0 +1,89 @@
<?
class BasicTest extends SimpleTest {
function __construct() {
require_once '../db.class.php';
DB::$user = 'libdb_user';
DB::$password = 'sdf235sklj';
DB::$dbName = 'libdb_test';
DB::query("DROP DATABASE libdb_test");
DB::query("CREATE DATABASE libdb_test");
DB::useDB('libdb_test');
}
function test_1_create_table() {
DB::query("CREATE TABLE `accounts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 255 ) NOT NULL ,
`password` VARCHAR( 255 ) NOT NULL ,
`age` INT NOT NULL DEFAULT '10',
`height` DOUBLE NOT NULL DEFAULT '10.0'
) ENGINE = InnoDB");
}
function test_1_5_empty_table() {
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
$this->assert($counter === strval(0));
$row = DB::queryFirstRow("SELECT * FROM accounts");
$this->assert($row === null);
$field = DB::queryFirstField("SELECT * FROM accounts");
$this->assert($field === null);
$field = DB::queryOneField('nothere', "SELECT * FROM accounts");
$this->assert($field === null);
$column = DB::queryFirstColumn("SELECT * FROM accounts");
$this->assert(is_array($column) && count($column) === 0);
$column = DB::queryOneColumn('nothere', "SELECT * FROM accounts"); //TODO: is this what we want?
$this->assert(is_array($column) && count($column) === 0);
}
function test_2_insert_row() {
DB::insert('accounts', array(
'username' => 'Abe',
'password' => 'hello'
));
$this->assert(DB::affectedRows() === 1);
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
$this->assert($counter === strval(1));
}
function test_3_more_inserts() {
DB::insert('`accounts`', array(
'username' => 'Bart',
'password' => 'hello',
'age' => 15,
'height' => 10.371
));
DB::insert('`libdb_test`.`accounts`', array(
'username' => 'Charlie\'s Friend',
'password' => 'goodbye',
'age' => 30,
'height' => 155.23
));
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
$this->assert($counter === strval(3));
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN %li AND height IN %ld AND username IN %ls",
array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
$this->assert($bart['username'] === 'Bart');
$charlie_password = DB::queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
$this->assert($charlie_password === 'goodbye');
}
}
?>

60
simpletest/test.php Executable file
View File

@@ -0,0 +1,60 @@
#!/usr/bin/php
<?
class SimpleTest {
protected function assert($boolean) {
if (! $boolean) $this->fail();
}
protected function fail($msg = '') {
echo "FAILURE! $msg\n";
debug_print_backtrace();
die;
}
public static function __listfiles($dir, $regex, $type='files', $rec = false) {
$A = array();
if (! $dir_handler = @opendir($dir)) return $A;
while (false !== ($filename = @readdir($dir_handler))) {
if ($filename == '.' || $filename == '..') continue;
if ($rec && is_dir("$dir/$filename")) $A = array_merge($A, File::listfiles("$dir/$filename", $regex, $type, true));
if (! preg_match($regex, $filename)) continue;
if ($type == 'files' && ! is_file("$dir/$filename")) continue;
if ($type == 'dirs' && ! is_dir("$dir/$filename")) continue;
if ($type == 'symlinks' && ! is_link("$dir/$filename")) continue;
$A[] = "$dir/$filename";
}
return $A;
}
}
$files = SimpleTest::__listfiles(__DIR__, '/^.*php$/i');
$classes_to_test = array();
foreach ($files as $fullpath) {
$filename = basename($fullpath);
if ($fullpath == __FILE__) continue;
require_once($fullpath);
$classes_to_test[] = str_replace('.php', '', $filename);
}
foreach ($classes_to_test as $class) {
$object = new $class();
foreach (get_class_methods($object) as $method) {
if (substr($method, 0, 2) == '__') continue;
echo "Running $class::$method..\n";
$object->$method();
}
}
?>