Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Dec 30, 2024
1 parent ea25201 commit 01a9013
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.repository

import android.content.Context
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.sync.account.TestAccountAuthenticator
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject
import at.bitfire.davdroid.db.Account as DbAccount

@HiltAndroidTest
class AccountRepositoryTest {

@Inject
lateinit var accountRepository: AccountRepository

@Inject @ApplicationContext
lateinit var context: Context

@Inject
lateinit var db: AppDatabase

@get:Rule
val hiltRule = HiltAndroidRule(this)

@Before
fun setUp() {
hiltRule.inject()
}


@Test
fun testRemoveOrphanedInDb() {
TestAccountAuthenticator.provide(accountType = context.getString(R.string.account_type)) { systemAccount ->
val dao = db.accountDao()
dao.insertOrIgnore(DbAccount(id = 1, name = systemAccount.name))
dao.insertOrIgnore(DbAccount(id = 2, name = "no-corresponding-system-account"))

accountRepository.removeOrphanedInDb()

// now the account without a corresponding system account should be removed
assertEquals(listOf(DbAccount(id = 1, name = systemAccount.name)), db.accountDao().getAll())
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package at.bitfire.davdroid.repository

import android.accounts.Account
import android.content.Context
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.sync.account.TestAccountAuthenticator
import dagger.Lazy
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
Expand All @@ -18,6 +20,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject
import at.bitfire.davdroid.db.Account as DbAccount

@HiltAndroidTest
class DavCollectionRepositoryTest {
Expand All @@ -41,26 +44,34 @@ class DavCollectionRepositoryTest {
@Inject
lateinit var serviceRepository: DavServiceRepository

var service: Service? = null
lateinit var account: Account
var serviceId: Long = 0L

@Before
fun setUp() {
hiltRule.inject()
service = createTestService(Service.TYPE_CARDDAV)!!

account = TestAccountAuthenticator.create()
db.accountDao().insertOrIgnore(DbAccount(name = account.name))

val service = Service(id=0, accountName=account.name, type= Service.TYPE_CALDAV, principal = null)
serviceId = serviceRepository.insertOrReplace(service)
}

@After
fun cleanUp() {
db.close()

serviceRepository.deleteAll()
TestAccountAuthenticator.remove(account)
}


@Test
fun testOnChangeListener_setForceReadOnly() = runBlocking {
val collectionId = db.collectionDao().insertOrUpdateByUrl(
Collection(
serviceId = service!!.id,
serviceId = serviceId,
type = Collection.TYPE_ADDRESSBOOK,
url = "https://example.com".toHttpUrl(),
forceReadOnly = false,
Expand Down Expand Up @@ -94,10 +105,4 @@ class DavCollectionRepositoryTest {

// Test helpers and dependencies

private fun createTestService(serviceType: String) : Service? {
val service = Service(id=0, accountName="test", type=serviceType, principal = null)
val serviceId = serviceRepository.insertOrReplace(service)
return serviceRepository.get(serviceId)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,61 @@

package at.bitfire.davdroid.repository

import android.accounts.Account
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.HomeSet
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.sync.account.TestAccountAuthenticator
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject
import at.bitfire.davdroid.db.Account as DbAccount

@HiltAndroidTest
class DavHomeSetRepositoryTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@Inject
lateinit var db: AppDatabase

@Inject
lateinit var repository: DavHomeSetRepository

@Inject
lateinit var serviceRepository: DavServiceRepository

lateinit var account: Account
var serviceId: Long = 0L


@Before
fun setUp() {
hiltRule.inject()

account = TestAccountAuthenticator.create()
db.accountDao().insertOrIgnore(DbAccount(name = account.name))

val service = Service(id=0, accountName=account.name, type= Service.TYPE_CALDAV, principal = null)
serviceId = serviceRepository.insertOrReplace(service)
}

@After
fun tearDown() {
TestAccountAuthenticator.remove(account)
}


@Test
fun testInsertOrUpdate() {
// should insert new row or update (upsert) existing row - without changing its key!
val serviceId = createTestService()

val entry1 = HomeSet(id=0, serviceId=serviceId, personal=true, url="https://example.com/1".toHttpUrl())
val insertId1 = repository.insertOrUpdateByUrl(entry1)
assertEquals(1L, insertId1)
Expand All @@ -57,8 +78,6 @@ class DavHomeSetRepositoryTest {
@Test
fun testDelete() {
// should delete row with given primary key (id)
val serviceId = createTestService()

val entry1 = HomeSet(id=1, serviceId=serviceId, personal=true, url= "https://example.com/1".toHttpUrl())

val insertId1 = repository.insertOrUpdateByUrl(entry1)
Expand All @@ -69,10 +88,4 @@ class DavHomeSetRepositoryTest {
assertEquals(null, repository.getById(1L))
}


private fun createTestService() : Long {
val service = Service(id=0, accountName="test", type= Service.TYPE_CALDAV, principal = null)
return serviceRepository.insertOrReplace(service)
}

}
Loading

0 comments on commit 01a9013

Please sign in to comment.