Skip to content

Commit

Permalink
test: added some necessary tests from OCLocalUserDataSourceTest and…
Browse files Browse the repository at this point in the history
… remove unnecessary ones from `DrawerViewModelTest`
  • Loading branch information
joragua committed Dec 11, 2024
1 parent e7e2c94 commit 2f0df52
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@
package com.owncloud.android.presentation.viewmodels

import com.owncloud.android.data.providers.LocalStorageProvider
import com.owncloud.android.domain.UseCaseResult
import com.owncloud.android.domain.user.model.UserQuota
import com.owncloud.android.domain.user.usecases.GetStoredQuotaUseCase
import com.owncloud.android.domain.user.usecases.GetStoredQuotaAsStreamUseCase
import com.owncloud.android.domain.user.usecases.GetUserQuotasUseCase
import com.owncloud.android.domain.utils.Event
import com.owncloud.android.presentation.common.DrawerViewModel
import com.owncloud.android.presentation.common.UIResult
import com.owncloud.android.providers.ContextProvider
import com.owncloud.android.testutil.OC_ACCOUNT_NAME
import com.owncloud.android.testutil.OC_USER_QUOTA
import com.owncloud.android.usecases.accounts.RemoveAccountUseCase
import io.mockk.every
import io.mockk.mockk
Expand All @@ -39,15 +34,14 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.dsl.module

@ExperimentalCoroutinesApi
class DrawerViewModelTest : ViewModelTest() {
private lateinit var drawerViewModel: DrawerViewModel
private lateinit var getStoredQuotaUseCase: GetStoredQuotaUseCase
private lateinit var getStoredQuotaAsStreamUseCase: GetStoredQuotaAsStreamUseCase
private lateinit var removeAccountUseCase: RemoveAccountUseCase
private lateinit var getUserQuotasUseCase: GetUserQuotasUseCase
private lateinit var localStorageProvider: LocalStorageProvider
Expand All @@ -56,68 +50,45 @@ class DrawerViewModelTest : ViewModelTest() {

private val commonException = Exception()

@Before
fun setUp() {
contextProvider = mockk()
@Before
fun setUp() {
contextProvider = mockk()

every { contextProvider.isConnected() } returns true
every { contextProvider.isConnected() } returns true

Dispatchers.setMain(testCoroutineDispatcher)
startKoin {
allowOverride(override = true)
modules(
module {
factory {
contextProvider
}
})
}
Dispatchers.setMain(testCoroutineDispatcher)
startKoin {
allowOverride(override = true)
modules(
module {
factory {
contextProvider
}
})
}

getStoredQuotaUseCase = mockk()
removeAccountUseCase = mockk()
getUserQuotasUseCase = mockk()
localStorageProvider = mockk()
getStoredQuotaAsStreamUseCase = mockk()
removeAccountUseCase = mockk()
getUserQuotasUseCase = mockk()
localStorageProvider = mockk()

testCoroutineDispatcher.pauseDispatcher()
testCoroutineDispatcher.pauseDispatcher()

drawerViewModel = DrawerViewModel(
getStoredQuotaUseCase = getStoredQuotaUseCase,
removeAccountUseCase = removeAccountUseCase,
getUserQuotasUseCase = getUserQuotasUseCase,
localStorageProvider = localStorageProvider,
coroutinesDispatcherProvider = coroutineDispatcherProvider,
contextProvider = contextProvider,
)
}
drawerViewModel = DrawerViewModel(
getStoredQuotaAsStreamUseCase = getStoredQuotaAsStreamUseCase,
removeAccountUseCase = removeAccountUseCase,
getUserQuotasUseCase = getUserQuotasUseCase,
localStorageProvider = localStorageProvider,
coroutinesDispatcherProvider = coroutineDispatcherProvider,
contextProvider = contextProvider,
accountName = OC_ACCOUNT_NAME
)
}

@After
override fun tearDown() {
super.tearDown()
stopKoin()
}
@After
override fun tearDown() {
super.tearDown()
stopKoin()
}

@Test
fun getStoredQuotaOk() {
every { getStoredQuotaUseCase(any()) } returns UseCaseResult.Success(OC_USER_QUOTA)
drawerViewModel.getStoredQuota(OC_ACCOUNT_NAME)

assertEmittedValues(
expectedValues = listOf<Event<UIResult<UserQuota>>>(
Event(UIResult.Loading()), Event(UIResult.Success(OC_USER_QUOTA))
),
liveData = drawerViewModel.userQuota
)
}

@Test
fun getStoredQuotaException() {
every { getStoredQuotaUseCase(any()) } returns UseCaseResult.Error(commonException)
drawerViewModel.getStoredQuota(OC_ACCOUNT_NAME)

assertEmittedValues(
expectedValues = listOf<Event<UIResult<UserQuota>>>
(Event(UIResult.Loading()), Event(UIResult.Error(commonException))),
liveData = drawerViewModel.userQuota
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*
* @author Abel García de Prada
* @author Aitor Ballesteros Pavón
* @author Jorge Aguado Recio
*
* Copyright (C) 2023 ownCloud GmbH.
* Copyright (C) 2024 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand All @@ -29,6 +30,9 @@ import com.owncloud.android.testutil.OC_USER_QUOTA
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
Expand Down Expand Up @@ -82,12 +86,16 @@ class OCLocalUserDataSourceTest {
}

@Test
fun `deleteQuotaForAccount removes user quota correctly`() {
fun `getQuotaForAccountAsFlow returns a Flow with an UserQuota`() = runTest {
every {
ocUserQuotaDao.getQuotaForAccountAsFlow(OC_ACCOUNT_NAME)
} returns flowOf(userQuotaEntity)

ocLocalUserDataSource.deleteQuotaForAccount(OC_ACCOUNT_NAME)
val userQuota = ocLocalUserDataSource.getQuotaForAccountAsFlow(OC_ACCOUNT_NAME).first()
assertEquals(userQuotaEntity.toModel(), userQuota)

verify(exactly = 1) {
ocUserQuotaDao.deleteQuotaForAccount(OC_ACCOUNT_NAME)
ocUserQuotaDao.getQuotaForAccountAsFlow(OC_ACCOUNT_NAME)
}
}

Expand All @@ -104,4 +112,29 @@ class OCLocalUserDataSourceTest {
ocUserQuotaDao.getAllUserQuotas()
}
}

@Test
fun `getAllUserQuotasAsFlow returns a Flow with a list of UserQuota`() = runTest {
every {
ocUserQuotaDao.getAllUserQuotasAsFlow()
} returns flowOf(listOf(userQuotaEntity))

val listOfUserQuotas = ocLocalUserDataSource.getAllUserQuotasAsFlow().first()
assertEquals(listOf(userQuotaEntity.toModel()), listOfUserQuotas)

verify(exactly = 1) {
ocUserQuotaDao.getAllUserQuotasAsFlow()
}
}

@Test
fun `deleteQuotaForAccount removes user quota correctly`() {

ocLocalUserDataSource.deleteQuotaForAccount(OC_ACCOUNT_NAME)

verify(exactly = 1) {
ocUserQuotaDao.deleteQuotaForAccount(OC_ACCOUNT_NAME)
}
}

}

0 comments on commit 2f0df52

Please sign in to comment.