Skip to content

Commit

Permalink
Unit tests for list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKruegerDev committed Apr 10, 2021
1 parent 33681d5 commit 820650a
Show file tree
Hide file tree
Showing 11 changed files with 1,034 additions and 26 deletions.
2 changes: 0 additions & 2 deletions tmdb-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ kotlin {
}
val jvmMain by getting {
dependencies {
implementation(Libs.Data.ktorJava)
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test-junit5"))

implementation(Libs.Data.ktorJava)
implementation(Libs.Kotlin.coroutines)
implementation(Libs.Testing.jupiter)
runtimeOnly(Libs.Testing.jupiterEngine)
Expand Down
9 changes: 5 additions & 4 deletions tmdb-api/src/commonMain/kotlin/app/moviebase/tmdb/Tmdb4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import io.ktor.client.request.*

class Tmdb4(tmdbApiKey: String) {

var authenticationToken: String? = null
var accessToken: String? = null

private val client = buildHttpClient {
it.header(TmdbUrlParameter.API_KEY, tmdbApiKey)
it.parameter(TmdbUrlParameter.API_KEY, tmdbApiKey)
}

private val authClient = buildHttpClient {
it.header(TmdbUrlParameter.API_KEY, tmdbApiKey)
// it.header("Authorization", "Bearer $authenticationToken")
it.parameter(TmdbUrlParameter.API_KEY, tmdbApiKey)
// it.header(TmdbUrlParameter.ACCESS_TOKEN, tmdbApiKey)
// it.header("Authorization", "Bearer accessToken")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class Tmdb4UpdateListRequest(
data class Tmdb4List(
@SerialName("poster_path") val posterPath: String? = null,
val id: Int,
@SerialName("backdrop_path") val backdropPath: String,
@SerialName("backdrop_path") val backdropPath: String? = null,
@SerialName("total_results") val totalResults: Int,
val public: Boolean,
val revenue: String,
Expand Down
6 changes: 1 addition & 5 deletions tmdb-api/src/jvmTest/kotlin/app/moviebase/tmdb/Tmdb.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package app.moviebase.tmdb.api
package app.moviebase.tmdb

import app.moviebase.tmdb.Tmdb3
import java.io.FileInputStream
import java.nio.file.Paths
import java.util.*
import app.moviebase.tmdb.Tmdb4
import kotlin.apply
import kotlin.io.use

val properties by lazy {
Properties().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,45 @@ import org.junit.jupiter.api.Test
class Tmdb4ListApiTest {

val client = mockHttpClient(
4,
"list/82963?page=1" to "list.json"
version = 4,
responses = mapOf(
"list/82963?page=1" to "list_82963.json",
"list/932?page=1" to "list_932.json",
"list/3321?page=1" to "list_3321.json",
)
)

val classToTest = Tmdb4ListApi(client)


@Test
fun `it can fetch movie details`() = runBlocking {
val list = classToTest.getList(
listId = 82963,
page = 1
)
fun `it can fetch list page 1`() = runBlocking {
val list = classToTest.getList(listId = 82963, page = 1)

assertThat(list.id).isEqualTo(82963)
assertThat(list.page).isEqualTo(1)
}

@Test
fun `it can fetch list with empty backdrop`() = runBlocking {
val list = classToTest.getList(listId = 932, page = 1)

assertThat(list.id).isEqualTo(932)
assertThat(list.backdropPath).isNull()
}

@Test
fun `it can fetch list with available content`() = runBlocking {
val list = classToTest.getList(listId = 3321, page = 1)

assertThat(list.id).isEqualTo(3321)
assertThat(list.page).isEqualTo(1)
assertThat(list.totalPages).isEqualTo(4)
assertThat(list.public).isTrue()
assertThat(list.results).isNotEmpty()
assertThat(list.posterPath).isEqualTo("/sSislgDL6SrfkE0upQTqJ6hUcVd.jpg")
assertThat(list.name).isEqualTo("Anime Movies")
assertThat(list.createdBy.name).isEqualTo("Star")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import org.junit.jupiter.api.Test
class TmdbAccountApiTest {

val client = mockHttpClient(
3,
"account?session_id=sessionId" to "tmdb3/movie_details.json"
version = 3,
responses = mapOf(
"account?session_id=sessionId" to "tmdb3/movie_details.json"
)
)

val classToTest = TmdbAccountApi(client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import org.junit.jupiter.api.Test
class TmdbMoviesApiTest {

val client = mockHttpClient(
3,
"movie/10140?language=en-US&append_to_response=release_dates,watch/providers" to "tmdb3/movie_details.json"
version = 3,
responses = mapOf(
"movie/10140?language=en-US&append_to_response=release_dates,watch/providers" to "tmdb3/movie_details.json"
)
)

val classToTest = TmdbMoviesApi(client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import io.ktor.http.*

fun mockHttpClient(
version: Int,
vararg responses: Pair<String, String>
responses: Map<String, String>
) = HttpClient(MockEngine) {
val jsonFiles = mutableMapOf<String, String>()
responses.forEach {
jsonFiles["https://api.themoviedb.org/$version/${it.first}"] = it.second
responses.entries.forEach {
jsonFiles["https://api.themoviedb.org/$version/${it.key}"] = it.value
}
val headers = headersOf("Content-Type" to listOf(ContentType.Application.Json.toString()))

Expand Down
Loading

0 comments on commit 820650a

Please sign in to comment.