Sunday, February 23, 2020

Issue with Fixutre loading with multiple test cases on Postgres - duplicate key value violates unique constraint

class Test1(TestCase):      fixtures = ['fixture.json']        @classmethod      def tearDownClass(cls):          super(TestCase, cls).tearDownClass()        def test_demo(self):          u = User.objects.get(username='testuser1')          #some testing - removed actual tests as we can replicate the issue with even this code    class Test2(TestCase):      fixtures = ['fixture.json']        def test_demo2(self):          u = User.objects.get(username='testuser1')          #Some testing

I have these two test cases and my fixture has 2 rows in total, one for the User table with pk=1, and the other for profile table (one-to-one relation with User table)

If I run the test cases independently, both of the work perfect;y. But if I run them both at the same time, I get this error

Could not load app1.Profile(pk=1): duplicate key value violates unique constraint "app1_profile_user_id_key"

From my understanding of the documentation, Django will rollback all fixtures after the TestCase is done so that the DB is back to the point where only migrations are done and no fixture data is present.  

 Few StackOverflow answers suggested that we need to make an explicit call to teardown after the test case is done. So I added this code
@classmethod      def tearDownClass(cls):          super(TestCase, cls).tearDownClass()
This helps in running both the test cases together. But this leads to a new issue of DB not being flushed between test cases. Filed a bug report in detail on Bug tracker


Is there something I'm missing here? Any help would be greatly appreciated.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10245efe-9789-4ff9-9eda-a3f4bbc343df%40googlegroups.com.

No comments:

Post a Comment