diff --git a/app/src/test/java/com/juandgaines/testground/data/UserApiFake.kt b/app/src/test/java/com/juandgaines/testground/data/UserApiFake.kt new file mode 100644 index 0000000..ceb7b87 --- /dev/null +++ b/app/src/test/java/com/juandgaines/testground/data/UserApiFake.kt @@ -0,0 +1,37 @@ +package com.juandgaines.testground.data + +import com.juandgaines.testground.domain.Coordinates +import com.juandgaines.testground.domain.Place +import com.juandgaines.testground.domain.Profile +import com.juandgaines.testground.domain.User +import java.util.UUID + +class UserApiFake : UserApi { + var users = (1..10).map { + User( + id = it.toString(), + username = "User$it" + ) + } + + var places = (1..10).map { + Place( + id = UUID.randomUUID().toString(), + name = "Place$it", + coordinates = Coordinates(it.toDouble(), it.toDouble()) + ) + } + + override suspend fun getUser(userId: String): User { + return users.find { it.id == userId } ?: throw Exception("User not found") + } + + override suspend fun getPlaces(userId: String): List { + return places.filter { it.id == userId } + } + + override suspend fun getProfile(userId: String): Profile { + val user = getUser(userId) + return Profile(user = user, places = getPlaces(userId)) + } +} \ No newline at end of file diff --git a/app/src/test/java/com/juandgaines/testground/data/UserRepositoryTest.kt b/app/src/test/java/com/juandgaines/testground/data/UserRepositoryTest.kt new file mode 100644 index 0000000..3c97e0b --- /dev/null +++ b/app/src/test/java/com/juandgaines/testground/data/UserRepositoryTest.kt @@ -0,0 +1,41 @@ +package com.juandgaines.testground.data + +import com.google.common.truth.Truth +import com.juandgaines.testground.util.MainDispatcherRule +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +@OptIn(ExperimentalCoroutinesApi::class) +class UserRepositoryTest { + + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + + private lateinit var repository: UserRepositoryImpl + private lateinit var api: UserApiFake + + @Before + fun setUp() { + // Setup for fake API tests + api = UserApiFake() + repository = UserRepositoryImpl(api) + } + + + @Test + fun givenValidUserId_whenGetProfileWithFakeApi_thenReturnsProfile() = runTest { + // Act + val profileResult = repository.getProfile("1") + + // Assert + Truth.assertThat(profileResult.isSuccess).isTrue() + Truth.assertThat(profileResult.getOrThrow().user.id).isEqualTo("1") + + val expectedPlaces = api.places.filter { it.id == "1" } + Truth.assertThat(profileResult.getOrThrow().places).isEqualTo(expectedPlaces) + } + +} \ No newline at end of file