From 0a3ce8ebe45d7615311f2f99b8e59ea8490411d4 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 6 Aug 2023 22:34:31 -0400 Subject: [PATCH] Clean up SetFetchIntervalTest --- .../manga/interactor/SetFetchIntervalTest.kt | 78 ++++++------------- 1 file changed, 25 insertions(+), 53 deletions(-) diff --git a/domain/src/test/java/tachiyomi/domain/manga/interactor/SetFetchIntervalTest.kt b/domain/src/test/java/tachiyomi/domain/manga/interactor/SetFetchIntervalTest.kt index d497d0b051..8c329ca228 100644 --- a/domain/src/test/java/tachiyomi/domain/manga/interactor/SetFetchIntervalTest.kt +++ b/domain/src/test/java/tachiyomi/domain/manga/interactor/SetFetchIntervalTest.kt @@ -6,8 +6,10 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.parallel.Execution import org.junit.jupiter.api.parallel.ExecutionMode import tachiyomi.domain.chapter.model.Chapter -import java.time.Duration import java.time.ZonedDateTime +import kotlin.time.Duration +import kotlin.time.Duration.Companion.hours +import kotlin.time.toJavaDuration @Execution(ExecutionMode.CONCURRENT) class SetFetchIntervalTest { @@ -22,49 +24,34 @@ class SetFetchIntervalTest { @Test fun `calculateInterval returns default of 7 days when less than 3 distinct days`() { - val chapters = mutableListOf() - (1..1).forEach { - val duration = Duration.ofHours(10) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..2).map { + chapterWithTime(chapter, 10.hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7 } @Test fun `calculateInterval returns 7 when 5 chapters in 1 day`() { - val chapters = mutableListOf() - (1..5).forEach { - val duration = Duration.ofHours(10) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..5).map { + chapterWithTime(chapter, 10.hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7 } @Test fun `calculateInterval returns 7 when 7 chapters in 48 hours, 2 day`() { - val chapters = mutableListOf() - (1..2).forEach { - val duration = Duration.ofHours(24L) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) - } - (1..5).forEach { - val duration = Duration.ofHours(48L) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..2).map { + chapterWithTime(chapter, 24.hours) + } + (1..5).map { + chapterWithTime(chapter, 48.hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7 } @Test fun `calculateInterval returns default of 1 day when interval less than 1`() { - val chapters = mutableListOf() - (1..5).forEach { - val duration = Duration.ofHours(15L * it) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..5).map { + chapterWithTime(chapter, (15 * it).hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 } @@ -72,61 +59,46 @@ class SetFetchIntervalTest { // Normal interval calculation @Test fun `calculateInterval returns 1 when 5 chapters in 120 hours, 5 days`() { - val chapters = mutableListOf() - (1..5).forEach { - val duration = Duration.ofHours(24L * it) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..5).map { + chapterWithTime(chapter, (24 * it).hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 } @Test fun `calculateInterval returns 2 when 5 chapters in 240 hours, 10 days`() { - val chapters = mutableListOf() - (1..5).forEach { - val duration = Duration.ofHours(48L * it) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..5).map { + chapterWithTime(chapter, (48 * it).hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 2 } @Test fun `calculateInterval returns floored value when interval is decimal`() { - val chapters = mutableListOf() - (1..5).forEach { - val duration = Duration.ofHours(25L * it) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..5).map { + chapterWithTime(chapter, (25 * it).hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 } @Test fun `calculateInterval returns 1 when 5 chapters in 215 hours, 5 days`() { - val chapters = mutableListOf() - (1..5).forEach { - val duration = Duration.ofHours(43L * it) - val newChapter = chapterAddTime(chapter, duration) - chapters.add(newChapter) + val chapters = (1..5).map { + chapterWithTime(chapter, (43 * it).hours) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 } @Test fun `calculateInterval returns interval based on fetch time if upload time not available`() { - val chapters = mutableListOf() - (1..5).forEach { - val duration = Duration.ofHours(25L * it) - val newChapter = chapterAddTime(chapter, duration).copy(dateUpload = 0L) - chapters.add(newChapter) + val chapters = (1..5).map { + chapterWithTime(chapter, (25 * it).hours).copy(dateUpload = 0L) } setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 } - private fun chapterAddTime(chapter: Chapter, duration: Duration): Chapter { - val newTime = testTime.plus(duration).toEpochSecond() * 1000 + private fun chapterWithTime(chapter: Chapter, duration: Duration): Chapter { + val newTime = testTime.plus(duration.toJavaDuration()).toEpochSecond() * 1000 return chapter.copy(dateFetch = newTime, dateUpload = newTime) } }