def should_raise_exception_on_connection_error(self): self.requests.get.side_effect = requests.ConnectionError() with pytest.raises(EchoNestError) as e: get_track_analysis('foo') assert e.value.message == 'Error connecting to the Echonest API'
def should_raise_exception_no_summary(self): self.requests.get.return_value = mock.MagicMock( status_code=httplib.OK, json=mock.MagicMock(return_value={})) with pytest.raises(EchoNestError) as e: get_track_analysis('foo') assert e.value.message == 'Response payload does not contain response element'
def should_raise_exception_not_200_response(self): self.requests.get.return_value = mock.MagicMock( status_code=httplib.NOT_FOUND) with pytest.raises(EchoNestError) as e: get_track_analysis('foo') assert e.value.message == 'Echonest API response code was 404 not 200'
def should_raise_exception_on_json_decode_error(self): self.requests.get.return_value = mock.MagicMock( status_code=httplib.OK, json=mock.MagicMock(side_effect=JSONDecodeError('foo', 'bar', 0))) with pytest.raises(EchoNestError) as e: get_track_analysis('foo') assert e.value.message == 'Unable to decode response data to dict'
def update_analysis(pk): """ Task for updating an existing tracks analysis asynchronously from Echo Nest. pk : str The UUID Primary Key of the Track """ track = Track.query.get(pk) if track is None: # TODO: Log This return False # Call Echo Nest try: analysis = get_track_analysis(track.spotify_uri) except EchoNestError: # TODO: Log This return False # commit analysis data to Track model track.audio_summary = analysis db.session.add(track) db.session.commit() return True
def should_return_audio_summary(self): analysis = get_track_analysis('foo') assert type(analysis) == dict assert analysis['danceability'] == 0.5164314670162907