test: implement date parsing and sorting unit tests using real-world CSV activity data. Improved OSs sort
This commit is contained in:
@@ -117,7 +117,12 @@ fun parseDateOnly(dateStr: String): Date? {
|
||||
}
|
||||
|
||||
fun parseFullDateTime(dateTimeStr: String): Date? {
|
||||
val clean = dateTimeStr.trim()
|
||||
var clean = dateTimeStr.trim()
|
||||
if (clean == "N/A" || clean.isBlank()) return null
|
||||
|
||||
// Strip leading weekday prefixes like "sex.", "seg.", "ter.", "qua.", "qui.", "sáb.", "dom.", "Sex", "Seg", etc.
|
||||
clean = clean.replace(Regex("^[a-zA-zA-ZáàâãéèêíóôõúçÁÀÂÃÉÈÍÓÔÕÚÇ]{2,4}\\.?\\s+"), "").trim()
|
||||
|
||||
val formats = listOf(
|
||||
"dd/MM/yyyy HH:mm:ss",
|
||||
"dd/MM/yyyy HH:mm",
|
||||
@@ -152,7 +157,7 @@ fun parseFullDateTime(dateTimeStr: String): Date? {
|
||||
// Try next
|
||||
}
|
||||
}
|
||||
return null
|
||||
return parseDateOnly(dateTimeStr)
|
||||
}
|
||||
|
||||
fun formatToShortDate(dataStr: String): String {
|
||||
|
||||
@@ -182,7 +182,22 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
||||
p1 == null && p2 == null -> a1.numeroOs.compareTo(a2.numeroOs)
|
||||
p1 == null -> 1 // Keep null/invalid date at the end
|
||||
p2 == null -> -1
|
||||
else -> p1.compareTo(p2)
|
||||
else -> {
|
||||
val comp = p1.compareTo(p2)
|
||||
if (comp != 0) {
|
||||
comp
|
||||
} else {
|
||||
val s1 = parseFullDateTime(a1.dataInicio)
|
||||
val s2 = parseFullDateTime(a2.dataInicio)
|
||||
val startComp = when {
|
||||
s1 == null && s2 == null -> 0
|
||||
s1 == null -> 1
|
||||
s2 == null -> -1
|
||||
else -> s1.compareTo(s2)
|
||||
}
|
||||
if (startComp != 0) startComp else a1.numeroOs.compareTo(a2.numeroOs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}.stateIn(
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,82 @@ class ExampleUnitTest {
|
||||
assertEquals(15, cal2.get(Calendar.MINUTE))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testParseFullDateTimeWithCsvFormats() {
|
||||
// Tests real date formats from documentacao/exemplo_testes/GestaoOS_Parada_project.csv
|
||||
val csvDates = listOf(
|
||||
"ter. 04/08/26 07:00",
|
||||
"sex. 14/08/26 07:00",
|
||||
"seg. 17/08/26 07:00",
|
||||
"ter. 18/08/26 17:00",
|
||||
"qua. 19/08/26 07:00",
|
||||
"qui. 27/08/26 17:00",
|
||||
"sáb. 22/08/26 07:00",
|
||||
"qua. 02/09/26 17:00",
|
||||
"sex. 11/09/26 16:00"
|
||||
)
|
||||
|
||||
for (str in csvDates) {
|
||||
val parsed = parseFullDateTime(str)
|
||||
assertNotNull("Failed to parse CSV date format: $str", parsed)
|
||||
}
|
||||
|
||||
val d1 = parseFullDateTime("sex. 14/08/26 07:00")!!
|
||||
val d2 = parseFullDateTime("ter. 18/08/26 17:00")!!
|
||||
val d3 = parseFullDateTime("qua. 02/09/26 17:00")!!
|
||||
assertTrue(d1.before(d2))
|
||||
assertTrue(d2.before(d3))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSortingChronologicalOrderWithCsvData() {
|
||||
// Activities created using real entries from documentacao/exemplo_testes/GestaoOS_Parada_project.csv
|
||||
val act1 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "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
|
||||
)
|
||||
val act3 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "3", descricao = "UTILIDADES", setor = "Geral", equipe = "Mecânica", recurso = "Sem Recurso",
|
||||
dataInicio = "sex. 14/08/26 07:00", dataFim = "qua. 02/09/26 17:00", percentualExecucao = 0
|
||||
)
|
||||
val act4 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "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
|
||||
)
|
||||
val act1024 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "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
|
||||
)
|
||||
val act1025 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "1025", descricao = "FABRICAÇÃO E MONTAGEM DA BASE ACIONAMENTO", setor = "Geral", equipe = "Mecânica", recurso = "GTEC 3",
|
||||
dataInicio = "seg. 17/08/26 07:00", dataFim = "qua. 19/08/26 17:00", percentualExecucao = 0
|
||||
)
|
||||
val act875 = com.example.data.model.MaintenanceActivity(
|
||||
numeroOs = "875", descricao = "SUBSTITUIR APARADORES DA CORREIA", setor = "Geral", equipe = "Mecânica", recurso = "LEANDRO",
|
||||
dataInicio = "qua. 26/08/26 07:00", dataFim = "qui. 27/08/26 17:00", percentualExecucao = 0
|
||||
)
|
||||
|
||||
val unorganizedList = listOf(act1, act3, act1025, act1024, act875, act4)
|
||||
|
||||
val sortedList = unorganizedList.sortedWith { a1, a2 ->
|
||||
val p1 = parseFullDateTime(a1.dataFim)
|
||||
val p2 = parseFullDateTime(a2.dataFim)
|
||||
when {
|
||||
p1 == null && p2 == null -> a1.numeroOs.compareTo(a2.numeroOs)
|
||||
p1 == null -> 1
|
||||
p2 == null -> -1
|
||||
else -> {
|
||||
val comp = p1.compareTo(p2)
|
||||
if (comp != 0) comp else a1.numeroOs.compareTo(a2.numeroOs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val sortedOsNumbers = sortedList.map { it.numeroOs }
|
||||
val expectedOsNumbers = listOf("4", "1024", "1025", "875", "3", "1")
|
||||
assertEquals(expectedOsNumbers, sortedOsNumbers)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFormatToShortDate() {
|
||||
assertEquals("14/08/26", formatToShortDate("Sex 14/08/26 07:00"))
|
||||
|
||||
Reference in New Issue
Block a user