Permalink
Browse files

MDL-27148 fix unit tests to accept arbitrary named params format from…

… get_in_or_equal
  • Loading branch information...
1 parent 906c957 commit c49bec382cb3594993dab996009d31d6e80ab92d @skodak skodak committed Apr 14, 2011
Showing with 61 additions and 32 deletions.
  1. +61 −32 lib/dml/simpletest/testdml.php
@@ -178,62 +178,82 @@ public function test_get_in_or_equal() {
// Correct usage of multiple values
$in_values = array('value1', 'value2', 'value3', 'value4');
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', true);
- $this->assertEqual("IN (:param01,:param02,:param03,:param04)", $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true);
$this->assertEqual(4, count($params));
reset($in_values);
+ $ps = array();
foreach ($params as $key => $value) {
$this->assertEqual(current($in_values), $value);
next($in_values);
+ $ps[] = ':'.$key;
}
+ $this->assertEqual("IN (".implode(',', $ps).")", $usql);
// Correct usage of single values (in array)
$in_values = array('value1');
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', true);
- $this->assertEqual("= :param01", $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true);
$this->assertEqual(1, count($params));
- $this->assertEqual($in_values[0], $params['param01']);
+ $value = reset($params);
+ $key = key($params);
+ $this->assertEqual("= :$key", $usql);
+ $this->assertEqual($in_value, $value);
// Correct usage of single value
$in_value = 'value1';
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', true);
- $this->assertEqual("= :param01", $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true);
$this->assertEqual(1, count($params));
- $this->assertEqual($in_value, $params['param01']);
+ $value = reset($params);
+ $key = key($params);
+ $this->assertEqual("= :$key", $usql);
+ $this->assertEqual($in_value, $value);
// SQL_PARAMS_NAMED - NOT IN or <>
// Correct usage of multiple values
$in_values = array('value1', 'value2', 'value3', 'value4');
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
- $this->assertEqual("NOT IN (:param01,:param02,:param03,:param04)", $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
$this->assertEqual(4, count($params));
reset($in_values);
+ $ps = array();
foreach ($params as $key => $value) {
$this->assertEqual(current($in_values), $value);
next($in_values);
+ $ps[] = ':'.$key;
}
+ $this->assertEqual("NOT IN (".implode(',', $ps).")", $usql);
// Correct usage of single values (in array)
$in_values = array('value1');
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
- $this->assertEqual("<> :param01", $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
$this->assertEqual(1, count($params));
- $this->assertEqual($in_values[0], $params['param01']);
+ $value = reset($params);
+ $key = key($params);
+ $this->assertEqual("<> :$key", $usql);
+ $this->assertEqual($in_value, $value);
// Correct usage of single value
$in_value = 'value1';
- list($usql, $params) = $DB->get_in_or_equal($in_value, SQL_PARAMS_NAMED, 'param01', false);
- $this->assertEqual("<> :param01", $usql);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
$this->assertEqual(1, count($params));
- $this->assertEqual($in_value, $params['param01']);
+ $value = reset($params);
+ $key = key($params);
+ $this->assertEqual("<> :$key", $usql);
+ $this->assertEqual($in_value, $value);
+
+ // make sure the param names are unique
+ list($usql1, $params1) = $DB->get_in_or_equal(array(1,2,3), SQL_PARAMS_NAMED, 'param');
+ list($usql2, $params2) = $DB->get_in_or_equal(array(1,2,3), SQL_PARAMS_NAMED, 'param');
+ $params1 = array_keys($params1);
+ $params2 = array_keys($params2);
+ $common = array_intersect($params1, $params2);
+ $this->assertEqual(count($common), 0);
// Some incorrect tests
// Incorrect usage passing not-allowed params type
$in_values = array(1, 2, 3);
try {
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_DOLLAR, 'param01', false);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_DOLLAR, 'param', false);
$this->fail('An Exception is missing, expected due to not supported SQL_PARAMS_DOLLAR');
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
@@ -243,7 +263,7 @@ public function test_get_in_or_equal() {
// Incorrect usage passing empty array
$in_values = array();
try {
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false);
$this->fail('An Exception is missing, expected due to empty array of items');
} catch (exception $e) {
$this->assertTrue($e instanceof coding_exception);
@@ -253,51 +273,60 @@ public function test_get_in_or_equal() {
// Correct usage passing empty array and $onemptyitems = NULL (equal = true, QM)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, NULL);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, NULL);
$this->assertEqual(' IS NULL', $usql);
$this->assertIdentical(array(), $params);
// Correct usage passing empty array and $onemptyitems = NULL (equal = false, NAMED)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, NULL);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, NULL);
$this->assertEqual(' IS NOT NULL', $usql);
$this->assertIdentical(array(), $params);
// Correct usage passing empty array and $onemptyitems = true (equal = true, QM)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, true);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, true);
$this->assertEqual('= ?', $usql);
$this->assertIdentical(array(true), $params);
// Correct usage passing empty array and $onemptyitems = true (equal = false, NAMED)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, true);
- $this->assertEqual('<> :param01', $usql);
- $this->assertIdentical(array('param01' => true), $params);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, true);
+ $this->assertEqual(1, count($params));
+ $value = reset($params);
+ $key = key($params);
+ $this->assertEqual('<> :'.$key, $usql);
+ $this->assertIdentical($value, true);
// Correct usage passing empty array and $onemptyitems = -1 (equal = true, QM)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, -1);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, -1);
$this->assertEqual('= ?', $usql);
$this->assertIdentical(array(-1), $params);
// Correct usage passing empty array and $onemptyitems = -1 (equal = false, NAMED)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, -1);
- $this->assertEqual('<> :param01', $usql);
- $this->assertIdentical(array('param01' => -1), $params);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, -1);
+ $this->assertEqual(1, count($params));
+ $value = reset($params);
+ $key = key($params);
+ $this->assertEqual('<> :'.$key, $usql);
+ $this->assertIdentical($value, -1);
// Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = true, QM)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param01', true, 'onevalue');
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, 'onevalue');
$this->assertEqual('= ?', $usql);
$this->assertIdentical(array('onevalue'), $params);
// Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = false, NAMED)
$in_values = array();
- list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param01', false, 'onevalue');
- $this->assertEqual('<> :param01', $usql);
- $this->assertIdentical(array('param01' => 'onevalue'), $params);
+ list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, 'onevalue');
+ $this->assertEqual(1, count($params));
+ $value = reset($params);
+ $key = key($params);
+ $this->assertEqual('<> :'.$key, $usql);
+ $this->assertIdentical($value, 'onevalue');
}
public function test_fix_table_names() {

0 comments on commit c49bec3

Please sign in to comment.