2023-11-06 12:36:50 +05:30
|
|
|
package com.example.hpostesting
|
|
|
|
|
|
2024-01-18 01:53:41 +05:30
|
|
|
import android.content.Context
|
2024-01-19 15:12:26 +05:30
|
|
|
import android.os.Build
|
2024-01-18 12:08:03 +05:30
|
|
|
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
|
|
|
|
import androidx.lifecycle.Observer
|
2024-01-19 15:12:26 +05:30
|
|
|
import androidx.work.Configuration
|
|
|
|
|
import androidx.work.testing.SynchronousExecutor
|
|
|
|
|
import androidx.work.testing.WorkManagerTestInitHelper
|
|
|
|
|
import com.example.hpostesting.data.Result
|
2024-01-18 12:08:03 +05:30
|
|
|
import com.example.hpostesting.data.model.login.LoginResponse
|
2024-01-18 01:53:41 +05:30
|
|
|
import com.example.hpostesting.data.repository.Repository
|
|
|
|
|
import com.example.hpostesting.domain.LogFileManager
|
2023-11-06 12:36:50 +05:30
|
|
|
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
2024-01-18 12:08:03 +05:30
|
|
|
import com.example.hpostesting.util.TestCoroutineRule
|
|
|
|
|
import io.mockk.MockKAnnotations
|
|
|
|
|
import io.mockk.coEvery
|
|
|
|
|
import io.mockk.coVerify
|
|
|
|
|
import io.mockk.every
|
|
|
|
|
import io.mockk.impl.annotations.MockK
|
|
|
|
|
import io.mockk.mockk
|
|
|
|
|
import io.mockk.verify
|
2024-01-19 15:12:26 +05:30
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2024-01-18 12:08:03 +05:30
|
|
|
import kotlinx.coroutines.test.runBlockingTest
|
|
|
|
|
import org.junit.After
|
2023-11-06 12:36:50 +05:30
|
|
|
import org.junit.Before
|
2024-01-18 12:08:03 +05:30
|
|
|
import org.junit.Rule
|
2023-11-06 12:36:50 +05:30
|
|
|
import org.junit.Test
|
2024-01-19 15:12:26 +05:30
|
|
|
import org.junit.runner.RunWith
|
|
|
|
|
import org.robolectric.RobolectricTestRunner
|
|
|
|
|
import org.robolectric.annotation.Config
|
2024-01-18 12:08:03 +05:30
|
|
|
import java.io.File
|
2024-01-18 01:53:41 +05:30
|
|
|
|
2024-01-19 15:12:26 +05:30
|
|
|
//@RunWith(RobolectricTestRunner::class)
|
|
|
|
|
//@Config(manifest = Config.NONE)
|
|
|
|
|
//@ExperimentalCoroutinesApi
|
|
|
|
|
//class HemocubeViewModelTest {
|
2024-01-18 12:08:03 +05:30
|
|
|
//
|
|
|
|
|
// @get:Rule
|
|
|
|
|
// val instantTaskExecutorRule = InstantTaskExecutorRule()
|
|
|
|
|
//
|
|
|
|
|
// @get:Rule
|
|
|
|
|
// val coroutineRule = TestCoroutineRule()
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var repository: Repository
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var logFileManager: LogFileManager
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var context: Context
|
|
|
|
|
//
|
|
|
|
|
// @MockK(relaxed = true)
|
|
|
|
|
// lateinit var observer: Observer<Result<LoginResponse>>
|
|
|
|
|
//
|
|
|
|
|
// private lateinit var viewModel: HemoCubeViewModel
|
|
|
|
|
//
|
|
|
|
|
// @Before
|
|
|
|
|
// fun setup() {
|
|
|
|
|
// MockKAnnotations.init(this)
|
|
|
|
|
//
|
2024-01-19 15:12:26 +05:30
|
|
|
// val config = Configuration.Builder()
|
|
|
|
|
// .setMinimumLoggingLevel(android.util.Log.DEBUG)
|
|
|
|
|
// .setExecutor(SynchronousExecutor())
|
|
|
|
|
// .build()
|
|
|
|
|
//
|
|
|
|
|
// WorkManagerTestInitHelper.initializeTestWorkManager(context, config)
|
|
|
|
|
//
|
2024-01-18 12:08:03 +05:30
|
|
|
// every { context.getSharedPreferences(any(), any()) } returns mockk()
|
|
|
|
|
//
|
|
|
|
|
// viewModel = HemoCubeViewModel(
|
|
|
|
|
// mockk(),
|
|
|
|
|
// mockk(),
|
|
|
|
|
// repository,
|
|
|
|
|
// logFileManager,
|
|
|
|
|
// mockk(),
|
|
|
|
|
// context
|
|
|
|
|
// )
|
|
|
|
|
// viewModel.loginResponse.observeForever(observer)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @After
|
|
|
|
|
// fun teardown() {
|
|
|
|
|
// viewModel.loginResponse.removeObserver(observer)
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `login() should update LiveData with success`() = coroutineRule.runBlockingTest {
|
|
|
|
|
//
|
|
|
|
|
// // Arrange
|
|
|
|
|
// coEvery { repository.login(any()) } returns Result.Success(mockk())
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.login(mockk())
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// coVerify { repository.login(any()) }
|
|
|
|
|
// verify { observer.onChanged(Result.Success(any())) }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `login() should update LiveData with error`() = coroutineRule.runBlockingTest {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// coEvery { repository.login(any()) } returns Result.Error(Exception("Login failed"))
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.login(mockk())
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// coVerify { repository.login(any()) }
|
|
|
|
|
// verify { observer.onChanged(Result.Error(any())) }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `uploadLogs() should update LiveData with success`() = coroutineRule.runBlockingTest {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// val file = mockk<File>(relaxed = true)
|
|
|
|
|
// val response = Result.Success(mockk<UploadLogsResponse>())
|
|
|
|
|
// every { logFileManager.createLogFile() } returns file
|
|
|
|
|
// coEvery { repository.uploadLogs(any()) } returns response
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.uploadLogs()
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// verify { observer.onChanged(response) }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Test
|
|
|
|
|
// fun `uploadLogs() should update LiveData with error`() = coroutineRule.runBlockingTest {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// val file = mockk<File>(relaxed = true)
|
|
|
|
|
// val response = Result.Error(Exception("Upload failed"))
|
|
|
|
|
// every { logFileManager.createLogFile() } returns file
|
|
|
|
|
// coEvery { repository.uploadLogs(any()) } returns response
|
|
|
|
|
//
|
|
|
|
|
// // Act
|
|
|
|
|
// viewModel.uploadLogs()
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// verify { observer.onChanged(response) }
|
|
|
|
|
// }
|
2024-01-19 15:12:26 +05:30
|
|
|
//}
|