test: add unit tests to MatchRepositoryImpl.kt

This commit is contained in:
Leonardo Murça 2025-07-19 16:58:32 -03:00
parent 7b8c8a5b2e
commit 2a9dcbc616

View file

@ -0,0 +1,118 @@
package xyz.leomurca.csgomatches.data.repository
import io.mockk.coEvery
import io.mockk.mockk
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import xyz.leomurca.csgomatches.data.model.LeagueDto
import xyz.leomurca.csgomatches.data.model.MatchDto
import xyz.leomurca.csgomatches.data.model.OpponentDto
import xyz.leomurca.csgomatches.data.model.OpponentRecord
import xyz.leomurca.csgomatches.data.model.PlayerDto
import xyz.leomurca.csgomatches.data.model.SerieDto
import xyz.leomurca.csgomatches.data.model.TeamDetailsDto
import xyz.leomurca.csgomatches.data.source.MatchDataSource
import xyz.leomurca.csgomatches.domain.model.MatchStatus
import xyz.leomurca.csgomatches.domain.model.Resource
import java.time.ZonedDateTime
import kotlin.test.Test
import kotlin.test.assertEquals
@OptIn(ExperimentalCoroutinesApi::class)
class MatchRepositoryImplTest {
private lateinit var remoteDataSource: MatchDataSource
private var testDispatcher: CoroutineDispatcher = StandardTestDispatcher()
private lateinit var repository: MatchRepositoryImpl
@Before
fun setUp() {
remoteDataSource = mockk()
repository = MatchRepositoryImpl(
testDispatcher,
remoteDataSource
)
Dispatchers.setMain(testDispatcher)
}
@After
fun tearDown() {
Dispatchers.resetMain()
}
@Test
fun `upcomingMatches - Resource Success from data source - returns Success mapped to domain`() =
runTest {
// Arrange
val dto = MatchDto(
beginAt = ZonedDateTime.parse("2025-07-20T14:00:00Z"),
opponents = listOf(OpponentDto(type = "Team", OpponentRecord(1, "Team A", "img"))),
league = LeagueDto(1, "League", "league.png"),
serie = SerieDto(1, "Serie 1"),
status = "running"
)
coEvery { remoteDataSource.upcomingMatches() } returns Resource.Success(listOf(dto))
// Act
val result = repository.upcomingMatches()
// Assert
assertTrue(result is Resource.Success)
val match = (result as Resource.Success).data.first()
assertEquals("Team A", match.opponents.first().name)
assertEquals(MatchStatus.LIVE, match.matchStatus)
}
@Test
fun `upcomingMatches - Resource Error from data source - returns Error`() = runTest {
// Arrange
coEvery { remoteDataSource.upcomingMatches() } returns Resource.Error("Network issue")
// Act
val result = repository.upcomingMatches()
// Assert
assertTrue(result is Resource.Error)
assertEquals("Network issue", (result as Resource.Error).message)
}
@Test
fun `teamDetails - Resource Success from data source - returns Success mapped to domain`() =
runTest {
// Arrange
val player = PlayerDto(1, "nick", "John", "Doe", "img.png")
val dto = TeamDetailsDto(10, "My Team", "logo.png", listOf(player))
coEvery { remoteDataSource.teamDetails("10") } returns Resource.Success(listOf(dto))
// Act
val result = repository.teamDetails("10")
// Assert
assertTrue(result is Resource.Success)
val team = (result as Resource.Success).data.first()
assertEquals("My Team", team.name)
assertEquals("John", team.players.first().firstName)
}
@Test
fun `teamDetails - Resource Error from data source - returns Error`() = runTest {
// Arrange
coEvery { remoteDataSource.teamDetails("123") } returns Resource.Error("Team not found")
// Act
val result = repository.teamDetails("123")
// Assert
assertTrue(result is Resource.Error)
assertEquals("Team not found", (result as Resource.Error).message)
}
}