add unit tests
This commit is contained in:
67
app/src/test/java/com/example/hpostesting/CsvWriterTest.kt
Normal file
67
app/src/test/java/com/example/hpostesting/CsvWriterTest.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
package com.example.hpostesting
|
||||
|
||||
import android.content.Context
|
||||
import com.example.hpostesting.data.CsvWriter
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.NONE)
|
||||
class CsvWriterTest {
|
||||
|
||||
private lateinit var csvWriter: CsvWriter
|
||||
private lateinit var context: Context
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
context = androidx.test.core.app.ApplicationProvider.getApplicationContext()
|
||||
csvWriter = CsvWriter(context)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun writeCsv_Success() {
|
||||
// Arrange
|
||||
val fileName = "test.csv"
|
||||
val data = listOf(
|
||||
arrayOf("1", "A", "1990", "Positive", "2024-02-04", "image_url1", "John Doe"),
|
||||
arrayOf("2", "B", "1985", "Negative", "2024-02-05", "image_url2", "Jane Doe")
|
||||
)
|
||||
|
||||
// Act
|
||||
val result = csvWriter.writeCsv(fileName, data)
|
||||
|
||||
// Assert
|
||||
Assert.assertTrue(result)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun writeCsv_Failure() {
|
||||
// // Arrange
|
||||
// val fileName = "test.csv"
|
||||
// val data = listOf(
|
||||
// arrayOf("1", "A", "1990", "Positive", "2024-02-04", "image_url1", "John Doe"),
|
||||
// arrayOf("2", "B", "1985", "Negative", "2024-02-05", "image_url2", "Jane Doe")
|
||||
// )
|
||||
//
|
||||
// // Mocking Environment.getExternalStorageDirectory()
|
||||
// val mockFile = Mockito.mock(File::class.java)
|
||||
// Mockito.`when`(Environment.getExternalStorageDirectory()).thenReturn(mockFile)
|
||||
// Mockito.`when`(mockFile.exists()).thenReturn(true)
|
||||
//
|
||||
// // Mocking FileWriter to simulate IOException
|
||||
// val mockWriter = Mockito.mock(FileWriter::class.java)
|
||||
// Mockito.`when`(mockWriter.write(Mockito.anyString())).thenThrow(IOException::class.java)
|
||||
// Mockito.`when`(mockFile.absolutePath).thenReturn("/fake/path/to/file.csv")
|
||||
// Mockito.`when`(FileWriter(mockFile)).thenReturn(mockWriter)
|
||||
//
|
||||
// // Act
|
||||
// val result = csvWriter.writeCsv(fileName, data)
|
||||
//
|
||||
// // Assert
|
||||
// Assert.assertFalse("Expected writeCsv to fail", result)
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
package com.example.hpostesting
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.example.hpostesting.presentation.trueheme.TrueHemeFragment
|
||||
import junit.framework.TestCase
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.ArgumentMatchers
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.MockitoAnnotations
|
||||
|
||||
class TrueHemeFragmentTest {
|
||||
|
||||
@Mock
|
||||
lateinit var mockContext: Context
|
||||
|
||||
@Mock
|
||||
private lateinit var mockSharedPreferences: SharedPreferences
|
||||
|
||||
private lateinit var fragment: TrueHemeFragment
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
fragment = TrueHemeFragment()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId to get device id`() {
|
||||
// Arrange
|
||||
Mockito.`when`(
|
||||
mockSharedPreferences.getString(
|
||||
ArgumentMatchers.anyString(),
|
||||
ArgumentMatchers.anyString()
|
||||
)
|
||||
).thenReturn("dummy_value")
|
||||
|
||||
// Act
|
||||
val deviceId = fragment.extractV2HardwareId("SNS HPP1-9000 SNE")
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HPP1-9000", deviceId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `updateDeviceId in shared pref`() {
|
||||
// Arrange
|
||||
Mockito.`when`(
|
||||
mockSharedPreferences.getString(
|
||||
ArgumentMatchers.anyString(),
|
||||
ArgumentMatchers.anyString()
|
||||
)
|
||||
).thenReturn("HPP1-0001")
|
||||
|
||||
// Act
|
||||
val deviceId = mockSharedPreferences.getString(
|
||||
ArgumentMatchers.anyString(),
|
||||
ArgumentMatchers.anyString()
|
||||
)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HPP1-0001", deviceId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `allReadingsComplete check`() {
|
||||
// Arrange
|
||||
val repeatReadingCount = 1
|
||||
val readingsPerSample = 1
|
||||
|
||||
// Act
|
||||
val result = fragment.allReadingsComplete(repeatReadingCount, readingsPerSample)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals(true, result)
|
||||
TestCase.assertEquals(fragment.allReadingsComplete(0, 1), false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV1HardwareId should return hardware ID when input contains SN`() {
|
||||
// Arrange
|
||||
val input = "Some text SN ABC123 some more text"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV1HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("ABC123", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV1HardwareId should return null when input does not contain SN`() {
|
||||
// Arrange
|
||||
val input = "Some text without SN"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV1HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertNull(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV1HardwareId should return null when input is empty`() {
|
||||
// Arrange
|
||||
val input = ""
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV1HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertNull(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV1HardwareId should return null when input is null`() {
|
||||
// Arrange
|
||||
val input: String? = null
|
||||
|
||||
// Act
|
||||
val result = input?.let { fragment.extractV1HardwareId(it) }
|
||||
|
||||
// Assert
|
||||
TestCase.assertNull(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV1HardwareId should return hardware ID when input contains SN in a specific format`() {
|
||||
// Arrange
|
||||
val input = """
|
||||
SN HCV-000-3001
|
||||
#BS
|
||||
#BC
|
||||
#SS
|
||||
#SC
|
||||
RESULT
|
||||
LB1 20636.32
|
||||
LB2 15855.67
|
||||
LB3 21801.36
|
||||
LB4 18362.33
|
||||
LS1 17287
|
||||
LS2 14855.67
|
||||
LS3 15282.31
|
||||
LS4 9737.98
|
||||
REND
|
||||
""".trimIndent()
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV1HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HCV-000-3001", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should return the correct hardware ID when it exists in the input`() {
|
||||
// Arrange
|
||||
val input = "SNS ABC123 SNE"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("ABC123", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should return null when no hardware ID is found in the input`() {
|
||||
// Arrange
|
||||
val input = "No hardware ID in this input"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertNull(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should handle whitespace around the hardware ID`() {
|
||||
// Arrange
|
||||
val input = "SNS XYZ789 SNE"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("XYZ789", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should handle provided input string with HCV-000-3013`() {
|
||||
// Arrange
|
||||
val input = "SNS HCV-000-3013 SNE\n" +
|
||||
"#SS1\n" +
|
||||
"#SC1\n" +
|
||||
"RESULT \n" +
|
||||
"LB1 23411.00\n" +
|
||||
"LB2 21417.00\n" +
|
||||
"LB3 23869.00\n" +
|
||||
"LB4 24967.00\n" +
|
||||
"LS1 3401.00\n" +
|
||||
"LS2 1107.00\n" +
|
||||
"LS3 14410.00\n" +
|
||||
"LS4 15047.00\n" +
|
||||
"REND\n"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HCV-000-3013", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should handle provided input string with HPP1-4001`() {
|
||||
// Arrange
|
||||
val input = "SNS HPP1-4001 SNE#SS1\n" +
|
||||
"#SC1\n" +
|
||||
"RESULT\n" +
|
||||
"LB1 23777\n" +
|
||||
"LB2 24130\n" +
|
||||
"LB3 23442\n" +
|
||||
"LB4 23945\n" +
|
||||
"LS1 2521\n" +
|
||||
"LS2 973\n" +
|
||||
"LS3 10252\n" +
|
||||
"LS4 11017\n" +
|
||||
"REND\n"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HPP1-4001", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should handle provided input string with HPP1-000-4001`() {
|
||||
// Arrange
|
||||
val input = "SNS HPP1-000-4001 SNE#SS1\n" +
|
||||
"#SC1\n" +
|
||||
"RESULT\n" +
|
||||
"LB1 23777\n" +
|
||||
"LB2 24130\n" +
|
||||
"LB3 23442\n" +
|
||||
"LB4 23945\n" +
|
||||
"LS1 2521\n" +
|
||||
"LS2 973\n" +
|
||||
"LS3 10252\n" +
|
||||
"LS4 11017\n" +
|
||||
"REND\n"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HPP1-000-4001", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should handle provided input string with HPP-000-4001`() {
|
||||
// Arrange
|
||||
val input = "SNS HPP-000-4001 SNE#SS1\n" +
|
||||
"#SC1\n" +
|
||||
"RESULT\n" +
|
||||
"LB1 23777\n" +
|
||||
"LB2 24130\n" +
|
||||
"LB3 23442\n" +
|
||||
"LB4 23945\n" +
|
||||
"LS1 2521\n" +
|
||||
"LS2 973\n" +
|
||||
"LS3 10252\n" +
|
||||
"LS4 11017\n" +
|
||||
"REND\n"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HPP-000-4001", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extractV2HardwareId should handle provided input string with HPP-000-5001`() {
|
||||
// Arrange
|
||||
val input = "SNS HPP-000-5001 SNE#SS1\n" +
|
||||
"#SC1\n" +
|
||||
"RESULT\n" +
|
||||
"LB1 23777\n" +
|
||||
"LB2 24130\n" +
|
||||
"LB3 23442\n" +
|
||||
"LB4 23945\n" +
|
||||
"LS1 2521\n" +
|
||||
"LS2 973\n" +
|
||||
"LS3 10252\n" +
|
||||
"LS4 11017\n" +
|
||||
"REND\n"
|
||||
|
||||
// Act
|
||||
val result = fragment.extractV2HardwareId(input)
|
||||
|
||||
// Assert
|
||||
TestCase.assertEquals("HPP-000-5001", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeviceRatioClassificationNormal() {
|
||||
val ratio = 0.25
|
||||
val result = fragment.deviceRatioClassification(ratio)
|
||||
TestCase.assertEquals("Normal", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeviceRatioClassificationNegativeBorderline() {
|
||||
val ratio = 0.31
|
||||
val result = fragment.deviceRatioClassification(ratio)
|
||||
TestCase.assertEquals("Negative Borderline, Repeat Test", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeviceRatioClassificationSickleCellTrait() {
|
||||
val ratio = 0.34
|
||||
val result = fragment.deviceRatioClassification(ratio)
|
||||
TestCase.assertEquals("Sickle Cell Trait", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeviceRatioClassificationPositiveForSickleCell() {
|
||||
val ratio = 0.37
|
||||
val result = fragment.deviceRatioClassification(ratio)
|
||||
TestCase.assertEquals("Positive for Sickle Cell. HPLC for Confirmation", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeviceRatioClassificationSickleCellDisease() {
|
||||
val ratio = 0.45
|
||||
val result = fragment.deviceRatioClassification(ratio)
|
||||
TestCase.assertEquals("Sickle Cell Disease", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeviceRatioClassificationInvalid() {
|
||||
val ratio: Double? = null
|
||||
val result = fragment.deviceRatioClassification(ratio)
|
||||
TestCase.assertEquals("Invalid", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user