Result is always 404, but the records exist. Where can the error be?

Asked

Viewed 54 times

-3

POST /sinalizar_interesse/9/3/ means that 9 is interested(a) in 3.

Code:

    def localiza_id(id_usuario):
        for pess in database['PESSOA']:
            if pess['id'] == id_usuario:
                return 200
        return 404
    @app.route('/sinalizar_interesse/<int:id_interessada>/<int:id_alvo>', methods=['POST'])
    def sinalizar_interesse(id_interessada, id_alvo):
        pessoa_busca = (id_interessada)
        pessoa_alvo = (id_alvo)
        if (localiza_id(pessoa_busca) or localiza_id(pessoa_alvo)) == 404:
            return 404
        if (localiza_id(pessoa_busca) and localiza_id(pessoa_alvo)) == 200:
            return 200

It is giving error in these tests. It should give the code 200, but independent of the test it returns 404. I do not understand what is wrong, I do not know if it is the method because it is using method POST and the tests use the method PUT and I’m not getting code 200.

Testing:

        #agora posso marcar interesse de 3 pra 9 e de 9 pra 3 sem problemas
        r = requests.put('http://localhost:5003/sinalizar_interesse/9/3/')
        self.assertEqual(r.status_code,200)
        r = requests.put('http://localhost:5003/sinalizar_interesse/3/9/')
        self.assertEqual(r.status_code,200)
  • 2

    The link http://localhost:5003//signal_interest/9/3/ does not imply anything in understanding the question... localhost is an environment on your machine, only you can access....

  • Read: https://answall.com/questions/447033/interesse-tinder-localhost#comment858395_447033

1 answer

0


I don’t know Python, but looking at his code you can see that there is a problem in his logic.

The endpoint with put does not exist (you test with put, but the endpoint created is with post. In this case, any of the verbs can be used, as long as you understand the difference between them. What will change is the semantics that you want.

The above point may not be the problem, because depending on the language/framework may work, but recommended the change so that your code is coherent.

What cat is incorrect in logic is the comparison of the result of the search of people. Note that you do "search person" OU "target person" and compares this result with 404. The same right below, using E and 200. To fix this part, compare BOTH results with 404 or 200.

Example: if (localiza_id(pessoa_busca) or localiza_id(pessoa_alvo)) == 404: should be if localiza_id(pessoa_busca) == 404 or localiza_id(pessoa_alvo) == 404:.

  • There are still things that can be improved in the code, but I’m on the mobile and I tried to indicate only what seems to be critical. Later I add some suggestions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.