Opened 6 years ago
Closed 6 years ago
#31308 closed Bug (invalid)
Fixture loading fails when more than one Test case uses same fixtures
| Reported by: | Lokesh Gurram | Owned by: | nobody |
|---|---|---|---|
| Component: | Testing framework | Version: | 2.2 |
| Severity: | Normal | Keywords: | Testcases, fixtures |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
If I have more than one TestCase using the same fixture, loading data between test cases raises unique constraint violation error on Postgres (works fine on SQLite)
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
Profile table has a one-to-one relation with User table.
[
{
"model": "app1.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": "app1.profile",
"pk": 1,
"fields": {
"user": 1,
"firebaseID": null,
"phone": null,
"completed_till": 0,
}
}
]
Running each Test Case separately works fine. But when we run them in one go, first Test case goes through and before the 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"
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.