データベースに重複レコードがある場合にIntegrityErrorが生成されたかどうかを確認するテストを作成しました。このシナリオを作成するために、私はREST APIを2回発行しています。コードは次のようになります。UnittestをDjango Restフレームワークで実行しているときのTransactionManagementError
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.
のようなエラー・スタックを取得します。
完全なトレースバックを表示してください。 – Alasdair