forked from philipplackner/IvyWallet-PL_Version
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrumentation test setup with Hilt
- Loading branch information
1 parent
c210a96
commit 8630573
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
43 changes: 43 additions & 0 deletions
43
common/android-test/src/main/java/com/ivy/common/androidtest/IvyAndroidTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.ivy.common.androidtest | ||
|
||
import android.content.Context | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.ivy.core.persistence.IvyWalletCoreDb | ||
import com.ivy.core.persistence.datastore.dataStore | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import javax.inject.Inject | ||
|
||
abstract class IvyAndroidTest { | ||
|
||
@get:Rule | ||
val hiltRule = HiltAndroidRule(this) | ||
|
||
@Inject | ||
lateinit var db: IvyWalletCoreDb | ||
|
||
protected lateinit var context: Context | ||
|
||
@Before | ||
open fun setUp() { | ||
context = ApplicationProvider.getApplicationContext() | ||
hiltRule.inject() | ||
db.clearAllTables() | ||
clearDataStore() | ||
} | ||
|
||
@After | ||
open fun tearDown() { | ||
db.close() | ||
} | ||
|
||
private fun clearDataStore() = runBlocking { | ||
context.dataStore.edit { | ||
it.clear() | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
common/android-test/src/main/java/com/ivy/common/androidtest/TestCorePersistenceModuleDI.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.ivy.common.androidtest | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.ivy.core.persistence.IvyWalletCoreDb | ||
import com.ivy.core.persistence.algorithm.accountcache.AccountCacheDao | ||
import com.ivy.core.persistence.algorithm.calc.RatesDao | ||
import com.ivy.core.persistence.dao.AttachmentDao | ||
import com.ivy.core.persistence.dao.account.AccountDao | ||
import com.ivy.core.persistence.dao.account.AccountFolderDao | ||
import com.ivy.core.persistence.dao.category.CategoryDao | ||
import com.ivy.core.persistence.dao.exchange.ExchangeRateDao | ||
import com.ivy.core.persistence.dao.exchange.ExchangeRateOverrideDao | ||
import com.ivy.core.persistence.dao.tag.TagDao | ||
import com.ivy.core.persistence.dao.trn.TransactionDao | ||
import com.ivy.core.persistence.dao.trn.TrnLinkRecordDao | ||
import com.ivy.core.persistence.dao.trn.TrnMetadataDao | ||
import com.ivy.core.persistence.dao.trn.TrnTagDao | ||
import com.ivy.core.persistence.di.CorePersistenceModuleDI | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import dagger.hilt.testing.TestInstallIn | ||
import javax.inject.Singleton | ||
|
||
|
||
@Module | ||
@TestInstallIn( | ||
components = [SingletonComponent::class], | ||
replaces = [CorePersistenceModuleDI::class] | ||
) | ||
object TestCorePersistenceModuleDI { | ||
@Provides | ||
@Singleton | ||
fun provideIvyWalletDb(@ApplicationContext appContext: Context): IvyWalletCoreDb = | ||
Room.inMemoryDatabaseBuilder(appContext, IvyWalletCoreDb::class.java).build() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAccountDao(db: IvyWalletCoreDb): AccountDao = db.accountDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAccountCacheDao(db: IvyWalletCoreDb): AccountCacheDao = db.accountCacheDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAccountFolderDao(db: IvyWalletCoreDb): AccountFolderDao = db.accountFolderDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideCategoryDao(db: IvyWalletCoreDb): CategoryDao = db.categoryDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideExchangeRateDao(db: IvyWalletCoreDb): ExchangeRateDao = db.exchangeRateDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideExchangeRateOverrideDao(db: IvyWalletCoreDb): ExchangeRateOverrideDao = | ||
db.exchangeRateOverrideDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRatesDao(db:IvyWalletCoreDb) : RatesDao = | ||
db.ratesDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideTagDao(db: IvyWalletCoreDb): TagDao = db.tagDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideTrnDao(db: IvyWalletCoreDb): TransactionDao = db.trnDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideTrnLinkRecordDao(db: IvyWalletCoreDb): TrnLinkRecordDao = db.trnLinkRecordDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideTrnMetadataDao(db: IvyWalletCoreDb): TrnMetadataDao = db.trnMetadataDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideTrnTagDao(db: IvyWalletCoreDb): TrnTagDao = db.trnTagDao() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAttachmentDao(db: IvyWalletCoreDb): AttachmentDao = db.attachmentDao() | ||
} |