gpt4 book ai didi

python - 模拟 python 数据库查询

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:22 24 4
gpt4 key购买 nike

我尝试通过模拟数据库查询进行测试,但收到错误:

Asssertion error:AssertionError: Expected call: execute()
Not called
and create_table() not defined.

我想要调用 execute() 并使用 create_table() 返回针对预定义值进行断言的响应。

应用程序.py

from flask import Flask,g

@app.before_request
def before_request():
g.db = mysql.connector.connect(user='root', password='root', database='mysql')

def create_table():
try:
cur = g.db.cursor() #here g is imported form Flask module
cursor.execute ('CREATE TABLE IF NOT EXISTS Man (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40)')
data = dict(Table='Man is created')
resp = jsonify(data)
cursor.close()
return resp

测试.py

  import unittest
from app import *
from mock import patch

class Test(unittest.TestCase):
def test_create(self):
with patch("app.g") as mock_g:
mock_g.db.cursor()
mock_g.execute.assert_called_with()
resp = create_table()
assertEqual(json, '{"Table":"Testmysql is created","Columns": ["id","name","email"]}')

我做错了什么?有人可以告诉我如何解决吗

最佳答案

我相信您需要在关闭光标之前添加更改,否则执行将不会发生。尝试在 cursor.close() 之前(或代替)添加 cursor.commit()

关于python - 模拟 python 数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31417536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com