在 Flask 路由中模拟数据库连接和返回值

问题描述 投票:0回答:1

我有以下烧瓶应用程序路线:

# routes.py

from flask import jsonify
from app.utils.db import db_connection, db_query

@api.route("/some/route", methods=["GET"])
@auth
def get_data_for_some_route(**kwargs) -> List[Dict]:

    # connect to db
    db_client = db_connection()
 
    # Query db for data
    data = db.query(db_client, GET_STUFF)

    # parse the data example
    parsed_data = data[0]
    
    return jsonify(parsed_data), 200

我如何为此路由编写 pytest,但模拟数据库连接和查询结果?

# test_routes.py

import pytest
from fixtures import SOME_ROUTE_JSON_DATA

mock_token = "12345"

def test_get_data_for_some_route(mocker, client):
    querystring1 = 'Hello'
    querystring2 = 'World'

    # Mock db query function
    mocked_db_query = mocker.patch('app.utils.database.db_connection')
    mocked_db_query.return_value = loads(SOME_ROUTE_JSON_DATA)

    # Call the route
    response = client.get(
        f"/some_route",
        headers={"Authorization": f"{mock_token}"}
    )

    # assert response.json is some value I expect it to be

我有点困惑如何在路线中模拟值。因此可以忽略数据库连接,我也可以说“这就是在这种情况下返回的数据,继续测试该函数,假装这就是数据。”

python flask pytest
1个回答
0
投票

要修补函数,您需要提供正在修补的函数的完整路径。在您的情况下,

db_connection
db_query
都需要修补。你可以做这样的事情:

import pytest
from app import app

# Sample JSON data for testing
SOME_ROUTE_JSON_DATA = '{"key": "value"}'

# Mock token for authorization
mock_token = "12345"

@pytest.fixture
def client():
    """Create a test client using Flask app context."""
    with app.test_client() as client:
        yield client

def test_get_data_for_some_route(mocker, client):
    # Mock db_connection and db_query functions
    mocked_db_connection = mocker.patch('app.utils.database.db_connection')
    mocked_db_query = mocker.patch('app.utils.database.db_query')
    
    # Set the return value for db_query to the sample JSON data
    mocked_db_query.return_value = json.loads(SOME_ROUTE_JSON_DATA)

    # Call the route with mocked data
    response = client.get(
        "/some_route",
        headers={"Authorization": f"Bearer {mock_token}"}
    )

    # Assert response status code
    assert response.status_code == 200

    # Assert response JSON data
    expected_data = {"key": "value"}  # Define the expected JSON response
    assert response.get_json() == expected_data

    # Assert that db_connection and db_query were called with expected arguments
    mocked_db_connection.assert_called_once()
    mocked_db_query.assert_called_once_with(mocked_db_connection.return_value, 'GET_STUFF')
© www.soinside.com 2019 - 2024. All rights reserved.