I wrote this function using Flask in Python and it should work fine:
from flask import request
import mysql.connector
from user import User
def update_status(self, id):
try:
data = request.get_json()
self.cursor.execute("UPDATE utenteflask SET status = %s WHERE id = %s", (data['status'], id))
self.conn.commit()
return "Status updated", 200
except Exception as e:
print(f"Error: {str(e)}")
return "Error occurred", 500
test:
@patch('flask.request')
def test_update_status(self, mock_request):
mock_cursor = Mock()
mock_connect.return_value.cursor.return_value = mock_cursor
user_id = 1
mock_request.get_json.return_value = {"status": "inactive"}
result, status_code = self.api.update_status(user_id)
self.assertEqual(status_code, 200)
self.assertEqual(result, "Status updated")
but when I use monk in the test it gives me this error:
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
Someone can help me? Thank you!