Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/src/test/java/com/juandgaines/testground/data/UserApiFake.kt
Original file line number Diff line number Diff line change
@@ -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<Place> {
return places.filter { it.id == userId }
}

override suspend fun getProfile(userId: String): Profile {
val user = getUser(userId)
return Profile(user = user, places = getPlaces(userId))
}
}
Original file line number Diff line number Diff line change
@@ -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)
}

}