Django newbie here. I'm trying to implement unit tests for a simple API I developed. Below you can find my test implementation which works fine:
from django.test import TestCase
from my_app.models import MyModel
class TestMyViewSet(TestCase):
"""
Unit tests for endpoints in MyViewSet.
"""
fixtures = ['my_app/fixtures/data.yaml']
def setUp(self):
# Making setup for the test case here.
def test_post_my_resource(self):
# Checking that fixture is loaded correctly.
self.assertEqual(MyModel.objects.all().count(),1)
request_body = {
'my_resource': "input_for_my_resource"
}
response = self.client.post('/my_resources/', data=request_body)
self.assertEqual(response.status_code, 201)
# self.assertEqual(MyModel.objects.all().count(),2)
But when I removed the last line self.assertEqual(MyModel.objects.all().count(),2)
from the comment to test that my_resource
is created successfully on the corresponding model by checking the number of instances, I got an error stating the following:
TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
What am I missing here?
Thanks in advance!
PS: I come across the following question: TransactionManagementError “You can't execute queries until the end of the 'atomic' block” while using signals, but only during Unit Testing but I'm not sure what's happening in my case is the same.