#31299: DB not flushed between TestCases
-------------------------------------+-------------------------------------
     Reporter:  Lokesh Gurram        |                    Owner:
                                     |  ChillarAnand
         Type:  Bug                  |                   Status:  closed
    Component:  Testing framework    |                  Version:  2.2
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  TestCases, Fixtures  |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Lokesh Gurram):

 If I have more than one TestCase using the same fixture, loading data
 between test cases raises unique constraint violation error

 {{{
 class Test1(TestCase):
     fixtures = ['fixture.json']

     def test_demo(self):
         u = User.objects.get(username='testuser1')
         #some testing

 class Test2(TestCase):
     fixtures = ['fixture.json']

     def test_demo2(self):
         #some testing
         pass
 }}}

 **where my fixture.json is**

 {{{
 [
 {
     "model": "advisory.user",
     "pk": 1,
     "fields": {
         "password": "",
         "last_login": null,
         "is_superuser": false,
         "username": "testuser1",
         "first_name": "",
         "last_name": "",
         "is_staff": false,
         "is_active": true,
         "date_joined": "2020-02-21T12:59:17.302Z",
         "email": null,
         "groups": [],
         "user_permissions": []
     }
 },
 {
     "model": "advisory.profile",
     "pk": 1,
     "fields": {
         "user": 1,
         "firebaseID": null,
         "phone": null,
         "completed_till": 0,
     }
 }
 ]

 }}}

 Running the each Test Case separately works fine. But when run them in one
 go, first Test case goes through and before second test case executes, I
 get this error -

 {{{
 django.db.utils.IntegrityError: Problem installing fixture
 'C:\...\app1\fixtures\fixture.json': Could not load app1.Profile(pk=1):
 duplicate key value violates unique constraint "app1_profile_user_id_key"
 }}}

 Making an explicit call to **tearDownClass** helped in fixing this issue
 but it gave the problem I first posted here.

 Any idea why the DB is not completely cleared before trying to load the
 fixtures for the next TestCase. I tested this with Django 2.2 on Postgres
 10/12.  The problem persists in both.
 Interestingly though testing on SQLite doesn't give any problem.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31299#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.ca5fa54a024fd2506e269b4d2b1b9df5%40djangoproject.com.

Reply via email to