verticalslice and reindex cleanups and tests
This commit is contained in:
15
db.class.php
15
db.class.php
@@ -882,10 +882,18 @@ class DBHelper {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static function verticalSlice($array, $field, $keyfield = null) {
|
public static function verticalSlice($array, $field, $keyfield = null) {
|
||||||
|
$array = (array) $array;
|
||||||
|
|
||||||
$R = array();
|
$R = array();
|
||||||
foreach ($array as $obj) {
|
foreach ($array as $obj) {
|
||||||
if ($keyfield) $R[$obj[$keyfield]] = $obj[$field];
|
if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n");
|
||||||
else $R[] = $obj[$field];
|
|
||||||
|
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;
|
return $R;
|
||||||
}
|
}
|
||||||
@@ -898,12 +906,15 @@ class DBHelper {
|
|||||||
public static function reIndex() {
|
public static function reIndex() {
|
||||||
$fields = func_get_args();
|
$fields = func_get_args();
|
||||||
$array = array_shift($fields);
|
$array = array_shift($fields);
|
||||||
|
$array = (array) $array;
|
||||||
|
|
||||||
$R = array();
|
$R = array();
|
||||||
foreach ($array as $obj) {
|
foreach ($array as $obj) {
|
||||||
$target =& $R;
|
$target =& $R;
|
||||||
|
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
|
if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n");
|
||||||
|
|
||||||
$nextkey = $obj[$field];
|
$nextkey = $obj[$field];
|
||||||
$target =& $target[$nextkey];
|
$target =& $target[$nextkey];
|
||||||
}
|
}
|
||||||
|
|||||||
51
simpletest/HelperTest.php
Normal file
51
simpletest/HelperTest.php
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<?
|
||||||
|
class HelperTest extends SimpleTest {
|
||||||
|
function test_1_verticalslice() {
|
||||||
|
$all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
|
||||||
|
$names = DBHelper::verticalSlice($all, 'username');
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
||||||
@@ -15,7 +15,7 @@ class ObjectTest extends SimpleTest {
|
|||||||
$this->mdb->query("CREATE TABLE `accounts` (
|
$this->mdb->query("CREATE TABLE `accounts` (
|
||||||
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||||
`username` VARCHAR( 255 ) NOT NULL ,
|
`username` VARCHAR( 255 ) NOT NULL ,
|
||||||
`password` VARCHAR( 255 ) NOT NULL ,
|
`password` VARCHAR( 255 ) NULL ,
|
||||||
`age` INT NOT NULL DEFAULT '10',
|
`age` INT NOT NULL DEFAULT '10',
|
||||||
`height` DOUBLE NOT NULL DEFAULT '10.0',
|
`height` DOUBLE NOT NULL DEFAULT '10.0',
|
||||||
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
|
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ require_once 'ObjectTest.php';
|
|||||||
require_once 'WhereClauseTest.php';
|
require_once 'WhereClauseTest.php';
|
||||||
require_once 'ErrorTest.php';
|
require_once 'ErrorTest.php';
|
||||||
require_once 'TransactionTest.php';
|
require_once 'TransactionTest.php';
|
||||||
|
require_once 'HelperTest.php';
|
||||||
|
|
||||||
$classes_to_test = array(
|
$classes_to_test = array(
|
||||||
'BasicTest',
|
'BasicTest',
|
||||||
@@ -44,6 +45,7 @@ $classes_to_test = array(
|
|||||||
'ObjectTest',
|
'ObjectTest',
|
||||||
'ErrorTest',
|
'ErrorTest',
|
||||||
'TransactionTest',
|
'TransactionTest',
|
||||||
|
'HelperTest',
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($is_php_53) {
|
if ($is_php_53) {
|
||||||
|
|||||||
Reference in New Issue
Block a user