Add unit test for readmanga source

This commit is contained in:
Admin
2020-01-31 19:51:57 +02:00
parent 9eda96c0d8
commit b1217d5f48
9 changed files with 105 additions and 11 deletions

View File

@@ -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'
}

View File

@@ -29,7 +29,7 @@ class KotatsuApp : Application() {
}
}, module {
single {
MangaLoaderContext(applicationContext)
MangaLoaderContext()
}
}, module {
single {

View File

@@ -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)
}

View File

@@ -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(

View File

@@ -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
}

View File

@@ -0,0 +1,6 @@
package org.koitharu.kotatsu.parsers
interface MangaParserTest {
fun testMangaList()
}

View File

@@ -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())
}
}

View File

@@ -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)
}
}

View 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)
}
}