From ed347e841a59c2f666b2675aed46e4da06891ada Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Thu, 20 Sep 2012 20:50:05 -0700 Subject: [PATCH] verticalslice and reindex cleanups and tests --- db.class.php | 15 ++++++++++-- simpletest/HelperTest.php | 51 +++++++++++++++++++++++++++++++++++++++ simpletest/ObjectTest.php | 2 +- simpletest/test.php | 2 ++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 simpletest/HelperTest.php diff --git a/db.class.php b/db.class.php index b617e6e..e50ae57 100644 --- a/db.class.php +++ b/db.class.php @@ -882,10 +882,18 @@ class DBHelper { */ public static function verticalSlice($array, $field, $keyfield = null) { + $array = (array) $array; + $R = array(); foreach ($array as $obj) { - if ($keyfield) $R[$obj[$keyfield]] = $obj[$field]; - else $R[] = $obj[$field]; + if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n"); + + if ($keyfield) { + if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n"); + $R[$obj[$keyfield]] = $obj[$field]; + } else { + $R[] = $obj[$field]; + } } return $R; } @@ -898,12 +906,15 @@ class DBHelper { public static function reIndex() { $fields = func_get_args(); $array = array_shift($fields); + $array = (array) $array; $R = array(); foreach ($array as $obj) { $target =& $R; foreach ($fields as $field) { + if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n"); + $nextkey = $obj[$field]; $target =& $target[$nextkey]; } diff --git a/simpletest/HelperTest.php b/simpletest/HelperTest.php new file mode 100644 index 0000000..3bbf405 --- /dev/null +++ b/simpletest/HelperTest.php @@ -0,0 +1,51 @@ +assert(count($names) === 5); + $this->assert($names[0] === 'Abe'); + + $ages = DBHelper::verticalSlice($all, 'age', 'username'); + $this->assert(count($ages) === 5); + $this->assert($ages['Abe'] === '700'); + } + + function test_2_reindex() { + $all = DB::query("SELECT * FROM accounts ORDER BY id ASC"); + $names = DBHelper::reIndex($all, 'username'); + $this->assert(count($names) === 5); + $this->assert($names['Bart']['username'] === 'Bart'); + $this->assert($names['Bart']['age'] === '15'); + + $names = DBHelper::reIndex($all, 'username', 'age'); + $this->assert($names['Bart']['15']['username'] === 'Bart'); + $this->assert($names['Bart']['15']['age'] === '15'); + } + + function test_3_empty() { + $none = DB::query("SELECT * FROM accounts WHERE username=%s", 'doesnotexist'); + $this->assert(is_array($none) && count($none) === 0); + $names = DBHelper::verticalSlice($none, 'username', 'age'); + $this->assert(is_array($names) && count($names) === 0); + + $names_other = DBHelper::reIndex($none, 'username', 'age'); + $this->assert(is_array($names_other) && count($names_other) === 0); + } + + function test_4_null() { + DB::query("UPDATE accounts SET password = NULL WHERE username=%s", 'Bart'); + + $all = DB::query("SELECT * FROM accounts ORDER BY id ASC"); + $ages = DBHelper::verticalSlice($all, 'age', 'password'); + $this->assert(count($ages) === 5); + $this->assert($ages[''] === '15'); + + $passwords = DBHelper::reIndex($all, 'password'); + $this->assert(count($passwords) === 5); + $this->assert($passwords['']['username'] === 'Bart'); + $this->assert($passwords['']['password'] === NULL); + } + +} +?> diff --git a/simpletest/ObjectTest.php b/simpletest/ObjectTest.php index f9c612b..bd90cdc 100644 --- a/simpletest/ObjectTest.php +++ b/simpletest/ObjectTest.php @@ -15,7 +15,7 @@ class ObjectTest extends SimpleTest { $this->mdb->query("CREATE TABLE `accounts` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` VARCHAR( 255 ) NOT NULL , - `password` VARCHAR( 255 ) NOT NULL , + `password` VARCHAR( 255 ) NULL , `age` INT NOT NULL DEFAULT '10', `height` DOUBLE NOT NULL DEFAULT '10.0', `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi' diff --git a/simpletest/test.php b/simpletest/test.php index 81493ae..09d4a1c 100755 --- a/simpletest/test.php +++ b/simpletest/test.php @@ -37,6 +37,7 @@ require_once 'ObjectTest.php'; require_once 'WhereClauseTest.php'; require_once 'ErrorTest.php'; require_once 'TransactionTest.php'; +require_once 'HelperTest.php'; $classes_to_test = array( 'BasicTest', @@ -44,6 +45,7 @@ $classes_to_test = array( 'ObjectTest', 'ErrorTest', 'TransactionTest', + 'HelperTest', ); if ($is_php_53) {