IntegrityErrors in tests
I'm creating a user in my test setup like so:
from django.contrib.auth.models import User
from rest_framework.test import APITestCase, APIClient
class ModelViewsTestCase(APITestCase):
def setUp(self):
self.user = User.objects.create_user(
email='[email protected]',
first_name='Test',
last_name='User',
password='password',
username='[email protected]'
)
self.client.force_authenticate(user=self.user)
and it appears to be causing an IntegrityError in the teardown process (specifically in the _post_teardown method on line 925 of django/test/testcases.py. Am I creating a User the wrong way in my setup method?
Traceback (most recent call last):
File "app/lib/python3.4/site-packages/django/db/backends/utils.py", line 63, in execute
return self.cursor.execute(sql)
psycopg2.IntegrityError: insert or update on table "easyaudit_crudevent" violates foreign key constraint "easyaudit_crudevent_user_id_09177b54_fk_auth_user_id"
DETAIL: Key (user_id)=(17) is not present in table "auth_user".
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app/lib/python3.4/site-packages/django/test/testcases.py", line 216, in __call__
self._post_teardown()
File "app/lib/python3.4/site-packages/django/test/testcases.py", line 925, in _post_teardown
self._fixture_teardown()
File "app/lib/python3.4/site-packages/django/test/testcases.py", line 1081, in _fixture_teardown
connections[db_name].check_constraints()
File "app/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 243, in check_constraints
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
File "app/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "app/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "app/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "app/lib/python3.4/site-packages/django/db/backends/utils.py", line 63, in execute
return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "easyaudit_crudevent" violates foreign key constraint "easyaudit_crudevent_user_id_09177b54_fk_auth_user_id"
DETAIL: Key (user_id)=(17) is not present in table "auth_user".
I thought this had resolved itself but I just had just forgotten to add easyaudit.middleware.easyaudit.EasyAuditMiddleware back to MIDDLEWARE in my app settings.
any other answer for this error? I have the same error when I run my command. Also I have the middleware in my app settings
Having the middleware in the app settings is the cause of the issue. I'm not sure what the fix is.
One issue we ran into was that during the CRUD signal system, it would run the code before its migration would be applied. Because of how we wrap the signal management into a transaction itself, you wouldn't notice it (unless you checked the log file).
So, when the test code ran after migrations, the issue would present itself.
I have a fix on my fork and intend to get it prepped and ready in the short term. No other issues appeared to crop up (our test suite ran to completion once the fix was applied).
This PR is not yet ready (probably will remove some of the formatting changes): https://github.com/jheld/django-easy-audit/tree/bugfix/content-type-models-transaction