MDL-33434 dml: define expected results when unique constraint is violated on insert.

This commit is contained in:
Eloy Lafuente (stronk7) 2012-05-31 00:58:19 +02:00
parent 4631e39533
commit cbdcdd4734

View file

@ -2078,6 +2078,34 @@ class dml_testcase extends database_driver_testcase {
$this->assertEquals(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onetext' => 1e300));
$this->assertEquals(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
// Test that inserting data violating one unique key leads to error.
// Empty the table completely.
$this->assertTrue($DB->delete_records($tablename));
// Add one unique constraint (index).
$key = new xmldb_key('testuk', XMLDB_KEY_UNIQUE, array('course', 'oneint'));
$dbman->add_key($table, $key);
// Let's insert one record violating the constraint multiple times.
$record = (object)array('course' => 1, 'oneint' => 1);
$this->assertTrue($DB->insert_record($tablename, $record, false)); // insert 1st. No problem expected.
// Re-insert same record, not returning id. dml_exception expected.
try {
$DB->insert_record($tablename, $record, false);
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
// Re-insert same record, returning id. dml_exception expected.
try {
$DB->insert_record($tablename, $record, true);
$this->fail("Expecting an exception, none occurred");
} catch (exception $e) {
$this->assertTrue($e instanceof dml_exception);
}
}
public function test_import_record() {