add two test

This commit is contained in:
Pritimay Sarkar
2023-11-17 14:48:53 +05:30
parent 0a2f1ba2fd
commit 7f2cafeef4
9 changed files with 280 additions and 93 deletions

View File

@@ -0,0 +1,127 @@
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)
}
}

View File

@@ -11,91 +11,91 @@ import java.math.RoundingMode
import java.text.DecimalFormat
class TestRightViewModelTest {
private val viewModel = TestRightViewModel()
private val data = InputData()
@Test
fun test_mapDeviceConstants() {
viewModel.mapDeviceConstants(data.inputRead)
assertEquals("0", DataHolder.deviceConstant!!.a)
assertEquals("1.69989422e-06", DataHolder.deviceConstant!!.b)
assertEquals("1.60642711e-01", DataHolder.deviceConstant!!.c)
assertEquals("3.85754470e+02", DataHolder.deviceConstant!!.d)
}
@Test
fun test_mapPixelNumberToWavelength() {
viewModel.mapDeviceConstants(data.inputRead)
viewModel.mapPixelNumberToWavelength()
val outputList = TestDataGenerator().getOutputMapPixelNumberToWavelength()
assertEquals(Constants.TEST_RIGHT_TOTAL_PIXEL, DataHolder.wavelengthToPixelArray.size)
assertEquals(Constants.TEST_RIGHT_TOTAL_PIXEL, outputList.size)
val df = DecimalFormat("#.###")
df.roundingMode = RoundingMode.FLOOR
for (i in 0 until Constants.TEST_RIGHT_TOTAL_PIXEL){
assertEquals(df.format(outputList[i]), df.format(DataHolder.wavelengthToPixelArray[i]))
}
}
// private val viewModel = TestRightViewModel()
// private val data = InputData()
//
// @Test
// fun test_mapIntensityValues() {
// val inputReference = TestDataGenerator().getInputReferenceMapIntensityValues()
// val inputSample = TestDataGenerator().getInputSampleMapIntensityValues()
//
// viewModel.mapIntensityValues(inputReference, true)
// viewModel.mapIntensityValues(inputSample, false)
//
// val outputReference = TestDataGenerator().getOutputReferenceMapIntensityValues()
// val outputSample = TestDataGenerator().getOutputSampleMapIntensityValues()
//
// assertEquals(outputReference, DataHolder.intensityReferenceArray)
// assertEquals(outputSample, viewModel.intensitySampleArray)
// fun test_mapDeviceConstants() {
// viewModel.mapDeviceConstants(data.inputRead)
// assertEquals("0", DataHolder.deviceConstant!!.a)
// assertEquals("1.69989422e-06", DataHolder.deviceConstant!!.b)
// assertEquals("1.60642711e-01", DataHolder.deviceConstant!!.c)
// assertEquals("3.85754470e+02", DataHolder.deviceConstant!!.d)
// }
//
// @Test
// fun test_mapPixelNumberToWavelength() {
// viewModel.mapDeviceConstants(data.inputRead)
// viewModel.mapPixelNumberToWavelength()
//
// val outputList = TestDataGenerator().getOutputMapPixelNumberToWavelength()
//
// assertEquals(Constants.TEST_RIGHT_TOTAL_PIXEL, DataHolder.wavelengthToPixelArray.size)
// assertEquals(Constants.TEST_RIGHT_TOTAL_PIXEL, outputList.size)
//
// val df = DecimalFormat("#.###")
// df.roundingMode = RoundingMode.FLOOR
//
// for (i in 0 until Constants.TEST_RIGHT_TOTAL_PIXEL){
// assertEquals(df.format(outputList[i]), df.format(DataHolder.wavelengthToPixelArray[i]))
// }
//
// }
//
//// @Test
//// fun test_mapIntensityValues() {
//// val inputReference = TestDataGenerator().getInputReferenceMapIntensityValues()
//// val inputSample = TestDataGenerator().getInputSampleMapIntensityValues()
////
//// viewModel.mapIntensityValues(inputReference, true)
//// viewModel.mapIntensityValues(inputSample, false)
////
//// val outputReference = TestDataGenerator().getOutputReferenceMapIntensityValues()
//// val outputSample = TestDataGenerator().getOutputSampleMapIntensityValues()
////
//// assertEquals(outputReference, DataHolder.intensityReferenceArray)
//// assertEquals(outputSample, viewModel.intensitySampleArray)
//// }
//
// @Test
// fun test_mapWavelengthToAbsorbance() {
//
// viewModel.mapDeviceConstants(data.inputRead)
// viewModel.mapPixelNumberToWavelength()
// viewModel.mapIntensityValues(TestDataGenerator().getInputReferenceMapIntensityValues(), true)
// viewModel.mapIntensityValues(TestDataGenerator().getInputSampleMapIntensityValues(), false)
// viewModel.patientDetails = PatientData("Surya", 2, "Male", TestRightResultType.UNDEFINED)
// viewModel.mapWavelengthToAbsorbance()
//
// val wavelengthList = TestDataGenerator().getOutputMapPixelNumberToWavelength()
// val absorbanceList = TestDataGenerator().getOutputMapWavelengthToAbsorbance()
//
// val df = DecimalFormat("#.###")
// df.roundingMode = RoundingMode.FLOOR
//
// assertEquals(Constants.TEST_RIGHT_TOTAL_PIXEL, viewModel.wavelengthToAbsorbance.size)
// for (i in 0 until Constants.TEST_RIGHT_TOTAL_PIXEL){
// assertEquals(df.format(wavelengthList[i]), df.format(viewModel.wavelengthToAbsorbance[i][0]))
// assertEquals(df.format(absorbanceList[i]), df.format(viewModel.wavelengthToAbsorbance[i][1]))
// }
// }
//
//
// @Test
// fun testRightViewModel_calculateDataForCSV() {
//
// viewModel.mapDeviceConstants(data.inputRead)
// viewModel.mapPixelNumberToWavelength()
// viewModel.mapIntensityValues(data.printForReference, true)
// viewModel.mapIntensityValues(data.printForSample, false)
//// viewModel.patientDetails = PatientData("Surya", 2, "Male", null)
//// viewModel.calculateResults()
// viewModel.mapWavelengthToAbsorbance()
//
// for (each in viewModel.wavelengthToAbsorbance){
// println(each[0].toString() + " -> " + each[1])
// }
// }
@Test
fun test_mapWavelengthToAbsorbance() {
viewModel.mapDeviceConstants(data.inputRead)
viewModel.mapPixelNumberToWavelength()
viewModel.mapIntensityValues(TestDataGenerator().getInputReferenceMapIntensityValues(), true)
viewModel.mapIntensityValues(TestDataGenerator().getInputSampleMapIntensityValues(), false)
viewModel.patientDetails = PatientData("Surya", 2, "Male", TestRightResultType.UNDEFINED)
viewModel.mapWavelengthToAbsorbance()
val wavelengthList = TestDataGenerator().getOutputMapPixelNumberToWavelength()
val absorbanceList = TestDataGenerator().getOutputMapWavelengthToAbsorbance()
val df = DecimalFormat("#.###")
df.roundingMode = RoundingMode.FLOOR
assertEquals(Constants.TEST_RIGHT_TOTAL_PIXEL, viewModel.wavelengthToAbsorbance.size)
for (i in 0 until Constants.TEST_RIGHT_TOTAL_PIXEL){
assertEquals(df.format(wavelengthList[i]), df.format(viewModel.wavelengthToAbsorbance[i][0]))
assertEquals(df.format(absorbanceList[i]), df.format(viewModel.wavelengthToAbsorbance[i][1]))
}
}
@Test
fun testRightViewModel_calculateDataForCSV() {
viewModel.mapDeviceConstants(data.inputRead)
viewModel.mapPixelNumberToWavelength()
viewModel.mapIntensityValues(data.printForReference, true)
viewModel.mapIntensityValues(data.printForSample, false)
// viewModel.patientDetails = PatientData("Surya", 2, "Male", null)
// viewModel.calculateResults()
viewModel.mapWavelengthToAbsorbance()
for (each in viewModel.wavelengthToAbsorbance){
println(each[0].toString() + " -> " + each[1])
}
}
}

View File

@@ -0,0 +1,28 @@
package com.example.hpostesting.util
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
@ExperimentalCoroutinesApi
class TestCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
private val testCoroutineDispatcher = TestCoroutineDispatcher()
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(testCoroutineDispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
cleanupTestCoroutines()
}
}