Permalink
Browse files

MDL-33434 dml: define expected results when unique constraint is viol…

…ated on insert.
  • Loading branch information...
1 parent 5bb8e80 commit 514da614c3b7ebe30281f2b43f11219191ec38e0 @stronk7 stronk7 committed May 30, 2012
Showing with 28 additions and 0 deletions.
  1. +28 −0 lib/dml/simpletest/testdml.php
@@ -2094,6 +2094,34 @@ public function test_insert_record() {
$this->assertEqual(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onetext' => 1e300));
$this->assertEqual(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() {

0 comments on commit 514da61

Please sign in to comment.