Skip to content

Commit 560d5fe

Browse files
committed
fixed search unit test
1 parent fa2ec9e commit 560d5fe

File tree

4 files changed

+28
-80
lines changed

4 files changed

+28
-80
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,18 @@ public open class SearchApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
8787
*
8888
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/search/search/)**
8989
*
90-
* @param query Search query keywords and optional field filters and operators.
90+
* @param query Search query keywords and optional field filters and operators. You can narrow down your search using field filters. The available filters are album, artist, track, year, upc, tag:hipster, tag:new, isrc, and genre. Each field filter only applies to certain result types.
91+
92+
The artist filter can be used while searching albums, artists or tracks.
93+
The album and year filters can be used while searching albums or tracks. You can filter on a single year or a range (e.g. 1955-1960).
94+
The genre filter can be use while searching tracks and artists.
95+
The isrc and track filters can be used while searching tracks.
96+
The upc, tag:new and tag:hipster filters can only be used while searching albums. The tag:new filter will return albums released in the past two weeks and tag:hipster can be used to return only albums with the lowest 10% popularity.
97+
98+
You can also use the NOT operator to exclude keywords from your search.
99+
100+
Example value:
101+
"remaster%20track:Doxy+artist:Miles%20Davis"
91102
* @param searchTypes A list of item types to search across. Search results include hits from all the specified item types.
92103
* @param limit Maximum number of results to return.
93104
Default: 20
@@ -103,7 +114,10 @@ public open class SearchApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
103114
- Playlist results are not affected by the market parameter.
104115
- If market is set to from_token, and a valid access token is specified in the request header, only content playable in the country associated with the user account, is returned.
105116
- Users can view the country that is associated with their account in the account settings. A user must grant access to the [SpotifyScope.USER_READ_PRIVATE] scope prior to when the access token is issued.
117+
**Note**: episodes will not be returned if this is NOT specified
106118
* @param includeExternal If true, the response will include any relevant audio content that is hosted externally. By default external content is filtered out from responses.
119+
*
120+
* @throws IllegalArgumentException if no search types are provided, or if [SearchType.EPISODE] is provided but [market] is not
107121
*/
108122
public suspend fun search(
109123
query: String,
@@ -114,6 +128,10 @@ public open class SearchApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
114128
includeExternal: Boolean? = null
115129
): SpotifySearchResult {
116130
require(searchTypes.isNotEmpty()) { "At least one search type must be provided" }
131+
if (SearchType.EPISODE in searchTypes) {
132+
requireNotNull(market) { "Market must be provided when SearchType.EPISODE is requested"}
133+
}
134+
117135
val jsonString = get(build(query, market, limit, offset, *searchTypes, includeExternal = includeExternal))
118136
val map = json.decodeFromString(MapSerializer(String.serializer(), JsonObject.serializer()), jsonString)
119137

@@ -184,6 +202,8 @@ public open class SearchApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
184202
- Playlist results are not affected by the market parameter.
185203
- If market is set to from_token, and a valid access token is specified in the request header, only content playable in the country associated with the user account, is returned.
186204
- Users can view the country that is associated with their account in the account settings. A user must grant access to the [SpotifyScope.USER_READ_PRIVATE] scope prior to when the access token is issued.
205+
206+
**Note**: episodes will not be returned if this is NOT specified
187207
* @param includeExternal If true, the response will include any relevant audio content that is hosted externally. By default external content is filtered out from responses.
188208
*/
189209
public fun searchRestAction(

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

Lines changed: 3 additions & 75 deletions
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.utils
33

4+
import com.adamratzman.spotify.SpotifyAppApi
5+
46
/*
57
* Copyright (C) 2012-2019 Neo Visionaries Inc.
68
*
@@ -20,81 +22,7 @@ package com.adamratzman.spotify.utils
2022
/**
2123
* [ISO 3166-1](http://en.wikipedia.org/wiki/ISO_3166-1) country code.
2224
*
23-
*
24-
*
25-
* Enum names of this enum themselves are represented by
26-
* [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)
27-
* code (2-letter upper-case alphabets). There are instance methods to get the
28-
* country name ([.getName]), the
29-
* [ISO 3166-1 alpha-3](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3)
30-
* code ([.getAlpha3]) and the
31-
* [ISO 3166-1 numeric](http://en.wikipedia.org/wiki/ISO_3166-1_numeric)
32-
* code ([.getNumeric]).
33-
* In addition, there are static methods to get a `Market` instance that
34-
* corresponds to a given alpha-2/alpha-3/numeric code ([.getByCode],
35-
* [.getByCode]).
36-
*
37-
*
38-
* <pre style="background-color: #EEEEEE; margin-left: 2em; margin-right: 2em; border: 1px solid black; padding: 0.5em;">
39-
* <span style="color: darkgreen;">// List all the country codes.</span>
40-
* for (Market code : Market.values())
41-
* {
42-
* <span style="color: darkgreen;">// For example, "[US] United States" is printed.</span>
43-
* System.out.format(<span style="color: darkred;">"[%s] %s\n"</span>, code, code.[.getName]);
44-
* }
45-
*
46-
* <span style="color: darkgreen;">// Get a Market instance by ISO 3166-1 code.</span>
47-
* Market code = Market.[getByCode][.getByCode](<span style="color: darkred;">"JP"</span>);
48-
*
49-
* <span style="color: darkgreen;">// Print all the information. Output will be:</span>
50-
* <span style="color: darkgreen;">//</span>
51-
* <span style="color: darkgreen;">// Country name = Japan</span>
52-
* <span style="color: darkgreen;">// ISO 3166-1 alpha-2 code = JP</span>
53-
* <span style="color: darkgreen;">// ISO 3166-1 alpha-3 code = JPN</span>
54-
* <span style="color: darkgreen;">// ISO 3166-1 numeric code = 392</span>
55-
* <span style="color: darkgreen;">// Assignment state = OFFICIALLY_ASSIGNED</span>
56-
* <span style="color: darkgreen;">//</span>
57-
* System.out.println(<span style="color: darkred;">"Country name = "</span> + code.[.getName]);
58-
* System.out.println(<span style="color: darkred;">"ISO 3166-1 alpha-2 code = "</span> + code.[.getAlpha2]);
59-
* System.out.println(<span style="color: darkred;">"ISO 3166-1 alpha-3 code = "</span> + code.[.getAlpha3]);
60-
* System.out.println(<span style="color: darkred;">"ISO 3166-1 numeric code = "</span> + code.[.getNumeric]);
61-
* System.out.println(<span style="color: darkred;">"Assignment state = "</span> + code.[.getAssignment]);
62-
*
63-
* <span style="color: darkgreen;">// Convert to a Locale instance.</span>
64-
*
65-
* <span style="color: darkgreen;">// Get a Market by a Locale instance.</span>
66-
* code = Market.[getByLocale][.getByLocale](locale);
67-
*
68-
* <span style="color: darkgreen;">// Get the currency of the country.</span>
69-
*
70-
* <span style="color: darkgreen;">// Get a list by a regular expression for names.
71-
* //
72-
* // The list will contain:
73-
* //
74-
* // Market.AE ed Arab Emirates
75-
* // Market.GB ed Kingdom
76-
* // Market.TZ : Tanzania, United Republic of
77-
* // Market.UK ed Kingdom
78-
* // Market.UM ed States Minor Outlying Islands
79-
* // Market.US ed States
80-
* //</span>
81-
* List&lt;Market&gt; list = Market.[findByName][.findByName](<span style="color: darkred;">".*United.*"</span>);
82-
*
83-
* <span style="color: darkgreen;">
84-
* // For backward compatibility for older versions than 1.16, some
85-
* // 4-letter ISO 3166-3 codes are accepted by getByCode(String, boolean)
86-
* // and its variants. To be concrete:
87-
* //
88-
* // [ANHH](https://en.wikipedia.org/wiki/ISO_3166-3#ANHH) : Market.AN
89-
* // [BUMM](https://en.wikipedia.org/wiki/ISO_3166-3#BUMM) : Market.BU
90-
* // [CSXX](https://en.wikipedia.org/wiki/ISO_3166-3#CSXX) : Market.CS
91-
* // [NTHH](https://en.wikipedia.org/wiki/ISO_3166-3#NTHH) : Market.NT
92-
* // [TPTL](https://en.wikipedia.org/wiki/ISO_3166-3#TPTL) : Market.TP
93-
* // [YUCS](https://en.wikipedia.org/wiki/ISO_3166-3#YUCS) : Market.YU
94-
* // [ZRCD](https://en.wikipedia.org/wiki/ISO_3166-3#ZRCD) : Market.ZR
95-
* //</span>
96-
* code = Market.[getByCode][.getByCode](<span style="color: darkred;">"ANHH"</span>);
97-
</pre> *
25+
* **Note**: Use [Market.FROM_TOKEN] if you want to use the client's locale. This should not be used with [SpotifyAppApi]
9826
*
9927
* @author Takahiko Kawasaki (https://github.com/TakahikoKawasaki/nv-i18n)
10028
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PublicPlaylistsApiTest : AbstractTest<GenericSpotifyApi>() {
3939
assertEquals("run2", api!!.playlists.getPlaylist("78eWnYKwDksmCHAjOUNPEj")?.name)
4040
assertNull(api!!.playlists.getPlaylist("nope"))
4141
assertTrue(api!!.playlists.getPlaylist("78eWnYKwDksmCHAjOUNPEj")!!.tracks.isNotEmpty())
42-
val playlistWithLocalAndNonLocalTracks = api!!.playlists.getPlaylist("0vzdw0N41qZLbRDqyx2cE0")!!.tracks
42+
val playlistWithLocalAndNonLocalTracks = api!!.playlists.getPlaylist("627gNjNzj3sOrSiDm5acc2")!!.tracks
4343
assertEquals(LocalTrack::class, playlistWithLocalAndNonLocalTracks[0].track!!::class)
4444
assertEquals(Track::class, playlistWithLocalAndNonLocalTracks[1].track!!::class)
4545

@@ -57,7 +57,7 @@ class PublicPlaylistsApiTest : AbstractTest<GenericSpotifyApi>() {
5757
if (!testPrereq()) return@runBlockingTest else api!!
5858

5959
assertTrue(api!!.playlists.getPlaylistTracks("78eWnYKwDksmCHAjOUNPEj").items.isNotEmpty())
60-
val playlist = api!!.playlists.getPlaylistTracks("0vzdw0N41qZLbRDqyx2cE0")
60+
val playlist = api!!.playlists.getPlaylistTracks("627gNjNzj3sOrSiDm5acc2")
6161
assertEquals(LocalTrack::class, playlist[0].track!!::class)
6262
assertEquals(Track::class, playlist[1].track!!::class)
6363
assertFailsWith<SpotifyException.BadRequestException> { api!!.playlists.getPlaylistTracks("adskjfjkasdf") }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SearchApiTest : AbstractTest<GenericSpotifyApi>() {
1818
return runBlockingTest {
1919
super.build<GenericSpotifyApi>()
2020
if (!testPrereq()) return@runBlockingTest else api!!
21-
val query = api!!.search.search("lo", *SearchApi.SearchType.values())
21+
val query = api!!.search.search("lo", *SearchApi.SearchType.values(), market = Market.US)
2222
assertTrue(
2323
query.albums?.items?.isNotEmpty() == true && query.tracks?.items?.isNotEmpty() == true && query.artists?.items?.isNotEmpty() == true &&
2424
query.playlists?.items?.isNotEmpty() == true && query.shows?.items?.isNotEmpty() == true && query.episodes?.items?.isNotEmpty() == true
@@ -28,7 +28,7 @@ class SearchApiTest : AbstractTest<GenericSpotifyApi>() {
2828
query2.albums == null && query2.tracks == null && query2.shows == null && query2.episodes == null &&
2929
query2.artists?.items?.isNotEmpty() == true && query2.playlists?.items?.isNotEmpty() == true
3030
)
31-
val query3 = api!!.search.search("lo", SearchApi.SearchType.SHOW, SearchApi.SearchType.EPISODE)
31+
val query3 = api!!.search.search("lo", SearchApi.SearchType.SHOW, SearchApi.SearchType.EPISODE, market = Market.US)
3232
assertTrue(query3.episodes?.items?.isNotEmpty() == true && query3.shows?.items?.isNotEmpty() == true)
3333
}
3434
}

0 commit comments

Comments
 (0)