"""Test for Deezer connector module.""" import pytest import requests from availability import exceptions from availability.connectors.stores import deezer def test_query_store_calls_correct_url(mocker, fake_response): """Assert correct url is called when searching for product by upc.""" requests_mock = mocker.patch( 'availability.connectors.stores.deezer.requests.get', return_value=fake_response) test_upc = 'test_upc' deezer.query_store(test_upc) requests_mock.assert_called_with( 'https://api.deezer.com/album/upc:{}'.format(test_upc)) def test_query_store_returns_dict(mocker, fake_response): """Assert dict is returned by query store.""" mocker.patch( 'availability.connectors.stores.deezer.requests.get', return_value=fake_response) test_upc = 'test_upc' response = deezer.query_store(test_upc) assert response == {} def test_query_store_handles_timeout_exception(mocker): """Assert timeout response is handled.""" mocker.patch( 'availability.connectors.stores.deezer.requests.get', side_effect=requests.Timeout) with pytest.raises(exceptions.StoreRequestError): deezer.query_store('test_upc') @pytest.mark.parametrize('json_error', [TypeError, ValueError]) def test_query_store_handles_json_exception(mocker, json_error, fake_response): """Assert json error caused by invalid data is handled.""" mocker.patch( 'availability.connectors.stores.deezer.requests.get', return_value=fake_response) mocker.patch( 'availability.connectors.stores.deezer.json.loads', side_effect=json_error) with pytest.raises(exceptions.StoreResponseParseError): deezer.query_store('test_upc')