127 lines
4.2 KiB
Kotlin
127 lines
4.2 KiB
Kotlin
package com.example.hpostesting
|
|
|
|
import android.content.Context
|
|
import android.content.SharedPreferences
|
|
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
|
import androidx.lifecycle.LiveData
|
|
import com.example.hpostesting.data.dao.HemoCubeDao
|
|
import com.example.hpostesting.data.model.Response
|
|
import com.example.hpostesting.data.model.patient.HemoCubeTestData
|
|
import com.example.hpostesting.data.model.patient.toHemoCubeTestData
|
|
import com.example.hpostesting.data.repository.DatabaseRepository
|
|
import com.example.hpostesting.data.repository.Repository
|
|
import com.example.hpostesting.presentation.hemocube.HemoCubeFragment
|
|
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
|
import com.example.hpostesting.presentation.hemocube.HemocubeActivity
|
|
import com.example.hpostesting.util.TestCoroutineRule
|
|
import `in`.sminnovations.hpostesting.databinding.FragmentHemoCubeReferenceBinding
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
import kotlinx.coroutines.test.advanceUntilIdle
|
|
import kotlinx.coroutines.test.runBlockingTest
|
|
import org.junit.Assert.assertEquals
|
|
import org.junit.Before
|
|
import org.junit.Rule
|
|
import org.junit.Test
|
|
import org.mockito.ArgumentMatchers.any
|
|
import org.mockito.ArgumentMatchers.anyString
|
|
import org.mockito.Mock
|
|
import org.mockito.Mockito.verify
|
|
import org.mockito.Mockito.`when`
|
|
import org.mockito.MockitoAnnotations
|
|
|
|
@ExperimentalCoroutinesApi
|
|
class HemoCubeViewModelTest {
|
|
|
|
// Add rule for testing LiveData
|
|
@get:Rule
|
|
val rule = InstantTaskExecutorRule()
|
|
|
|
// Add rule for testing coroutines
|
|
@get:Rule
|
|
val coroutineRule = TestCoroutineRule()
|
|
|
|
// Mock dependencies
|
|
@Mock
|
|
lateinit var hemoCubeDao: HemoCubeDao
|
|
|
|
@Mock
|
|
lateinit var repository: Repository
|
|
|
|
// Mock context
|
|
@Mock
|
|
lateinit var context: Context
|
|
|
|
// Mock LiveData for testing
|
|
@Mock
|
|
lateinit var networkStatusLiveData: LiveData<Boolean>
|
|
|
|
// Initialize the ViewModel
|
|
lateinit var viewModel: HemoCubeViewModel
|
|
|
|
private lateinit var hemoCubeFragment: HemoCubeFragment
|
|
|
|
@Before
|
|
fun setUp() {
|
|
MockitoAnnotations.initMocks(this)
|
|
// viewModel = HemoCubeViewModel(hemoCubeDao, repository, context)
|
|
hemoCubeFragment = HemoCubeFragment()
|
|
}
|
|
|
|
@Test
|
|
fun `uploadHemoCubeResultToDatabase with online status should call addResultTestToDb`() =
|
|
coroutineRule.runBlockingTest {
|
|
// Mock data and setup
|
|
val isOnline = true
|
|
val testStatus = true
|
|
val kitSerial = "12345"
|
|
|
|
// Mock the necessary methods
|
|
`when`(repository.addTestToDatabase(viewModel.testDetails!!))
|
|
.thenReturn(Response.Success("Success"))
|
|
|
|
// Call the function to be tested
|
|
viewModel.uploadHemoCubeResultToDatabase(isOnline, testStatus, kitSerial)
|
|
|
|
// Verify that addResultTestToDb is called
|
|
advanceUntilIdle()
|
|
assertEquals("Local", viewModel.fireBaseUpload.value)
|
|
}
|
|
|
|
// Similar tests can be written for other methods in HemoCubeViewModel
|
|
|
|
@Mock
|
|
private lateinit var mockSharedPreferences: SharedPreferences
|
|
|
|
@Mock
|
|
private lateinit var mockActivity: HemocubeActivity // Replace with your actual Activity class
|
|
|
|
@Mock
|
|
private lateinit var mockBinding: FragmentHemoCubeReferenceBinding // Replace with your actual Binding class
|
|
|
|
@Test
|
|
fun `findResult with valid input`() {
|
|
// Arrange
|
|
val validString = "valid string"
|
|
val fullReadOutput = "full read output"
|
|
`when`(mockSharedPreferences.getString(anyString(), anyString())).thenReturn("dummy_value")
|
|
`when`(mockActivity.runOnUiThread(any())).thenAnswer {
|
|
val runnable = it.getArgument(0, Runnable::class.java)
|
|
runnable.run()
|
|
}
|
|
|
|
// Act
|
|
val result = hemoCubeFragment.findResult(calculatedRatio = 0.06)
|
|
|
|
// Assert
|
|
// Add appropriate assertions based on the behavior you expect
|
|
// verify(mockSharedPreferences).edit()
|
|
// verify(mockBinding).btnSubmit.visibility = View.VISIBLE
|
|
// verify(mockBinding).btnSubmit.isEnabled = true
|
|
// Add more verifications as needed
|
|
|
|
assertEquals("Normal", result)
|
|
}
|
|
|
|
|
|
|
|
} |