Skip to content

Commit

Permalink
MDL-20734 Changed tests behaviour so insert/update/set_field will be …
Browse files Browse the repository at this point in the history
…expected to cast

empty strings (in numeric columns) to 0
  • Loading branch information
stronk7 committed Nov 3, 2009
1 parent fdc45ac commit 2f9c169
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions lib/dml/simpletest/testdml.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,23 +1239,22 @@ public function test_insert_record() {
$this->assertTrue($e instanceof dml_exception);
}

// Check empty string data causes exception in numeric types
// Check empty string data is stored as 0 in numeric datatypes
$record->oneint = ''; // empty string
$record->onenum = 0;
try {
$DB->insert_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
$recid = $DB->insert_record($tablename, $record);
$rs = $DB->get_recordset($tablename, array('id' => $recid));
$record = $rs->current();
$rs->close();
$this->assertTrue(is_numeric($record->oneint) && $record->oneint == 0);

$record->oneint = 0;
$record->onenum = ''; // empty string
try {
$DB->insert_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
$recid = $DB->insert_record($tablename, $record);
$rs = $DB->get_recordset($tablename, array('id' => $recid));
$record = $rs->current();
$rs->close();
$this->assertTrue(is_numeric($record->onenum) && $record->onenum == 0);

// Check empty strings are set properly in string types
$record->oneint = 0;
Expand Down Expand Up @@ -1468,23 +1467,18 @@ public function test_update_record() {
$this->assertTrue($e instanceof dml_exception);
}

// Check empty string data causes exception in numeric types
// Check empty string data is stored as 0 in numeric datatypes
$record->oneint = ''; // empty string
$record->onenum = 0;
try {
$DB->update_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
$DB->update_record($tablename, $record);
$record = $DB->get_record($tablename, array('course' => 2));
$this->assertTrue(is_numeric($record->oneint) && $record->oneint == 0);

$record->oneint = 0;
$record->onenum = ''; // empty string
try {
$DB->update_record($tablename, $record);
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
$DB->update_record($tablename, $record);
$record = $DB->get_record($tablename, array('course' => 2));
$this->assertTrue(is_numeric($record->onenum) && $record->onenum == 0);

// Check empty strings are set properly in string types
$record->oneint = 0;
Expand Down Expand Up @@ -1642,19 +1636,14 @@ public function test_set_field_select() {
$this->assertTrue($e instanceof dml_exception);
}

// Check empty string data causes exception in numeric types
try {
$DB->set_field_select($tablename, 'oneint', '', 'id = ?', array(1));
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
try {
$DB->set_field_select($tablename, 'onenum', '', 'id = ?', array(1));
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
// Check empty string data is stored as 0 in numeric datatypes
$DB->set_field_select($tablename, 'oneint', '', 'id = ?', array(1));
$field = $DB->get_field($tablename, 'oneint', array('id' => 1));
$this->assertTrue(is_numeric($field) && $field == 0);

$DB->set_field_select($tablename, 'onenum', '', 'id = ?', array(1));
$field = $DB->get_field($tablename, 'onenum', array('id' => 1));
$this->assertTrue(is_numeric($field) && $field == 0);

// Check empty strings are set properly in string types
$DB->set_field_select($tablename, 'onechar', '', 'id = ?', array(1));
Expand Down

0 comments on commit 2f9c169

Please sign in to comment.