【发布时间】:2020-08-28 01:24:01
【问题描述】:
我正在尝试使嵌套的transaction.atomic() 工作。以下代码块在退出第一个transaction.atomic() 时崩溃,并出现以下错误MySQLdb._exceptions.OperationalError: (1305, 'SAVEPOINT s4568333760_x1 does not exist')
from django.contrib.auth.models import User
from django.test import TransactionTestCase
from django.db import transaction
class FooTest(TransactionTestCase):
def test_bar(self):
with transaction.atomic():
with transaction.atomic():
u = User.objects.create_user(username="abc", password="pass")
print("created user: {}".format(u.username))
这似乎是因为 Django 在测试期间未能执行TRANSACTION START 或SET AUTOCOMMIT=0。我通过查看本地 MySQL 查询日志知道这一点。
当然,我的最终测试并没有那么简单,但下面的例子显示了应该工作的概念不应该。
是我做错了什么还是 Django 的错误?
【问题讨论】:
标签: python mysql django unit-testing transactions