Add unit test for readmanga source
parent
9eda96c0d8
commit
b1217d5f48
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue