mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
MDL-33434 dml: define expected results when unique constraint is violated on insert.
This commit is contained in:
parent
4631e39533
commit
cbdcdd4734
1 changed files with 28 additions and 0 deletions
|
@ -2078,6 +2078,34 @@ class dml_testcase extends database_driver_testcase {
|
||||||
$this->assertEquals(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
|
$this->assertEquals(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
|
||||||
$id = $DB->insert_record($tablename, array('onetext' => 1e300));
|
$id = $DB->insert_record($tablename, array('onetext' => 1e300));
|
||||||
$this->assertEquals(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
|
$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() {
|
public function test_import_record() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue