Fix mocking in Spotify tests

The mock wasn't being triggered; these tests were going to the network. Now we
don't match on the query string and instead test that it was correct by
actually parsing it.
This commit is contained in:
Adrian Sampson 2014-09-16 10:43:53 -07:00
parent 9a382eb581
commit 0bdd0c7159

View file

@ -7,6 +7,7 @@ from beets import config
from beets.library import Item
from beetsplug import spotify
from helper import TestHelper
import urlparse
class ArgumentsMock(object):
@ -16,6 +17,11 @@ class ArgumentsMock(object):
self.verbose = True
def _params(url):
"""Get the query parameters from a URL."""
return urlparse.parse_qs(urlparse.urlparse(url).query)
class SpotifyPluginTest(_common.TestCase, TestHelper):
def setUp(self):
@ -37,6 +43,7 @@ class SpotifyPluginTest(_common.TestCase, TestHelper):
def test_empty_query(self):
self.assertEqual(None, self.spotify.query_spotify(self.lib, "1=2"))
@responses.activate
def test_missing_request(self):
response_body = str(
'{'
@ -52,9 +59,7 @@ class SpotifyPluginTest(_common.TestCase, TestHelper):
'}'
'}'
)
responses.add(responses.GET,
'https://api.spotify.com/v1/search?q=duifhjslkef+album'
'%3Alkajsdflakjsd+artist%3A&type=track',
responses.add(responses.GET, 'https://api.spotify.com/v1/search',
body=response_body, status=200,
content_type='application/json')
item = Item(
@ -67,6 +72,14 @@ class SpotifyPluginTest(_common.TestCase, TestHelper):
item.add(self.lib)
self.assertEquals([], self.spotify.query_spotify(self.lib, ""))
params = _params(responses.calls[0].request.url)
self.assertEquals(
params['q'],
['duifhjslkef album:lkajsdflakjsd artist:ujydfsuihse'],
)
self.assertEquals(params['type'], ['track'])
@responses.activate
def test_track_request(self):
response_body = str(
'{'
@ -164,10 +177,7 @@ class SpotifyPluginTest(_common.TestCase, TestHelper):
'}'
'}'
)
responses.add(responses.GET,
'https://api.spotify.com/v1/search?q=Happy+album%3A'
'Despicable%20Me%202+artist%3APharrell%20'
'Williams&type=track',
responses.add(responses.GET, 'https://api.spotify.com/v1/search',
body=response_body, status=200,
content_type='application/json')
item = Item(
@ -183,6 +193,13 @@ class SpotifyPluginTest(_common.TestCase, TestHelper):
self.assertEquals("6NPVjNh8Jhru9xOmyQigds", results[0]['id'])
self.spotify.output_results(results)
params = _params(responses.calls[0].request.url)
self.assertEquals(
params['q'],
['Happy album:Despicable Me 2 artist:Pharrell Williams'],
)
self.assertEquals(params['type'], ['track'])
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)