50
50
$this->assertEqual(where_clause('f1', 'v1'), "WHERE f1 = 'v1'");
51
51
$this->assertEqual(where_clause('f1', 'v1', 'f2', 2), "WHERE f1 = 'v1' AND f2 = '2'");
52
52
$this->assertEqual(where_clause('f1', 'v1', 'f2', 1.75, 'f3', 'v3'), "WHERE f1 = 'v1' AND f2 = '1.75' AND f3 = 'v3'");
53
$this->assertEqual(where_clause('f1', NULL), "WHERE f1 IS NULL");
55
56
function test_record_exists() {
56
57
$this->assertTrue(record_exists($this->table, 'numberfield', 101, 'id', 1));
57
58
$this->assertFalse(record_exists($this->table, 'numberfield', 102, 'id', 1));
59
$this->assertTrue(record_exists($this->table, 'numberfield', NULL));
60
62
function test_record_exists_select() {
61
63
$this->assertTrue(record_exists_select($this->table, 'numberfield = 101 AND id = 1'));
62
64
$this->assertFalse(record_exists_select($this->table, 'numberfield = 102 AND id = 1'));
65
$this->assertTrue(record_exists_select($this->table, 'numberfield IS NULL'));
65
68
function test_record_exists_sql() {
67
70
$this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 101 AND id = 1"));
68
71
$this->assertFalse(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield = 102 AND id = 1"));
72
$this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE numberfield IS NULL"));
72
76
function test_get_record() {
73
77
// Get particular records.
74
$this->assert(new CheckSpecifiedFieldsExpectation($this->objects[1]), get_record($this->table, 'id', 1), 'id = 1');
75
$this->assert(new CheckSpecifiedFieldsExpectation($this->objects[3]), get_record($this->table, 'textfield', 'tadpole', 'numberfield', 103), 'textfield = tadpole AND numberfield = 103');
78
$this->assert(new CheckSpecifiedFieldsExpectation($this->objects[1]), get_record($this->table, 'id', 1));
79
$this->assert(new CheckSpecifiedFieldsExpectation($this->objects[3]), get_record($this->table, 'textfield', 'tadpole', 'numberfield', 103));
80
$this->assert(new CheckSpecifiedFieldsExpectation($this->objects[5]), get_record($this->table, 'numberfield', null));
77
82
// Abiguous get attempt, should return one, and print a warning in debug mode.
208
214
set_field($this->table, 'numberfield', -1, 'textfield', 'tadpole', 'id', 4);
209
215
$this->assertEqual(get_field($this->table, 'numberfield', 'textfield', 'tadpole', 'id', 4), -1);
217
set_field($this->table, 'textfield', null, 'id', 5);
218
$this->assertNull(get_field($this->table, 'textfield', 'id', 5));
212
221
function test_delete_records() {
213
222
delete_records($this->table, 'id', 666);
214
$this->assertEqual(count_records($this->table), 4);
223
$this->assertEqual(count_records($this->table), 5);
215
224
delete_records($this->table, 'id', 1);
216
$this->assertEqual(count_records($this->table), 3);
225
$this->assertEqual(count_records($this->table), 4);
217
226
delete_records($this->table, 'textfield', 'tadpole');
227
$this->assertEqual(count_records($this->table), 2);
228
delete_records($this->table, 'numberfield', NULL);
218
229
$this->assertEqual(count_records($this->table), 1);
221
232
function test_delete_records2() {
222
233
delete_records($this->table, 'textfield', 'tadpole', 'id', 4);
223
$this->assertEqual(count_records($this->table), 3);
234
$this->assertEqual(count_records($this->table), 4);
224
235
delete_records($this->table);
225
236
$this->assertEqual(count_records($this->table), 0);
228
239
function test_delete_records_select() {
229
240
delete_records_select($this->table, "textfield LIKE 't%'");
230
$this->assertEqual(count_records($this->table), 1);
241
$this->assertEqual(count_records($this->table), 2);
231
242
delete_records_select($this->table, "'1' = '1'");
232
243
$this->assertEqual(count_records($this->table), 0);
246
function test_update_record() {
252
$obj->textfield = 'changed entry';
253
$obj->numberfield = 123;
254
$this->assertTrue(update_record($this->table, $obj));
255
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple update (%s)'), get_record($this->table, 'id', $obj->id));
257
// Simple incomplete update
260
$obj->numberfield = 123;
261
$this->assertTrue(update_record($this->table, $obj));
262
$obj->textfield = 'toad';
263
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple update (%s)'), get_record($this->table, 'id', $obj->id));
265
// Simple incomplete update
268
$obj->numberfield = 123;
269
$obj->textfield = null;
270
$this->assertTrue(update_record($this->table, $obj));
271
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple update (%s)'), get_record($this->table, 'id', $obj->id));
235
275
//function insert_record($table, $dataobject, $returnid=true, $primarykey='id', $feedback=true) {
236
276
function test_insert_record() {
237
279
// Simple insert with $returnid
238
280
$obj = new stdClass;
239
281
$obj->textfield = 'new entry';
240
282
$obj->numberfield = 123;
241
$this->assertEqual(insert_record($this->table, $obj), 5);
243
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), get_record($this->table, 'id', 5));
283
$this->assertEqual(insert_record($this->table, $obj), 6);
285
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), get_record($this->table, 'id', $obj->id));
245
287
// Simple insert without $returnid
246
288
$obj = new stdClass;
247
289
$obj->textfield = 'newer entry';
248
290
$obj->numberfield = 321;
249
291
$this->assertEqual(insert_record($this->table, $obj, false), true);
251
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert without returnid (%s)'), get_record($this->table, 'id', 6));
293
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert without returnid (%s)'), get_record($this->table, 'id', $obj->id));
253
295
// Insert with missing columns - should get defaults.
254
296
$obj = new stdClass;
255
297
$obj->textfield = 'partial entry';
256
$this->assertEqual(insert_record($this->table, $obj), 7);
298
$this->assertEqual(insert_record($this->table, $obj), 8);
258
300
$obj->numberfield = 0xDefa;
259
$got = get_record($this->table, 'id', 7);
260
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with missing columns - should get defaults (%s)'), get_record($this->table, 'id', 7));
301
$got = get_record($this->table, 'id', 8);
302
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with missing columns - should get defaults (%s)'), get_record($this->table, 'id', $obj->id));
262
304
// Insert with extra columns - should be ingnored.
263
305
$obj = new stdClass;
264
306
$obj->textfield = 'entry with extra';
265
307
$obj->numberfield = 747;
266
308
$obj->unused = 666;
267
$this->assertEqual(insert_record($this->table, $obj), 8);
309
$this->assertEqual(insert_record($this->table, $obj), 9);
269
311
unset($obj->unused);
270
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with extra columns - should be ingnored (%s)'), get_record($this->table, 'id', 8));
312
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Insert with extra columns - should be ingnored (%s)'), get_record($this->table, 'id', $obj->id));
314
// Simple insert with $returnid and NULL values
316
$obj->textfield = null;
317
$obj->numberfield = null;
318
$this->assertEqual(insert_record($this->table, $obj), 10);
320
$new = get_record($this->table, 'id', $obj->id);
321
$this->assert(new CheckSpecifiedFieldsExpectation($obj, 'Simple insert with returnid (%s)'), $new);
322
$this->assertNull($new->textfield);
323
$this->assertNull($new->numberfield);
272
325
// Insert into nonexistant table - should fail.
273
326
$obj = new stdClass;
274
327
$obj->textfield = 'new entry';
275
328
$obj->numberfield = 123;
276
329
$this->assertFalse(insert_record('nonexistant_table', $obj), 'Insert into nonexistant table');
278
// Insert bad data - error should be printed.
280
$obj->textfield = 'new entry';
281
$obj->numberfield = 'not a number';
283
$this->assertFalse(insert_record($this->table, $obj), 'Insert bad data - should fail.');
284
$result = ob_get_contents();
286
$this->assert(new TextExpectation('ERROR:'), $result, 'Insert bad data - error should have been printed. This is known not to work on MySQL.');