Avoid reserved words in dmllib unit tests.

(Except that TEXT is not a reserved word after all, although some things like TINYTEXT are. Wheird!)
This commit is contained in:
tjhunt 2006-08-24 16:56:59 +00:00
parent 81abf10971
commit 3830553d47

View file

@ -19,7 +19,7 @@ require_once($CFG->libdir . '/dmllib.php');
class datalib_test extends prefix_changing_test_case { class datalib_test extends prefix_changing_test_case {
var $table = 'table'; var $table = 'table';
var $data = array( var $data = array(
array('id', 'text', 'number'), array('id', 'textfield', 'numberfield'),
array( 1, 'frog', 101), array( 1, 'frog', 101),
array( 2, 'toad', 102), array( 2, 'toad', 102),
array( 3, 'tadpole', 103), array( 3, 'tadpole', 103),
@ -53,26 +53,26 @@ class datalib_test extends prefix_changing_test_case {
} }
function test_record_exists() { function test_record_exists() {
$this->assertTrue(record_exists($this->table, 'number', 101, 'id', 1)); $this->assertTrue(record_exists($this->table, 'numberfield', 101, 'id', 1));
$this->assertFalse(record_exists($this->table, 'number', 102, 'id', 1)); $this->assertFalse(record_exists($this->table, 'numberfield', 102, 'id', 1));
} }
function test_record_exists_select() { function test_record_exists_select() {
$this->assertTrue(record_exists_select($this->table, 'number = 101 AND id = 1')); $this->assertTrue(record_exists_select($this->table, 'numberfield = 101 AND id = 1'));
$this->assertFalse(record_exists_select($this->table, 'number = 102 AND id = 1')); $this->assertFalse(record_exists_select($this->table, 'numberfield = 102 AND id = 1'));
} }
function test_record_exists_sql() { function test_record_exists_sql() {
global $CFG; global $CFG;
$this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE number = 101 AND id = 1")); $this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 101 AND id = 1"));
$this->assertFalse(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE number = 102 AND id = 1")); $this->assertFalse(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 102 AND id = 1"));
} }
function test_get_record() { function test_get_record() {
// Get particular records. // Get particular records.
$this->assert(new CheckSpecifiedFieldsExpectation($this->objects[1]), get_record($this->table, 'id', 1), 'id = 1'); $this->assert(new CheckSpecifiedFieldsExpectation($this->objects[1]), get_record($this->table, 'id', 1), 'id = 1');
$this->assert(new CheckSpecifiedFieldsExpectation($this->objects[3]), get_record($this->table, 'text', 'tadpole', 'number', 103), 'text = tadpole AND number = 103'); $this->assert(new CheckSpecifiedFieldsExpectation($this->objects[3]), get_record($this->table, 'textfield', 'tadpole', 'numberfield', 103), 'textfield = tadpole AND numberfield = 103');
// Abiguous get attempt, should return one, and print a warning in debug mode. // Abiguous get attempt, should return one, and print a warning in debug mode.
global $CFG; global $CFG;
@ -80,14 +80,14 @@ class datalib_test extends prefix_changing_test_case {
$CFG->debug = 0; $CFG->debug = 0;
ob_start(); ob_start();
$record = get_record($this->table, 'text', 'tadpole'); $record = get_record($this->table, 'textfield', 'tadpole');
$result = ob_get_contents(); $result = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assertEqual('', $result, '%s (No error ouside debug mode).'); $this->assertEqual('', $result, '%s (No error ouside debug mode).');
$CFG->debug = E_ALL; $CFG->debug = E_ALL;
ob_start(); ob_start();
$record = get_record($this->table, 'text', 'tadpole'); $record = get_record($this->table, 'textfield', 'tadpole');
$result = ob_get_contents(); $result = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.'); $this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.');
@ -97,19 +97,19 @@ class datalib_test extends prefix_changing_test_case {
// Return only specified fields // Return only specified fields
$expected = new stdClass; $expected = new stdClass;
$expected->id = 3; $expected->id = 3;
$expected->text = 'tadpole'; $expected->textfield = 'tadpole';
$result = get_record($this->table, 'id', '3', '', '', '', '', 'id,text'); $result = get_record($this->table, 'id', '3', '', '', '', '', 'id,textfield');
$this->assert(new CheckSpecifiedFieldsExpectation($expected), $result); $this->assert(new CheckSpecifiedFieldsExpectation($expected), $result);
$this->assertFalse(isset($result->number)); $this->assertFalse(isset($result->numberfield));
$expected = new stdClass; $expected = new stdClass;
$expected->text = 'tadpole'; $expected->textfield = 'tadpole';
$expected->number = 103; $expected->numberfield = 103;
$result = get_record($this->table, 'id', '3', '', '', '', '', 'text,number'); $result = get_record($this->table, 'id', '3', '', '', '', '', 'textfield,numberfield');
$this->assert(new CheckSpecifiedFieldsExpectation($expected), $result); $this->assert(new CheckSpecifiedFieldsExpectation($expected), $result);
$this->assertFalse(isset($result->id)); $this->assertFalse(isset($result->id));
// Attempting to get a non-existant records should return false. // Attempting to get a non-existant records should return false.
$this->assertFalse(get_record($this->table, 'text', 'not there'), 'attempt to get non-existant record'); $this->assertFalse(get_record($this->table, 'textfield', 'not there'), 'attempt to get non-existant record');
} }
function test_get_record_sql() { function test_get_record_sql() {
@ -122,20 +122,20 @@ class datalib_test extends prefix_changing_test_case {
$CFG->debug = 0; $CFG->debug = 0;
ob_start(); ob_start();
$record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE text = 'tadpole'"); $record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE textfield = 'tadpole'");
$result = ob_get_contents(); $result = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assertEqual('', $result, '%s (No error ouside debug mode).'); $this->assertEqual('', $result, '%s (No error ouside debug mode).');
$CFG->debug = E_ALL; $CFG->debug = E_ALL;
ob_start(); ob_start();
$record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE text = 'tadpole'"); $record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE textfield = 'tadpole'");
$result = ob_get_contents(); $result = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.'); $this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.');
ob_start(); ob_start();
$record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE text = 'tadpole'", true); $record = get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE textfield = 'tadpole'", true);
$result = ob_get_contents(); $result = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assertEqual('', $result, '%s (No error ouside debug mode).'); $this->assertEqual('', $result, '%s (No error ouside debug mode).');
@ -143,7 +143,7 @@ class datalib_test extends prefix_changing_test_case {
$CFG->debug = $old_debug; $CFG->debug = $old_debug;
// Attempting to get a non-existant records should return false. // Attempting to get a non-existant records should return false.
$this->assertFalse(get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE text = 'not there'"), 'attempt to get non-existant record'); $this->assertFalse(get_record_sql("SELECT * FROM {$CFG->prefix}" . $this->table . " WHERE textfield = 'not there'"), 'attempt to get non-existant record');
} }
function test_get_record_select() { function test_get_record_select() {
@ -156,14 +156,14 @@ class datalib_test extends prefix_changing_test_case {
$CFG->debug = 0; $CFG->debug = 0;
ob_start(); ob_start();
$record = get_record_select($this->table, "text = 'tadpole'"); $record = get_record_select($this->table, "textfield = 'tadpole'");
$result = ob_get_contents(); $result = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assertEqual('', $result, '%s (No error ouside debug mode).'); $this->assertEqual('', $result, '%s (No error ouside debug mode).');
$CFG->debug = E_ALL; $CFG->debug = E_ALL;
ob_start(); ob_start();
$record = get_record_select($this->table, "text = 'tadpole'"); $record = get_record_select($this->table, "textfield = 'tadpole'");
$result = ob_get_contents(); $result = ob_get_contents();
ob_end_clean(); ob_end_clean();
$this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.'); $this->assert(new TextExpectation('Error:'), $result, 'Error in debug mode.');
@ -173,40 +173,40 @@ class datalib_test extends prefix_changing_test_case {
// Return only specified fields // Return only specified fields
$expected = new stdClass; $expected = new stdClass;
$expected->id = 1; $expected->id = 1;
$expected->text = 'frog'; $expected->textfield = 'frog';
$result = get_record_select($this->table, "text = 'frog'", 'id,text'); $result = get_record_select($this->table, "textfield = 'frog'", 'id,textfield');
$this->assert(new CheckSpecifiedFieldsExpectation($expected), $result); $this->assert(new CheckSpecifiedFieldsExpectation($expected), $result);
$this->assertFalse(isset($result->number)); $this->assertFalse(isset($result->numberfield));
// Attempting to get a non-existant records should return false. // Attempting to get a non-existant records should return false.
$this->assertFalse(get_record_select($this->table, 'id > 666'), 'attempt to get non-existant record'); $this->assertFalse(get_record_select($this->table, 'id > 666'), 'attempt to get non-existant record');
} }
function test_get_field() { function test_get_field() {
$this->assertEqual(get_field($this->table, 'number', 'id', 1), 101); $this->assertEqual(get_field($this->table, 'numberfield', 'id', 1), 101);
$this->assertEqual(get_field($this->table, 'text', 'number', 102), 'toad'); $this->assertEqual(get_field($this->table, 'textfield', 'numberfield', 102), 'toad');
$this->assertEqual(get_field($this->table, 'number', 'text', 'tadpole', 'id', 4), 104); $this->assertEqual(get_field($this->table, 'numberfield', 'textfield', 'tadpole', 'id', 4), 104);
$this->assertEqual(get_field($this->table, 'number + id', 'text', 'tadpole', 'id', 4), 108); $this->assertEqual(get_field($this->table, 'numberfield + id', 'textfield', 'tadpole', 'id', 4), 108);
} }
function test_get_field_select() { function test_get_field_select() {
$this->assertEqual(get_field_select($this->table, 'number', 'id = 1'), 101); $this->assertEqual(get_field_select($this->table, 'numberfield', 'id = 1'), 101);
} }
function test_get_field_sql() { function test_get_field_sql() {
global $CFG; global $CFG;
$this->assertEqual(get_field_sql("SELECT number FROM {$CFG->prefix}$this->table WHERE id = 1"), 101); $this->assertEqual(get_field_sql("SELECT numberfield FROM {$CFG->prefix}$this->table WHERE id = 1"), 101);
} }
function test_set_field() { function test_set_field() {
set_field($this->table, 'number', 12345, 'id', 1); set_field($this->table, 'numberfield', 12345, 'id', 1);
$this->assertEqual(get_field($this->table, 'number', 'id', 1), 12345); $this->assertEqual(get_field($this->table, 'numberfield', 'id', 1), 12345);
set_field($this->table, 'text', 'newvalue', 'number', 102); set_field($this->table, 'textfield', 'newvalue', 'numberfield', 102);
$this->assertEqual(get_field($this->table, 'text', 'number', 102), 'newvalue'); $this->assertEqual(get_field($this->table, 'textfield', 'numberfield', 102), 'newvalue');
set_field($this->table, 'number', -1, 'text', 'tadpole', 'id', 4); set_field($this->table, 'numberfield', -1, 'textfield', 'tadpole', 'id', 4);
$this->assertEqual(get_field($this->table, 'number', 'text', 'tadpole', 'id', 4), -1); $this->assertEqual(get_field($this->table, 'numberfield', 'textfield', 'tadpole', 'id', 4), -1);
} }
function test_delete_records() { function test_delete_records() {
@ -214,19 +214,19 @@ class datalib_test extends prefix_changing_test_case {
$this->assertEqual(count_records($this->table), 4); $this->assertEqual(count_records($this->table), 4);
delete_records($this->table, 'id', 1); delete_records($this->table, 'id', 1);
$this->assertEqual(count_records($this->table), 3); $this->assertEqual(count_records($this->table), 3);
delete_records($this->table, 'text', 'tadpole'); delete_records($this->table, 'textfield', 'tadpole');
$this->assertEqual(count_records($this->table), 1); $this->assertEqual(count_records($this->table), 1);
} }
function test_delete_records2() { function test_delete_records2() {
delete_records($this->table, 'text', 'tadpole', 'id', 4); delete_records($this->table, 'textfield', 'tadpole', 'id', 4);
$this->assertEqual(count_records($this->table), 3); $this->assertEqual(count_records($this->table), 3);
delete_records($this->table); delete_records($this->table);
$this->assertEqual(count_records($this->table), 0); $this->assertEqual(count_records($this->table), 0);
} }
function test_delete_records_select() { function test_delete_records_select() {
delete_records_select($this->table, "text LIKE 't%'"); delete_records_select($this->table, "textfield LIKE 't%'");
$this->assertEqual(count_records($this->table), 1); $this->assertEqual(count_records($this->table), 1);
delete_records_select($this->table, "'1' = '1'"); delete_records_select($this->table, "'1' = '1'");
$this->assertEqual(count_records($this->table), 0); $this->assertEqual(count_records($this->table), 0);
@ -236,33 +236,33 @@ class datalib_test extends prefix_changing_test_case {
function test_insert_record() { function test_insert_record() {
// Simple insert with $returnid // Simple insert with $returnid
$obj = new stdClass; $obj = new stdClass;
$obj->text = 'new entry'; $obj->textfield = 'new entry';
$obj->number = 123; $obj->numberfield = 123;
$this->assertEqual(insert_record($this->table, $obj), 5); $this->assertEqual(insert_record($this->table, $obj), 5);
$obj->id = 5; $obj->id = 5;
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), get_record($this->table, 'id', 5)); $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), get_record($this->table, 'id', 5));
// Simple insert without $returnid // Simple insert without $returnid
$obj = new stdClass; $obj = new stdClass;
$obj->text = 'newer entry'; $obj->textfield = 'newer entry';
$obj->number = 321; $obj->numberfield = 321;
$this->assertEqual(insert_record($this->table, $obj, false), true); $this->assertEqual(insert_record($this->table, $obj, false), true);
$obj->id = 6; $obj->id = 6;
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert without returnid (%s)'), get_record($this->table, 'id', 6)); $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert without returnid (%s)'), get_record($this->table, 'id', 6));
// Insert with missing columns - should get defaults. // Insert with missing columns - should get defaults.
$obj = new stdClass; $obj = new stdClass;
$obj->text = 'partial entry'; $obj->textfield = 'partial entry';
$this->assertEqual(insert_record($this->table, $obj), 7); $this->assertEqual(insert_record($this->table, $obj), 7);
$obj->id = 7; $obj->id = 7;
$obj->number = 0xDefa; $obj->numberfield = 0xDefa;
$got = get_record($this->table, 'id', 7); $got = get_record($this->table, 'id', 7);
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with missing columns - should get defaults (%s)'), get_record($this->table, 'id', 7)); $this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with missing columns - should get defaults (%s)'), get_record($this->table, 'id', 7));
// Insert with extra columns - should be ingnored. // Insert with extra columns - should be ingnored.
$obj = new stdClass; $obj = new stdClass;
$obj->text = 'entry with extra'; $obj->textfield = 'entry with extra';
$obj->number = 747; $obj->numberfield = 747;
$obj->unused = 666; $obj->unused = 666;
$this->assertEqual(insert_record($this->table, $obj), 8); $this->assertEqual(insert_record($this->table, $obj), 8);
$obj->id = 8; $obj->id = 8;
@ -271,14 +271,14 @@ class datalib_test extends prefix_changing_test_case {
// Insert into nonexistant table - should fail. // Insert into nonexistant table - should fail.
$obj = new stdClass; $obj = new stdClass;
$obj->text = 'new entry'; $obj->textfield = 'new entry';
$obj->number = 123; $obj->numberfield = 123;
$this->assertFalse(insert_record('nonexistant_table', $obj), 'Insert into nonexistant table'); $this->assertFalse(insert_record('nonexistant_table', $obj), 'Insert into nonexistant table');
// Insert bad data - error should be printed. // Insert bad data - error should be printed.
$obj = new stdClass; $obj = new stdClass;
$obj->text = 'new entry'; $obj->textfield = 'new entry';
$obj->number = 'not a number'; $obj->numberfield = 'not a number';
ob_start(); ob_start();
$this->assertFalse(insert_record($this->table, $obj), 'Insert bad data - should fail.'); $this->assertFalse(insert_record($this->table, $obj), 'Insert bad data - should fail.');
$result = ob_get_contents(); $result = ob_get_contents();