1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-14 18:57:30 +00:00

Fix tests

This commit is contained in:
Ammar Githam 2021-07-11 22:43:59 +09:00
parent 6763ccb6d5
commit 3d5569c46b
4 changed files with 73 additions and 67 deletions

View File

@ -148,6 +148,8 @@ android {
exclude 'META-INF/LICENSE.md' exclude 'META-INF/LICENSE.md'
exclude 'META-INF/LICENSE-notice.md' exclude 'META-INF/LICENSE-notice.md'
exclude 'META-INF/atomicfu.kotlin_module' exclude 'META-INF/atomicfu.kotlin_module'
exclude 'META-INF/AL2.0'
exclude 'META-INF/LGPL2.1'
} }
testOptions.unitTests { testOptions.unitTests {
@ -254,5 +256,7 @@ dependencies {
androidTestImplementation 'com.android.support:support-annotations:28.0.0' androidTestImplementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation "androidx.room:room-testing:2.3.0" 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'
} }

View File

@ -27,7 +27,6 @@ public class MigrationTest {
public MigrationTest() { public MigrationTest() {
final String canonicalName = AppDatabase.class.getCanonicalName(); final String canonicalName = AppDatabase.class.getCanonicalName();
assert canonicalName != null;
helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
canonicalName, canonicalName,
new FrameworkSQLiteOpenHelperFactory()); new FrameworkSQLiteOpenHelperFactory());

View File

@ -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; @RunWith(AndroidJUnit4::class)
import androidx.room.Room; class RecentSearchDaoTest {
import androidx.test.core.app.ApplicationProvider; private lateinit var db: AppDatabase
import androidx.test.runner.AndroidJUnit4; private lateinit var dao: RecentSearchDao
import com.google.common.collect.ImmutableList; @get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
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;
@Before @Before
public void createDb() { fun createDb() {
final Context context = ApplicationProvider.getApplicationContext(); val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build(); db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
dao = db.recentSearchDao(); dao = db.recentSearchDao()
} }
@After @After
public void closeDb() { fun closeDb() {
db.close(); db.close()
} }
@ExperimentalCoroutinesApi
@Test @Test
public void writeQueryDelete() { fun writeQueryDelete() = runBlockingTest {
final RecentSearch recentSearch = insertRecentSearch("1", "test1", FavoriteType.HASHTAG); val recentSearch = insertRecentSearch(1, "1", "test1", FavoriteType.HASHTAG)
final RecentSearch byIgIdAndType = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG); val byIgIdAndType = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG)
Assertions.assertEquals(recentSearch, byIgIdAndType); Assertions.assertNotNull(byIgIdAndType)
dao.deleteRecentSearch(byIgIdAndType); Assertions.assertEquals(recentSearch, byIgIdAndType)
final RecentSearch deleted = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG); dao.deleteRecentSearch(byIgIdAndType ?: throw NullPointerException())
Assertions.assertNull(deleted); val deleted = dao.getRecentSearchByIgIdAndType("1", FavoriteType.HASHTAG)
Assertions.assertNull(deleted)
} }
@ExperimentalCoroutinesApi
@Test @Test
public void queryAllOrdered() { fun queryAllOrdered() = runBlockingTest {
final List<RecentSearch> insertListReversed = ImmutableList val insertListReversed: List<RecentSearch> = listOf(
.<RecentSearch>builder() insertRecentSearch(1, "1", "test1", FavoriteType.HASHTAG),
.add(insertRecentSearch("1", "test1", FavoriteType.HASHTAG)) insertRecentSearch(2, "2", "test2", FavoriteType.LOCATION),
.add(insertRecentSearch("2", "test2", FavoriteType.LOCATION)) insertRecentSearch(3, "3", "test3", FavoriteType.USER),
.add(insertRecentSearch("3", "test3", FavoriteType.USER)) insertRecentSearch(4, "4", "test4", FavoriteType.USER),
.add(insertRecentSearch("4", "test4", FavoriteType.USER)) insertRecentSearch(5, "5", "test5", FavoriteType.USER)
.add(insertRecentSearch("5", "test5", FavoriteType.USER)) ).asReversed() // important
.build() val fromDb: List<RecentSearch?> = dao.getAllRecentSearches()
.reverse(); // important Assertions.assertIterableEquals(insertListReversed, fromDb)
final List<RecentSearch> fromDb = dao.getAllRecentSearches();
Assertions.assertIterableEquals(insertListReversed, fromDb);
} }
@NonNull private fun insertRecentSearch(id: Int, igId: String, name: String, type: FavoriteType): RecentSearch {
private RecentSearch insertRecentSearch(final String igId, final String name, final FavoriteType type) { val recentSearch = RecentSearch(
final RecentSearch recentSearch = new RecentSearch( id,
igId, igId,
name, name,
null, null,
null, null,
type, type,
LocalDateTime.now() LocalDateTime.now()
); )
dao.insertRecentSearch(recentSearch); runBlocking { dao.insertRecentSearch(recentSearch) }
return recentSearch; return recentSearch
} }
} }

View File

@ -132,7 +132,9 @@ abstract class AppDatabase : RoomDatabase() {
db.execSQL("ALTER TABLE " + Favorite.TABLE_NAME + "_backup RENAME TO " + Favorite.TABLE_NAME) 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) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS `dm_last_notified` (" + database.execSQL("CREATE TABLE IF NOT EXISTS `dm_last_notified` (" +
"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + "`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`)") 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) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS `recent_searches` (" + database.execSQL("CREATE TABLE IF NOT EXISTS `recent_searches` (" +
"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +