From e62da931a4aa4bee19c4e60fb6d4f2cee2197e4c Mon Sep 17 00:00:00 2001 From: johnGachihi Date: Sat, 23 Nov 2019 13:57:23 +0300 Subject: [PATCH 1/3] Re-Renamed Result back to FirebaseResult to solve issue [#57] again. --- .../droidconke19/datastates/Result.kt | 6 ------ .../droidconke19/datastates/RunCatching.kt | 6 +++--- .../repository/AboutDetailsRepo.kt | 6 +++--- .../droidconke19/repository/AgendaRepo.kt | 6 +++--- .../repository/AnnouncementRepo.kt | 6 +++--- .../repository/EventFeedbackRepo.kt | 6 +++--- .../droidconke19/repository/EventTypeRepo.kt | 6 +++--- .../repository/ReserveSeatRepo.kt | 13 ++++++------ .../droidconke19/repository/RoomRepo.kt | 6 +++--- .../repository/SessionDataRepo.kt | 20 +++++++++---------- .../repository/SessionFeedbackRepo.kt | 6 +++--- .../droidconke19/repository/SessionsRepo.kt | 6 +++--- .../droidconke19/repository/SpeakersRepo.kt | 6 +++--- .../repository/UpdateTokenRepo.kt | 6 +++--- build.gradle | 2 +- 15 files changed, 50 insertions(+), 57 deletions(-) delete mode 100644 app/src/main/java/com/android254/droidconke19/datastates/Result.kt diff --git a/app/src/main/java/com/android254/droidconke19/datastates/Result.kt b/app/src/main/java/com/android254/droidconke19/datastates/Result.kt deleted file mode 100644 index ea37927..0000000 --- a/app/src/main/java/com/android254/droidconke19/datastates/Result.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.android254.droidconke19.datastates - -sealed class Result{ -data class Success(val data: T) : Result() -data class Error(val exception: String?) : Result() -} \ No newline at end of file diff --git a/app/src/main/java/com/android254/droidconke19/datastates/RunCatching.kt b/app/src/main/java/com/android254/droidconke19/datastates/RunCatching.kt index dbd6e00..205ab4a 100644 --- a/app/src/main/java/com/android254/droidconke19/datastates/RunCatching.kt +++ b/app/src/main/java/com/android254/droidconke19/datastates/RunCatching.kt @@ -5,10 +5,10 @@ import kotlinx.coroutines.withContext suspend fun T.runCatching( block: suspend T.() -> R -): Result = withContext(Dispatchers.IO) { +): FirebaseResult = withContext(Dispatchers.IO) { return@withContext try { - Result.Success(block()) + FirebaseResult.Success(block()) } catch (e: Exception) { - Result.Error(e.message) + FirebaseResult.Error(e.message) } } \ No newline at end of file diff --git a/app/src/main/java/com/android254/droidconke19/repository/AboutDetailsRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/AboutDetailsRepo.kt index 15abcd7..f6aa79b 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/AboutDetailsRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/AboutDetailsRepo.kt @@ -1,6 +1,6 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.AboutDetailsModel import com.android254.droidconke19.utils.await @@ -9,13 +9,13 @@ import com.google.firebase.firestore.Query import com.google.firebase.firestore.ktx.toObjects interface AboutDetailsRepo { - suspend fun getAboutDetails(aboutType: String): Result> + suspend fun getAboutDetails(aboutType: String): FirebaseResult> } class AboutDetailsRepoImpl(val firestore: FirebaseFirestore) : AboutDetailsRepo { - override suspend fun getAboutDetails(aboutType: String): Result> = + override suspend fun getAboutDetails(aboutType: String): FirebaseResult> = runCatching { val snapshot = firestore.collection(aboutType) .orderBy("id", Query.Direction.ASCENDING) diff --git a/app/src/main/java/com/android254/droidconke19/repository/AgendaRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/AgendaRepo.kt index 57ca6d2..8cb9afb 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/AgendaRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/AgendaRepo.kt @@ -1,6 +1,6 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.AgendaModel import com.android254.droidconke19.utils.await @@ -9,12 +9,12 @@ import com.google.firebase.firestore.Query import com.google.firebase.firestore.ktx.toObjects interface AgendaRepo { - suspend fun agendaData(): Result> + suspend fun agendaData(): FirebaseResult> } class AgendaRepoImpl(val firestore: FirebaseFirestore) : AgendaRepo { - override suspend fun agendaData(): Result> = + override suspend fun agendaData(): FirebaseResult> = runCatching { val snapshot = firestore .collection("agenda_2019") diff --git a/app/src/main/java/com/android254/droidconke19/repository/AnnouncementRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/AnnouncementRepo.kt index 4e0f69d..6bb81c9 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/AnnouncementRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/AnnouncementRepo.kt @@ -1,6 +1,6 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.Announcement import com.android254.droidconke19.utils.await @@ -8,12 +8,12 @@ import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.ktx.toObjects interface AnnouncementRepo { - suspend fun getAnnouncements(): Result> + suspend fun getAnnouncements(): FirebaseResult> } class AnnouncementRepoImpl(val firestore: FirebaseFirestore) : AnnouncementRepo { - override suspend fun getAnnouncements(): Result> = + override suspend fun getAnnouncements(): FirebaseResult> = runCatching { val snapshot = firestore.collection("announcements").get().await() snapshot.toObjects() diff --git a/app/src/main/java/com/android254/droidconke19/repository/EventFeedbackRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/EventFeedbackRepo.kt index 7b3590e..c902b09 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/EventFeedbackRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/EventFeedbackRepo.kt @@ -1,18 +1,18 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.UserEventFeedback import com.google.firebase.firestore.FirebaseFirestore import kotlinx.coroutines.tasks.await interface EventFeedbackRepo { - suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): Result + suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): FirebaseResult } class EventFeedbackRepoImpl(val firestore: FirebaseFirestore) : EventFeedbackRepo { - override suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): Result = + override suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): FirebaseResult = runCatching { firestore.collection("event_feedback_2019").add(userEventFeedback).await() "Thank you for your feedback" diff --git a/app/src/main/java/com/android254/droidconke19/repository/EventTypeRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/EventTypeRepo.kt index ed0dc10..03ea122 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/EventTypeRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/EventTypeRepo.kt @@ -1,6 +1,6 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.EventTypeModel import com.android254.droidconke19.utils.await @@ -9,12 +9,12 @@ import com.google.firebase.firestore.Query import com.google.firebase.firestore.ktx.toObjects interface EventTypeRepo { - suspend fun getSessionData(): Result> + suspend fun getSessionData(): FirebaseResult> } class EventTypeRepoImpl(val firestore: FirebaseFirestore) : EventTypeRepo { - override suspend fun getSessionData(): Result> = + override suspend fun getSessionData(): FirebaseResult> = runCatching { val snapshots = firestore.collection("event_types") .orderBy("id", Query.Direction.ASCENDING) diff --git a/app/src/main/java/com/android254/droidconke19/repository/ReserveSeatRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/ReserveSeatRepo.kt index 9cccf05..4081e65 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/ReserveSeatRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/ReserveSeatRepo.kt @@ -1,30 +1,29 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.models.ReserveSeatModel import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestoreException import kotlinx.coroutines.tasks.await -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching interface ReserveSeatRepo { - suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): Result + suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult - suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): Result + suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult } class ReserveSeatRepoImpl(val firestore: FirebaseFirestore) : ReserveSeatRepo { - override suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): Result { + override suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult { return if (!isSeatReserved(reserveSeatModel)) { runCatching { firestore.collection("reserved_seats").add(reserveSeatModel).await() "Seat successfully reserved" } } else { - return Result.Error("Seat already reserved") + return FirebaseResult.Error("Seat already reserved") } } @@ -43,7 +42,7 @@ class ReserveSeatRepoImpl(val firestore: FirebaseFirestore) : ReserveSeatRepo { } - override suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): Result { + override suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult { return runCatching { val snapshot = firestore.collection("reserved_seats") .whereEqualTo("day_number", reserveSeatModel.day_number) diff --git a/app/src/main/java/com/android254/droidconke19/repository/RoomRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/RoomRepo.kt index 36e6711..f402bcf 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/RoomRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/RoomRepo.kt @@ -1,18 +1,18 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.RoomModel import com.android254.droidconke19.utils.await import com.google.firebase.firestore.FirebaseFirestore interface RoomRepo { - suspend fun getRoomDetails(roomId: Int): Result + suspend fun getRoomDetails(roomId: Int): FirebaseResult } class RoomRepoImpl(private val firestore: FirebaseFirestore) : RoomRepo { - override suspend fun getRoomDetails(roomId: Int): Result { + override suspend fun getRoomDetails(roomId: Int): FirebaseResult { return runCatching { val snapshot = firestore.collection("rooms") .whereEqualTo("id", roomId) diff --git a/app/src/main/java/com/android254/droidconke19/repository/SessionDataRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/SessionDataRepo.kt index 90e91a7..ff0b99e 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/SessionDataRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/SessionDataRepo.kt @@ -2,7 +2,7 @@ package com.android254.droidconke19.repository import com.android254.droidconke19.database.AppDatabase import com.android254.droidconke19.database.dao.SessionsDao -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.SessionsModel import com.google.firebase.firestore.FirebaseFirestore @@ -10,13 +10,13 @@ import com.google.firebase.firestore.FirebaseFirestoreException import kotlinx.coroutines.tasks.await interface SessionDataRepo { - suspend fun getSessionData(dayNumber: String, sessionId: Int): Result + suspend fun getSessionData(dayNumber: String, sessionId: Int): FirebaseResult - suspend fun getStarredSessions(userId: String): Result> + suspend fun getStarredSessions(userId: String): FirebaseResult> - suspend fun starrSession(dayNumber: String, sessionId: Int, userId: String, slug: String): Result + suspend fun starrSession(dayNumber: String, sessionId: Int, userId: String, slug: String): FirebaseResult - suspend fun unstarrSession(dayNumber: String, sessionId: Int, userId: String): Result + suspend fun unstarrSession(dayNumber: String, sessionId: Int, userId: String): FirebaseResult suspend fun clearStarredSessions(userId: String) } @@ -26,7 +26,7 @@ class SessionDataRepoImpl(db: AppDatabase, private val firestore: FirebaseFirest private val starredSessionCollection = "starred_sessions" - override suspend fun getSessionData(dayNumber: String, sessionId: Int): Result = + override suspend fun getSessionData(dayNumber: String, sessionId: Int): FirebaseResult = runCatching { val snapshot = firestore.collection(dayNumber) .whereEqualTo("id", sessionId) @@ -53,7 +53,7 @@ class SessionDataRepoImpl(db: AppDatabase, private val firestore: FirebaseFirest } } - override suspend fun getStarredSessions(userId: String): Result> = + override suspend fun getStarredSessions(userId: String): FirebaseResult> = runCatching { val snapshot = firestore.collection(starredSessionCollection) .whereEqualTo("user_id", userId) @@ -77,7 +77,7 @@ class SessionDataRepoImpl(db: AppDatabase, private val firestore: FirebaseFirest slugs } - override suspend fun starrSession(dayNumber: String, sessionId: Int, userId: String, slug: String): Result { + override suspend fun starrSession(dayNumber: String, sessionId: Int, userId: String, slug: String): FirebaseResult { return if(!isSessionStarred(dayNumber, sessionId, userId)) { runCatching { val data = hashMapOf( @@ -93,11 +93,11 @@ class SessionDataRepoImpl(db: AppDatabase, private val firestore: FirebaseFirest "Session added to favourites" } } else { - Result.Error("Session already starred") + FirebaseResult.Error("Session already starred") } } - override suspend fun unstarrSession(dayNumber: String, sessionId: Int, userId: String): Result = + override suspend fun unstarrSession(dayNumber: String, sessionId: Int, userId: String): FirebaseResult = runCatching { val snapshot = firestore.collection(starredSessionCollection) .whereEqualTo("day", dayNumber) diff --git a/app/src/main/java/com/android254/droidconke19/repository/SessionFeedbackRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/SessionFeedbackRepo.kt index dbc150e..94b9dd5 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/SessionFeedbackRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/SessionFeedbackRepo.kt @@ -1,18 +1,18 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.SessionsUserFeedback import com.google.firebase.firestore.FirebaseFirestore import kotlinx.coroutines.tasks.await interface SessionFeedbackRepo { - suspend fun sendFeedBack(userSessionFeedback: SessionsUserFeedback): Result + suspend fun sendFeedBack(userSessionFeedback: SessionsUserFeedback): FirebaseResult } class SessionFeedbackRepoImpl(private val firestore: FirebaseFirestore) : SessionFeedbackRepo { - override suspend fun sendFeedBack(userSessionFeedback: SessionsUserFeedback): Result = + override suspend fun sendFeedBack(userSessionFeedback: SessionsUserFeedback): FirebaseResult = runCatching { firestore.collection("session_feedback_2019") .add(userSessionFeedback) diff --git a/app/src/main/java/com/android254/droidconke19/repository/SessionsRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/SessionsRepo.kt index 9c0cac4..a6b4776 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/SessionsRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/SessionsRepo.kt @@ -1,7 +1,7 @@ package com.android254.droidconke19.repository import com.android254.droidconke19.database.AppDatabase -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.SessionsModel import com.android254.droidconke19.models.SpeakersModel @@ -11,12 +11,12 @@ import com.google.firebase.firestore.Query import com.google.firebase.firestore.ktx.toObjects interface SessionsRepo { - suspend fun getSessions(sessionDay: String): Result> + suspend fun getSessions(sessionDay: String): FirebaseResult> } class SessionsRepoImpl(db: AppDatabase, private val firestore: FirebaseFirestore) : SessionsRepo { - override suspend fun getSessions(sessionDay: String): Result> = + override suspend fun getSessions(sessionDay: String): FirebaseResult> = runCatching { val snapshots = firestore.collection(sessionDay) .orderBy("id", Query.Direction.ASCENDING) diff --git a/app/src/main/java/com/android254/droidconke19/repository/SpeakersRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/SpeakersRepo.kt index 8635e84..650c32f 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/SpeakersRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/SpeakersRepo.kt @@ -1,6 +1,6 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.android254.droidconke19.models.SpeakersModel import com.android254.droidconke19.utils.await @@ -8,12 +8,12 @@ import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.ktx.toObjects interface SpeakersRepo { - suspend fun getSpeakersInfo(speakerId: Int): Result> + suspend fun getSpeakersInfo(speakerId: Int): FirebaseResult> } class SpeakersRepoImpl(private val firestore: FirebaseFirestore) : SpeakersRepo { - override suspend fun getSpeakersInfo(speakerId: Int): Result> = + override suspend fun getSpeakersInfo(speakerId: Int): FirebaseResult> = runCatching { val snapshot = firestore.collection("speakers2019") .whereEqualTo("id", speakerId) diff --git a/app/src/main/java/com/android254/droidconke19/repository/UpdateTokenRepo.kt b/app/src/main/java/com/android254/droidconke19/repository/UpdateTokenRepo.kt index 23af027..720f4f3 100644 --- a/app/src/main/java/com/android254/droidconke19/repository/UpdateTokenRepo.kt +++ b/app/src/main/java/com/android254/droidconke19/repository/UpdateTokenRepo.kt @@ -1,17 +1,17 @@ package com.android254.droidconke19.repository -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.datastates.runCatching import com.google.firebase.firestore.FirebaseFirestore import kotlinx.coroutines.tasks.await interface UpdateTokenRepo { - suspend fun updateToken(userId: String, refreshToken: String): Result + suspend fun updateToken(userId: String, refreshToken: String): FirebaseResult } class UpdateTokenRepoImpl(val firestore: FirebaseFirestore) : UpdateTokenRepo { - override suspend fun updateToken(userId: String, refreshToken: String): Result = + override suspend fun updateToken(userId: String, refreshToken: String): FirebaseResult = runCatching { firestore.collection("users") .document(userId) diff --git a/build.gradle b/build.gradle index 731f8b0..570ea07 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:3.5.1' - classpath 'com.google.gms:google-services:4.3.2' + classpath 'com.google.gms:google-services:4.3.3' classpath 'io.fabric.tools:gradle:1.31.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version" From cb30952195306db412802a400e7dedc258068e99 Mon Sep 17 00:00:00 2001 From: Michael Bukachi Date: Sun, 24 Nov 2019 23:30:54 +0300 Subject: [PATCH 2/3] fix: Correct broken tests for AboutViewModel This commit addresses issues raised in #65 --- .../droidconke19/viewmodels/AboutViewModel.kt | 13 ++-- .../viewmodels/AboutViewModelTest.kt | 60 +++++++++---------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/android254/droidconke19/viewmodels/AboutViewModel.kt b/app/src/main/java/com/android254/droidconke19/viewmodels/AboutViewModel.kt index 06cb25c..dc701dc 100644 --- a/app/src/main/java/com/android254/droidconke19/viewmodels/AboutViewModel.kt +++ b/app/src/main/java/com/android254/droidconke19/viewmodels/AboutViewModel.kt @@ -7,6 +7,7 @@ import com.android254.droidconke19.datastates.Result import com.android254.droidconke19.models.AboutDetailsModel import com.android254.droidconke19.repository.AboutDetailsRepo import com.android254.droidconke19.utils.NonNullMediatorLiveData +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch class AboutViewModel(private val aboutDetailsRepo: AboutDetailsRepo) : ViewModel() { @@ -30,8 +31,8 @@ class AboutViewModel(private val aboutDetailsRepo: AboutDetailsRepo) : ViewModel fun getSponsorsError(): LiveData = sponsorsError - fun fetchAboutDetails(aboutType: String) { - viewModelScope.launch { + fun fetchAboutDetails(aboutType: String, scope: CoroutineScope = viewModelScope) { + scope.launch { when (val value = aboutDetailsRepo.getAboutDetails(aboutType)) { is Result.Success -> detailsStateMediatorLiveData.postValue(value.data) is Result.Error -> detailsError.postValue(value.exception) @@ -39,8 +40,8 @@ class AboutViewModel(private val aboutDetailsRepo: AboutDetailsRepo) : ViewModel } } - fun getOrganizers(aboutType: String) { - viewModelScope.launch { + fun getOrganizers(aboutType: String, scope: CoroutineScope = viewModelScope) { + scope.launch { when (val value = aboutDetailsRepo.getAboutDetails(aboutType)) { is Result.Success -> organizersMediatorLiveData.postValue(value.data) is Result.Error -> organizersError.postValue(value.exception) @@ -49,8 +50,8 @@ class AboutViewModel(private val aboutDetailsRepo: AboutDetailsRepo) : ViewModel } - fun getSponsors(aboutType: String) { - viewModelScope.launch { + fun getSponsors(aboutType: String, scope: CoroutineScope = viewModelScope) { + scope.launch { when (val value = aboutDetailsRepo.getAboutDetails(aboutType)) { is Result.Success -> sponsorsMediatorLiveData.postValue(value.data) is Result.Error -> sponsorsError.postValue(value.exception) diff --git a/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt b/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt index 6b260b5..a878f1f 100644 --- a/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt +++ b/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt @@ -3,7 +3,6 @@ package com.android254.droidconke19.viewmodels import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.android254.droidconke19.CoroutinesRule import com.android254.droidconke19.datastates.Result -import com.android254.droidconke19.observeOnce import com.android254.droidconke19.repository.AboutDetailsRepo import io.mockk.MockKAnnotations import io.mockk.coEvery @@ -11,6 +10,7 @@ import io.mockk.impl.annotations.InjectMockKs import io.mockk.impl.annotations.MockK import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ObsoleteCoroutinesApi +import kotlinx.coroutines.test.runBlockingTest import org.hamcrest.CoreMatchers.`is` import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.empty @@ -41,80 +41,76 @@ class AboutViewModelTest { } @Test - fun `test fetchAboutDetails`() { + fun `test fetchAboutDetails`() = runBlockingTest { coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList()) - aboutViewModel.fetchAboutDetails("value") + aboutViewModel.fetchAboutDetails("value", this) - aboutViewModel.getAboutDetailsResponse().observeOnce { - assertThat(it, `is`(empty())) - } + val response = aboutViewModel.getAboutDetailsResponse() + + assertThat(response.value, `is`(empty())) } @Test - fun `test fetchAboutDetails error `() { + fun `test fetchAboutDetails error `() = runBlockingTest { coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error") - aboutViewModel.fetchAboutDetails("value") + aboutViewModel.fetchAboutDetails("value", this) + + val error = aboutViewModel.getAboutDetailsError() - aboutViewModel.getAboutDetailsError().observeOnce { - assertThat(it, `is`("Some error")) - } + assertThat(error.value, `is`("Some error")) } @Test - fun `test getOrganizers`() { + fun `test getOrganizers`() = runBlockingTest { coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList()) - aboutViewModel.getOrganizers("value") - - aboutViewModel.getAboutDetailsResponse().observeOnce { - assertThat(it, `is`(empty())) - } + aboutViewModel.getOrganizers("value", this) + val response = aboutViewModel.getOrganizersResponse() + assertThat(response.value, `is`(empty())) } @Test - fun `test getOrganizers error `() { + fun `test getOrganizers error `() = runBlockingTest { coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error") - aboutViewModel.getOrganizers("value") + aboutViewModel.getOrganizers("value", this) - aboutViewModel.getAboutDetailsError().observeOnce { - assertThat(it, `is`("Some error")) - } + val error = aboutViewModel.getOrganizerError() + assertThat(error.value, `is`("Some error")) } @Test - fun `test getSponsors`() { + fun `test getSponsors`() = runBlockingTest { coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList()) - aboutViewModel.getSponsors("value") + aboutViewModel.getSponsors("value", this) - aboutViewModel.getAboutDetailsResponse().observeOnce { - assertThat(it, `is`(empty())) - } + val response = aboutViewModel.getSponsorsResponse() + assertThat(response.value, `is`(empty())) } @Test - fun `test getSponsors error `() { + fun `test getSponsors error `() = runBlockingTest { coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error") - aboutViewModel.getSponsors("value") + aboutViewModel.getSponsors("value", this) + + val error = aboutViewModel.getSponsorsError() - aboutViewModel.getAboutDetailsError().observeOnce { - assertThat(it, `is`("Some error")) - } + assertThat(error.value, `is`("Some error")) } } \ No newline at end of file From 6bbe7f8cb91473e3713bd02999640be6cac475d4 Mon Sep 17 00:00:00 2001 From: Michael Bukachi Date: Sun, 24 Nov 2019 23:33:13 +0300 Subject: [PATCH 3/3] fix: Correct broken tests for AboutViewModel This commit addresses issues raised in #65 --- .../droidconke19/viewmodels/AboutViewModelTest.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt b/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt index a878f1f..ea2984a 100644 --- a/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt +++ b/app/src/test/java/com/android254/droidconke19/viewmodels/AboutViewModelTest.kt @@ -2,7 +2,7 @@ package com.android254.droidconke19.viewmodels import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.android254.droidconke19.CoroutinesRule -import com.android254.droidconke19.datastates.Result +import com.android254.droidconke19.datastates.FirebaseResult import com.android254.droidconke19.repository.AboutDetailsRepo import io.mockk.MockKAnnotations import io.mockk.coEvery @@ -43,7 +43,7 @@ class AboutViewModelTest { @Test fun `test fetchAboutDetails`() = runBlockingTest { - coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList()) + coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns FirebaseResult.Success(emptyList()) aboutViewModel.fetchAboutDetails("value", this) @@ -56,7 +56,7 @@ class AboutViewModelTest { @Test fun `test fetchAboutDetails error `() = runBlockingTest { - coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error") + coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns FirebaseResult.Error("Some error") aboutViewModel.fetchAboutDetails("value", this) @@ -69,7 +69,7 @@ class AboutViewModelTest { @Test fun `test getOrganizers`() = runBlockingTest { - coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList()) + coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns FirebaseResult.Success(emptyList()) aboutViewModel.getOrganizers("value", this) @@ -80,7 +80,7 @@ class AboutViewModelTest { @Test fun `test getOrganizers error `() = runBlockingTest { - coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error") + coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns FirebaseResult.Error("Some error") aboutViewModel.getOrganizers("value", this) @@ -92,7 +92,7 @@ class AboutViewModelTest { @Test fun `test getSponsors`() = runBlockingTest { - coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList()) + coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns FirebaseResult.Success(emptyList()) aboutViewModel.getSponsors("value", this) @@ -104,7 +104,7 @@ class AboutViewModelTest { @Test fun `test getSponsors error `() = runBlockingTest { - coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error") + coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns FirebaseResult.Error("Some error") aboutViewModel.getSponsors("value", this)