Browse Source

Fix tests

renovate/androidx.fragment-fragment-ktx-1.x
Ammar Githam 4 years ago
parent
commit
3d5569c46b
  1. 4
      app/build.gradle
  2. 1
      app/src/androidTest/java/awais/instagrabber/db/MigrationTest.java
  3. 127
      app/src/androidTest/java/awais/instagrabber/db/dao/RecentSearchDaoTest.kt
  4. 8
      app/src/main/java/awais/instagrabber/db/AppDatabase.kt

4
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'
}

1
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());

127
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<Context>()
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<RecentSearch> insertListReversed = ImmutableList
.<RecentSearch>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<RecentSearch> fromDb = dao.getAllRecentSearches();
Assertions.assertIterableEquals(insertListReversed, fromDb);
fun queryAllOrdered() = runBlockingTest {
val insertListReversed: List<RecentSearch> = 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<RecentSearch?> = 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
}
}

8
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, " +

Loading…
Cancel
Save