Ammar Githam
4 years ago
4 changed files with 73 additions and 67 deletions
-
4app/build.gradle
-
1app/src/androidTest/java/awais/instagrabber/db/MigrationTest.java
-
127app/src/androidTest/java/awais/instagrabber/db/dao/RecentSearchDaoTest.kt
-
8app/src/main/java/awais/instagrabber/db/AppDatabase.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 |
@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 |
@After |
||||
public void closeDb() { |
|
||||
db.close(); |
|
||||
|
fun closeDb() { |
||||
|
db.close() |
||||
} |
} |
||||
|
|
||||
|
@ExperimentalCoroutinesApi |
||||
@Test |
@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 |
@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 |
||||
} |
} |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue