From 30825c9d6919ae7c19f3993aa2de2145c5b349ab Mon Sep 17 00:00:00 2001 From: charlie mangano Date: Thu, 19 Dec 2024 20:09:55 +0100 Subject: [PATCH] test: adapt tests to modifications to user data classes --- .../periodpals/model/user/UserDtoTest.kt | 25 +------ .../model/user/UserModelSupabaseTest.kt | 13 ++-- .../model/user/UserViewModelTest.kt | 28 ++------ .../periodpals/services/GPSServiceImplTest.kt | 65 ++++++++----------- 4 files changed, 39 insertions(+), 92 deletions(-) diff --git a/app/src/test/java/com/android/periodpals/model/user/UserDtoTest.kt b/app/src/test/java/com/android/periodpals/model/user/UserDtoTest.kt index 3e6f7e6d1..f64e0528a 100644 --- a/app/src/test/java/com/android/periodpals/model/user/UserDtoTest.kt +++ b/app/src/test/java/com/android/periodpals/model/user/UserDtoTest.kt @@ -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 @@ -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() { diff --git a/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt b/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt index e0dce0249..049ff08cd 100644 --- a/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt +++ b/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt @@ -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 @@ -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) @@ -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) @@ -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) } } diff --git a/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt b/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt index a61ce0f44..2ec85c0e8 100644 --- a/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt +++ b/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt @@ -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) } @@ -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) -> Unit>(0)(listOf(user)) } @@ -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) @@ -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) diff --git a/app/src/test/java/com/android/periodpals/services/GPSServiceImplTest.kt b/app/src/test/java/com/android/periodpals/services/GPSServiceImplTest.kt index 219d98a93..ffd5b048e 100644 --- a/app/src/test/java/com/android/periodpals/services/GPSServiceImplTest.kt +++ b/app/src/test/java/com/android/periodpals/services/GPSServiceImplTest.kt @@ -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 @@ -58,7 +57,7 @@ class GPSServiceImplTest { @Mock private lateinit var mockPermissionLauncher: ActivityResultLauncher> 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 @@ -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) @@ -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) @@ -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") @@ -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") @@ -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 * */