1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-18 20:56:46 -06:00

Try to switch to next school year before it starts (#2278)

This commit is contained in:
Mikołaj Pich 2023-08-23 12:24:17 +02:00 committed by GitHub
parent 533157709b
commit 2e2b13384a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 16 deletions

View File

@ -191,7 +191,7 @@ ext {
} }
dependencies { dependencies {
implementation 'io.github.wulkanowy:sdk:2.0.8' implementation 'io.github.wulkanowy:sdk:2.0.9-SNAPSHOT'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3' coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'

View File

@ -41,7 +41,7 @@ class SemesterRepository @Inject constructor(
val isRefreshOnModeChangeRequired = when { val isRefreshOnModeChangeRequired = when {
Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.HEBE -> { Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.HEBE -> {
semesters.firstOrNull { it.isCurrent }?.let { semesters.firstOrNull { it.isCurrent() }?.let {
0 == it.diaryId && 0 == it.kindergartenDiaryId 0 == it.diaryId && 0 == it.kindergartenDiaryId
} == true } == true
} }
@ -49,7 +49,7 @@ class SemesterRepository @Inject constructor(
} }
val isRefreshOnNoCurrentAppropriate = val isRefreshOnNoCurrentAppropriate =
refreshOnNoCurrent && !semesters.any { semester -> semester.isCurrent } refreshOnNoCurrent && !semesters.any { semester -> semester.isCurrent() }
return forceRefresh || isNoSemesters || isRefreshOnModeChangeRequired || isRefreshOnNoCurrentAppropriate return forceRefresh || isNoSemesters || isRefreshOnModeChangeRequired || isRefreshOnNoCurrentAppropriate
} }

View File

@ -1,16 +1,27 @@
package io.github.wulkanowy.utils package io.github.wulkanowy.utils
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import java.time.LocalDate
import java.time.LocalDate.now import java.time.LocalDate.now
import java.time.Month
inline val Semester.isCurrent: Boolean fun Semester.isCurrent(now: LocalDate = now()): Boolean {
get() = now() in start..end val shiftedStart = if (start.month == Month.SEPTEMBER) {
start.minusDays(3)
} else start
val shiftedEnd = if (end.month == Month.AUGUST || end.month == Month.SEPTEMBER) {
end.minusDays(3)
} else end
return now in shiftedStart..shiftedEnd
}
fun List<Semester>.getCurrentOrLast(): Semester { fun List<Semester>.getCurrentOrLast(): Semester {
if (isEmpty()) throw RuntimeException("Empty semester list") if (isEmpty()) throw RuntimeException("Empty semester list")
// when there is only one current semester // when there is only one current semester
singleOrNull { it.isCurrent }?.let { return it } singleOrNull { it.isCurrent() }?.let { return it }
// when there is more than one current semester - find one with higher id // when there is more than one current semester - find one with higher id
singleOrNull { semester -> semester.semesterId == maxByOrNull { it.semesterId }?.semesterId }?.let { return it } singleOrNull { semester -> semester.semesterId == maxByOrNull { it.semesterId }?.semesterId }?.let { return it }

View File

@ -15,6 +15,7 @@ import io.mockk.impl.annotations.MockK
import io.mockk.impl.annotations.SpyK import io.mockk.impl.annotations.SpyK
import io.mockk.just import io.mockk.just
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals import org.junit.Assert.assertNotEquals
import org.junit.Before import org.junit.Before
@ -81,15 +82,15 @@ class SemesterRepositoryTest {
} }
@Test @Test
fun getSemesters_invalidDiary_scrapper() { fun getSemesters_invalidDiary_scrapper() = runTest {
val badSemesters = listOf( val badSemesters = listOf(
getSemesterPojo(0, 1, now().minusMonths(6), now().minusMonths(3)), getSemesterPojo(0, 2, now().minusMonths(6), now()),
getSemesterPojo(0, 2, now().minusMonths(3), now()) getSemesterPojo(0, 2, now(), now().plusMonths(6)),
) )
val goodSemesters = listOf( val goodSemesters = listOf(
getSemesterPojo(1, 1, now().minusMonths(6), now().minusMonths(3)), getSemesterPojo(1, 2, now().minusMonths(6), now()),
getSemesterPojo(1, 2, now().minusMonths(3), now()) getSemesterPojo(2, 3, now(), now().plusMonths(6)),
) )
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returnsMany listOf( coEvery { semesterDb.loadAll(student.studentId, student.classId) } returnsMany listOf(
@ -101,7 +102,9 @@ class SemesterRepositoryTest {
coEvery { semesterDb.deleteAll(any()) } just Runs coEvery { semesterDb.deleteAll(any()) } just Runs
coEvery { semesterDb.insertSemesters(any()) } returns listOf() coEvery { semesterDb.insertSemesters(any()) } returns listOf()
val items = runBlocking { semesterRepository.getSemesters(student.copy(loginMode = Sdk.Mode.SCRAPPER.name)) } val items = semesterRepository.getSemesters(
student = student.copy(loginMode = Sdk.Mode.SCRAPPER.name)
)
assertEquals(2, items.size) assertEquals(2, items.size)
assertNotEquals(0, items[0].diaryId) assertNotEquals(0, items[0].diaryId)
} }
@ -188,15 +191,15 @@ class SemesterRepositoryTest {
} }
@Test @Test
fun getSemesters_doubleCurrent_refreshOnNoCurrent() { fun getSemesters_doubleCurrent_refreshOnNoCurrent() = runTest {
val semesters = listOf( val semesters = listOf(
getSemesterEntity(1, 1, now(), now()), getSemesterEntity(1, 1, now().minusMonths(1), now().plusMonths(1)),
getSemesterEntity(1, 2, now(), now()) getSemesterEntity(1, 2, now().minusMonths(1), now().plusMonths(1))
) )
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters
val items = runBlocking { semesterRepository.getSemesters(student, refreshOnNoCurrent = true) } val items = semesterRepository.getSemesters(student, refreshOnNoCurrent = true)
assertEquals(2, items.size) assertEquals(2, items.size)
} }

View File

@ -8,6 +8,40 @@ import kotlin.test.assertEquals
class SemesterExtensionKtTest { class SemesterExtensionKtTest {
@Test
fun `check is first semester is current`() {
val first = getSemesterEntity(
semesterName = 1,
start = LocalDate.of(2023, 9, 1),
end = LocalDate.of(2024, 1, 31),
)
// first boundary - school-year start
assertEquals(false, first.isCurrent(LocalDate.of(2023, 8, 28)))
assertEquals(true, first.isCurrent(LocalDate.of(2023, 8, 29)))
// second boundary
assertEquals(true, first.isCurrent(LocalDate.of(2024, 1, 31)))
assertEquals(false, first.isCurrent(LocalDate.of(2024, 2, 1)))
}
@Test
fun `check is second semester is current`() {
val second = getSemesterEntity(
semesterName = 2,
start = LocalDate.of(2024, 2, 1),
end = LocalDate.of(2024, 9, 1),
)
// first boundary
assertEquals(false, second.isCurrent(LocalDate.of(2024, 1, 31)))
assertEquals(true, second.isCurrent(LocalDate.of(2024, 2, 1)))
// second boundary - school-year end
assertEquals(true, second.isCurrent(LocalDate.of(2024, 8, 29)))
assertEquals(false, second.isCurrent(LocalDate.of(2024, 8, 30)))
}
@Test(expected = IllegalArgumentException::class) @Test(expected = IllegalArgumentException::class)
fun `get current semester when current is doubled`() { fun `get current semester when current is doubled`() {
val semesters = listOf( val semesters = listOf(