Auto-commit 2026-03-30 16:30
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package com.rssuper.repository
|
||||
|
||||
import com.rssuper.database.daos.FeedItemDao
|
||||
import com.rssuper.database.entities.FeedItemEntity
|
||||
import com.rssuper.state.State
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
|
||||
class FeedRepositoryTest {
|
||||
|
||||
private lateinit var feedItemDao: FeedItemDao
|
||||
private lateinit var feedRepository: FeedRepository
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
feedItemDao = Mockito.mock(FeedItemDao::class.java)
|
||||
feedRepository = FeedRepositoryImpl(feedItemDao)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetFeedItemsSuccess() = runTest {
|
||||
val items = listOf(
|
||||
FeedItemEntity(
|
||||
id = "1",
|
||||
subscriptionId = "sub1",
|
||||
title = "Test Item",
|
||||
published = java.util.Date()
|
||||
)
|
||||
)
|
||||
|
||||
val stateFlow = MutableStateFlow<State<List<FeedItemEntity>>>(State.Success(items))
|
||||
`when`(feedItemDao.getItemsBySubscription("sub1")).thenReturn(stateFlow)
|
||||
|
||||
feedRepository.getFeedItems("sub1").collect { state ->
|
||||
assert(state is State.Success)
|
||||
assert((state as State.Success).data == items)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertFeedItemSuccess() = runTest {
|
||||
val item = FeedItemEntity(
|
||||
id = "1",
|
||||
subscriptionId = "sub1",
|
||||
title = "Test Item",
|
||||
published = java.util.Date()
|
||||
)
|
||||
|
||||
`when`(feedItemDao.insertItem(item)).thenReturn(1L)
|
||||
|
||||
val result = feedRepository.insertFeedItem(item)
|
||||
assert(result == 1L)
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException::class)
|
||||
fun testInsertFeedItemError() = runTest {
|
||||
`when`(feedItemDao.insertItem(Mockito.any())).thenThrow(RuntimeException("Database error"))
|
||||
|
||||
feedRepository.insertFeedItem(FeedItemEntity(
|
||||
id = "1",
|
||||
subscriptionId = "sub1",
|
||||
title = "Test Item",
|
||||
published = java.util.Date()
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.rssuper.repository
|
||||
|
||||
import com.rssuper.database.daos.SubscriptionDao
|
||||
import com.rssuper.database.entities.SubscriptionEntity
|
||||
import com.rssuper.state.State
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
import java.util.Date
|
||||
|
||||
class SubscriptionRepositoryTest {
|
||||
|
||||
private lateinit var subscriptionDao: SubscriptionDao
|
||||
private lateinit var subscriptionRepository: SubscriptionRepository
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
subscriptionDao = Mockito.mock(SubscriptionDao::class.java)
|
||||
subscriptionRepository = SubscriptionRepositoryImpl(subscriptionDao)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetAllSubscriptionsSuccess() = runTest {
|
||||
val subscriptions = listOf(
|
||||
SubscriptionEntity(
|
||||
id = "1",
|
||||
url = "https://example.com/feed.xml",
|
||||
title = "Test Feed",
|
||||
createdAt = Date(),
|
||||
updatedAt = Date()
|
||||
)
|
||||
)
|
||||
|
||||
val stateFlow = MutableStateFlow<State<List<SubscriptionEntity>>>(State.Success(subscriptions))
|
||||
`when`(subscriptionDao.getAllSubscriptions()).thenReturn(stateFlow)
|
||||
|
||||
subscriptionRepository.getAllSubscriptions().collect { state ->
|
||||
assert(state is State.Success)
|
||||
assert((state as State.Success).data == subscriptions)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetEnabledSubscriptionsSuccess() = runTest {
|
||||
val subscriptions = listOf(
|
||||
SubscriptionEntity(
|
||||
id = "1",
|
||||
url = "https://example.com/feed.xml",
|
||||
title = "Test Feed",
|
||||
enabled = true,
|
||||
createdAt = Date(),
|
||||
updatedAt = Date()
|
||||
)
|
||||
)
|
||||
|
||||
val stateFlow = MutableStateFlow<State<List<SubscriptionEntity>>>(State.Success(subscriptions))
|
||||
`when`(subscriptionDao.getEnabledSubscriptions()).thenReturn(stateFlow)
|
||||
|
||||
subscriptionRepository.getEnabledSubscriptions().collect { state ->
|
||||
assert(state is State.Success)
|
||||
assert((state as State.Success).data == subscriptions)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertSubscriptionSuccess() = runTest {
|
||||
val subscription = SubscriptionEntity(
|
||||
id = "1",
|
||||
url = "https://example.com/feed.xml",
|
||||
title = "Test Feed",
|
||||
createdAt = Date(),
|
||||
updatedAt = Date()
|
||||
)
|
||||
|
||||
`when`(subscriptionDao.insertSubscription(subscription)).thenReturn(1L)
|
||||
|
||||
val result = subscriptionRepository.insertSubscription(subscription)
|
||||
assert(result == 1L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateSubscriptionSuccess() = runTest {
|
||||
val subscription = SubscriptionEntity(
|
||||
id = "1",
|
||||
url = "https://example.com/feed.xml",
|
||||
title = "Test Feed",
|
||||
enabled = true,
|
||||
createdAt = Date(),
|
||||
updatedAt = Date()
|
||||
)
|
||||
|
||||
`when`(subscriptionDao.updateSubscription(subscription)).thenReturn(1)
|
||||
|
||||
val result = subscriptionRepository.updateSubscription(subscription)
|
||||
assert(result == 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetEnabledSuccess() = runTest {
|
||||
`when`(subscriptionDao.setEnabled("1", true)).thenReturn(1)
|
||||
|
||||
val result = subscriptionRepository.setEnabled("1", true)
|
||||
assert(result == 1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.rssuper.state
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class StateTest {
|
||||
|
||||
@Test
|
||||
fun testIdleState() {
|
||||
val state: State<String> = State.Idle
|
||||
assertTrue(state is State.Idle)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadingState() {
|
||||
val state: State<String> = State.Loading
|
||||
assertTrue(state is State.Loading)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSuccessState() {
|
||||
val data = "test data"
|
||||
val state: State<String> = State.Success(data)
|
||||
|
||||
assertTrue(state is State.Success)
|
||||
assertEquals(data, (state as State.Success).data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testErrorState() {
|
||||
val message = "test error"
|
||||
val state: State<String> = State.Error(message)
|
||||
|
||||
assertTrue(state is State.Error)
|
||||
assertEquals(message, (state as State.Error).message)
|
||||
assertEquals(null, (state as State.Error).cause)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testErrorStateWithCause() {
|
||||
val message = "test error"
|
||||
val cause = RuntimeException("cause")
|
||||
val state: State<String> = State.Error(message, cause)
|
||||
|
||||
assertTrue(state is State.Error)
|
||||
assertEquals(message, (state as State.Error).message)
|
||||
assertEquals(cause, (state as State.Error).cause)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testErrorType() {
|
||||
assertTrue(ErrorType.NETWORK != ErrorType.DATABASE)
|
||||
assertTrue(ErrorType.PARSING != ErrorType.AUTH)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testErrorDetails() {
|
||||
val details = ErrorDetails(ErrorType.NETWORK, "Network error", true)
|
||||
|
||||
assertEquals(ErrorType.NETWORK, details.type)
|
||||
assertEquals("Network error", details.message)
|
||||
assertTrue(details.retryable)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.rssuper.viewmodel
|
||||
|
||||
import com.rssuper.repository.FeedRepository
|
||||
import com.rssuper.state.State
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
|
||||
class FeedViewModelTest {
|
||||
|
||||
private lateinit var feedRepository: FeedRepository
|
||||
private lateinit var viewModel: FeedViewModel
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
feedRepository = Mockito.mock(FeedRepository::class.java)
|
||||
viewModel = FeedViewModel(feedRepository)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInitialState() = runTest {
|
||||
var stateEmitted = false
|
||||
viewModel.feedState.collect { state ->
|
||||
assert(state is State.Idle)
|
||||
stateEmitted = true
|
||||
}
|
||||
assert(stateEmitted)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadFeedItems() = runTest {
|
||||
val items = listOf(
|
||||
com.rssuper.database.entities.FeedItemEntity(
|
||||
id = "1",
|
||||
subscriptionId = "sub1",
|
||||
title = "Test Item",
|
||||
published = java.util.Date()
|
||||
)
|
||||
)
|
||||
|
||||
val stateFlow = MutableStateFlow<State<List<com.rssuper.database.entities.FeedItemEntity>>>(State.Success(items))
|
||||
`when`(feedRepository.getFeedItems("sub1")).thenReturn(stateFlow)
|
||||
|
||||
viewModel.loadFeedItems("sub1")
|
||||
|
||||
var receivedState: State<List<com.rssuper.database.entities.FeedItemEntity>>? = null
|
||||
viewModel.feedState.collect { state ->
|
||||
receivedState = state
|
||||
}
|
||||
|
||||
assert(receivedState is State.Success)
|
||||
assert((receivedState as State.Success).data == items)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMarkAsRead() = runTest {
|
||||
`when`(feedRepository.markAsRead("1", true)).thenReturn(1)
|
||||
`when`(feedRepository.getUnreadCount("sub1")).thenReturn(5)
|
||||
|
||||
viewModel.markAsRead("1", true)
|
||||
|
||||
var unreadCountState: State<Int>? = null
|
||||
viewModel.unreadCount.collect { state ->
|
||||
unreadCountState = state
|
||||
}
|
||||
|
||||
assert(unreadCountState is State.Success)
|
||||
assert((unreadCountState as State.Success).data == 5)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.rssuper.viewmodel
|
||||
|
||||
import com.rssuper.repository.SubscriptionRepository
|
||||
import com.rssuper.state.State
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.`when`
|
||||
import java.util.Date
|
||||
|
||||
class SubscriptionViewModelTest {
|
||||
|
||||
private lateinit var subscriptionRepository: SubscriptionRepository
|
||||
private lateinit var viewModel: SubscriptionViewModel
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
subscriptionRepository = Mockito.mock(SubscriptionRepository::class.java)
|
||||
viewModel = SubscriptionViewModel(subscriptionRepository)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInitialState() = runTest {
|
||||
var stateEmitted = false
|
||||
viewModel.subscriptionsState.collect { state ->
|
||||
assert(state is State.Idle)
|
||||
stateEmitted = true
|
||||
}
|
||||
assert(stateEmitted)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadAllSubscriptions() = runTest {
|
||||
val subscriptions = listOf(
|
||||
com.rssuper.database.entities.SubscriptionEntity(
|
||||
id = "1",
|
||||
url = "https://example.com/feed.xml",
|
||||
title = "Test Feed",
|
||||
createdAt = Date(),
|
||||
updatedAt = Date()
|
||||
)
|
||||
)
|
||||
|
||||
val stateFlow = MutableStateFlow<State<List<com.rssuper.database.entities.SubscriptionEntity>>>(State.Success(subscriptions))
|
||||
`when`(subscriptionRepository.getAllSubscriptions()).thenReturn(stateFlow)
|
||||
|
||||
viewModel.loadAllSubscriptions()
|
||||
|
||||
var receivedState: State<List<com.rssuper.database.entities.SubscriptionEntity>>? = null
|
||||
viewModel.subscriptionsState.collect { state ->
|
||||
receivedState = state
|
||||
}
|
||||
|
||||
assert(receivedState is State.Success)
|
||||
assert((receivedState as State.Success).data == subscriptions)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetEnabled() = runTest {
|
||||
val subscriptions = listOf(
|
||||
com.rssuper.database.entities.SubscriptionEntity(
|
||||
id = "1",
|
||||
url = "https://example.com/feed.xml",
|
||||
title = "Test Feed",
|
||||
enabled = true,
|
||||
createdAt = Date(),
|
||||
updatedAt = Date()
|
||||
)
|
||||
)
|
||||
|
||||
val stateFlow = MutableStateFlow<State<List<com.rssuper.database.entities.SubscriptionEntity>>>(State.Success(subscriptions))
|
||||
`when`(subscriptionRepository.setEnabled("1", true)).thenReturn(1)
|
||||
`when`(subscriptionRepository.getEnabledSubscriptions()).thenReturn(stateFlow)
|
||||
|
||||
viewModel.setEnabled("1", true)
|
||||
|
||||
var receivedState: State<List<com.rssuper.database.entities.SubscriptionEntity>>? = null
|
||||
viewModel.enabledSubscriptionsState.collect { state ->
|
||||
receivedState = state
|
||||
}
|
||||
|
||||
assert(receivedState is State.Success)
|
||||
assert((receivedState as State.Success).data == subscriptions)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetError() = runTest {
|
||||
`when`(subscriptionRepository.setError("1", "Test error")).thenReturn(1)
|
||||
|
||||
viewModel.setError("1", "Test error")
|
||||
|
||||
var stateEmitted = false
|
||||
viewModel.subscriptionsState.collect { state ->
|
||||
stateEmitted = true
|
||||
}
|
||||
assert(stateEmitted)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user