Merge branch 'unit-tests' into 'preprod'
add setup for two unit tests See merge request sminnovations/hpos!14
This commit is contained in:
@@ -80,6 +80,12 @@ dependencies {
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
// Mockito dependencies
|
||||
testImplementation 'org.mockito:mockito-core:3.12.4'
|
||||
androidTestImplementation 'org.mockito:mockito-android:3.12.4'
|
||||
androidTestImplementation 'org.mockito:mockito-inline:3.12.4'
|
||||
testImplementation "androidx.arch.core:core-testing:2.2.0"
|
||||
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
|
||||
|
||||
|
||||
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
|
||||
|
||||
@@ -15,13 +15,14 @@ import com.google.firebase.ktx.Firebase
|
||||
import com.google.firebase.storage.ktx.storage
|
||||
import kotlinx.coroutines.tasks.await
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
class DatabaseRepository : Repository {
|
||||
class DatabaseRepository @Inject constructor() : Repository {
|
||||
|
||||
private val db: FirebaseFirestore = Firebase.firestore
|
||||
private val storage = Firebase.storage
|
||||
|
||||
suspend fun addTestToDatabase(data: HemoCubeTestData?): Response<String> {
|
||||
override suspend fun addTestToDatabase(data: HemoCubeTestData?): Response<String> {
|
||||
return try {
|
||||
val userdata = db.collection("patientData").whereEqualTo("_id", data!!._id).get().await()
|
||||
if (userdata.documents.isNotEmpty()) {
|
||||
@@ -36,7 +37,7 @@ class DatabaseRepository : Repository {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addTestToDatabase(data: UserData?): Response<String> {
|
||||
override suspend fun addTestToDatabase(data: UserData?): Response<String> {
|
||||
return try {
|
||||
val userdata = db.collection("patientData").whereEqualTo("_id", data!!._id).get().await()
|
||||
if (userdata.documents.isNotEmpty()) {
|
||||
@@ -52,7 +53,7 @@ class DatabaseRepository : Repository {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addTestToDatabaseforBufferCheck(data: BufferCheckData?): Response<String> {
|
||||
override suspend fun addTestToDatabaseforBufferCheck(data: BufferCheckData?): Response<String> {
|
||||
return try {
|
||||
db.collection("buffers").add(data!!).await()
|
||||
Response.Success(data!!.kitno)
|
||||
@@ -62,7 +63,7 @@ class DatabaseRepository : Repository {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addDiagnostics(data: DiagnosticsData?): Response<String> {
|
||||
override suspend fun addDiagnostics(data: DiagnosticsData?): Response<String> {
|
||||
return try {
|
||||
db.collection("diagnostics").add(data!!).await()
|
||||
Response.Success(data!!.deviceId)
|
||||
@@ -72,7 +73,7 @@ class DatabaseRepository : Repository {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun uploadFileToStorage(patientID: String, filePath: String): Response<Boolean> {
|
||||
override suspend fun uploadFileToStorage(patientID: String, filePath: String): Response<Boolean> {
|
||||
try {
|
||||
|
||||
val file = Uri.fromFile(File(filePath))
|
||||
@@ -131,7 +132,7 @@ class DatabaseRepository : Repository {
|
||||
return db.collection("devices").get().await().toObjects(DeviceData::class.java)
|
||||
}
|
||||
|
||||
suspend fun getDeviceDataById(deviceId: String): DeviceData? {
|
||||
override suspend fun getDeviceDataById(deviceId: String): DeviceData? {
|
||||
val querySnapshot = db.collection("devices").get().await()
|
||||
val allDeviceDataList = querySnapshot.toObjects(DeviceData::class.java)
|
||||
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
package com.example.hpostesting.data.repository
|
||||
|
||||
import com.example.hpostesting.data.model.Response
|
||||
import com.example.hpostesting.data.model.diagnostics.DiagnosticsData
|
||||
import com.example.hpostesting.data.model.patient.BufferCheckData
|
||||
import com.example.hpostesting.data.model.patient.DeviceData
|
||||
import com.example.hpostesting.data.model.patient.HemoCubeTestData
|
||||
import com.example.hpostesting.data.model.patient.UserData
|
||||
|
||||
interface Repository {
|
||||
// suspend fun addToDatabase(data: PatientDetails)
|
||||
suspend fun addTestToDatabase(data: HemoCubeTestData?): Response<String>
|
||||
|
||||
suspend fun addTestToDatabase(data: UserData?): Response<String>
|
||||
|
||||
suspend fun addTestToDatabaseforBufferCheck(data: BufferCheckData?): Response<String>
|
||||
|
||||
suspend fun addDiagnostics(data: DiagnosticsData?): Response<String>
|
||||
|
||||
suspend fun uploadFileToStorage(patientID: String, filePath: String): Response<Boolean>
|
||||
|
||||
suspend fun getDeviceDataById(deviceId: String): DeviceData?
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.example.hpostesting.data.dao.UserDao
|
||||
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
||||
import com.example.hpostesting.data.repository.DatabaseRepository
|
||||
import com.example.hpostesting.data.repository.LocalFileRepository
|
||||
import com.example.hpostesting.data.repository.Repository
|
||||
import com.example.hpostesting.domain.SaveRawData
|
||||
import com.example.hpostesting.domain.SaveRawDataTest
|
||||
import dagger.Module
|
||||
@@ -69,4 +70,10 @@ object AppModule {
|
||||
fun provideDatabaseRepository(): DatabaseRepository {
|
||||
return DatabaseRepository()
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRepository(): Repository {
|
||||
return DatabaseRepository()
|
||||
}
|
||||
}
|
||||
@@ -539,7 +539,7 @@ class HemoCubeFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun findResult(calculatedRatio: Double?): String {
|
||||
fun findResult(calculatedRatio: Double?): String {
|
||||
try {
|
||||
hemoCubeViewModel.messages.postValue("result classification")
|
||||
if (calculatedRatio != null) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.example.hpostesting.data.model.patient.HemoCubeTestData
|
||||
import com.example.hpostesting.data.model.patient.UserData
|
||||
import com.example.hpostesting.data.model.patient.toHemoCubeTestData
|
||||
import com.example.hpostesting.data.repository.DatabaseRepository
|
||||
import com.example.hpostesting.data.repository.Repository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.SimpleDateFormat
|
||||
@@ -31,7 +32,7 @@ import javax.inject.Inject
|
||||
@HiltViewModel
|
||||
class HemoCubeViewModel @Inject constructor(
|
||||
private val hemoCubeDao: HemoCubeDao,
|
||||
private val repository: DatabaseRepository,
|
||||
private val repository: Repository,
|
||||
context: Context,
|
||||
) : ViewModel() {
|
||||
var isServiceConnected = false
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user