From bc22808b0ea2ece030ff3e9ef24d5af231e22b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Thu, 1 Sep 2022 17:48:46 +0200 Subject: [PATCH] Add support for matching last kingergarten semester if there is no any current (#1962) --- .../wulkanowy/utils/SemesterExtension.kt | 3 + .../utils/SemesterExtensionKtTest.kt | 72 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/src/test/java/io/github/wulkanowy/utils/SemesterExtensionKtTest.kt diff --git a/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt index 6e11a8b2..380d6bf6 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt @@ -15,5 +15,8 @@ fun List.getCurrentOrLast(): Semester { // when there is more than one current semester - find one with higher id singleOrNull { semester -> semester.semesterId == maxByOrNull { it.semesterId }?.semesterId }?.let { return it } + // when there is no active kindergarten semester - get one from last year + singleOrNull { semester -> semester.schoolYear == maxByOrNull { it.schoolYear }?.schoolYear }?.let { return it } + throw IllegalArgumentException("Duplicated last semester! Semesters: ${joinToString(separator = "\n")}") } diff --git a/app/src/test/java/io/github/wulkanowy/utils/SemesterExtensionKtTest.kt b/app/src/test/java/io/github/wulkanowy/utils/SemesterExtensionKtTest.kt new file mode 100644 index 00000000..b7d3ecc9 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/utils/SemesterExtensionKtTest.kt @@ -0,0 +1,72 @@ +package io.github.wulkanowy.utils + +import io.github.wulkanowy.data.db.entities.Semester +import io.github.wulkanowy.getSemesterEntity +import org.junit.Test +import java.time.LocalDate +import kotlin.test.assertEquals + +class SemesterExtensionKtTest { + + @Test(expected = IllegalArgumentException::class) + fun `get current semester when current is doubled`() { + val semesters = listOf( + getSemesterEntity(1, 1, LocalDate.now(), LocalDate.now()), + getSemesterEntity(1, 1, LocalDate.now(), LocalDate.now()) + ) + + semesters.getCurrentOrLast() + } + + @Test(expected = RuntimeException::class) + fun `get current semester when there is empty list`() { + val semesters = listOf() + + semesters.getCurrentOrLast() + } + + @Test + fun `get current kindergarten semester when there is no any current`() { + val semesters = listOf( + createSemesterEntity( + kindergartenDiaryId = 281, + schoolYear = 2020, + semesterId = 0, + start = LocalDate.of(2020, 9, 1), + end = LocalDate.of(2021, 8, 31), + ), + createSemesterEntity( + kindergartenDiaryId = 342, + schoolYear = 2021, + semesterId = 0, + start = LocalDate.of(2021, 9, 1), + end = LocalDate.of(2022, 8, 31), + ), + ) + + val res = semesters.getCurrentOrLast() + + assertEquals(2021, res.schoolYear) + } + + private fun createSemesterEntity( + diaryId: Int = 0, + kindergartenDiaryId: Int = 0, + semesterId: Int = 0, + schoolYear: Int = 0, + start: LocalDate = LocalDate.now(), + end: LocalDate = LocalDate.now().plusMonths(6), + ) = Semester( + studentId = 1, + diaryId = diaryId, + kindergartenDiaryId = kindergartenDiaryId, + semesterId = semesterId, + diaryName = "$semesterId", + schoolYear = schoolYear, + classId = 0, + semesterName = semesterId, + unitId = 1, + start = start, + end = end + ) +}