diff --git a/integration_tests/ctesque/src/test/java/android/database/SQLiteDatabaseTest.java b/integration_tests/ctesque/src/test/java/android/database/SQLiteDatabaseTest.java index 0b87f8b79f8..b2cb71475e8 100644 --- a/integration_tests/ctesque/src/test/java/android/database/SQLiteDatabaseTest.java +++ b/integration_tests/ctesque/src/test/java/android/database/SQLiteDatabaseTest.java @@ -1,11 +1,15 @@ package android.database; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import android.content.ContentValues; +import android.database.sqlite.SQLiteConstraintException; import android.database.sqlite.SQLiteDatabase; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.common.base.Ascii; +import com.google.common.base.Throwables; import java.io.File; import java.util.Arrays; import org.junit.After; @@ -86,4 +90,29 @@ public void shouldGetBlobFromEmptyString() { data.moveToFirst(); assertThat(data.getBlob(0)).isEqualTo(new byte[] {0}); } + + @Test + public void shouldThrowWhenForeignKeysConstraintIsViolated() { + database.execSQL( + "CREATE TABLE artist(\n" + + " artistid INTEGER PRIMARY KEY, \n" + + " artistname TEXT\n" + + ");\n"); + + database.execSQL( + "CREATE TABLE track(\n" + + " trackid INTEGER, \n" + + " trackname TEXT, \n" + + " trackartist INTEGER,\n" + + " FOREIGN KEY(trackartist) REFERENCES artist(artistid)\n" + + ");"); + + database.execSQL("PRAGMA foreign_keys=ON"); + database.execSQL("INSERT into artist (artistid, artistname) VALUES (1, 'Kanye')"); + database.execSQL( + "INSERT into track (trackid, trackname, trackartist) VALUES (1, 'Good Life', 1)"); + SQLiteConstraintException ex = + assertThrows(SQLiteConstraintException.class, () -> database.execSQL("delete from artist")); + assertThat(Ascii.toLowerCase(Throwables.getStackTraceAsString(ex))).contains("foreign key"); + } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java b/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java index 64b1e6fe05c..d17e43c0068 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/SQLiteDatabaseTest.java @@ -839,20 +839,6 @@ public void testRawQueryWithCommonTableExpression() { } } - @Test - public void shouldThrowWhenForeignKeysConstraintIsViolated() { - database.execSQL("CREATE TABLE master (master_value INTEGER)"); - database.execSQL( - "CREATE TABLE slave (master_value INTEGER REFERENCES" + " master(master_value))"); - database.execSQL("PRAGMA foreign_keys=ON"); - try { - database.execSQL("INSERT INTO slave(master_value) VALUES (1)"); - fail("Foreign key constraint is violated but exception is not thrown"); - } catch (SQLiteException e) { - assertThat(e.getCause()).hasMessageThat().contains("foreign"); - } - } - @Test public void shouldBeAbleToBeUsedFromDifferentThread() { final CountDownLatch sync = new CountDownLatch(1);