Skip to content

Commit

Permalink
fix: Analytics Improvements + Update test cases (openedx#277)
Browse files Browse the repository at this point in the history
- Updated the analytics with iOS parity
- Updated test cases
  • Loading branch information
omerhabib26 authored Apr 1, 2024
1 parent 0a1bf3e commit cffbde3
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ enum class CourseAnalyticsEvent(val eventName: String, val biValue: String) {
),
PLS_BANNER_VIEWED(
"PLS:Banner Viewed",
"edx.bi.app.coursedates.pls_banner.viewed"
"edx.bi.app.dates.pls_banner.viewed"
),
PLS_SHIFT_BUTTON_CLICKED(
"PLS:Shift Button Clicked",
"edx.bi.app.dates.pls_banner.shift_dates.clicked"
),
PLS_SHIFT_DATES(
"PLS:Shift Dates",
"edx.bi.app.coursedates.pls_banner.shift_dates"
"edx.bi.app.dates.pls_banner.shift_dates"
),
DATES_CALENDAR_SYNC_TOGGLE(
"Dates:CalendarSync Toggle",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.openedx.core.system.notifier.CourseStructureUpdated
import org.openedx.course.data.storage.CoursePreferences
import org.openedx.course.domain.interactor.CourseInteractor
import org.openedx.course.presentation.CourseAnalytics
import org.openedx.course.presentation.CourseAnalyticsEvent
import org.openedx.course.presentation.calendarsync.CalendarManager
import java.net.UnknownHostException
import java.util.Date
Expand Down Expand Up @@ -136,12 +137,12 @@ class CourseContainerViewModelTest {
)
every { networkConnection.isOnline() } returns true
coEvery { interactor.preloadCourseStructure(any()) } throws UnknownHostException()
every { analytics.logEvent(any(), any()) } returns Unit
every { analytics.logEvent(CourseAnalyticsEvent.DASHBOARD.eventName, any()) } returns Unit
viewModel.preloadCourseStructure()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.preloadCourseStructure(any()) }
verify(exactly = 1) { analytics.logEvent(any(), any()) }
verify(exactly = 1) { analytics.logEvent(CourseAnalyticsEvent.DASHBOARD.eventName, any()) }

val message = viewModel.errorMessage.value
assertEquals(noInternet, message)
Expand All @@ -167,12 +168,12 @@ class CourseContainerViewModelTest {
)
every { networkConnection.isOnline() } returns true
coEvery { interactor.preloadCourseStructure(any()) } throws Exception()
every { analytics.logEvent(any(), any()) } returns Unit
every { analytics.logEvent(CourseAnalyticsEvent.DASHBOARD.eventName, any()) } returns Unit
viewModel.preloadCourseStructure()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.preloadCourseStructure(any()) }
verify(exactly = 1) { analytics.logEvent(any(), any()) }
verify(exactly = 1) { analytics.logEvent(CourseAnalyticsEvent.DASHBOARD.eventName, any()) }

val message = viewModel.errorMessage.value
assertEquals(somethingWrong, message)
Expand All @@ -199,12 +200,12 @@ class CourseContainerViewModelTest {
every { networkConnection.isOnline() } returns true
coEvery { interactor.preloadCourseStructure(any()) } returns Unit
every { interactor.getCourseStructureFromCache() } returns courseStructure
every { analytics.logEvent(any(), any()) } returns Unit
every { analytics.logEvent(CourseAnalyticsEvent.DASHBOARD.eventName, any()) } returns Unit
viewModel.preloadCourseStructure()
advanceUntilIdle()

coVerify(exactly = 1) { interactor.preloadCourseStructure(any()) }
verify(exactly = 1) { analytics.logEvent(any(), any()) }
verify(exactly = 1) { analytics.logEvent(CourseAnalyticsEvent.DASHBOARD.eventName, any()) }

assert(viewModel.errorMessage.value == null)
assert(viewModel.showProgress.value == false)
Expand Down Expand Up @@ -325,5 +326,4 @@ class CourseContainerViewModelTest {
assert(viewModel.errorMessage.value == null)
assert(viewModel.showProgress.value == false)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.openedx.core.system.notifier.CourseDashboardUpdate
import org.openedx.core.system.notifier.CourseNotifier
import org.openedx.course.domain.interactor.CourseInteractor
import org.openedx.course.presentation.CourseAnalytics
import org.openedx.course.presentation.CourseAnalyticsEvent
import java.net.UnknownHostException

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -239,14 +240,24 @@ class CourseDetailsViewModelTest {
coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit
every { networkConnection.isOnline() } returns true
coEvery { interactor.getCourseDetails(any()) } returns mockCourse
every { analytics.logEvent(any(), any()) } returns Unit
every {
analytics.logEvent(
CourseAnalyticsEvent.COURSE_ENROLL_CLICKED.eventName,
any()
)
} returns Unit


viewModel.enrollInACourse("", "")
advanceUntilIdle()

coVerify(exactly = 1) { interactor.enrollInACourse(any()) }
verify(exactly = 1) { analytics.logEvent(any(), any()) }
verify(exactly = 1) {
analytics.logEvent(
CourseAnalyticsEvent.COURSE_ENROLL_CLICKED.eventName,
any()
)
}

val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage
assertEquals(somethingWrong, message?.message)
Expand All @@ -267,7 +278,18 @@ class CourseDetailsViewModelTest {
)
every { config.isPreLoginExperienceEnabled() } returns false
every { preferencesManager.user } returns null
every { analytics.logEvent(any(), any()) } returns Unit
every {
analytics.logEvent(
CourseAnalyticsEvent.COURSE_ENROLL_CLICKED.eventName,
any()
)
} returns Unit
every {
analytics.logEvent(
CourseAnalyticsEvent.COURSE_ENROLL_SUCCESS.eventName,
any()
)
} returns Unit
coEvery { interactor.enrollInACourse(any()) } returns Unit
coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit
every { networkConnection.isOnline() } returns true
Expand All @@ -279,7 +301,18 @@ class CourseDetailsViewModelTest {
advanceUntilIdle()

coVerify(exactly = 1) { interactor.enrollInACourse(any()) }
verify(exactly = 2) { analytics.logEvent(any(), any()) }
verify(exactly = 1) {
analytics.logEvent(
CourseAnalyticsEvent.COURSE_ENROLL_CLICKED.eventName,
any()
)
}
verify(exactly = 1) {
analytics.logEvent(
CourseAnalyticsEvent.COURSE_ENROLL_SUCCESS.eventName,
any()
)
}

assert(viewModel.uiMessage.value == null)
assert(viewModel.uiState.value is CourseDetailsUIState.CourseData)
Expand Down Expand Up @@ -318,5 +351,4 @@ class CourseDetailsViewModelTest {
val count = overview.contains("black")
assert(!count)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import org.openedx.core.module.db.DownloadModelEntity
import org.openedx.core.module.db.DownloadedState
import org.openedx.core.module.db.FileType
import org.openedx.core.presentation.CoreAnalytics
import org.openedx.core.presentation.CoreAnalyticsEvent
import org.openedx.core.system.ResourceManager
import org.openedx.core.system.connection.NetworkConnection
import org.openedx.core.system.notifier.CourseNotifier
Expand Down Expand Up @@ -439,7 +440,12 @@ class CourseOutlineViewModelTest {
every { interactor.getCourseStructureFromCache() } returns courseStructure
every { networkConnection.isWifiConnected() } returns true
every { networkConnection.isOnline() } returns true
every { coreAnalytics.logEvent(any(), any()) } returns Unit
every {
coreAnalytics.logEvent(
CoreAnalyticsEvent.VIDEO_DOWNLOAD_SUBSECTION.eventName,
any()
)
} returns Unit
coEvery { workerController.saveModels(any()) } returns Unit
coEvery { interactor.getCourseStatus(any()) } returns CourseComponentStatus("id")
coEvery { downloadDao.readAllData() } returns flow { emit(emptyList()) }
Expand All @@ -461,7 +467,12 @@ class CourseOutlineViewModelTest {

viewModel.saveDownloadModels("", "")
advanceUntilIdle()
verify(exactly = 1) { coreAnalytics.logEvent(any(), any()) }
verify(exactly = 1) {
coreAnalytics.logEvent(
CoreAnalyticsEvent.VIDEO_DOWNLOAD_SUBSECTION.eventName,
any()
)
}

assert(viewModel.uiMessage.value == null)
}
Expand Down Expand Up @@ -530,5 +541,4 @@ class CourseOutlineViewModelTest {
assert(viewModel.uiMessage.value != null)
assert(!viewModel.hasInternetConnection)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.*
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import org.junit.Rule
Expand All @@ -24,6 +28,7 @@ import org.openedx.core.system.notifier.CourseNotifier
import org.openedx.core.system.notifier.CourseVideoPositionChanged
import org.openedx.course.data.repository.CourseRepository
import org.openedx.course.presentation.CourseAnalytics
import org.openedx.course.presentation.CourseAnalyticsEvent

@OptIn(ExperimentalCoroutinesApi::class)
class VideoUnitViewModelTest {
Expand Down Expand Up @@ -66,7 +71,12 @@ class VideoUnitViewModelTest {
any()
)
} throws Exception()
every { courseAnalytics.logEvent(any(), any()) } returns Unit
every {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
} returns Unit
viewModel.markBlockCompleted("", "")
advanceUntilIdle()

Expand All @@ -76,7 +86,12 @@ class VideoUnitViewModelTest {
any()
)
}
verify(exactly = 1) { courseAnalytics.logEvent(any(), any()) }
verify(exactly = 1) {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
}
}

@Test
Expand All @@ -95,7 +110,12 @@ class VideoUnitViewModelTest {
any()
)
} returns Unit
every { courseAnalytics.logEvent(any(), any()) } returns Unit
every {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
} returns Unit
viewModel.markBlockCompleted("", "")
advanceUntilIdle()

Expand All @@ -105,7 +125,12 @@ class VideoUnitViewModelTest {
any()
)
}
verify(exactly = 1) { courseAnalytics.logEvent(any(), any()) }
verify(exactly = 1) {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package org.openedx.course.presentation.unit.video

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import org.openedx.core.system.notifier.CourseNotifier
import org.openedx.core.system.notifier.CourseVideoPositionChanged
import org.openedx.course.data.repository.CourseRepository
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.*
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.system.notifier.CourseNotifier
import org.openedx.core.system.notifier.CourseVideoPositionChanged
import org.openedx.course.data.repository.CourseRepository
import org.openedx.course.presentation.CourseAnalytics
import org.openedx.course.presentation.CourseAnalyticsEvent

@OptIn(ExperimentalCoroutinesApi::class)
class VideoViewModelTest {
Expand All @@ -45,7 +50,8 @@ class VideoViewModelTest {

@Test
fun `sendTime test`() = runTest {
val viewModel = VideoViewModel("", courseRepository, notifier, preferenceManager, courseAnalytics)
val viewModel =
VideoViewModel("", courseRepository, notifier, preferenceManager, courseAnalytics)
coEvery { notifier.send(CourseVideoPositionChanged("", 0, false)) } returns Unit
viewModel.sendTime()
advanceUntilIdle()
Expand All @@ -55,14 +61,20 @@ class VideoViewModelTest {

@Test
fun `markBlockCompleted exception`() = runTest {
val viewModel = VideoViewModel("", courseRepository, notifier, preferenceManager, courseAnalytics)
val viewModel =
VideoViewModel("", courseRepository, notifier, preferenceManager, courseAnalytics)
coEvery {
courseRepository.markBlocksCompletion(
any(),
any()
)
} throws Exception()
every { courseAnalytics.logEvent(any(), any()) } returns Unit
every {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
} returns Unit
viewModel.markBlockCompleted("", "")
advanceUntilIdle()

Expand All @@ -72,20 +84,31 @@ class VideoViewModelTest {
any()
)
}
verify(exactly = 1) { courseAnalytics.logEvent(any(), any()) }
verify(exactly = 1) {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
}

}

@Test
fun `markBlockCompleted success`() = runTest {
val viewModel = VideoViewModel("", courseRepository, notifier, preferenceManager, courseAnalytics)
val viewModel =
VideoViewModel("", courseRepository, notifier, preferenceManager, courseAnalytics)
coEvery {
courseRepository.markBlocksCompletion(
any(),
any()
)
} returns Unit
every { courseAnalytics.logEvent(any(), any()) } returns Unit
every {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
} returns Unit
viewModel.markBlockCompleted("", "")
advanceUntilIdle()

Expand All @@ -95,7 +118,11 @@ class VideoViewModelTest {
any()
)
}
verify(exactly = 1) { courseAnalytics.logEvent(any(), any()) }
verify(exactly = 1) {
courseAnalytics.logEvent(
CourseAnalyticsEvent.VIDEO_COMPLETED.eventName,
any()
)
}
}

}
}

0 comments on commit cffbde3

Please sign in to comment.