Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SQLiteDatabaseTest foreign key test to match Android behavior #6643

Merged
merged 1 commit into from Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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