Skip to content

Commit

Permalink
fix tests (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
hryh27 authored Apr 12, 2023
1 parent 9fe15bd commit e1e1eec
Show file tree
Hide file tree
Showing 18 changed files with 647 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.raccoongang.course.presentation.container
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.raccoongang.core.system.connection.NetworkConnection
import com.raccoongang.core.R
import com.raccoongang.core.domain.model.CourseStructure
import com.raccoongang.core.domain.model.CoursewareAccess
import com.raccoongang.core.system.ResourceManager
import com.raccoongang.core.system.notifier.CourseNotifier
import com.raccoongang.core.system.notifier.CourseStructureUpdated
Expand Down Expand Up @@ -35,6 +37,30 @@ class CourseContainerViewModelTest {
private val noInternet = "Slow or no internet connection"
private val somethingWrong = "Something went wrong"

private val courseStructure = CourseStructure(
root = "",
blockData = listOf(),
id = "id",
name = "Course name",
number = "",
org = "Org",
start = null,
startDisplay = "",
startType = "",
end = null,
coursewareAccess = CoursewareAccess(
true,
"",
"",
"",
"",
""
),
media = null,
certificate = null,
isSelfPaced = false
)

@Before
fun setUp() {
Dispatchers.setMain(dispatcher)
Expand Down Expand Up @@ -84,21 +110,23 @@ class CourseContainerViewModelTest {
val viewModel = CourseContainerViewModel("", interactor,resourceManager, notifier, networkConnection)
every { networkConnection.isOnline() } returns true
coEvery { interactor.preloadCourseStructure(any()) } returns Unit
every { interactor.getCourseStructureFromCache() } returns courseStructure
viewModel.preloadCourseStructure()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.preloadCourseStructure(any()) }

assert(viewModel.errorMessage.value == null)
assert(viewModel.showProgress.value == false)
assert(viewModel.dataReady.value == true)
assert(viewModel.dataReady.value != null)
}

@Test
fun `preloadCourseStructure success without internet`() = runTest {
val viewModel = CourseContainerViewModel("", interactor,resourceManager, notifier, networkConnection)
every { networkConnection.isOnline() } returns false
coEvery { interactor.preloadCourseStructureFromCache(any()) } returns Unit
every { interactor.getCourseStructureFromCache() } returns courseStructure
viewModel.preloadCourseStructure()
advanceUntilIdle()

Expand All @@ -107,7 +135,7 @@ class CourseContainerViewModelTest {

assert(viewModel.errorMessage.value == null)
assert(viewModel.showProgress.value == false)
assert(viewModel.dataReady.value == true)
assert(viewModel.dataReady.value != null)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ class CourseDetailsViewModelTest {
CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseDetails(any()) } throws UnknownHostException()
coEvery { interactor.getEnrolledCourseById(any()) } throws UnknownHostException()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.getCourseDetails(any()) }
coVerify(exactly = 1) { interactor.getEnrolledCourseById(any()) }

val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage

Expand All @@ -73,11 +71,9 @@ class CourseDetailsViewModelTest {
CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseDetails(any()) } throws Exception()
coEvery { interactor.getEnrolledCourseById(any()) } throws Exception()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.getCourseDetails(any()) }
coVerify(exactly = 1) { interactor.getEnrolledCourseById(any()) }

val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage

Expand All @@ -91,12 +87,10 @@ class CourseDetailsViewModelTest {
CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier)
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseDetails(any()) } returns mockk()
coEvery { interactor.getEnrolledCourseById(any()) } returns mockk()

advanceUntilIdle()

coVerify(exactly = 1) { interactor.getCourseDetails(any()) }
coVerify(exactly = 1) { interactor.getEnrolledCourseById(any()) }

assert(viewModel.uiMessage.value == null)
assert(viewModel.uiState.value is CourseDetailsUIState.CourseData)
Expand All @@ -108,14 +102,11 @@ class CourseDetailsViewModelTest {
CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier)
every { networkConnection.isOnline() } returns false
coEvery { interactor.getCourseDetailsFromCache(any()) } returns mockk()
coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockk()

advanceUntilIdle()

coVerify(exactly = 0) { interactor.getCourseDetails(any()) }
coVerify(exactly = 0) { interactor.getEnrolledCourseById(any()) }
coVerify(exactly = 1) { interactor.getCourseDetailsFromCache(any()) }
coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) }

assert(viewModel.uiMessage.value == null)
assert(viewModel.uiState.value is CourseDetailsUIState.CourseData)
Expand All @@ -126,7 +117,6 @@ class CourseDetailsViewModelTest {
val viewModel =
CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier)
coEvery { interactor.enrollInACourse(any()) } throws UnknownHostException()
coEvery { interactor.getEnrolledCourseById(any()) } returns mockk()
coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseDetails(any()) } returns mockk()
Expand All @@ -147,7 +137,6 @@ class CourseDetailsViewModelTest {
val viewModel =
CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier)
coEvery { interactor.enrollInACourse(any()) } throws Exception()
coEvery { interactor.getEnrolledCourseById(any()) } returns mockk()
coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseDetails(any()) } returns mockk()
Expand All @@ -168,7 +157,6 @@ class CourseDetailsViewModelTest {
val viewModel =
CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier)
coEvery { interactor.enrollInACourse(any()) } returns Unit
coEvery { interactor.getEnrolledCourseById(any()) } returns mockk()
coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseDetails(any()) } returns mockk()
Expand All @@ -179,7 +167,6 @@ class CourseDetailsViewModelTest {
advanceUntilIdle()

coVerify(exactly = 1) { interactor.enrollInACourse(any()) }
coVerify(exactly = 2) { interactor.getEnrolledCourseById(any()) }

assert(viewModel.uiMessage.value == null)
assert(viewModel.uiState.value is CourseDetailsUIState.CourseData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,10 @@ class HandoutsViewModelTest {

private val interactor = mockk<CourseInteractor>()

//region mockEnrolledCourse

private val mockCourseEnrolled = EnrolledCourse(
auditAccessExpires = Date(),
created = "created",
certificate = Certificate(""),
mode = "mode",
isActive = true,
course = EnrolledCourseData(
id = "id",
name = "name",
number = "",
org = "Org",
start = Date(),
startDisplay = "",
startType = "",
end = null,
dynamicUpgradeDeadline = "",
subscriptionId = "",
coursewareAccess = CoursewareAccess(
true,
"",
"",
"",
"",
""
),
media = null,
courseImage = "",
courseAbout = "",
courseSharingUtmParameters = CourseSharingUtmParameters("", ""),
courseUpdates = "",
courseHandouts = "",
discussionUrl = "",
videoOutline = "",
isSelfPaced = false
)
)
//region mockHandoutsModel

private val handoutsModel = HandoutsModel("")

//endregion

@Before
Expand All @@ -80,36 +46,29 @@ class HandoutsViewModelTest {

@Test
fun `getEnrolledCourse no internet connection exception`() = runTest {
val viewModel = HandoutsViewModel("","Handouts", interactor)
coEvery { interactor.getEnrolledCourseFromCacheById(any()) } throws UnknownHostException()
coEvery { interactor.getAnnouncements(any()) } returns mockk()
val viewModel = HandoutsViewModel("", "Handouts", interactor)
coEvery { interactor.getHandouts(any()) } throws UnknownHostException()

advanceUntilIdle()
coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) }

assert(viewModel.htmlContent.value == null)
}

@Test
fun `getEnrolledCourse unknown exception`() = runTest {
val viewModel = HandoutsViewModel("","Handouts", interactor)
coEvery { interactor.getEnrolledCourseFromCacheById(any()) } throws Exception()
coEvery { interactor.getAnnouncements(any()) } returns mockk()

val viewModel = HandoutsViewModel("", "Handouts", interactor)
coEvery { interactor.getHandouts(any()) } throws Exception()
advanceUntilIdle()
coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) }

assert(viewModel.htmlContent.value == null)
}

@Test
fun `getEnrolledCourse handouts success`() = runTest {
val viewModel = HandoutsViewModel("",HandoutsType.Handouts.name, interactor)
coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockCourseEnrolled
val viewModel = HandoutsViewModel("", HandoutsType.Handouts.name, interactor)
coEvery { interactor.getHandouts(any()) } returns HandoutsModel("hello")

advanceUntilIdle()
coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) }
coVerify(exactly = 1) { interactor.getHandouts(any()) }
coVerify(exactly = 0) { interactor.getAnnouncements(any()) }

Expand All @@ -118,12 +77,15 @@ class HandoutsViewModelTest {

@Test
fun `getEnrolledCourse announcements success`() = runTest {
val viewModel = HandoutsViewModel("",HandoutsType.Announcements.name, interactor)
coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockCourseEnrolled
coEvery { interactor.getAnnouncements(any()) } returns listOf(AnnouncementModel("date","content"))
val viewModel = HandoutsViewModel("", HandoutsType.Announcements.name, interactor)
coEvery { interactor.getAnnouncements(any()) } returns listOf(
AnnouncementModel(
"date",
"content"
)
)

advanceUntilIdle()
coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) }
coVerify(exactly = 0) { interactor.getHandouts(any()) }
coVerify(exactly = 1) { interactor.getAnnouncements(any()) }

Expand All @@ -132,12 +94,19 @@ class HandoutsViewModelTest {

@Test
fun `injectDarkMode test`() = runTest {
val viewModel = HandoutsViewModel("",HandoutsType.Announcements.name, interactor)
coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockCourseEnrolled
coEvery { interactor.getAnnouncements(any()) } returns listOf(AnnouncementModel("date","content"))
viewModel.injectDarkMode(viewModel.htmlContent.value.toString(), ULong.MAX_VALUE, ULong.MAX_VALUE)
val viewModel = HandoutsViewModel("", HandoutsType.Announcements.name, interactor)
coEvery { interactor.getAnnouncements(any()) } returns listOf(
AnnouncementModel(
"date",
"content"
)
)
viewModel.injectDarkMode(
viewModel.htmlContent.value.toString(),
ULong.MAX_VALUE,
ULong.MAX_VALUE
)
advanceUntilIdle()
coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) }
coVerify(exactly = 0) { interactor.getHandouts(any()) }
coVerify(exactly = 1) { interactor.getAnnouncements(any()) }

Expand Down
Loading

0 comments on commit e1e1eec

Please sign in to comment.