def test_should_call_make_request_with_correct_arguments(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    resp = api.get_logistics()

    mock_make_request.assert_called_with('GET', '/logisticas/servicos')
def test_should_return_correct_data(mocker):
    mock_request = mocker.patch('bling.base.requests.Session.request')

    api = Api(api_key='fake-api-key')
    resp = api._make_request('GET', '/pedido')

    assert resp == mock_request.return_value.json.return_value
Exemple #3
0
def test_should_return_correct_content(mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    objs = api.get_invoices(['17/05/2019', '17/05/2019'], 1)

    assert objs == mock_get_objects.return_value
def test_should_return_correct_data(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    resp = api.issue_invoice(12345, 1)

    assert resp == mock_make_request.return_value
Exemple #5
0
def test_should_return_correct_content(mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    objs = api.get_products()

    assert objs == mock_get_objects.return_value
def test_should_return_correct_content(mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    accounts = api.get_accounts_payable(['17/05/2019', '17/05/2019'], 'pago')

    assert accounts == mock_get_objects.return_value
Exemple #7
0
def test_should_call_make_requests_at_least_2_times(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    mock_make_request.side_effect = [{
        'retorno': {
            'notasfiscais': [{
                'notafiscal': {
                    'numero': '12345'
                }
            }]
        }
    }, {
        'retorno': {
            'erros': []
        }
    }]

    api = Api(api_key='fake-api-key')
    api._get_objects('notasfiscais', 'notafiscal')

    assert mock_make_request.call_count == 2

    expected_calls = [
        call('GET', '/notasfiscais/page=1', params=None),
        call('GET', '/notasfiscais/page=2', params=None)
    ]
    mock_make_request.assert_has_calls(expected_calls, any_order=True)
def test_should_return_correct_data(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    mock_make_request.return_value = {
        'retorno': {
            'situacoes': [{
                'situacao': {
                    'id': '9',
                    'idHerdado': '0',
                    'nome': 'Atendido',
                    'cor': '#3FB57A'
                }
            }]
        }
    }

    module = 'Vendas'

    api = Api(api_key='fake-api-key')
    resp = api.get_situations(module)

    assert resp == [{
        'situacao': {
            'id': '9',
            'idHerdado': '0',
            'nome': 'Atendido',
            'cor': '#3FB57A'
        }
    }]
Exemple #9
0
def test_should_call_make_request_with_correct_arguments(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    resp = api.get_invoice(12345, 1)

    mock_make_request.assert_called_with('GET', '/notafiscal/12345/1')
Exemple #10
0
def test_should_call_make_request_with_correct_arguments(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    api.get_product(12345)

    mock_make_request.assert_called_with('GET', '/produto/12345')
Exemple #11
0
def test_should_return_correct_data_for_incoming_invoice(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    resp = mock_make_request.return_value = {
        'retorno': {
            'notasfiscais': [
                {
                    'notafiscal': {
                        'numero': '12345',
                        'tipo': 'E'
                    }
                },
                {
                    'notafiscal': {
                        'numero': '12345',
                        'tipo': 'S'
                    }
                },
            ]
        }
    }

    api = Api(api_key='fake-api-key')
    invoice = api.get_invoice(12345, 1, 'E')

    assert invoice == resp['retorno']['notasfiscais'][0]['notafiscal']
Exemple #12
0
def test_should_call_get_objects_with_default_arguments_when_no_arguments_are_provided(
        mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    api.get_products()

    expected_params = {'estoque': 'S', 'imagem': 'N'}
    mock_get_objects.assert_called_with('produtos', 'produto', expected_params)
Exemple #13
0
def test_should_call_get_objects_with_correct_arguments_when_no_filters_are_provided(mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    api.get_orders()

    expected_params = {}
    mock_get_objects.assert_called_with(
        'pedidos', 'pedido', expected_params
    )
def test_should_call_make_request_with_default_arguments(mocker):
    mock_request = mocker.patch('bling.base.requests.Session.request')

    api = Api(api_key='fake-api-key')
    api._make_request('GET', '/pedido')

    mock_request.assert_called_with(
        'GET',
        'https://bling.com.br/Api/v2/pedido/json/?apikey=fake-api-key',
        params=None,
        data=None)
def test_should_call_make_request_with_correct_arguments(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    module = 'Vendas'

    api = Api(api_key='fake-api-key')
    resp = api.get_situations(module)

    mock_make_request.assert_called_with(
        'GET',
        '/situacao/%s' % module
    )
def test_should_update_stock(mocker):
    mock_update_product = mocker.patch.object(Api, 'update_product')

    code = 'SKU-123'
    qty = 5

    api = Api(api_key='fake-api-key')
    resp = api.update_stock(code, qty)

    expected_xml = '<produto><codigo>SKU-123</codigo><estoque>5</estoque></produto>'
    mock_update_product.assert_called_with(code, expected_xml)

    assert resp == mock_update_product.return_value
def test_should_raise_ApiError_when_a_RequestException_occurs(mocker):
    mock_request = mocker.patch('bling.base.requests.Session.request')

    request = mock.Mock()
    mock_request.side_effect = RequestException(request=request, )

    api = Api(api_key='fake-api-key')

    with pytest.raises(ApiError) as e:
        api._make_request('GET', '/pedido')

    assert e.value.request == request
    assert e.value.response == None
def test_should_update_product(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    code = 'SKU-123'
    xml = '<produto></produto>'
    resp = api.update_product(code, xml)

    mock_make_request.assert_called_with('POST',
                                         '/produto/{}'.format(code),
                                         data={'xml': xml})

    assert resp == mock_make_request.return_value
def test_should_call_make_request_with_sendEmail_param(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    api.issue_invoice(12345, 1)

    mock_make_request.assert_called_with(
        'POST', '/notafiscal',
        data={
            'number': 12345,
            'serie': 1,
            'sendEmail': 'true'
        }
    )
def test_should_issue_invoice_but_dont_send_email(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    resp = api.issue_invoice(12345, 1, send_email=False)

    mock_make_request.assert_called_with(
        'POST', '/notafiscal',
        data={
            'number': 12345,
            'serie': 1,
            'sendEmail': 'false'
        }
    )
def test_should_create_order_without_nfe(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    api = Api(api_key='fake-api-key')
    xml = '<pedido></pedido>'
    resp = api.create_order(xml)

    mock_make_request.assert_called_with('POST',
                                         '/pedido',
                                         data={
                                             'xml': xml,
                                             'gerarnfe': False
                                         })

    assert resp == mock_make_request.return_value
Exemple #22
0
def test_should_call_get_objects_with_correct_arguments_when_all_filters_are_provided(
        mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    api.get_accounts_receivable(issued_date=['17/05/2019', '17/05/2019'],
                                due_date=['17/05/2019', '17/05/2019'],
                                situation='pago')

    expected_params = {
        'filters':
        'dataEmissao[17/05/2019 TO 17/05/2019];dataVencimento[17/05/2019 TO 17/05/2019];situacao[pago]'
    }
    mock_get_objects.assert_called_with('contasreceber', 'contaReceber',
                                        expected_params)
def test_should_update_situation(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    number = '12345'
    situation_id = 7

    api = Api(api_key='fake-api-key')
    resp = api.update_order_situation(number, situation_id)

    xml = '<pedido><idSituacao>%s</idSituacao></pedido>' % situation_id

    mock_make_request.assert_called_with('PUT',
                                         '/pedido/%s' % number,
                                         params={'xml': xml})

    assert resp == mock_make_request.return_value
def test_should_return_correct_data(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    mock_make_request.return_value = {
        'retorno': {
            'logisticas': [[{
                'logistica': {
                    'id_logistica': '57389'
                }
            }]]
        }
    }

    api = Api(api_key='fake-api-key')
    resp = api.get_logistics()

    assert resp == [{'id_logistica': '57389'}]
Exemple #25
0
def test_should_call_get_objects_with_correct_arguments_when_all_filters_are_provided(
        mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    api.get_invoices(
        issued_date=['17/05/2019', '17/05/2019'],
        situation='pago',
        type='S',
    )

    expected_params = {
        'filters':
        'dataEmissao[17/05/2019 TO 17/05/2019];situacao[pago];tipo[S]'
    }
    mock_get_objects.assert_called_with('notasfiscais', 'notafiscal',
                                        expected_params)
Exemple #26
0
def test_should_return_correct_data(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    mock_make_request.return_value = {
        'retorno': {
            'produtos': [{
                'produto': {
                    'numero': '12345'
                }
            }]
        }
    }

    api = Api(api_key='fake-api-key')
    resp = api.get_product(12345)

    assert resp == {'numero': '12345'}
Exemple #27
0
def test_should_call_get_objects_with_correct_arguments_when_all_filters_are_provided(mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    api.get_orders(
        issued_date=['17/05/2019', '17/05/2019'],
        change_date=['17/05/2019', '17/05/2019'],
        expected_date=['17/05/2019', '17/05/2019'],
        situation_id=1,
        contact_id=1,
    )

    expected_params = {
        'filters': 'dataEmissao[17/05/2019 TO 17/05/2019];dataAlteracao[17/05/2019 TO 17/05/2019];dataPrevista[17/05/2019 TO 17/05/2019];idSituacao[1];idContato[1]'
    }
    mock_get_objects.assert_called_with(
        'pedidos', 'pedido', expected_params
    )
Exemple #28
0
def test_should_update_tracking_code(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    nf_number = '12345'
    nf_series = '1'
    service_id = '3797312'
    tracking = 'PX0183938391262'

    api = Api(api_key='fake-api-key')
    resp = api.update_tracking_code(nf_number, nf_series, service_id, tracking)

    expected_uri = '/logistica/rastreamento/notafiscal/{}/{}'.format(
        nf_number, nf_series)
    expected_xml = '<rastreamentos><rastreamento><id_servico>{}</id_servico><codigo>{}</codigo></rastreamento></rastreamentos>'.format(
        service_id, tracking)
    mock_make_request.assert_called_with('POST',
                                         expected_uri,
                                         data={'xml': expected_xml})

    assert resp == mock_make_request.return_value
Exemple #29
0
def test_should_return_correct_data(mocker):
    mock_make_request = mocker.patch.object(Api, '_make_request')

    mock_make_request.side_effect = [{
        'retorno': {
            'notasfiscais': [{
                'notafiscal': {
                    'numero': '12345'
                }
            }]
        }
    }, {
        'retorno': {
            'erros': []
        }
    }]

    api = Api(api_key='fake-api-key')
    objs = api._get_objects('notasfiscais', 'notafiscal')

    assert objs == [{'numero': '12345'}]
Exemple #30
0
def test_should_call_get_objects_with_correct_arguments_when_all_arguments_are_provided(
        mocker):
    mock_get_objects = mocker.patch.object(Api, '_get_objects')

    api = Api(api_key='fake-api-key')
    api.get_products(creation_date=['01/04/2020', '18/04/2020'],
                     modification_date=['01/04/2020', '18/04/2020'],
                     store_creation_date=['01/04/2020', '18/04/2020'],
                     store_modification_date=['01/04/2020', '18/04/2020'],
                     situation='A',
                     type='P',
                     store_code='8987292029202')

    expected_params = {
        'estoque':
        'S',
        'imagem':
        'N',
        'loja':
        '8987292029202',
        'filters':
        'dataInclusao[01/04/2020 TO 18/04/2020];dataAlteracao[01/04/2020 TO 18/04/2020];dataInclusaoLoja[01/04/2020 TO 18/04/2020];dataAlteracaoLoja[01/04/2020 TO 18/04/2020];tipo[P];situacao[A]'
    }
    mock_get_objects.assert_called_with('produtos', 'produto', expected_params)