Skip to content

Commit 1688435

Browse files
committed
add cached test responses, prepare for creation of mock api for non-jvm tests
1 parent 9b05d21 commit 1688435

File tree

385 files changed

+10232
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

385 files changed

+10232
-160
lines changed

src/androidTest/kotlin/com/adamratzman/spotify/CommonImpl.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ package com.adamratzman.spotify
44
import android.os.Build.VERSION
55
import java.lang.reflect.Field
66
import java.lang.reflect.Modifier
7-
import java.util.concurrent.Executors
8-
import kotlin.coroutines.CoroutineContext
9-
import kotlinx.coroutines.CoroutineScope
10-
import kotlinx.coroutines.asCoroutineDispatcher
11-
import kotlinx.coroutines.runBlocking
127

138
private fun setFinalStatic(field: Field, newValue: Any?) {
149
field.isAccessible = true
@@ -30,6 +25,7 @@ actual fun getTestTokenString(): String? = getEnvironmentVariable("SPOTIFY_TOKEN
3025
actual fun isHttpLoggingEnabled(): Boolean = getEnvironmentVariable("SPOTIFY_LOG_HTTP") == "true"
3126
actual fun arePlayerTestsEnabled(): Boolean = getEnvironmentVariable("SPOTIFY_ENABLE_PLAYER_TESTS")?.toBoolean() == true
3227
actual fun areLivePkceTestsEnabled(): Boolean = getEnvironmentVariable("VERBOSE_TEST_ENABLED")?.toBoolean() ?: false
28+
actual fun getResponseCacher(): ResponseCacher? = null
3329

3430
actual suspend fun buildSpotifyApi(): GenericSpotifyApi? {
3531
val clientId = getTestClientId()

src/commonMain/kotlin/com.adamratzman.spotify/http/Endpoints.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
100100
}
101101

102102
@Suppress("UNCHECKED_CAST")
103-
private suspend fun <ReturnType: String?> execute(
103+
internal open suspend fun <ReturnType: String?> execute(
104104
url: String,
105105
body: String? = null,
106106
method: HttpRequestMethod = HttpRequestMethod.GET,

src/commonMain/kotlin/com.soywiz.korim.format.jpg/JPEG.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public object JPEG : ImageFormat("jpg", "jpeg") {
2424
}
2525

2626
override fun readImage(s: SyncStream, props: ImageDecodingProps): ImageData {
27+
@Suppress("DEPRECATION")
2728
return ImageData(listOf(ImageFrame(JPEGDecoder.decode(s.readAll()))))
2829
}
2930

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
22
package com.adamratzman.spotify
33

4+
import com.adamratzman.spotify.http.HttpRequest
5+
import com.adamratzman.spotify.http.HttpResponse
46
import kotlin.test.assertTrue
57
import kotlinx.coroutines.CoroutineScope
68
import kotlinx.coroutines.Dispatchers
@@ -16,8 +18,13 @@ expect fun getTestClientSecret(): String?
1618
expect fun getTestRedirectUri(): String?
1719
expect fun getTestTokenString(): String?
1820
expect fun isHttpLoggingEnabled(): Boolean
19-
2021
expect suspend fun buildSpotifyApi(): GenericSpotifyApi?
22+
expect fun getResponseCacher(): ResponseCacher?
23+
24+
interface ResponseCacher {
25+
val cachedResponsesDirectoryPath: String
26+
fun cacheResponse(className: String, testName: String, responseNumber: Int, request: HttpRequest, response: HttpResponse)
27+
}
2128

2229
suspend inline fun <reified T : Throwable> assertFailsWithSuspend(crossinline block: suspend () -> Unit) {
2330
val noExceptionMessage = "Expected ${T::class.simpleName} exception to be thrown, but no exception was thrown."

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
88
abstract class AbstractTest<T : GenericSpotifyApi> {
99
lateinit var api: T
1010
var apiInitialized: Boolean = false
11+
var requestNumber = 0
1112

12-
suspend inline fun <reified Z : T> buildApi() {
13+
suspend inline fun <reified Z : T> buildApi(testName: String) {
1314
if (apiInitialized) return
1415

1516
val api = buildSpotifyApi()
1617
if (api != null && api is Z) {
18+
api.spotifyApiOptions.httpResponseSubscriber = { request, response ->
19+
getResponseCacher()?.cacheResponse(
20+
this::class.qualifiedName!!,
21+
testName,
22+
requestNumber,
23+
request,
24+
response
25+
)
26+
requestNumber++
27+
}
28+
1729
this.api = api
1830
apiInitialized = true
1931
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ import kotlin.test.assertNotNull
1313
import kotlin.test.assertNull
1414
import kotlinx.coroutines.ExperimentalCoroutinesApi
1515
import com.adamratzman.spotify.runTestOnDefaultDispatcher
16+
import kotlinx.coroutines.test.TestResult
1617

1718
class ClientEpisodeApiTest : AbstractTest<SpotifyClientApi>() {
1819
@Test
19-
fun testGetEpisode() = runTestOnDefaultDispatcher {
20-
buildApi<SpotifyClientApi>()
20+
fun testGetEpisode(): TestResult = runTestOnDefaultDispatcher {
21+
buildApi<SpotifyClientApi>(::testGetEpisode.name)
2122
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
2223

2324
assertNull(api.episodes.getEpisode("nonexistant episode"))
2425
assertNotNull(api.episodes.getEpisode("3lMZTE81Pbrp0U12WZe27l"))
2526
}
2627

2728
@Test
28-
fun testGetEpisodes() = runTestOnDefaultDispatcher {
29-
buildApi<SpotifyClientApi>()
29+
fun testGetEpisodes(): TestResult = runTestOnDefaultDispatcher {
30+
buildApi<SpotifyClientApi>(::testGetEpisodes.name)
3031
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
3132

3233
assertFailsWith<BadRequestException> { api.episodes.getEpisodes("hi", "dad") }

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import com.adamratzman.spotify.SpotifyClientApi
88
import kotlin.test.Test
99
import kotlinx.coroutines.ExperimentalCoroutinesApi
1010
import com.adamratzman.spotify.runTestOnDefaultDispatcher
11+
import kotlinx.coroutines.test.TestResult
1112

1213
class ClientFollowingApiTest : AbstractTest<SpotifyClientApi>() {
1314
@Test
14-
fun testFollowUnfollowArtists() = runTestOnDefaultDispatcher {
15+
fun testFollowUnfollowArtists(): TestResult = runTestOnDefaultDispatcher {
1516
return@runTestOnDefaultDispatcher // TODO https://github.com/adamint/spotify-web-api-kotlin/issues/309
1617

1718
/*buildApi(SpotifyClientApi::class)
@@ -54,7 +55,7 @@ class ClientFollowingApiTest : AbstractTest<SpotifyClientApi>() {
5455
}
5556

5657
@Test
57-
fun testFollowUnfollowUsers() = runTestOnDefaultDispatcher {
58+
fun testFollowUnfollowUsers(): TestResult = runTestOnDefaultDispatcher {
5859
return@runTestOnDefaultDispatcher // TODO https://github.com/adamint/spotify-web-api-kotlin/issues/309
5960

6061
/*buildApi(SpotifyClientApi::class)
@@ -76,7 +77,7 @@ class ClientFollowingApiTest : AbstractTest<SpotifyClientApi>() {
7677
}
7778

7879
@Test
79-
fun testFollowUnfollowPlaylists() = runTestOnDefaultDispatcher {
80+
fun testFollowUnfollowPlaylists(): TestResult = runTestOnDefaultDispatcher {
8081
return@runTestOnDefaultDispatcher // TODO https://github.com/adamint/spotify-web-api-kotlin/issues/309
8182

8283
/*buildApi(SpotifyClientApi::class)

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import kotlin.test.assertFalse
1313
import kotlin.test.assertTrue
1414
import kotlinx.coroutines.ExperimentalCoroutinesApi
1515
import com.adamratzman.spotify.runTestOnDefaultDispatcher
16+
import kotlinx.coroutines.test.TestResult
1617

1718
class ClientLibraryApiTest : AbstractTest<SpotifyClientApi>() {
1819
@Test
19-
fun testLibraryTracks() = runTestOnDefaultDispatcher {
20-
buildApi<SpotifyClientApi>()
20+
fun testLibraryTracks(): TestResult = runTestOnDefaultDispatcher {
21+
buildApi<SpotifyClientApi>(::testLibraryTracks.name)
2122
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
2223

2324
val testTrack = "3yi3SEVFj0mSiYVu8xT9sF"
@@ -49,8 +50,8 @@ class ClientLibraryApiTest : AbstractTest<SpotifyClientApi>() {
4950
}
5051

5152
@Test
52-
fun testLibraryAlbums() = runTestOnDefaultDispatcher {
53-
buildApi<SpotifyClientApi>()
53+
fun testLibraryAlbums(): TestResult = runTestOnDefaultDispatcher {
54+
buildApi<SpotifyClientApi>(::testLibraryAlbums.name)
5455
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
5556

5657
val testAlbum = "1UAt4G020TgW3lb2CkXr2N"
@@ -82,8 +83,8 @@ class ClientLibraryApiTest : AbstractTest<SpotifyClientApi>() {
8283
}
8384

8485
@Test
85-
fun testLibraryEpisodes() = runTestOnDefaultDispatcher {
86-
buildApi<SpotifyClientApi>()
86+
fun testLibraryEpisodes(): TestResult = runTestOnDefaultDispatcher {
87+
buildApi<SpotifyClientApi>(::testLibraryEpisodes.name)
8788
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
8889

8990
val testEpisode = "5outVI1srKZtqwPrthvkKb"
@@ -112,8 +113,8 @@ class ClientLibraryApiTest : AbstractTest<SpotifyClientApi>() {
112113
}
113114

114115
@Test
115-
fun testLibraryShows() = runTestOnDefaultDispatcher {
116-
buildApi<SpotifyClientApi>()
116+
fun testLibraryShows(): TestResult = runTestOnDefaultDispatcher {
117+
buildApi<SpotifyClientApi>(::testLibraryShows.name)
117118
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
118119

119120
val testShow = "6z4NLXyHPga1UmSJsPK7G1"
@@ -142,8 +143,8 @@ class ClientLibraryApiTest : AbstractTest<SpotifyClientApi>() {
142143
}
143144

144145
@Test
145-
fun testInvalidInputs() = runTestOnDefaultDispatcher {
146-
buildApi<SpotifyClientApi>()
146+
fun testInvalidInputs(): TestResult = runTestOnDefaultDispatcher {
147+
buildApi<SpotifyClientApi>(::testInvalidInputs.name)
147148
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
148149

149150
// tracks

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import kotlin.test.Test
1010
import kotlin.test.assertTrue
1111
import kotlinx.coroutines.ExperimentalCoroutinesApi
1212
import com.adamratzman.spotify.runTestOnDefaultDispatcher
13+
import kotlinx.coroutines.test.TestResult
1314

1415
class ClientPersonalizationApiTest : AbstractTest<SpotifyClientApi>() {
1516
@Test
16-
fun testGetTopArtists() = runTestOnDefaultDispatcher {
17-
buildApi<SpotifyClientApi>()
17+
fun testGetTopArtists(): TestResult = runTestOnDefaultDispatcher {
18+
buildApi<SpotifyClientApi>(::testGetTopArtists.name)
1819
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
1920

2021
assertTrue(
@@ -26,8 +27,8 @@ class ClientPersonalizationApiTest : AbstractTest<SpotifyClientApi>() {
2627
}
2728

2829
@Test
29-
fun testGetTopTracks() = runTestOnDefaultDispatcher {
30-
buildApi<SpotifyClientApi>()
30+
fun testGetTopTracks(): TestResult = runTestOnDefaultDispatcher {
31+
buildApi<SpotifyClientApi>(::testGetTopTracks.name)
3132
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
3233

3334
assertTrue(api.personalization.getTopTracks(5).items.isNotEmpty())

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.adamratzman.spotify.models.toArtistUri
1717
import com.adamratzman.spotify.models.toPlaylistUri
1818
import com.adamratzman.spotify.models.toShowUri
1919
import com.adamratzman.spotify.models.toTrackUri
20+
import com.adamratzman.spotify.runTestOnDefaultDispatcher
2021
import kotlin.test.Test
2122
import kotlin.test.assertEquals
2223
import kotlin.test.assertNotNull
@@ -26,21 +27,21 @@ import kotlin.time.ExperimentalTime
2627
import kotlin.time.measureTime
2728
import kotlinx.coroutines.ExperimentalCoroutinesApi
2829
import kotlinx.coroutines.delay
29-
import com.adamratzman.spotify.runTestOnDefaultDispatcher
30+
import kotlinx.coroutines.test.TestResult
3031

3132
class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
3233
@Test
33-
fun testGetDevices() = runTestOnDefaultDispatcher {
34-
buildApi<SpotifyClientApi>()
34+
fun testGetDevices(): TestResult = runTestOnDefaultDispatcher {
35+
buildApi<SpotifyClientApi>(::testGetDevices.name)
3536
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
3637
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
3738

3839
assertTrue(api.player.getDevices().isNotEmpty())
3940
}
4041

4142
@Test
42-
fun testGetCurrentContext() = runTestOnDefaultDispatcher {
43-
buildApi<SpotifyClientApi>()
43+
fun testGetCurrentContext(): TestResult = runTestOnDefaultDispatcher {
44+
buildApi<SpotifyClientApi>(::testGetCurrentContext.name)
4445
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
4546
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
4647

@@ -70,17 +71,17 @@ class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
7071
}
7172

7273
@Test
73-
fun testGetRecentlyPlayed() = runTestOnDefaultDispatcher {
74-
buildApi<SpotifyClientApi>()
74+
fun testGetRecentlyPlayed(): TestResult = runTestOnDefaultDispatcher {
75+
buildApi<SpotifyClientApi>(::testGetRecentlyPlayed.name)
7576
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
7677
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
7778

7879
api.player.getRecentlyPlayed()
7980
}
8081

8182
@Test
82-
fun testGetCurrentlyPlaying() = runTestOnDefaultDispatcher {
83-
buildApi<SpotifyClientApi>()
83+
fun testGetCurrentlyPlaying(): TestResult = runTestOnDefaultDispatcher {
84+
buildApi<SpotifyClientApi>(::testGetCurrentlyPlaying.name)
8485
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
8586
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
8687

@@ -109,8 +110,8 @@ class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
109110
}
110111

111112
@Test
112-
fun testAddItemToEndOfQueue() = runTestOnDefaultDispatcher {
113-
buildApi<SpotifyClientApi>()
113+
fun testAddItemToEndOfQueue(): TestResult = runTestOnDefaultDispatcher {
114+
buildApi<SpotifyClientApi>(::testAddItemToEndOfQueue.name)
114115
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
115116
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
116117

@@ -132,8 +133,8 @@ class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
132133
}
133134

134135
@Test
135-
fun testSeek() = runTestOnDefaultDispatcher {
136-
buildApi<SpotifyClientApi>()
136+
fun testSeek(): TestResult = runTestOnDefaultDispatcher {
137+
buildApi<SpotifyClientApi>(::testSeek.name)
137138
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
138139
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
139140

@@ -181,8 +182,8 @@ class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
181182
}*/
182183

183184
@Test
184-
fun testStartPlayback() = runTestOnDefaultDispatcher {
185-
buildApi<SpotifyClientApi>()
185+
fun testStartPlayback(): TestResult = runTestOnDefaultDispatcher {
186+
buildApi<SpotifyClientApi>(::testStartPlayback.name)
186187
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
187188
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
188189

@@ -261,8 +262,8 @@ class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
261262
}
262263

263264
@Test
264-
fun testSkipForwardBackward() = runTestOnDefaultDispatcher {
265-
buildApi<SpotifyClientApi>()
265+
fun testSkipForwardBackward(): TestResult = runTestOnDefaultDispatcher {
266+
buildApi<SpotifyClientApi>(::testSkipForwardBackward.name)
266267
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
267268
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
268269

@@ -287,8 +288,8 @@ class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
287288
}
288289

289290
@Test
290-
fun testTransferPlayback() = runTestOnDefaultDispatcher {
291-
buildApi<SpotifyClientApi>()
291+
fun testTransferPlayback(): TestResult = runTestOnDefaultDispatcher {
292+
buildApi<SpotifyClientApi>(::testTransferPlayback.name)
292293
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
293294
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
294295

0 commit comments

Comments
 (0)