Add unit test for readmanga source
This commit is contained in:
@@ -74,4 +74,5 @@ dependencies {
|
||||
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
|
||||
|
||||
testImplementation 'junit:junit:4.13'
|
||||
testImplementation 'org.mockito:mockito-core:2.23.0'
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class KotatsuApp : Application() {
|
||||
}
|
||||
}, module {
|
||||
single {
|
||||
MangaLoaderContext(applicationContext)
|
||||
MangaLoaderContext()
|
||||
}
|
||||
}, module {
|
||||
single {
|
||||
|
||||
@@ -6,10 +6,9 @@ import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
import org.koitharu.kotatsu.utils.ext.await
|
||||
|
||||
class MangaLoaderContext(context: Context) : KoinComponent {
|
||||
class MangaLoaderContext : KoinComponent {
|
||||
|
||||
private val okHttp by inject<OkHttpClient>()
|
||||
private val preferences = context.getSharedPreferences("sources", Context.MODE_PRIVATE)
|
||||
|
||||
suspend fun get(url: String, block: (Request.Builder.() -> Unit)? = null): Response {
|
||||
val request = Request.Builder()
|
||||
@@ -38,11 +37,4 @@ class MangaLoaderContext(context: Context) : KoinComponent {
|
||||
}
|
||||
return okHttp.newCall(request.build()).await()
|
||||
}
|
||||
|
||||
fun getStringOption(name: String, default: String? = null) =
|
||||
preferences.getString(name, default)
|
||||
|
||||
fun getIntOption(name: String, default: Int) = preferences.getInt(name, default)
|
||||
|
||||
fun getBooleanOption(name: String, default: Boolean) = preferences.getBoolean(name, default)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import org.koitharu.kotatsu.domain.exceptions.ParseException
|
||||
import org.koitharu.kotatsu.utils.ext.longHashCode
|
||||
import org.koitharu.kotatsu.utils.ext.parseHtml
|
||||
import org.koitharu.kotatsu.utils.ext.safe
|
||||
import org.koitharu.kotatsu.utils.ext.withDomain
|
||||
|
||||
class ReadmangaRepository(loaderContext: MangaLoaderContext) : MangaRepository(loaderContext) {
|
||||
|
||||
@@ -23,7 +24,8 @@ class ReadmangaRepository(loaderContext: MangaLoaderContext) : MangaRepository(l
|
||||
return root.select("div.tile").mapNotNull { node ->
|
||||
val imgDiv = node.selectFirst("div.img") ?: return@mapNotNull null
|
||||
val descDiv = node.selectFirst("div.desc") ?: return@mapNotNull null
|
||||
val href = imgDiv.selectFirst("a").attr("href") ?: return@mapNotNull null
|
||||
val href = imgDiv.selectFirst("a").attr("href")?.withDomain("readmanga.me")
|
||||
?: return@mapNotNull null
|
||||
val title = descDiv.selectFirst("h3")?.selectFirst("a")?.text()
|
||||
?: return@mapNotNull null
|
||||
Manga(
|
||||
|
||||
@@ -7,4 +7,9 @@ fun String.longHashCode(): Long {
|
||||
h = 31 * h + this[i].toLong()
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
fun String.withDomain(domain: String) = when {
|
||||
this.startsWith("/") -> "http://$domain"
|
||||
else -> this
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.koitharu.kotatsu.parsers
|
||||
|
||||
interface MangaParserTest {
|
||||
|
||||
fun testMangaList()
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.koitharu.kotatsu.parsers
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
import org.junit.BeforeClass
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.dsl.module
|
||||
import org.koitharu.kotatsu.domain.MangaLoaderContext
|
||||
import org.koitharu.kotatsu.domain.MangaRepository
|
||||
|
||||
abstract class RepositoryTestEnvironment {
|
||||
|
||||
lateinit var repository: MangaRepository
|
||||
|
||||
@BeforeClass
|
||||
fun initialize(cls: Class<out MangaRepository>) {
|
||||
startKoin {
|
||||
modules(listOf(
|
||||
module {
|
||||
factory {
|
||||
OkHttpClient()
|
||||
}
|
||||
}, module {
|
||||
single {
|
||||
MangaLoaderContext()
|
||||
}
|
||||
}
|
||||
))
|
||||
}
|
||||
val constructor = cls.getConstructor(MangaLoaderContext::class.java)
|
||||
repository = constructor.newInstance(MangaLoaderContext())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.koitharu.kotatsu.parsers.repository
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert
|
||||
import org.junit.BeforeClass
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koitharu.kotatsu.domain.repository.ReadmangaRepository
|
||||
import org.koitharu.kotatsu.parsers.MangaParserTest
|
||||
import org.koitharu.kotatsu.parsers.RepositoryTestEnvironment
|
||||
import org.koitharu.kotatsu.utils.MyAsserts
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class ReadmangaRuTest : MangaParserTest {
|
||||
|
||||
@Test
|
||||
override fun testMangaList() {
|
||||
val list = runBlocking { repository.getList(1) }
|
||||
Assert.assertTrue(list.size == 70)
|
||||
val item = list[40]
|
||||
Assert.assertTrue(item.title.isNotEmpty())
|
||||
Assert.assertTrue(item.rating in 0f..1f)
|
||||
MyAsserts.assertValidUrl(item.url)
|
||||
MyAsserts.assertValidUrl(item.coverUrl)
|
||||
}
|
||||
|
||||
companion object : RepositoryTestEnvironment() {
|
||||
|
||||
@JvmStatic
|
||||
@BeforeClass
|
||||
fun setUp() = initialize(ReadmangaRepository::class.java)
|
||||
}
|
||||
}
|
||||
22
app/src/test/java/org/koitharu/kotatsu/utils/MyAsserts.kt
Normal file
22
app/src/test/java/org/koitharu/kotatsu/utils/MyAsserts.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
package org.koitharu.kotatsu.utils
|
||||
|
||||
import org.junit.Assert
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
object MyAsserts {
|
||||
|
||||
private val VALID_RESPONSE_CODES = arrayOf(
|
||||
HttpURLConnection.HTTP_OK,
|
||||
HttpURLConnection.HTTP_MOVED_PERM,
|
||||
HttpURLConnection.HTTP_MOVED_TEMP
|
||||
)
|
||||
|
||||
fun assertValidUrl(url: String) {
|
||||
val cn = URL(url).openConnection() as HttpURLConnection
|
||||
cn.connect()
|
||||
val code = cn.responseCode
|
||||
Assert.assertTrue("Invalid response code $code", code in VALID_RESPONSE_CODES)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user