test: implement date parsing and sorting unit tests using real-world CSV activity data. Improved OSs sort
This commit is contained in:
@@ -346,4 +346,78 @@ class ExampleRobolectricTest {
|
||||
kotlinx.coroutines.Dispatchers.resetMain()
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
|
||||
@Test
|
||||
fun `test sorting using CSV date formats from GestaoOS_Parada_project`() {
|
||||
val testDispatcher = kotlinx.coroutines.test.UnconfinedTestDispatcher()
|
||||
kotlinx.coroutines.Dispatchers.setMain(testDispatcher)
|
||||
try {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val db = androidx.room.Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
|
||||
.allowMainThreadQueries()
|
||||
.setQueryExecutor { it.run() }
|
||||
.setTransactionExecutor { it.run() }
|
||||
.build()
|
||||
val repository = com.example.data.repository.MaintenanceRepository(db.maintenanceDao(), context)
|
||||
|
||||
val act1 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "OS-1",
|
||||
descricao = "PARADA ANUAL 2026",
|
||||
setor = "Geral",
|
||||
equipe = "Mecânica",
|
||||
recurso = "Sem Recurso",
|
||||
dataInicio = "ter. 04/08/26 07:00",
|
||||
dataFim = "sex. 11/09/26 16:00",
|
||||
percentualExecucao = 0,
|
||||
synced = true
|
||||
)
|
||||
val act2 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "OS-1024",
|
||||
descricao = "SUBSTITUIÇÃO DA CHAPA DE FIXAÇÃO DOS APARADORES",
|
||||
setor = "Geral",
|
||||
equipe = "Mecânica",
|
||||
recurso = "GTEC 1",
|
||||
dataInicio = "seg. 17/08/26 07:00",
|
||||
dataFim = "ter. 18/08/26 17:00",
|
||||
percentualExecucao = 0,
|
||||
synced = true
|
||||
)
|
||||
val act3 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "OS-4",
|
||||
descricao = "B-111113",
|
||||
setor = "Geral",
|
||||
equipe = "Mecânica",
|
||||
recurso = "Sem Recurso",
|
||||
dataInicio = "sex. 14/08/26 07:00",
|
||||
dataFim = "sex. 14/08/26 07:00",
|
||||
percentualExecucao = 0,
|
||||
synced = true
|
||||
)
|
||||
|
||||
kotlinx.coroutines.runBlocking {
|
||||
db.maintenanceDao().insertActivities(listOf(act1, act2, act3))
|
||||
|
||||
val viewModel = com.example.ui.viewmodel.MaintenanceViewModel(repository)
|
||||
val collectionJob = viewModel.viewModelScope.launch {
|
||||
viewModel.filteredActivities.collect {}
|
||||
}
|
||||
|
||||
Thread.sleep(200)
|
||||
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||
|
||||
val filtered = viewModel.filteredActivities.value
|
||||
assertEquals(3, filtered.size)
|
||||
// Order by dataFim: OS-4 (14/08/26 07:00) -> OS-1024 (18/08/26 17:00) -> OS-1 (11/09/26 16:00)
|
||||
assertEquals("OS-4", filtered[0].numeroOs)
|
||||
assertEquals("OS-1024", filtered[1].numeroOs)
|
||||
assertEquals("OS-1", filtered[2].numeroOs)
|
||||
|
||||
collectionJob.cancel()
|
||||
}
|
||||
db.close()
|
||||
} finally {
|
||||
kotlinx.coroutines.Dispatchers.resetMain()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user