Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix SQLiteDatabaseTest foreign key test to match Android behavior
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
1 parent
f4d64f8
commit 19467ec
Showing
42 changed files
with
1,018 additions
and
1,476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
integration_tests/ctesque/src/test/java/android/database/SQLiteDatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
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; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.annotation.internal.DoNotInstrument; | ||
|
||
/** Compatibility test for {@link android.database.sqlite.SQLiteDatabase} */ | ||
@DoNotInstrument | ||
@RunWith(AndroidJUnit4.class) | ||
public class SQLiteDatabaseTest { | ||
|
||
private SQLiteDatabase database; | ||
private File databasePath; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
databasePath = ApplicationProvider.getApplicationContext().getDatabasePath("database.db"); | ||
databasePath.getParentFile().mkdirs(); | ||
|
||
database = SQLiteDatabase.openOrCreateDatabase(databasePath, null); | ||
database.execSQL( | ||
"CREATE TABLE table_name (\n" | ||
+ " id INTEGER PRIMARY KEY AUTOINCREMENT,\n" | ||
+ " first_column VARCHAR(255),\n" | ||
+ " second_column BINARY,\n" | ||
+ " name VARCHAR(255),\n" | ||
+ " big_int INTEGER\n" | ||
+ ");"); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
database.close(); | ||
assertThat(databasePath.delete()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void shouldGetBlobFromString() { | ||
String s = "this is a string"; | ||
ContentValues values = new ContentValues(); | ||
values.put("first_column", s); | ||
|
||
database.insert("table_name", null, values); | ||
|
||
Cursor data = | ||
database.query("table_name", new String[] {"first_column"}, null, null, null, null, null); | ||
assertThat(data.getCount()).isEqualTo(1); | ||
data.moveToFirst(); | ||
byte[] columnBytes = data.getBlob(0); | ||
byte[] expected = Arrays.copyOf(s.getBytes(), s.length() + 1); // include zero-terminal | ||
assertThat(columnBytes).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
public void shouldGetBlobFromNullString() { | ||
ContentValues values = new ContentValues(); | ||
values.put("first_column", (String) null); | ||
database.insert("table_name", null, values); | ||
|
||
Cursor data = | ||
database.query("table_name", new String[] {"first_column"}, null, null, null, null, null); | ||
assertThat(data.getCount()).isEqualTo(1); | ||
data.moveToFirst(); | ||
assertThat(data.getBlob(0)).isEqualTo(null); | ||
} | ||
|
||
@Test | ||
public void shouldGetBlobFromEmptyString() { | ||
ContentValues values = new ContentValues(); | ||
values.put("first_column", ""); | ||
database.insert("table_name", null, values); | ||
|
||
Cursor data = | ||
database.query("table_name", new String[] {"first_column"}, null, null, null, null, null); | ||
assertThat(data.getCount()).isEqualTo(1); | ||
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.