Skip to content

Commit

Permalink
Improve fidelity of SQLite error messages
Browse files Browse the repository at this point in the history
For #8469

PiperOrigin-RevId: 582331582
  • Loading branch information
hoisie committed Nov 14, 2023
1 parent d2d6b77 commit 8fa37a8
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,24 @@ public void fts4() {
assertThat(results.getCount()).isEqualTo(0);
results.close();
}

@Test
public void uniqueConstraintViolation_errorMessage() {
database.execSQL(
"CREATE TABLE my_table(\n"
+ " _id INTEGER PRIMARY KEY AUTOINCREMENT, \n"
+ " unique_column TEXT UNIQUE\n"
+ ");\n");
ContentValues values = new ContentValues();
values.put("unique_column", "test");
database.insertOrThrow("my_table", null, values);
SQLiteConstraintException exception =
assertThrows(
SQLiteConstraintException.class,
() -> database.insertOrThrow("my_table", null, values));
assertThat(exception.getMessage())
.isEqualTo(
"UNIQUE constraint failed: my_table.unique_column (code 2067"
+ " SQLITE_CONSTRAINT_UNIQUE)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.TruthJUnit.assume;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.robolectric.annotation.SQLiteMode.Mode.LEGACY;
import static org.robolectric.shadows.ShadowLegacySQLiteConnection.convertSQLWithLocalizedUnicodeCollator;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatatypeMismatchException;
import android.database.sqlite.SQLiteStatement;
Expand Down Expand Up @@ -188,6 +190,23 @@ public void interruption_doesNotConcurrentlyModifyDatabase() {
ShadowLegacySQLiteConnection.reset();
}

@Test
public void uniqueConstraintViolation_errorMessage() {
database.execSQL(
"CREATE TABLE my_table(\n"
+ " _id INTEGER PRIMARY KEY AUTOINCREMENT, \n"
+ " unique_column TEXT UNIQUE\n"
+ ");\n");
ContentValues values = new ContentValues();
values.put("unique_column", "test");
database.insertOrThrow("my_table", null, values);
SQLiteConstraintException exception =
assertThrows(
SQLiteConstraintException.class,
() -> database.insertOrThrow("my_table", null, values));
assertThat(exception.getMessage()).endsWith("(code 2067 SQLITE_CONSTRAINT_UNIQUE)");
}

@Test
public void test_setUseInMemoryDatabase() {
assume().that(SQLiteLibraryLoader.isOsSupported()).isTrue();
Expand Down
Loading

0 comments on commit 8fa37a8

Please sign in to comment.