Refactor application class

This commit is contained in:
Koitharu
2023-08-22 16:17:18 +03:00
parent 05dbd11fc1
commit ca1380e2b1
24 changed files with 64 additions and 49 deletions

View File

@@ -0,0 +1,98 @@
package org.koitharu.kotatsu.core.backup
import org.junit.Assert.assertEquals
import org.junit.Test
import org.koitharu.kotatsu.core.db.entity.MangaEntity
import org.koitharu.kotatsu.core.db.entity.TagEntity
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
import org.koitharu.kotatsu.history.data.HistoryEntity
import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.model.MangaState
import org.koitharu.kotatsu.parsers.model.SortOrder
import java.util.concurrent.TimeUnit
class JsonSerializerTest {
@Test
fun toFavouriteEntity() {
val entity = FavouriteEntity(
mangaId = 40,
categoryId = 20,
sortKey = 1,
createdAt = System.currentTimeMillis(),
deletedAt = 0L,
)
val json = JsonSerializer(entity).toJson()
val result = JsonDeserializer(json).toFavouriteEntity()
assertEquals(entity, result)
}
@Test
fun toMangaEntity() {
val entity = MangaEntity(
id = 231,
title = "Lorem Ipsum",
altTitle = "Lorem Ispum 2",
url = "erw",
publicUrl = "hthth",
rating = 0.78f,
isNsfw = true,
coverUrl = "5345",
largeCoverUrl = null,
state = MangaState.FINISHED.name,
author = "RERE",
source = MangaSource.DUMMY.name,
)
val json = JsonSerializer(entity).toJson()
val result = JsonDeserializer(json).toMangaEntity()
assertEquals(entity, result)
}
@Test
fun toTagEntity() {
val entity = TagEntity(
id = 934023534,
title = "Adventure",
key = "adventure",
source = MangaSource.DUMMY.name,
)
val json = JsonSerializer(entity).toJson()
val result = JsonDeserializer(json).toTagEntity()
assertEquals(entity, result)
}
@Test
fun toHistoryEntity() {
val entity = HistoryEntity(
mangaId = 304135341,
createdAt = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(6),
updatedAt = System.currentTimeMillis(),
chapterId = 29014843034,
page = 35,
scroll = 24.0f,
percent = 0.6f,
deletedAt = 0L,
)
val json = JsonSerializer(entity).toJson()
val result = JsonDeserializer(json).toHistoryEntity()
assertEquals(entity, result)
}
@Test
fun toFavouriteCategoryEntity() {
val entity = FavouriteCategoryEntity(
categoryId = 142,
createdAt = System.currentTimeMillis(),
sortKey = 14,
title = "Read later",
order = SortOrder.RATING.name,
track = false,
isVisibleInLibrary = true,
deletedAt = 0L,
)
val json = JsonSerializer(entity).toJson()
val result = JsonDeserializer(json).toFavouriteCategoryEntity()
assertEquals(entity, result)
}
}

View File

@@ -0,0 +1,52 @@
package org.koitharu.kotatsu.core.github
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.koitharu.kotatsu.BuildConfig
class VersionIdTest {
@Test
fun testVersionIdParse() {
val version = VersionId("2.0")
assertEquals(version.major, 2)
assertEquals(version.minor, 0)
assertEquals(version.build, 0)
assertEquals(version.variantType, "")
assertEquals(version.variantNumber, 0)
}
@Test
fun testVersionIdVariantParse() {
val version = VersionId("2.0.1-b1")
assertEquals(version.major, 2)
assertEquals(version.minor, 0)
assertEquals(version.build, 1)
assertEquals(version.variantType, "b")
assertEquals(version.variantNumber, 1)
val version2 = VersionId("2.0.1-beta1")
assertEquals(compareValues(version, version2), 0)
}
@Test
fun testVersionIdCompare() {
val version1 = VersionId("1.99.99")
val version2 = VersionId("2.0.0")
assertTrue(version1 < version2)
}
@Test
fun testVersionIdVariantCompare() {
val version1 = VersionId("2.0.1-a2")
val version2 = VersionId("2.0.1-b1")
assertTrue(version1 < version2)
}
@Test
fun testCurrentVersion() {
val version1 = VersionId("2.4.6")
val version2 = VersionId(BuildConfig.VERSION_NAME)
assertTrue(version1 < version2)
}
}

View File

@@ -0,0 +1,78 @@
package org.koitharu.kotatsu.reader.domain
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.reader.ui.pager.ReaderPage
import kotlin.random.Random
class ChapterPagesTest {
@Test
fun getChaptersSize() {
val pages = ChapterPages()
pages.addFirst(1L, List(12) { page(1L) })
pages.addFirst(2L, List(17) { page(2L) })
assertEquals(2, pages.chaptersSize)
}
@Test
fun removeFirst() {
val pages = ChapterPages()
pages.addLast(1L, List(12) { page(1L) })
pages.addLast(2L, List(17) { page(2L) })
pages.addLast(4L, List(2) { page(4L) })
pages.removeFirst()
assertEquals(2, pages.chaptersSize)
assertEquals(17 + 2, pages.size)
}
@Test
fun removeLast() {
val pages = ChapterPages()
pages.addLast(1L, List(12) { page(1L) })
pages.addLast(2L, List(17) { page(2L) })
pages.addLast(4L, List(2) { page(4L) })
pages.removeLast()
assertEquals(2, pages.chaptersSize)
assertEquals(12 + 17, pages.size)
}
@Test
fun clear() {
val pages = ChapterPages()
pages.addLast(1L, List(12) { page(1L) })
pages.addLast(2L, List(17) { page(2L) })
pages.addLast(4L, List(2) { page(4L) })
pages.clear()
assertEquals(0, pages.chaptersSize)
assertEquals(0, pages.size)
assertEquals(0, pages.size(1L))
assertEquals(0, pages.size(2L))
assertEquals(0, pages.size(4L))
}
@Test
fun subList() {
val pages = ChapterPages()
pages.addLast(1L, List(12) { page(1L) })
pages.addLast(2L, List(17) { page(2L) })
pages.addFirst(4L, List(2) { page(4L) })
val subList = pages.subList(2L)
assertEquals(17, subList.size)
assertEquals(2L, subList.first().chapterId)
assertEquals(2L, subList.last().chapterId)
assertTrue(subList.all { it.chapterId == 2L })
assertEquals(subList.size, pages.size(2L))
}
private fun page(chapterId: Long) = ReaderPage(
id = Random.nextLong(),
url = "http://localhost",
preview = null,
chapterId = chapterId,
index = Random.nextInt(),
source = MangaSource.DUMMY,
)
}

View File

@@ -0,0 +1,60 @@
package org.koitharu.kotatsu.util
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.coroutines.yield
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.koitharu.kotatsu.core.util.CompositeMutex
class CompositeMutexTest {
@Test
fun singleLock() = runTest {
val mutex = CompositeMutex<Int>()
mutex.lock(1)
mutex.lock(2)
mutex.unlock(1)
assert(mutex.size == 1)
mutex.unlock(2)
assert(mutex.isEmpty())
}
@Test
fun doubleLock() = runTest {
val mutex = CompositeMutex<Int>()
repeat(2) {
launch(Dispatchers.Default) {
mutex.lock(1)
}
}
yield()
assertEquals(1, mutex.size)
mutex.unlock(1)
val tryLock = withTimeoutOrNull(1000) {
mutex.lock(1)
}
assertNull(tryLock)
}
@Test
fun cancellation() = runTest {
val mutex = CompositeMutex<Int>()
mutex.lock(1)
val job = launch {
try {
mutex.lock(1)
} finally {
mutex.unlock(1)
}
}
withTimeout(2000) {
job.cancelAndJoin()
}
}
}