0

Here's the code of my test:

@pytest.mark.run(order=18)
def test_post(client):
    """
        Test whether the test client has been added or not.
    """

    print(f"\n\n {'>>>'*6} TESTING CLIENT POST {'<<<'*6} \n\n")
    access_token = cognito_auth.get_access_token({
        "username": os.environ["TEST_USER_EMAIL"],
        "password": os.environ["TEST_USER_PASSWORD"]
    })
    data = {
        "client_code": "999999.9.9",
        "name": "AUTOMATED TEST CLIENT",
        "short_name": "AUTOMATED TEST CLIENT",
        "br_cnpj": "123809128312",
        "br_im": "213798238974324",
        "br_ie": "7893248932794324",
        "address_id": 7665,
        "is_inserted": False,
        "skin_id": 1,
        "plan_id": 1,
        "organization": "CFR-100000",
        "is_api_connected": False
    }

    response = client.post('http://localhost:5000/dev/api/client', json=data, headers={
        "Authorization": f"Bearer {access_token}"
    })

    print("THE RESPONSE")
    print(response.json)

According to this doc, everything should be fine, but instead, I get the following postgres error:

{'error': {'code': 500, 'type': '/errors/internal-server-error', 'message': '(psycopg2.errors.InvalidTextRepresentation) invalid input syntax for type integer: ""\nLINE 1: ... plan_id, organization, is_api_connected) VALUES (\'\', \'99999...\n                                                             ^\n\n[SQL: INSERT INTO tb_client (client_id, client_code, name, short_name, br_cnpj, br_im, br_ie, address_id, is_inserted, skin_id, plan_id, organization, is_api_connected) VALUES (%(client_id)s, %(client_code)s, %(name)s, %(short_name)s, %(br_cnpj)s, %(br_im)s, %(br_ie)s, %(address_id)s, %(is_inserted)s, %(skin_id)s, %(plan_id)s, %(organization)s, %(is_api_connected)s) ON CONFLICT ON CONSTRAINT tb_client_client_code_key DO NOTHING]\n[parameters: {\'client_id\': \'\', \'client_code\': \'999999.9.9\', \'name\': \'AUTOMATED TEST CLIENT\', \'short_name\': \'AUTOMATED TEST CLIENT\', \'br_cnpj\': \'123809128312\', \'br_im\': \'213798238974324\', \'br_ie\': \'7893248932794324\', \'address_id\': 7665, \'is_inserted\': False, \'skin_id\': 1, \'plan_id\': 1, \'organization\': \'CFR-100000\', \'is_api_connected\': False}]\n(Background on this error at: http://sqlalche.me/e/13/9h9h)'}}

Is the client post function seriously only expecting strings for json? It seems that the problem goes away when I use only strings, but I'm not expecting that on the API.

Even if I include "'Content-Type': 'application/json'" on the headers, I get the same error. What could be happening?

Ericson Willians
  • 7,606
  • 11
  • 63
  • 114
  • looks like an actual test failure- note the error is being thrown by your database driver! What does your endpoint look like? – Paul Becotte Dec 08 '20 at 19:17
  • (Also, am assuming the `client` fixture you're using is the one from pytest-flask?) – Paul Becotte Dec 08 '20 at 19:21
  • Yes, it is from the pytest-flask. – Ericson Willians Dec 08 '20 at 19:24
  • The error says `invalid input syntax for type integer` - you sure the passed data are correct? Some integer field is getting a string value. https://stackoverflow.com/questions/23911844/error-invalid-input-syntax-for-integer/23912251 – Dušan Maďar Dec 08 '20 at 19:27
  • Yes, it is. I test the endpoint in the terminal using httpie using the same input data. – Ericson Willians Dec 08 '20 at 19:28
  • Well, based on the error - the only empty string the SQL query uses is to set `client_id` (it tries to set it to `''`). – Dušan Maďar Dec 08 '20 at 19:34
  • 1
    So- a parameter in the api is getting transformed into a sqlalchemy value wrong. In your endpoint, you can run this with a debugger and see how they are getting transmitted, and what doesn't look as expected. Remember it is possible that your tests are using a different app context than your dev instance - maybe there is a middleware that is not installed for example that is translating json into python objects... – Paul Becotte Dec 08 '20 at 19:38

0 Answers0