1+ from urllib .parse import parse_qs , urlparse
2+
13from listennotes import podcast_api
4+ from listennotes .errors import AuthenticationError
25
36
47class TestClient (object ):
@@ -10,7 +13,201 @@ def test_set_apikey(self):
1013 client = podcast_api .Client (api_key = api_key )
1114 assert client .request_headers .get ("X-ListenAPI-Key" ) == api_key
1215
13- def test_search (self ):
16+ def test_search_with_mock (self ):
1417 client = podcast_api .Client ()
15- response = client .search (q = "dummy" )
18+ term = "dummy"
19+ sort_by_date = 1
20+ response = client .search (q = term , sort_by_date = sort_by_date )
1621 assert len (response .json ().get ("results" , [])) > 0
22+ assert response .request .method == "GET"
23+ url = urlparse (response .url )
24+ assert url .path == "/api/v2/search"
25+ params = parse_qs (url .query )
26+ assert params ["q" ][0 ] == term
27+ assert params ["sort_by_date" ][0 ] == "1"
28+
29+ def test_search_with_authentication_error (self ):
30+ api_key = "wrong key"
31+ client = podcast_api .Client (api_key = api_key )
32+ term = "dummy"
33+ sort_by_date = 1
34+ try :
35+ client .search (q = term , sort_by_date = sort_by_date )
36+ except AuthenticationError :
37+ pass
38+ except Exception :
39+ assert False
40+ else :
41+ assert False
42+
43+ def test_typeahead_with_mock (self ):
44+ client = podcast_api .Client ()
45+ term = "dummy"
46+ show_podcasts = 1
47+ response = client .typeahead (q = term , show_podcasts = show_podcasts )
48+ assert len (response .json ().get ("terms" , [])) > 0
49+ assert response .request .method == "GET"
50+ url = urlparse (response .url )
51+ assert url .path == "/api/v2/typeahead"
52+ params = parse_qs (url .query )
53+ assert params ["q" ][0 ] == term
54+ assert params ["show_podcasts" ][0 ] == "1"
55+
56+ def test_fetch_best_podcasts_with_mock (self ):
57+ client = podcast_api .Client ()
58+ genre_id = 23
59+ response = client .fetch_best_podcasts (genre_id = genre_id )
60+ assert response .json ().get ("total" , 0 ) > 0
61+ assert response .request .method == "GET"
62+ url = urlparse (response .url )
63+ assert url .path == "/api/v2/best_podcasts"
64+ params = parse_qs (url .query )
65+ assert params ["genre_id" ][0 ] == str (genre_id )
66+
67+ def test_fetch_podcast_by_id_with_mock (self ):
68+ client = podcast_api .Client ()
69+ podcast_id = "asdfsdaf"
70+ response = client .fetch_podcast_by_id (id = podcast_id )
71+ assert len (response .json ().get ("episodes" , [])) > 0
72+ assert response .request .method == "GET"
73+ url = urlparse (response .url )
74+ assert url .path == "/api/v2/podcasts/%s" % podcast_id
75+
76+ def test_fetch_episode_by_id_with_mock (self ):
77+ client = podcast_api .Client ()
78+ episode_id = "asdfsdaf"
79+ response = client .fetch_episode_by_id (id = episode_id )
80+ assert len (response .json ().get ("podcast" , {}).get ("rss" )) > 0
81+ assert response .request .method == "GET"
82+ url = urlparse (response .url )
83+ assert url .path == "/api/v2/episodes/%s" % episode_id
84+
85+ def test_batch_fetch_podcasts_with_mock (self ):
86+ client = podcast_api .Client ()
87+ ids = "996,777,888,1000"
88+ response = client .batch_fetch_podcasts (ids = ids )
89+ assert parse_qs (response .request .body )["ids" ][0 ] == ids
90+ assert len (response .json ().get ("podcasts" , [])) > 0
91+ assert response .request .method == "POST"
92+ url = urlparse (response .url )
93+ assert url .path == "/api/v2/podcasts"
94+
95+ def test_batch_fetch_episodes_with_mock (self ):
96+ client = podcast_api .Client ()
97+ ids = "996,777,888,100220"
98+ response = client .batch_fetch_episodes (ids = ids )
99+ assert parse_qs (response .request .body )["ids" ][0 ] == ids
100+ assert len (response .json ().get ("episodes" , [])) > 0
101+ assert response .request .method == "POST"
102+ url = urlparse (response .url )
103+ assert url .path == "/api/v2/episodes"
104+
105+ def test_fetch_curated_podcasts_list_by_id_with_mock (self ):
106+ client = podcast_api .Client ()
107+ curated_list_id = "asdfsdaf"
108+ response = client .fetch_curated_podcasts_list_by_id (id = curated_list_id )
109+ assert len (response .json ().get ("podcasts" , [])) > 0
110+ assert response .request .method == "GET"
111+ url = urlparse (response .url )
112+ assert url .path == "/api/v2/curated_podcasts/%s" % curated_list_id
113+
114+ def test_fetch_curated_podcasts_lists_with_mock (self ):
115+ client = podcast_api .Client ()
116+ page = 2
117+ response = client .fetch_curated_podcasts_lists (page = page )
118+ assert response .json ().get ("total" ) > 0
119+ assert response .request .method == "GET"
120+ url = urlparse (response .url )
121+ params = parse_qs (url .query )
122+ assert params ["page" ][0 ] == str (page )
123+ assert url .path == "/api/v2/curated_podcasts"
124+
125+ def test_fetch_podcast_genres_with_mock (self ):
126+ client = podcast_api .Client ()
127+ top_level_only = 1
128+ response = client .fetch_podcast_genres (top_level_only = top_level_only )
129+ assert len (response .json ().get ("genres" , [])) > 0
130+ assert response .request .method == "GET"
131+ url = urlparse (response .url )
132+ params = parse_qs (url .query )
133+ assert params ["top_level_only" ][0 ] == str (top_level_only )
134+ assert url .path == "/api/v2/genres"
135+
136+ def test_fetch_podcast_regions_with_mock (self ):
137+ client = podcast_api .Client ()
138+ response = client .fetch_podcast_regions ()
139+ assert len (response .json ().get ("regions" , {}).keys ()) > 0
140+ assert response .request .method == "GET"
141+ url = urlparse (response .url )
142+ assert url .path == "/api/v2/regions"
143+
144+ def test_fetch_podcast_languages_with_mock (self ):
145+ client = podcast_api .Client ()
146+ response = client .fetch_podcast_languages ()
147+ assert len (response .json ().get ("languages" , [])) > 0
148+ assert response .request .method == "GET"
149+ url = urlparse (response .url )
150+ assert url .path == "/api/v2/languages"
151+
152+ def test_just_listen_with_mock (self ):
153+ client = podcast_api .Client ()
154+ response = client .just_listen ()
155+ assert response .json ().get ("audio_length_sec" , 0 ) > 0
156+ assert response .request .method == "GET"
157+ url = urlparse (response .url )
158+ assert url .path == "/api/v2/just_listen"
159+
160+ def test_fetch_recommendations_for_podcast_with_mock (self ):
161+ client = podcast_api .Client ()
162+ podcast_id = "adfsddf"
163+ response = client .fetch_recommendations_for_podcast (id = podcast_id )
164+ assert len (response .json ().get ("recommendations" , [])) > 0
165+ assert response .request .method == "GET"
166+ url = urlparse (response .url )
167+ assert url .path == "/api/v2/podcasts/%s/recommendations" % podcast_id
168+
169+ def test_fetch_recommendations_for_episode_with_mock (self ):
170+ client = podcast_api .Client ()
171+ episode_id = "adfsddf"
172+ response = client .fetch_recommendations_for_episode (id = episode_id )
173+ assert len (response .json ().get ("recommendations" , [])) > 0
174+ assert response .request .method == "GET"
175+ url = urlparse (response .url )
176+ assert url .path == "/api/v2/episodes/%s/recommendations" % episode_id
177+
178+ def test_fetch_playlist_by_id_with_mock (self ):
179+ client = podcast_api .Client ()
180+ playlist_id = "adfsddf"
181+ response = client .fetch_playlist_by_id (id = playlist_id )
182+ assert len (response .json ().get ("items" , [])) > 0
183+ assert response .request .method == "GET"
184+ url = urlparse (response .url )
185+ assert url .path == "/api/v2/playlists/%s" % playlist_id
186+
187+ def test_fetch_my_playlists_with_mock (self ):
188+ client = podcast_api .Client ()
189+ page = 2
190+ response = client .fetch_my_playlists (page = page )
191+ assert len (response .json ().get ("playlists" , [])) > 0
192+ assert response .request .method == "GET"
193+ url = urlparse (response .url )
194+ assert url .path == "/api/v2/playlists"
195+
196+ def test_submit_podcast_with_mock (self ):
197+ client = podcast_api .Client ()
198+ rss = "http://myrss.com/rss"
199+ response = client .submit_podcast (rss = rss )
200+ assert parse_qs (response .request .body )["rss" ][0 ] == rss
201+ assert len (response .json ().get ("status" , "" )) > 0
202+ assert response .request .method == "POST"
203+ url = urlparse (response .url )
204+ assert url .path == "/api/v2/podcasts/submit"
205+
206+ def test_delete_podcast_with_mock (self ):
207+ client = podcast_api .Client ()
208+ podcast_id = "asdfasdfdf"
209+ response = client .delete_podcast (id = podcast_id )
210+ assert len (response .json ().get ("status" , "" )) > 0
211+ assert response .request .method == "DELETE"
212+ url = urlparse (response .url )
213+ assert url .path == "/api/v2/podcasts/%s" % podcast_id
0 commit comments