def test_get_objects_multiple_pages(mocker): mock_make_request = mocker.patch.object(Api, '_make_request') expected_first_data = { 'meta': { 'next': '/api/v1/pedido/search/?limit=50&offset=50' } } expected_second_data = {'meta': {'next': None}} mock_make_request.side_effect = [expected_first_data, expected_second_data] api = Api(api_key='fake-api-key', app_key='fake-app-key') objs = api._get_objects('https://api.awsli.com.br/api/v1/pedido/search') first_page = next(objs) mock_make_request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search', params={}) assert first_page == expected_first_data second_page = next(objs) mock_make_request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search/?limit=50&offset=50', params={}) assert second_page == expected_second_data assert mock_make_request.call_count == 2 with pytest.raises(StopIteration): next(objs)
def test_build_session(mocker): api = Api(api_key='fake-api-key', app_key='fake-app-key') assert isinstance(api.session, Session) assert api.session.headers['Content-type'] == 'application/json' assert api.session.headers[ 'Authorization'] == 'chave_api fake-api-key aplicacao fake-app-key'
def test_mark_order_as_delivered(mocker): mock_update_order_status = mocker.patch.object(Api, 'update_order_status') api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.mark_order_as_delivered(12345) mock_update_order_status.assert_called_with(12345, 'pedido_entregue') assert res == mock_update_order_status.return_value
def test_get_paid_orders(mocker): mock_get_orders = mocker.patch.object(Api, 'get_orders') api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.get_paid_orders() mock_get_orders.assert_called_with(limit=50, situacao_id=4) assert res == mock_get_orders.return_value
def test_mark_order_as_in_separation(mocker): mock_update_order_status = mocker.patch.object(Api, 'update_order_status') api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.mark_order_as_in_separation(12345) mock_update_order_status.assert_called_with(12345, 'pedido_em_separacao') assert res == mock_update_order_status.return_value
def test_get_category(mocker): mock_make_request = mocker.patch.object(Api, '_make_request') api = Api(api_key='fake-api-key', app_key='fake-app-key') category = api.get_category(12345) mock_make_request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/categoria/12345') assert category == mock_make_request.return_value
def test_get_product_images(mocker): mock_make_request = mocker.patch.object(Api, '_make_request') api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.get_product_images(12345) mock_make_request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/produto_imagem/?produto=12345') assert res == mock_make_request.return_value
def test_get_products(mocker): mock_get_objects = mocker.patch.object(Api, '_get_objects') api = Api(api_key='fake-api-key', app_key='fake-app-key') order = api.get_products() mock_get_objects.assert_called_with( 'https://api.awsli.com.br/api/v1/produto/', limit=50) assert order == mock_get_objects.return_value
def test_get_clients(mocker): mock_get_objects = mocker.patch.object(Api, '_get_objects') api = Api(api_key='fake-api-key', app_key='fake-app-key') pages = api.get_clients() mock_get_objects.assert_called_with( 'https://api.awsli.com.br/api/v1/clientes', limit=50) assert pages == mock_get_objects.return_value
def test_get_brands_with_custom_limit(mocker): mock_get_objects = mocker.patch.object(Api, '_get_objects') api = Api(api_key='fake-api-key', app_key='fake-app-key') pages = api.get_brands(limit=20) mock_get_objects.assert_called_with( 'https://api.awsli.com.br/api/v1/marca', limit=20) assert pages == mock_get_objects.return_value
def test_get_orders_with_custom_limit(mocker): mock_get_objects = mocker.patch.object(Api, '_get_objects') api = Api(api_key='fake-api-key', app_key='fake-app-key') order = api.get_orders(limit=20) mock_get_objects.assert_called_with( 'https://api.awsli.com.br/api/v1/pedido/search', limit=20) assert order == mock_get_objects.return_value
def test_get_order(mocker): mock_make_request = mocker.patch.object(Api, '_make_request') api = Api(api_key='fake-api-key', app_key='fake-app-key') order = api.get_order(12345) mock_make_request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/pedido/12345') assert order == mock_make_request.return_value
def test_get_objects(mocker): mock_make_request = mocker.patch.object(Api, '_make_request') api = Api(api_key='fake-api-key', app_key='fake-app-key') objs = api._get_objects('https://api.awsli.com.br/api/v1/pedido/search') page = next(objs) mock_make_request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search', params={}) assert page == mock_make_request.return_value
def test_make_request_should_raise_general_exception_for_HTTPError(mocker): mock_session = mocker.patch('lojaintegrada.requests.Session') mock_session.return_value.request.side_effect = HTTPError( response=mock.Mock(status_code=400)) api = Api(api_key='fake-api-key', app_key='fake-app-key') with pytest.raises(ApiError): res = api._make_request( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search')
def test_get_in_separation_orders_with_custom_limit(mocker): mock_get_orders = mocker.patch.object(Api, 'get_orders') api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.get_in_separation_orders(limit=20) mock_get_orders.assert_called_with( limit=20, situacao_id=15 ) assert res == mock_get_orders.return_value
def test_get_status_history_with_order_id(mocker): mock_get_objects = mocker.patch.object(Api, '_get_objects') api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.get_status_history(order_id=12345) mock_get_objects.assert_called_with( 'https://api.awsli.com.br/api/v1/situacao_historico/search', limit=50, numero=12345 ) assert res == mock_get_objects.return_value
def test_update_order_status(mocker): mock_make_request = mocker.patch.object(Api, '_make_request') expected_data = {'codigo': 'pedido_pago'} api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.update_order_status(12345, 'pedido_pago') mock_make_request.assert_called_with( 'PUT', 'https://api.awsli.com.br/api/v1/situacao/pedido/12345', data=expected_data ) assert res == mock_make_request.return_value
def test_make_request_should_raise_general_exception_for_RequestException( mocker): mock_session = mocker.patch('lojaintegrada.requests.Session') mock_request = mock.Mock() mock_session.return_value.request.side_effect = RequestException( request=mock_request) api = Api(api_key='fake-api-key', app_key='fake-app-key') with pytest.raises(ApiError) as e: res = api._make_request( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search') assert e.value.request == mock_request
def test_make_request(mocker): mock_session = mocker.patch('lojaintegrada.requests.Session') api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api._make_request('GET', 'https://api.awsli.com.br/api/v1/pedido/search') mock_session.return_value.request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search', json=None, params=None) mock_session.return_value.request.return_value.raise_for_status.assert_called_with( ) assert res == mock_session.return_value.request.return_value.json.return_value
def test_make_request_should_retry_when_rate_limit_reached(mocker): mock_sleep = mocker.patch('lojaintegrada.time.sleep') mock_session = mocker.patch('lojaintegrada.requests.Session') mock_session.return_value.request.side_effect = [ HTTPError(response=mock.Mock(status_code=429)), mock.DEFAULT ] api = Api(api_key='fake-api-key', app_key='fake-app-key') api._make_request('GET', 'https://api.awsli.com.br/api/v1/pedido/search') mock_sleep.assert_called_with(10) assert mock_session.return_value.request.call_count == 2 mock_session.return_value.request.assert_called_with( 'GET', 'https://api.awsli.com.br/api/v1/pedido/search', json=None, params=None)
def test_update_order_status(mocker): mock_make_request = mocker.patch.object(Api, '_make_request') shipping_id = '12345' tracking_code = 'PU13898292932BR' expected_data = {'objeto': tracking_code} api = Api(api_key='fake-api-key', app_key='fake-app-key') res = api.update_tracking_code(shipping_id, tracking_code) mock_make_request.assert_called_with( 'PUT', 'https://api.awsli.com.br/api/v1/pedido_envio/{}'.format( shipping_id ), data=expected_data ) assert res == mock_make_request.return_value
def build_lojaintegrada_api(api_key=environ['LOJA_INTEGRADA_API_KEY'], app_key=environ['LOJA_INTEGRADA_APP_KEY']): return Api(api_key, app_key)
import logging from decouple import config from lojaintegrada import Api """ Before running this example, create a .env file with two vars: API_KEY='your-api-key' APP_KEY='your-app-key' """ logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG, datefmt='%I:%M:%S') api_key = config('API_KEY') app_key = config('APP_KEY') api = Api(api_key, app_key) for page in api.get_categories(limit=5): for obj in page['objects']: print(obj)