Created
March 24, 2026 02:45
-
-
Save psh/aea5047f3398f27eed7b8c5a5fb6b58f to your computer and use it in GitHub Desktop.
A useful base class for unit testing database code
This file contains hidden or 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
| package fr.free.nrw.commons | |
| import android.database.Cursor | |
| import androidx.room.Room | |
| import androidx.test.core.app.ApplicationProvider | |
| import fr.free.nrw.commons.data.DBOpenHelper | |
| import fr.free.nrw.commons.db.AppDatabase | |
| import junit.framework.TestCase.assertTrue | |
| import org.junit.After | |
| import org.junit.Assert.assertEquals | |
| import org.junit.runner.RunWith | |
| import org.robolectric.RobolectricTestRunner | |
| import org.robolectric.annotation.Config | |
| @RunWith(RobolectricTestRunner::class) | |
| @Config(sdk = [21], application = TestCommonsApplication::class) | |
| abstract class InMemoryDatabaseTest { | |
| val roomDatabase: AppDatabase by lazy { | |
| Room.inMemoryDatabaseBuilder( | |
| context = ApplicationProvider.getApplicationContext(), | |
| klass = AppDatabase::class.java | |
| ).build() | |
| } | |
| val openHelper: DBOpenHelper by lazy { | |
| DBOpenHelper(roomDatabase.openHelper) | |
| } | |
| @After | |
| fun cleanup() { | |
| roomDatabase.close() | |
| } | |
| fun clearAllTables() = roomDatabase.clearAllTables() | |
| // Just test that there is a table with the correct name | |
| fun assertTablesExist(vararg tableNames: String) { | |
| tableNames.forEach(::assertTableExists) | |
| } | |
| // Test that the given table exists, and if you give field names, verify they exist in | |
| // that table. | |
| fun assertTableExists(tableName: String, expectedFields: Array<String> = emptyArray()) { | |
| var cursor: Cursor? = null | |
| try { | |
| cursor = openHelper.writableDatabase.query( | |
| "select * from $tableName where 0=1" | |
| ) | |
| if (expectedFields.isNotEmpty()) { | |
| assertEquals( | |
| "Wrong column count for \"$tableName\"", | |
| expectedFields.size, cursor.columnCount | |
| ) | |
| expectedFields.forEach { | |
| // Unknown fields have a negative index | |
| assertTrue( | |
| "Field \"$it\" not found in \"$tableName\"", | |
| cursor.getColumnIndex(it) >= 0 | |
| ) | |
| } | |
| } | |
| } finally { | |
| cursor?.close() | |
| } | |
| } | |
| fun assertRowCount(tableName: String, expectedCount: Int) { | |
| var cursor: Cursor? = null | |
| try { | |
| cursor = openHelper.writableDatabase.query( | |
| "select count(*) from $tableName" | |
| ) | |
| assertTrue(cursor.moveToFirst()) | |
| val actualCount = cursor.getInt(0) | |
| assertEquals("Wrong row count for \"$tableName\"", expectedCount, actualCount) | |
| } finally { | |
| cursor?.close() | |
| } | |
| } | |
| } |
Author
psh
commented
Mar 24, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment