From 3d5569c46bfb3bab5033da5c89be77bd6328a6de Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 11 Jul 2021 22:43:59 +0900 Subject: [PATCH] Fix tests --- app/build.gradle | 4 + .../awais/instagrabber/db/MigrationTest.java | 1 - .../db/dao/RecentSearchDaoTest.kt | 127 +++++++++--------- .../java/awais/instagrabber/db/AppDatabase.kt | 8 +- 4 files changed, 73 insertions(+), 67 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 36916314..d5448f0a 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -148,6 +148,8 @@ android { exclude 'META-INF/LICENSE.md' exclude 'META-INF/LICENSE-notice.md' exclude 'META-INF/atomicfu.kotlin_module' + exclude 'META-INF/AL2.0' + exclude 'META-INF/LGPL2.1' } testOptions.unitTests { @@ -254,5 +256,7 @@ dependencies { androidTestImplementation 'com.android.support:support-annotations:28.0.0' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation "androidx.room:room-testing:2.3.0" + androidTestImplementation "androidx.arch.core:core-testing:2.1.0" + androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0' } diff --git a/app/src/androidTest/java/awais/instagrabber/db/MigrationTest.java b/app/src/androidTest/java/awais/instagrabber/db/MigrationTest.java index 5156c5ff..27784122 100644 --- a/app/src/androidTest/java/awais/instagrabber/db/MigrationTest.java +++ b/app/src/androidTest/java/awais/instagrabber/db/MigrationTest.java @@ -27,7 +27,6 @@ public class MigrationTest { public MigrationTest() { final String canonicalName = AppDatabase.class.getCanonicalName(); - assert canonicalName != null; helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), canonicalName, new FrameworkSQLiteOpenHelperFactory()); diff --git a/app/src/androidTest/java/awais/instagrabber/db/dao/RecentSearchDaoTest.kt b/app/src/androidTest/java/awais/instagrabber/db/dao/RecentSearchDaoTest.kt index c8a48775..0292f187 100644 --- a/app/src/androidTest/java/awais/instagrabber/db/dao/RecentSearchDaoTest.kt +++ b/app/src/androidTest/java/awais/instagrabber/db/dao/RecentSearchDaoTest.kt @@ -1,82 +1,81 @@ -package awais.instagrabber.db.dao; +package awais.instagrabber.db.dao -import android.content.Context; +import android.content.Context +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import androidx.test.runner.AndroidJUnit4 +import awais.instagrabber.db.AppDatabase +import awais.instagrabber.db.entities.RecentSearch +import awais.instagrabber.models.enums.FavoriteType +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runBlockingTest +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.jupiter.api.Assertions +import org.junit.runner.RunWith +import java.time.LocalDateTime -import androidx.annotation.NonNull; -import androidx.room.Room; -import androidx.test.core.app.ApplicationProvider; -import androidx.test.runner.AndroidJUnit4; +@RunWith(AndroidJUnit4::class) +class RecentSearchDaoTest { + private lateinit var db: AppDatabase + private lateinit var dao: RecentSearchDao -import com.google.common.collect.ImmutableList; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.jupiter.api.Assertions; -import org.junit.runner.RunWith; - -import java.time.LocalDateTime; -import java.util.List; - -import awais.instagrabber.db.AppDatabase; -import awais.instagrabber.db.entities.RecentSearch; -import awais.instagrabber.models.enums.FavoriteType; - -@RunWith(AndroidJUnit4.class) -public class RecentSearchDaoTest { - private static final String TAG = RecentSearchDaoTest.class.getSimpleName(); - - private RecentSearchDao dao; - private AppDatabase db; + @get:Rule + var instantExecutorRule = InstantTaskExecutorRule() @Before - public void createDb() { - final Context context = ApplicationProvider.getApplicationContext(); - db = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build(); - dao = db.recentSearchDao(); + fun createDb() { + val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build() + dao = db.recentSearchDao() } @After - public void closeDb() { - db.close(); + fun closeDb() { + db.close() } + @ExperimentalCoroutinesApi @Test - public void writeQueryDelete() { - final RecentSearch recentSearch = insertRecentSearch("1", "test1", FavoriteType.HASHTAG); - final RecentSearch byIgIdAndType = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG); - Assertions.assertEquals(recentSearch, byIgIdAndType); - dao.deleteRecentSearch(byIgIdAndType); - final RecentSearch deleted = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG); - Assertions.assertNull(deleted); + fun writeQueryDelete() = runBlockingTest { + val recentSearch = insertRecentSearch(1, "1", "test1", FavoriteType.HASHTAG) + val byIgIdAndType = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG) + Assertions.assertNotNull(byIgIdAndType) + Assertions.assertEquals(recentSearch, byIgIdAndType) + dao.deleteRecentSearch(byIgIdAndType ?: throw NullPointerException()) + val deleted = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG) + Assertions.assertNull(deleted) } + @ExperimentalCoroutinesApi @Test - public void queryAllOrdered() { - final List insertListReversed = ImmutableList - .builder() - .add(insertRecentSearch("1", "test1", FavoriteType.HASHTAG)) - .add(insertRecentSearch("2", "test2", FavoriteType.LOCATION)) - .add(insertRecentSearch("3", "test3", FavoriteType.USER)) - .add(insertRecentSearch("4", "test4", FavoriteType.USER)) - .add(insertRecentSearch("5", "test5", FavoriteType.USER)) - .build() - .reverse(); // important - final List fromDb = dao.getAllRecentSearches(); - Assertions.assertIterableEquals(insertListReversed, fromDb); + fun queryAllOrdered() = runBlockingTest { + val insertListReversed: List = listOf( + insertRecentSearch(1, "1", "test1", FavoriteType.HASHTAG), + insertRecentSearch(2, "2", "test2", FavoriteType.LOCATION), + insertRecentSearch(3, "3", "test3", FavoriteType.USER), + insertRecentSearch(4, "4", "test4", FavoriteType.USER), + insertRecentSearch(5, "5", "test5", FavoriteType.USER) + ).asReversed() // important + val fromDb: List = dao.getAllRecentSearches() + Assertions.assertIterableEquals(insertListReversed, fromDb) } - @NonNull - private RecentSearch insertRecentSearch(final String igId, final String name, final FavoriteType type) { - final RecentSearch recentSearch = new RecentSearch( - igId, - name, - null, - null, - type, - LocalDateTime.now() - ); - dao.insertRecentSearch(recentSearch); - return recentSearch; + private fun insertRecentSearch(id: Int, igId: String, name: String, type: FavoriteType): RecentSearch { + val recentSearch = RecentSearch( + id, + igId, + name, + null, + null, + type, + LocalDateTime.now() + ) + runBlocking { dao.insertRecentSearch(recentSearch) } + return recentSearch } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/db/AppDatabase.kt b/app/src/main/java/awais/instagrabber/db/AppDatabase.kt index 1a357974..3ba02b16 100644 --- a/app/src/main/java/awais/instagrabber/db/AppDatabase.kt +++ b/app/src/main/java/awais/instagrabber/db/AppDatabase.kt @@ -132,7 +132,9 @@ abstract class AppDatabase : RoomDatabase() { db.execSQL("ALTER TABLE " + Favorite.TABLE_NAME + "_backup RENAME TO " + Favorite.TABLE_NAME) } } - private val MIGRATION_4_5: Migration = object : Migration(4, 5) { + + @JvmField + val MIGRATION_4_5: Migration = object : Migration(4, 5) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("CREATE TABLE IF NOT EXISTS `dm_last_notified` (" + "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + @@ -142,7 +144,9 @@ abstract class AppDatabase : RoomDatabase() { database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_dm_last_notified_thread_id` ON `dm_last_notified` (`thread_id`)") } } - private val MIGRATION_5_6: Migration = object : Migration(5, 6) { + + @JvmField + val MIGRATION_5_6: Migration = object : Migration(5, 6) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("CREATE TABLE IF NOT EXISTS `recent_searches` (" + "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +