Skip to content

Commit

Permalink
test: adapt tests to modifications to user data classes
Browse files Browse the repository at this point in the history
  • Loading branch information
charliemangano committed Dec 19, 2024
1 parent 2101ceb commit 30825c9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 92 deletions.
25 changes: 2 additions & 23 deletions app/src/test/java/com/android/periodpals/model/user/UserDtoTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.android.periodpals.model.user

import com.android.periodpals.model.location.Location
import com.android.periodpals.model.location.parseLocationGIS
import org.junit.Assert.assertEquals
import org.junit.Test

Expand All @@ -15,30 +13,11 @@ class UserDtoTest {
val id = "test_id"
val preferredDistance = 500
val fcmToken = "test_fcm_token"
val locationGIS = parseLocationGIS(Location.DEFAULT_LOCATION)
}

val input =
UserDto(
name,
imageUrl,
description,
dob,
preferredDistance,
fcmToken,
locationGIS,
)
val input = UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken)

val output =
User(
name,
imageUrl,
description,
dob,
preferredDistance,
fcmToken,
locationGIS,
)
val output = User(name, imageUrl, description, dob, preferredDistance, fcmToken)

@Test
fun asUserIsCorrect() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.android.periodpals.model.user

import com.android.periodpals.model.location.Location
import com.android.periodpals.model.location.parseLocationGIS
import io.github.jan.supabase.createSupabaseClient
import io.github.jan.supabase.postgrest.Postgrest
import io.ktor.client.engine.mock.MockEngine
Expand Down Expand Up @@ -32,11 +30,10 @@ class UserRepositorySupabaseTest {
val id = "test_id"
val preferredDistance = 500
val fcmToken = "test_fcm_token"
val locationGIS = parseLocationGIS(Location.DEFAULT_LOCATION)
}

private val defaultUserDto: UserDto =
UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken, locationGIS)
UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken)
private val defaultUser: User =
User(name, imageUrl, description, dob, preferredDistance, fcmToken)

Expand All @@ -51,8 +48,7 @@ class UserRepositorySupabaseTest {
"\"description\":\"${description}\"," +
"\"dob\":\"${dob}\"," +
"\"preferred_distance\":\"${preferredDistance}\"," +
"\"fcm_token\":\"${fcmToken}\"," +
"\"locationGIS\":{\"type\":\"Point\",\"coordinates\":[6.5665, 46.5186]}}" +
"\"fcm_token\":\"${fcmToken}\"}" +
"]")
}
install(Postgrest)
Expand Down Expand Up @@ -85,7 +81,10 @@ class UserRepositorySupabaseTest {
runTest {
val userRepositorySupabase = UserRepositorySupabase(supabaseClientSuccess)
userRepositorySupabase.loadUserProfile(
id, { result = it }, { fail("should not call onFailure") })
id,
{ result = it },
{ fail("should not call onFailure") },
)
assertEquals(defaultUserDto, result)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,7 @@ class UserViewModelTest {

@Test
fun loadUserIsSuccessful() = runTest {
val user =
UserDto(
name,
imageUrl,
description,
dob,
preferredDistance,
fcmToken,
locationGIS,
)
val user = UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken)
val expected = user.asUser()

doAnswer { it.getArgument<(UserDto) -> Unit>(1)(user) }
Expand All @@ -104,16 +95,7 @@ class UserViewModelTest {

@Test
fun loadUsersIsSuccessful() = runTest {
val user =
UserDto(
name,
imageUrl,
description,
dob,
preferredDistance,
fcmToken,
locationGIS,
)
val user = UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken)
val expected = user.asUser()

doAnswer { it.getArgument<(List<UserDto>) -> Unit>(0)(listOf(user)) }
Expand All @@ -138,8 +120,7 @@ class UserViewModelTest {

@Test
fun saveUserIsSuccessful() = runTest {
val expected =
UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken, locationGIS).asUser()
val expected = UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken).asUser()

doAnswer { it.getArgument<(UserDto) -> Unit>(1)(expected.asUserDto()) }
.`when`(userModel)
Expand All @@ -152,8 +133,7 @@ class UserViewModelTest {

@Test
fun saveUserHasFailed() = runTest {
val test =
UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken, locationGIS).asUser()
val test = UserDto(name, imageUrl, description, dob, preferredDistance, fcmToken).asUser()

doAnswer { it.getArgument<(Exception) -> Unit>(2)(Exception("failed")) }
.`when`(userModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import androidx.compose.runtime.mutableStateOf
import androidx.core.app.ActivityCompat
import com.android.periodpals.model.authentication.AuthenticationViewModel
import com.android.periodpals.model.location.Location
import com.android.periodpals.model.location.UserLocationViewModel
import com.android.periodpals.model.location.parseLocationGIS
import com.android.periodpals.model.user.AuthenticationUserData
import com.android.periodpals.model.user.User
import com.android.periodpals.model.user.UserViewModel
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
Expand Down Expand Up @@ -58,7 +57,7 @@ class GPSServiceImplTest {

@Mock private lateinit var mockPermissionLauncher: ActivityResultLauncher<Array<String>>
private lateinit var authenticationViewModel: AuthenticationViewModel
private lateinit var userViewModel: UserViewModel
private lateinit var userLocationViewModel: UserLocationViewModel

// Used to get the FusedLocationProviderClient
private lateinit var mockLocationServices: MockedStatic<LocationServices>
Expand Down Expand Up @@ -86,7 +85,7 @@ class GPSServiceImplTest {
mockActivity = mock(ComponentActivity::class.java)
mockFusedLocationClient = mock(FusedLocationProviderClient::class.java)
authenticationViewModel = mock(AuthenticationViewModel::class.java)
userViewModel = mock(UserViewModel::class.java)
userLocationViewModel = mock(UserLocationViewModel::class.java)

mockLocationServices = mockStatic(LocationServices::class.java)

Expand Down Expand Up @@ -127,7 +126,7 @@ class GPSServiceImplTest {
)

// Create instance of GPSServiceImpl...
gpsService = GPSServiceImpl(mockActivity, authenticationViewModel, userViewModel)
gpsService = GPSServiceImpl(mockActivity, authenticationViewModel, userLocationViewModel)

// ... and verify that registerForActivityResult was called
verify(mockActivity)
Expand Down Expand Up @@ -360,17 +359,16 @@ class GPSServiceImplTest {
}

@Test
fun `switchFromPreciseToApproximate should call saveUser with proper arguments`() {
val userNoLocation =
User("test name", "test url", "test description", "test dob", 1, "test fcm")
fun `switchFromPreciseToApproximate should call uploadUserLocation with proper arguments`() {
val mockLat = 42.0
val mockLong = 16.0

`when`(userViewModel.user).thenReturn(mutableStateOf(userNoLocation))
`when`(userViewModel.loadUser(any(), any(), any())).doAnswer {
val onSuccess = it.arguments[1] as () -> Unit
`when`(authenticationViewModel.loadAuthenticationUserData(any(), any())).doAnswer {
val onSuccess = it.arguments[0] as () -> Unit
onSuccess()
}
`when`(authenticationViewModel.authUserData)
.thenReturn(mutableStateOf(AuthenticationUserData("test uid", "test email")))

// set the private _location value
val locationField = GPSServiceImpl::class.java.getDeclaredField("_location")
Expand All @@ -381,33 +379,28 @@ class GPSServiceImplTest {
gpsService.askPermissionAndStartUpdates()
gpsService.switchFromPreciseToApproximate()

val userExpected =
User(
"test name",
"test url",
"test description",
"test dob",
1,
"test fcm",
parseLocationGIS(Location(mockLat, mockLong, "test location")),
verify(userLocationViewModel)
.uploadUserLocation(
eq("test uid"),
eq(parseLocationGIS(mutableStateFlow.value)),
any(),
any(),
)
verify(userViewModel).saveUser(eq(userExpected), any(), any())
}

@Test
fun `cleanup should call saveUser with proper arguments`() {
val userNoLocation =
User("test name", "test url", "test description", "test dob", 1, "test fcm")
fun `cleanup should call uploadUserLocation with proper arguments`() {
val mockLat = 42.0
val mockLong = 16.0

val gpsService = GPSServiceImpl(mockActivity, authenticationViewModel, userViewModel)
val gpsService = GPSServiceImpl(mockActivity, authenticationViewModel, userLocationViewModel)

`when`(userViewModel.user).thenReturn(mutableStateOf(userNoLocation))
`when`(userViewModel.loadUser(any(), any(), any())).doAnswer {
val onSuccess = it.arguments[1] as () -> Unit
`when`(authenticationViewModel.loadAuthenticationUserData(any(), any())).doAnswer {
val onSuccess = it.arguments[0] as () -> Unit
onSuccess()
}
`when`(authenticationViewModel.authUserData)
.thenReturn(mutableStateOf(AuthenticationUserData("test uid", "test email")))

// set the private _location value
val locationField = GPSServiceImpl::class.java.getDeclaredField("_location")
Expand All @@ -417,17 +410,13 @@ class GPSServiceImplTest {

gpsService.cleanup()

val userExpected =
User(
"test name",
"test url",
"test description",
"test dob",
1,
"test fcm",
parseLocationGIS(Location(mockLat, mockLong, "test location")),
verify(userLocationViewModel)
.uploadUserLocation(
eq("test uid"),
eq(parseLocationGIS(mutableStateFlow.value)),
any(),
any(),
)
verify(userViewModel).saveUser(eq(userExpected), any(), any())
}

/** Mocks permissions granted for precise and approximate * */
Expand Down

0 comments on commit 30825c9

Please sign in to comment.