Skip to content

Commit

Permalink
Fix SQLiteDatabaseTest foreign key test to match Android behavior
Browse files Browse the repository at this point in the history
SQLiteDatabaseTest.shouldThrowWhenForeignKeysConstraintIsViolated failed if run
on a real Android. It expected that a foreign key exception be thrown. In real
Android, however, no exception was thrown. Only a message is printed in logcat.

Update the test to delete a row that has a reference to another table. This
causes an exception in both real Android and Robolectric. Move this to a
ctesque test to ensure fidelity with real Android.

PiperOrigin-RevId: 386770883
  • Loading branch information
hoisie committed Aug 8, 2021
1 parent 9545127 commit 8b2a625
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
@@ -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;
Expand Down Expand Up @@ -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");
}
}
Expand Up @@ -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);
Expand Down

0 comments on commit 8b2a625

Please sign in to comment.