【发布时间】:2016-05-18 13:44:23
【问题描述】:
我已经编写了一个测试来检查是否在数据库中出现重复记录的情况下引发了 IntegrityError。为了创建那个场景,我发出了两次 REST API。代码如下所示:
class TestPost(APITestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
common.add_users()
def tearDown(self):
super().tearDown()
self.client.logout()
def test_duplicate_record(self):
# first time
response = self.client.post('/api/v1/trees/', dict(alias="some name", path="some path"))
# same request second time
response = self.client.post('/api/v1/trees/', dict(alias="some name", path="some path"))
self.assertEqual(response.status_code, status.HTTP_400_BAD_RREQUEST)
但是我得到一个像这样的错误堆栈
"An error occurred in the current transaction. You can't "
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
我怎样才能避免这个错误,这当然是不可取的。
【问题讨论】:
-
请显示完整的回溯。
标签: django django-rest-framework django-testing