Clean up SetFetchIntervalTest

This commit is contained in:
arkon 2023-08-06 22:34:31 -04:00
parent 3ebf39bd55
commit 0a3ce8ebe4

View file

@ -6,8 +6,10 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Execution import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode import org.junit.jupiter.api.parallel.ExecutionMode
import tachiyomi.domain.chapter.model.Chapter import tachiyomi.domain.chapter.model.Chapter
import java.time.Duration
import java.time.ZonedDateTime import java.time.ZonedDateTime
import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours
import kotlin.time.toJavaDuration
@Execution(ExecutionMode.CONCURRENT) @Execution(ExecutionMode.CONCURRENT)
class SetFetchIntervalTest { class SetFetchIntervalTest {
@ -22,49 +24,34 @@ class SetFetchIntervalTest {
@Test @Test
fun `calculateInterval returns default of 7 days when less than 3 distinct days`() { fun `calculateInterval returns default of 7 days when less than 3 distinct days`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..2).map {
(1..1).forEach { chapterWithTime(chapter, 10.hours)
val duration = Duration.ofHours(10)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7
} }
@Test @Test
fun `calculateInterval returns 7 when 5 chapters in 1 day`() { fun `calculateInterval returns 7 when 5 chapters in 1 day`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..5).map {
(1..5).forEach { chapterWithTime(chapter, 10.hours)
val duration = Duration.ofHours(10)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7
} }
@Test @Test
fun `calculateInterval returns 7 when 7 chapters in 48 hours, 2 day`() { fun `calculateInterval returns 7 when 7 chapters in 48 hours, 2 day`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..2).map {
(1..2).forEach { chapterWithTime(chapter, 24.hours)
val duration = Duration.ofHours(24L) } + (1..5).map {
val newChapter = chapterAddTime(chapter, duration) chapterWithTime(chapter, 48.hours)
chapters.add(newChapter)
}
(1..5).forEach {
val duration = Duration.ofHours(48L)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7
} }
@Test @Test
fun `calculateInterval returns default of 1 day when interval less than 1`() { fun `calculateInterval returns default of 1 day when interval less than 1`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..5).map {
(1..5).forEach { chapterWithTime(chapter, (15 * it).hours)
val duration = Duration.ofHours(15L * it)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
} }
@ -72,61 +59,46 @@ class SetFetchIntervalTest {
// Normal interval calculation // Normal interval calculation
@Test @Test
fun `calculateInterval returns 1 when 5 chapters in 120 hours, 5 days`() { fun `calculateInterval returns 1 when 5 chapters in 120 hours, 5 days`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..5).map {
(1..5).forEach { chapterWithTime(chapter, (24 * it).hours)
val duration = Duration.ofHours(24L * it)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
} }
@Test @Test
fun `calculateInterval returns 2 when 5 chapters in 240 hours, 10 days`() { fun `calculateInterval returns 2 when 5 chapters in 240 hours, 10 days`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..5).map {
(1..5).forEach { chapterWithTime(chapter, (48 * it).hours)
val duration = Duration.ofHours(48L * it)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 2 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 2
} }
@Test @Test
fun `calculateInterval returns floored value when interval is decimal`() { fun `calculateInterval returns floored value when interval is decimal`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..5).map {
(1..5).forEach { chapterWithTime(chapter, (25 * it).hours)
val duration = Duration.ofHours(25L * it)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
} }
@Test @Test
fun `calculateInterval returns 1 when 5 chapters in 215 hours, 5 days`() { fun `calculateInterval returns 1 when 5 chapters in 215 hours, 5 days`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..5).map {
(1..5).forEach { chapterWithTime(chapter, (43 * it).hours)
val duration = Duration.ofHours(43L * it)
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
} }
@Test @Test
fun `calculateInterval returns interval based on fetch time if upload time not available`() { fun `calculateInterval returns interval based on fetch time if upload time not available`() {
val chapters = mutableListOf<Chapter>() val chapters = (1..5).map {
(1..5).forEach { chapterWithTime(chapter, (25 * it).hours).copy(dateUpload = 0L)
val duration = Duration.ofHours(25L * it)
val newChapter = chapterAddTime(chapter, duration).copy(dateUpload = 0L)
chapters.add(newChapter)
} }
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1 setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
} }
private fun chapterAddTime(chapter: Chapter, duration: Duration): Chapter { private fun chapterWithTime(chapter: Chapter, duration: Duration): Chapter {
val newTime = testTime.plus(duration).toEpochSecond() * 1000 val newTime = testTime.plus(duration.toJavaDuration()).toEpochSecond() * 1000
return chapter.copy(dateFetch = newTime, dateUpload = newTime) return chapter.copy(dateFetch = newTime, dateUpload = newTime)
} }
} }