Skip to content

Commit 828f938

Browse files
committed
fix tests
1 parent f983c19 commit 828f938

File tree

11 files changed

+18
-20
lines changed

11 files changed

+18
-20
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ val dokkaJar: TaskProvider<Jar> by tasks.registering(Jar::class) {
8383
kotlin {
8484
explicitApiWarning()
8585

86-
android {
86+
androidTarget {
8787
compilations.all { kotlinOptions.jvmTarget = "1.8" }
8888

8989
mavenPublication { setupPom(artifactId) }

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ pluginManagement {
3434
}
3535
}
3636

37-
rootProject.name = "spotify-web-api-kotlin"
37+
rootProject.name = "spotify-api-kotlin"

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/pub/UserApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public open class UserApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
2424
*
2525
* @return All publicly-available information about the user
2626
*/
27-
public suspend fun getProfile(user: String): SpotifyPublicUser? = catch {
27+
public suspend fun getProfile(user: String): SpotifyPublicUser? = catch(/* some incorrect user ids will return 500 */ catchInternalServerError = true) {
2828
get(endpointBuilder("/users/${UserUri(user).id.encodeUrl()}").toString())
2929
.toObject(SpotifyPublicUser.serializer(), api, json)
3030
}

src/commonMain/kotlin/com.adamratzman.spotify/utils/Utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal expect fun formatDate(date: Long): String
1717

1818
internal fun jsonMap(vararg pairs: Pair<String, JsonElement>) = pairs.toMap().toMutableMap()
1919

20-
internal suspend inline fun <T> catch(crossinline function: suspend () -> T): T? {
20+
internal suspend inline fun <T> catch(catchInternalServerError: Boolean = false, crossinline function: suspend () -> T): T? {
2121
return try {
2222
function()
2323
} catch (e: SpotifyException.BadRequestException) {

src/commonTest/kotlin/com.adamratzman/spotify/AbstractTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ abstract class AbstractTest<T : GenericSpotifyApi> {
1616

1717
val api = buildSpotifyApi(testClassQualifiedName, testName)
1818
if (api != null && api is Z) {
19+
api.spotifyApiOptions.retryOnInternalServerErrorTimes = 10
1920
api.spotifyApiOptions.httpResponseSubscriber = { request, response ->
2021
getResponseCacher()?.cacheResponse(
2122
testClassQualifiedName,

src/commonTest/kotlin/com.adamratzman/spotify/Common.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.coroutines.test.runTest
1111
import kotlinx.coroutines.withContext
1212
import kotlinx.serialization.Serializable
1313
import kotlin.test.assertTrue
14+
import kotlin.time.Duration.Companion.seconds
1415

1516
expect fun areLivePkceTestsEnabled(): Boolean
1617
expect fun arePlayerTestsEnabled(): Boolean
@@ -41,8 +42,7 @@ suspend inline fun <reified T : Throwable> assertFailsWithSuspend(crossinline bl
4142
}
4243
}
4344

44-
@OptIn(ExperimentalCoroutinesApi::class)
45-
fun <T> runTestOnDefaultDispatcher(block: suspend CoroutineScope.() -> T): TestResult = runTest {
45+
fun <T> runTestOnDefaultDispatcher(block: suspend CoroutineScope.() -> T): TestResult = runTest(timeout = 60.seconds) {
4646
withContext(Dispatchers.Default) {
4747
block()
4848
}

src/commonTest/kotlin/com.adamratzman/spotify/priv/ClientEpisodeApiTest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ class ClientEpisodeApiTest : AbstractTest<SpotifyClientApi>() {
3030
buildApi<SpotifyClientApi>(::testGetEpisodes.name)
3131
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
3232

33-
assertFailsWith<BadRequestException> { api.episodes.getEpisodes("hi", "dad") }
34-
assertFailsWith<BadRequestException> {
35-
api.episodes.getEpisodes("1cfOhXP4GQCd5ZFHoSF8gg", "j").map { it?.name }
36-
}
33+
assertEquals(listOf(null, null), api.episodes.getEpisodes("hi", "dad"))
34+
assertEquals(null, api.episodes.getEpisodes("1cfOhXP4GQCd5ZFHoSF8gg", "j")[1])
3735
assertEquals(
3836
listOf("The Great Inflation (Classic)"),
3937
api.episodes.getEpisodes("3lMZTE81Pbrp0U12WZe27l").map { it?.name }

src/commonTest/kotlin/com.adamratzman/spotify/priv/ClientShowApiTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ClientShowApiTest : AbstractTest<SpotifyClientApi>() {
2323

2424
assertNull(api.shows.getShow("invalid-show"))
2525
assertEquals("Freakonomics Radio", api.shows.getShow("spotify:show:6z4NLXyHPga1UmSJsPK7G1")?.name)
26+
assertEquals("Freakonomics Radio", api.shows.getShow("spotify:show:6z4NLXyHPga1UmSJsPK7G1", Market.FROM_TOKEN)?.name)
2627
}
2728

2829
@Test

src/commonTest/kotlin/com.adamratzman/spotify/pub/EpisodeApiTest.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import com.adamratzman.spotify.runTestOnDefaultDispatcher
1010
import com.adamratzman.spotify.utils.Market
1111
import kotlinx.coroutines.ExperimentalCoroutinesApi
1212
import kotlinx.coroutines.test.TestResult
13-
import kotlin.test.Test
14-
import kotlin.test.assertEquals
15-
import kotlin.test.assertFailsWith
16-
import kotlin.test.assertNull
13+
import kotlin.test.*
1714

1815
class EpisodeApiTest : AbstractTest<GenericSpotifyApi>() {
1916
private val market = Market.US
@@ -33,10 +30,12 @@ class EpisodeApiTest : AbstractTest<GenericSpotifyApi>() {
3330
fun testGetEpisodes(): TestResult = runTestOnDefaultDispatcher {
3431
buildApi(::testGetEpisodes.name)
3532

36-
assertFailsWith<BadRequestException> { api.episodes.getEpisodes("hi", "dad", market = market) }
37-
assertFailsWith<BadRequestException> {
38-
api.episodes.getEpisodes("1cfOhXP4GQCd5ZFHoSF8gg", "j", market = market).map { it?.name }
39-
}
33+
assertEquals(listOf(null, null), api.episodes.getEpisodes("hi", "dad", market = market))
34+
35+
val firstResultNotNullSecondNull = api.episodes.getEpisodes("1cfOhXP4GQCd5ZFHoSF8gg", "j", market = market).map { it?.name }
36+
assertTrue(firstResultNotNullSecondNull[0] != null)
37+
assertTrue(firstResultNotNullSecondNull[1] == null)
38+
4039
assertEquals(
4140
listOf("The Great Inflation (Classic)"),
4241
api.episodes.getEpisodes("3lMZTE81Pbrp0U12WZe27l", market = market).map { it?.name }

src/commonTest/kotlin/com.adamratzman/spotify/pub/PublicArtistsApiTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,5 @@ class PublicArtistsApiTest : AbstractTest<GenericSpotifyApi>() {
6969
assertFailsWith<SpotifyException.BadRequestException> { api.artists.getArtistTopTracks("no") }
7070
assertTrue(api.artists.getArtistTopTracks("4ZGK4hkNX6pilPpyy4YJJW").isNotEmpty())
7171
assertTrue(api.artists.getArtistTopTracks("4ZGK4hkNX6pilPpyy4YJJW", Market.FR).isNotEmpty())
72-
assertTrue(api.artists.getArtistTopTracks("4ZGK4hkNX6pilPpyy4YJJW", Market.FROM_TOKEN).isNotEmpty())
7372
}
7473
}

0 commit comments

Comments
 (0)