cleanup some error-checking code

add lots more tests for that code
This commit is contained in:
Sergey Tsalkov
2021-06-27 03:17:47 +00:00
parent 50ed9675f3
commit a10b76b2de
2 changed files with 113 additions and 9 deletions

View File

@@ -617,18 +617,22 @@ class MeekroDB {
$queryParts[] = $query;
}
if ($use_named_args && $use_numbered_args) {
throw new MeekroDBException("You can't mix named and numbered args!");
if ($use_named_args) {
if ($use_numbered_args) {
throw new MeekroDBException("You can't mix named and numbered args!");
}
if (count($args) != 1 || !is_array($args[0])) {
throw new MeekroDBException("If you use named args, you must pass an assoc array of args!");
}
}
if ($use_named_args && count($args) != 1) {
throw new MeekroDBException("If you use named args, you must pass an assoc array of args!");
if ($use_numbered_args) {
if ($max_numbered_arg+1 > count($args)) {
throw new MeekroDBException(sprintf('Expected %d args, but only got %d!', $max_numbered_arg+1, count($args)));
}
}
if ($use_numbered_args && $max_numbered_arg+1 > count($args)) {
throw new MeekroDBException(sprintf('Expected %d args, but only got %d!', $max_numbered_arg+1, count($args)));
}
return $queryParts;
}

View File

@@ -180,6 +180,106 @@ class HookTest extends SimpleTest {
DB::removeHooks('pre_parse');
}
function test_9_enough_args() {
$error_worked = false;
try {
DB::query("SELECT * FROM accounts WHERE id=%i AND username=%s", 1);
} catch (MeekroDBException $e) {
if ($e->getMessage() == 'Expected 2 args, but only got 1!') {
$error_worked = true;
}
}
$this->assert($error_worked);
}
function test_10_named_keys_present() {
$error_worked = false;
try {
DB::query("SELECT * FROM accounts WHERE id=%i_id AND username=%s_username", array('username' => 'asdf'));
} catch (MeekroDBException $e) {
if ($e->getMessage() == "Couldn't find named arg id!") {
$error_worked = true;
}
}
$this->assert($error_worked);
}
function test_11_expect_array() {
$error_worked = false;
try {
DB::query("SELECT * FROM accounts WHERE id IN %li", 5);
} catch (MeekroDBException $e) {
if ($e->getMessage() == "Expected an array for arg 0 but didn't get one!") {
$error_worked = true;
}
}
$this->assert($error_worked);
}
function test_12_named_keys_without_array() {
$error_worked = false;
try {
DB::query("SELECT * FROM accounts WHERE id=%i_named", 1);
} catch (MeekroDBException $e) {
if ($e->getMessage() == "If you use named args, you must pass an assoc array of args!") {
$error_worked = true;
}
}
$this->assert($error_worked);
}
function test_13_mix_named_numbered_args() {
$error_worked = false;
try {
DB::query("SELECT * FROM accounts WHERE id=%i_named AND username=%s", array('named' => 1));
} catch (MeekroDBException $e) {
if ($e->getMessage() == "You can't mix named and numbered args!") {
$error_worked = true;
}
}
$this->assert($error_worked);
}
function test_14_arrays_not_empty() {
$error_worked = false;
try {
DB::query("SELECT * FROM accounts WHERE id IN %li", array());
} catch (MeekroDBException $e) {
if ($e->getMessage() == "Arg 0 array can't be empty!") {
$error_worked = true;
}
}
$this->assert($error_worked);
}
function test_15_named_array_not_empty() {
$error_worked = false;
try {
DB::query("SELECT * FROM accounts WHERE id IN %li_ids", array('ids' => array()));
} catch (MeekroDBException $e) {
if ($e->getMessage() == "Arg ids array can't be empty!") {
$error_worked = true;
}
}
$this->assert($error_worked);
}
}