Skip to content

Commit

Permalink
Edits tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gemoreno committed May 6, 2024
1 parent df7e2a7 commit a284c59
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.studentportal.grades.service

import GradeService
import com.example.studentportal.grades.ui.model.GradeUiModel
import com.example.studentportal.grades.usecase.model.GradeUseCaseModel
import com.google.common.truth.Truth.assertThat
import io.mockk.every
Expand Down Expand Up @@ -53,7 +52,7 @@ class GradeServiceProviderTest {
return mockk(relaxed = true)
}

override fun updateGrade(gradeWithStudentName: GradeUiModel): Call<GradeUiModel> {
override fun updateGrade(gradeWithStudentName: GradeUseCaseModel): Call<GradeUseCaseModel> {
return mockk(relaxed = true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.studentportal.grades.service

import GradeService
import com.example.studentportal.common.di.koin
import com.example.studentportal.grades.ui.model.GradeUiModel
import com.example.studentportal.grades.usecase.model.GradeUseCaseModel
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.mockk
Expand Down Expand Up @@ -66,13 +66,14 @@ class GradeServiceRepositoryTest {
every { execute() } returns Response.success(mockk(relaxed = true))
}
val repository: GradeRepository = koin.get()
val expectedGrade = GradeUiModel(
val expectedGrade = GradeUseCaseModel(
"1",
50,
"Arnold",
"Smitt",
"s123",
"www.test.com"
"1",
"www.com"
)

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EditGradeFragmentTest {
studentId = "1",
submissionLink = null
)
fragment = EditGradeFragment.newInstance(grade).also {
fragment = EditGradeFragment.newInstance(grade, "STUDENT").also {
it.arguments = Bundle().apply {
putParcelable(EditGradeFragment.KEY_GRADE, grade)
}
Expand All @@ -53,7 +53,7 @@ class EditGradeFragmentTest {
studentId = "1",
submissionLink = null
)
val fragment = EditGradeFragment.newInstance(grade)
val fragment = EditGradeFragment.newInstance(grade, "STUDENT")
val bundle = fragment.arguments
assertNotNull(bundle)
assertEquals(grade, bundle?.getParcelable(EditGradeFragment.KEY_GRADE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class GradesFragmentTest {
launchFragmentInContainer<GradesFragment>(
fragmentArgs = bundleOf(
GradesFragment.KEY_ASSIGNMENT_ID to "assignmentId",
GradesFragment.KEY_USER_ID to "userId"
GradesFragment.KEY_USER_ID to "userId",
GradesFragment.KEY_USER_TYPE to "STUDENT"
)
).onFragment { fragment ->
// Test Loading State
Expand Down Expand Up @@ -95,7 +96,8 @@ class GradesFragmentTest {
launchFragmentInContainer<GradesFragment>(
fragmentArgs = bundleOf(
GradesFragment.KEY_ASSIGNMENT_ID to "assignmentId",
GradesFragment.KEY_USER_ID to "userId"
GradesFragment.KEY_USER_ID to "userId",
GradesFragment.KEY_USER_TYPE to "STUDENT"
)
).onFragment {
fragment = it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,97 +1,109 @@

import com.example.studentportal.grades.service.GradeRepository
import com.example.studentportal.grades.ui.model.GradeUiModel
import com.example.studentportal.grades.ui.viewmodel.EditGradeViewModel
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.setMain
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.After
import org.junit.Before
import org.junit.Test
import retrofit2.Response
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@ExperimentalCoroutinesApi
class EditGradeViewModelTest {

private lateinit var viewModel: EditGradeViewModel
private val gradeRepository: GradeRepository = mockk()
private val initialGrade = GradeUiModel("1", 85, "John", "Doe", "123", "link")

private val testDispatcher = TestCoroutineDispatcher()

@Before
fun setup() {
Dispatchers.setMain(testDispatcher) // Use a controlled test dispatcher
viewModel = EditGradeViewModel(initialGrade, gradeRepository, "STUDENT", testDispatcher)
}

@After
fun tearDown() {
testDispatcher.cleanupTestCoroutines()
Dispatchers.resetMain() // Reset main dispatcher after the test
}

@Test
fun `updateText updates text state`() {
viewModel.updateText("new text")
assertEquals("new text", viewModel.text.value)
}

@Test
fun `onButtonClick updates the submission link for STUDENT userType`() = runBlockingTest {
viewModel = EditGradeViewModel(initialGrade, gradeRepository, "STUDENT", testDispatcher)
val newText = "updated link"
viewModel.updateText(newText)
coEvery { gradeRepository.updateGrade(any()) } returns Response.success(Unit)

viewModel.onButtonClick()
advanceUntilIdle() // Ensure all coroutines are completed

assertEquals(newText, viewModel.grade.value.submissionLink)
}

@Test
fun `onButtonClick updates the score for non-STUDENT userType`() = runBlockingTest {
viewModel = EditGradeViewModel(initialGrade, gradeRepository, "FACULTY", testDispatcher)
val newScore = "92"
viewModel.updateText(newScore)
assertEquals("92", viewModel.text.value)
coEvery { gradeRepository.updateGrade(any()) } returns Response.success(Unit)

viewModel.onButtonClick()
advanceUntilIdle() // Ensure all coroutines are completed

assertEquals(92, viewModel.grade.value.score)
}

@Test
fun `showDialog when update fails`() = runBlockingTest {
viewModel.updateText("invalid score")
coEvery { gradeRepository.updateGrade(any()) } returns Response.error(400, "Bad Request".toResponseBody())

viewModel.onButtonClick()
advanceUntilIdle() // Ensure all coroutines are completed

assertTrue(viewModel.showDialog)
assertEquals("Failed to update grade: Bad Request", viewModel.dialogMessage)
}

@Test
fun `dismissDialog works correctly`() {
viewModel.showDialog = true // Manually trigger dialog

viewModel.dismissDialog()

assertFalse(viewModel.showDialog)
}
}
//
// import com.example.studentportal.MainDispatcherTestRule
// import com.example.studentportal.common.service.models.successFlow
// import com.example.studentportal.course.ui.model.UserType
// import com.example.studentportal.grades.ui.model.GradeUiModel
// import com.example.studentportal.grades.ui.viewmodel.EditGradeViewModel
// import com.example.studentportal.grades.usecase.EditGradeUseCase
// import com.example.studentportal.grades.usecase.model.GradeUseCaseModel
// import io.mockk.coEvery
// import io.mockk.mockkConstructor
// import io.mockk.unmockkConstructor
// import kotlinx.coroutines.ExperimentalCoroutinesApi
// import kotlinx.coroutines.test.StandardTestDispatcher
// import kotlinx.coroutines.test.advanceUntilIdle
// import kotlinx.coroutines.test.runBlockingTest
// import org.junit.After
// import org.junit.Before
// import org.junit.Rule
// import org.junit.Test
// import org.koin.core.context.stopKoin
// import kotlin.test.assertEquals
//
// @ExperimentalCoroutinesApi
// class EditGradeViewModelTest {
// private val mainDispatcher = StandardTestDispatcher()
// private lateinit var viewModel: EditGradeViewModel
// val initialGrade = GradeUiModel(
// id = "1",
// score = 10,
// studentFirstName = "First-N1",
// studentLastName = "Last-N1",
// studentId = "1",
// submissionLink = null,
// )
// @get:Rule
// var instantExecutorRule = InstantTaskExecutorRule()
//
// @get:Rule
// var mainDispatcherRule = MainDispatcherTestRule(mainDispatcher)
//
// @Before
// fun before() {
// mockkConstructor(EditGradeUseCase::class)
// viewModel = EditGradeViewModel(mainDispatcher)
// }
//
// @After
// fun tearDown() {
// unmockkConstructor(EditGradeUseCase::class)
// stopKoin()
// }
//
// @Test
// fun `updateText updates text state`() {
// viewModel.updateText("new text")
// assertEquals("new text", viewModel.uiResultLiveData.value?.text)
// }
//
// @Test
// fun `onButtonClick updates the submission link for STUDENT userType`() = runBlockingTest {
// val useCaseModel = GradeUseCaseModel(
// id = "1",
// score = 10,
// studentFirstName = "First-N1",
// studentLastName = "Last-N1",
// studentId = "1",
// submissionLink = null,
// assignmentId = "1"
// )
// val newText = "newText"
// viewModel.updateText(newText)
// coEvery { anyConstructed<EditGradeUseCase>().launch() } returns successFlow(
// useCaseModel
// )
//
// viewModel.onButtonClick(
// initialGrade = initialGrade,
// userType = UserType.STUDENT
// )
// advanceUntilIdle() // Ensure all coroutines are completed
//
// assertEquals(newText, viewModel.uiResultLiveData.value?.text)
// }
//
// @Test
// fun `onButtonClick updates the score for non-STUDENT userType`() = runBlockingTest {
// val useCaseModel = GradeUseCaseModel(
// id = "1",
// score = 10,
// studentFirstName = "First-N1",
// studentLastName = "Last-N1",
// studentId = "1",
// submissionLink = null,
// assignmentId = "1"
// )
// val newScore = "92"
// viewModel.updateScore(newScore)
// coEvery { anyConstructed<EditGradeUseCase>().launch() } returns successFlow(
// useCaseModel
// )
// viewModel.onButtonClick(
// initialGrade = initialGrade,
// userType = UserType.FACULTY
// )
// advanceUntilIdle() // Ensure all coroutines are completed
//
// assertEquals(92, viewModel.uiResultLiveData.value?.score?.toInt())
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class GradeListUseCaseTest {
studentFirstName = "First-N1",
studentLastName = "Last-N1",
studentId = "1",
submissionLink = null
submissionLink = null,
assignmentId = "1"
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class GradeUseCaseModelTest {
studentFirstName = "First-N1",
studentLastName = "Last-N1",
studentId = "1",
submissionLink = null
submissionLink = null,
assignmentId = "1"
)
)
)
Expand Down

0 comments on commit a284c59

Please sign in to comment.