Refactor attendance mapper

This commit is contained in:
Mikołaj Pich 2020-07-26 20:48:18 +02:00
parent bc276b21b5
commit d8acc8c362
6 changed files with 163 additions and 128 deletions

View file

@ -7,6 +7,12 @@ class Attendance {
var number: Int = 0
lateinit var category: Category
var excusable: Boolean = false
var excuseStatus: SentExcuse.Status? = null
@SerializedName("IdPoraLekcji")
var timeId: Int = 0
@ -16,37 +22,9 @@ class Attendance {
@SerializedName("PrzedmiotNazwa")
lateinit var subject: String
lateinit var name: String
@SerializedName("IdKategoria")
var categoryId: Int = -1
var presence: Boolean = false
var absence: Boolean = false
var exemption: Boolean = false
var lateness: Boolean = false
var excused: Boolean = false
var deleted: Boolean = false
var excusable: Boolean = false
var excuseStatus: SentExcuse.Status? = null
object Types {
const val PRESENCE = "x-obecnosc"
const val ABSENCE_UNEXCUSED = "x-nieobecnosc-nieuspr"
const val ABSENCE_EXCUSED = "x-nieobecnosc-uspr"
const val ABSENCE_FOR_SCHOOL_REASONS = "x-nieobecnosc-przycz-szkol"
const val UNEXCUSED_LATENESS = "x-sp-nieusprawiedliwione"
const val EXCUSED_LATENESS = "x-sp-spr"
const val EXEMPTION = "x-sp-zwolnienie"
}
enum class Category(val id: Int, val title: String) {
ALL(-1, "Wszystkie"),
UNKNOWN(0, "Nieznany"),
@ -57,6 +35,11 @@ class Attendance {
EXCUSED_LATENESS(5, "Spóźnienie usprawiedliwione"),
ABSENCE_FOR_SCHOOL_REASONS(6, "Nieobecność z przyczyn szkolnych"),
EXEMPTION(7, "Zwolnienie"),
DELETED(8, "Usunięty wpis")
DELETED(8, "Usunięty wpis");
companion object {
@JvmStatic
fun getCategoryById(id: Int) = values().singleOrNull { category -> category.id == id } ?: UNKNOWN
}
}
}

View file

@ -3,15 +3,7 @@ package io.github.wulkanowy.sdk.scrapper.attendance
import com.google.gson.GsonBuilder
import com.google.gson.internal.LinkedTreeMap
import com.google.gson.reflect.TypeToken
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.ABSENCE_EXCUSED
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.ABSENCE_FOR_SCHOOL_REASONS
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.ABSENCE_UNEXCUSED
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.EXCUSED_LATENESS
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.EXEMPTION
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.PRESENCE
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.UNEXCUSED_LATENESS
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.UNKNOWN
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.values
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category
import io.github.wulkanowy.sdk.scrapper.timetable.CacheResponse.Time
import io.github.wulkanowy.sdk.scrapper.toLocalDate
import java.time.LocalDate
@ -22,16 +14,10 @@ fun AttendanceResponse.mapAttendanceList(start: LocalDate, end: LocalDate?, time
return lessons.map {
val sentExcuse = sentExcuses.firstOrNull { excuse -> excuse.date == it.date && excuse.timeId == it.timeId }
it.apply {
presence = it.categoryId == PRESENCE.id || it.categoryId == ABSENCE_FOR_SCHOOL_REASONS.id
absence = it.categoryId == ABSENCE_UNEXCUSED.id || it.categoryId == ABSENCE_EXCUSED.id
lateness = it.categoryId == EXCUSED_LATENESS.id || it.categoryId == UNEXCUSED_LATENESS.id
excused = it.categoryId == ABSENCE_EXCUSED.id || it.categoryId == EXCUSED_LATENESS.id
exemption = it.categoryId == EXEMPTION.id
excusable = excuseActive && (absence || lateness) && !excused && sentExcuse == null
name = (values().singleOrNull { category -> category.id == categoryId } ?: UNKNOWN).title
number = times.single { time -> time.id == it.timeId }.number
if (sentExcuse != null)
excuseStatus = SentExcuse.Status.getByValue(sentExcuse.status)
category = Category.getCategoryById(categoryId)
excusable = excuseActive && (category == Category.ABSENCE_UNEXCUSED || category == Category.UNEXCUSED_LATENESS) && sentExcuse == null
if (sentExcuse != null) excuseStatus = SentExcuse.Status.getByValue(sentExcuse.status)
}
}.filter {
it.date.toLocalDate() >= start && it.date.toLocalDate() <= endDate

View file

@ -116,21 +116,18 @@ class ScrapperRemoteTest : BaseTest() {
assertEquals("Zajęcia z wychowawcą", subject)
assertEquals(getDate(2018, 10, 1), date)
assertEquals("Obecność", name)
assertTrue(presence)
assertEquals("Obecność", category.name)
}
attendance[1].run {
assertEquals("Nieobecność nieusprawiedliwiona", name)
assertTrue(absence)
assertFalse(excused)
assertEquals("Nieobecność nieusprawiedliwiona", category.name)
}
assertEquals("Nieobecność nieusprawiedliwiona", attendance[3].name)
assertEquals("Nieobecność nieusprawiedliwiona", attendance[4].name)
assertEquals("Nieobecność usprawiedliwiona", attendance[5].name)
assertEquals("Spóźnienie nieusprawiedliwione", attendance[6].name)
assertEquals("Obecność", attendance[9].name)
assertEquals("Nieobecność nieusprawiedliwiona", attendance[3].category.name)
assertEquals("Nieobecność nieusprawiedliwiona", attendance[4].category.name)
assertEquals("Nieobecność usprawiedliwiona", attendance[5].category.name)
assertEquals("Spóźnienie nieusprawiedliwione", attendance[6].category.name)
assertEquals("Obecność", attendance[9].category)
}
@Test

View file

@ -40,15 +40,8 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(76, timeId)
assertEquals(getDate(2018, 10, 2), date)
assertEquals("Zajęcia artystyczne", subject)
assertEquals("Obecność", name)
assertTrue(presence)
assertFalse(excused)
assertEquals(Attendance.Category.PRESENCE, category)
assertEquals(SentExcuse.Status.WAITING, excuseStatus)
assertFalse(absence)
assertFalse(exemption)
assertFalse(lateness)
assertFalse(deleted)
assertFalse(excusable)
}
}
@ -61,15 +54,8 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(77, timeId)
assertEquals(getDate(2018, 10, 2), date)
assertEquals("Informatyka", subject)
assertEquals("Nieobecność nieusprawiedliwiona", name)
assertTrue(absence)
assertFalse(excused)
assertEquals(Attendance.Category.ABSENCE_UNEXCUSED, category)
assertEquals(SentExcuse.Status.ACCEPTED, excuseStatus)
assertFalse(exemption)
assertFalse(presence)
assertFalse(lateness)
assertFalse(deleted)
assertFalse(excusable)
}
}
@ -82,15 +68,8 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(76, timeId)
assertEquals(getDate(2018, 10, 3), date)
assertEquals("Matematyka", subject)
assertEquals("Nieobecność usprawiedliwiona", name)
assertTrue(absence)
assertTrue(excused)
assertEquals(Attendance.Category.ABSENCE_EXCUSED, category)
assertEquals(SentExcuse.Status.DENIED, excuseStatus)
assertFalse(exemption)
assertFalse(presence)
assertFalse(lateness)
assertFalse(deleted)
assertFalse(excusable)
}
}
@ -103,15 +82,9 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(77, timeId)
assertEquals(getDate(2018, 10, 3), date)
assertEquals("Godzina wychowawcza", subject)
assertEquals("Spóźnienie nieusprawiedliwione", name)
assertTrue(lateness)
assertFalse(excused)
assertEquals(Attendance.Category.UNEXCUSED_LATENESS, category)
assertNull(excuseStatus)
assertFalse(exemption)
assertFalse(presence)
assertFalse(absence)
assertFalse(deleted)
assertTrue(excusable)
}
}
@ -124,15 +97,8 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(76, timeId)
assertEquals(getDate(2018, 10, 4), date)
assertEquals("Historia", subject)
assertEquals("Spóźnienie usprawiedliwione", name)
assertTrue(lateness)
assertTrue(excused)
assertEquals(Attendance.Category.EXCUSED_LATENESS, category)
assertNull(excuseStatus)
assertFalse(exemption)
assertFalse(presence)
assertFalse(absence)
assertFalse(deleted)
assertFalse(excusable)
}
}
@ -145,15 +111,8 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(77, timeId)
assertEquals(getDate(2018, 10, 4), date)
assertEquals("Język angielski", subject)
assertEquals("Nieobecność z przyczyn szkolnych", name)
assertTrue(presence)
assertFalse(excused)
assertEquals(Attendance.Category.ABSENCE_FOR_SCHOOL_REASONS, category)
assertNull(excuseStatus)
assertFalse(lateness)
assertFalse(exemption)
assertFalse(absence)
assertFalse(deleted)
assertFalse(excusable)
}
}
@ -166,15 +125,8 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(76, timeId)
assertEquals(getDate(2018, 10, 5), date)
assertEquals("Informatyka", subject)
assertEquals("Zwolnienie", name)
assertTrue(exemption)
assertFalse(excused)
assertEquals(Attendance.Category.EXEMPTION, category)
assertNull(excuseStatus)
assertFalse(lateness)
assertFalse(presence)
assertFalse(absence)
assertFalse(deleted)
assertFalse(excusable)
}
}
@ -187,15 +139,8 @@ class AttendanceTest : BaseLocalTest() {
assertEquals(77, timeId)
assertEquals(getDate(2018, 10, 5), date)
assertEquals("Informatyka", subject)
assertEquals("Nieznany", name)
assertFalse(exemption)
assertFalse(excused)
assertEquals(Attendance.Category.UNKNOWN, category)
assertNull(excuseStatus)
assertFalse(lateness)
assertFalse(presence)
assertFalse(absence)
assertFalse(deleted)
assertFalse(excusable)
}
}

View file

@ -4,6 +4,13 @@ import io.github.wulkanowy.sdk.mobile.dictionaries.Dictionaries
import io.github.wulkanowy.sdk.pojo.Absent
import io.github.wulkanowy.sdk.pojo.Attendance
import io.github.wulkanowy.sdk.pojo.AttendanceSummary
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.ABSENCE_EXCUSED
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.ABSENCE_FOR_SCHOOL_REASONS
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.ABSENCE_UNEXCUSED
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.EXCUSED_LATENESS
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.EXEMPTION
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.PRESENCE
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance.Category.UNEXCUSED_LATENESS
import io.github.wulkanowy.sdk.scrapper.toLocalDate
import io.github.wulkanowy.sdk.toLocalDate
import io.github.wulkanowy.sdk.mobile.attendance.Attendance as ApiAttendance
@ -34,19 +41,19 @@ fun List<ApiAttendance>.mapAttendance(dictionaries: Dictionaries) = map {
fun List<ScrapperAttendance>.mapAttendance() = map {
Attendance(
number = it.number,
name = it.name,
name = it.category.name,
subject = it.subject,
date = it.date.toLocalDate(),
timeId = it.timeId,
absence = it.absence,
categoryId = it.categoryId,
deleted = it.deleted,
deleted = false,
excuseStatus = it.excuseStatus,
excusable = it.excusable,
excused = it.excused,
exemption = it.exemption,
lateness = it.lateness,
presence = it.presence,
excuseStatus = it.excuseStatus
absence = it.category == ABSENCE_UNEXCUSED || it.category == ABSENCE_EXCUSED,
excused = it.category == ABSENCE_EXCUSED || it.category == EXCUSED_LATENESS,
exemption = it.category == EXEMPTION,
lateness = it.category == EXCUSED_LATENESS || it.category == UNEXCUSED_LATENESS,
presence = it.category == PRESENCE || it.category == ABSENCE_FOR_SCHOOL_REASONS
)
}

View file

@ -0,0 +1,117 @@
package io.github.wulkanowy.sdk.mapper
import io.github.wulkanowy.sdk.mobile.BaseLocalTest
import io.github.wulkanowy.sdk.scrapper.attendance.Attendance
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.Date
class AttendanceMapperTest : BaseLocalTest() {
@Test
fun getAttendance_presence() {
createAttendance(Attendance.Category.PRESENCE).mapAttendance()[0].run {
assertTrue(presence)
assertFalse(excused)
assertFalse(absence)
assertFalse(exemption)
assertFalse(lateness)
}
}
@Test
fun getAttendance_absence() {
createAttendance(Attendance.Category.ABSENCE_UNEXCUSED).mapAttendance()[0].run {
assertTrue(absence)
assertFalse(excused)
assertFalse(exemption)
assertFalse(presence)
assertFalse(lateness)
}
}
@Test
fun getAttendance_absenceExcused() {
createAttendance(Attendance.Category.ABSENCE_EXCUSED).mapAttendance()[0].run {
assertTrue(absence)
assertTrue(excused)
assertFalse(exemption)
assertFalse(presence)
assertFalse(lateness)
}
}
@Test
fun getAttendance_lateness() {
createAttendance(Attendance.Category.UNEXCUSED_LATENESS).mapAttendance()[0].run {
assertTrue(lateness)
assertFalse(excused)
assertFalse(exemption)
assertFalse(presence)
assertFalse(absence)
}
}
@Test
fun getAttendance_latenessExcused() {
createAttendance(Attendance.Category.EXCUSED_LATENESS).mapAttendance()[0].run {
assertTrue(lateness)
assertTrue(excused)
assertFalse(exemption)
assertFalse(presence)
assertFalse(absence)
}
}
@Test
fun getAttendance_absenceForSchoolReason() {
createAttendance(Attendance.Category.ABSENCE_FOR_SCHOOL_REASONS).mapAttendance()[0].run {
assertTrue(presence)
assertFalse(excused)
assertFalse(lateness)
assertFalse(exemption)
assertFalse(absence)
}
}
@Test
fun getAttendance_exemption() {
createAttendance(Attendance.Category.EXEMPTION).mapAttendance()[0].run {
assertTrue(exemption)
assertFalse(excused)
assertFalse(lateness)
assertFalse(presence)
assertFalse(absence)
}
}
@Test
fun getAttendance_unknown() {
createAttendance(Attendance.Category.UNKNOWN).mapAttendance()[0].run {
assertFalse(exemption)
assertFalse(excused)
assertFalse(lateness)
assertFalse(presence)
assertFalse(absence)
}
}
private fun createAttendance(cat: Attendance.Category) = listOf(Attendance().apply {
number = 0
category = cat
subject = ""
date = Date()
timeId = 1
excusable = false
excuseStatus = null
})
}