grantmcconnaughey / django-field-history

A Django app to track changes to model fields.
BSD 3-Clause "New" or "Revised" License
314 stars 33 forks source link

Use of thread local in middleware makes tests flaky #37

Open twschiller opened 4 years ago

twschiller commented 4 years ago

We had an issue in https://github.com/twschiller/open-synthesis/issues/214 where we'd get a database integrity error about the FieldHistory referencing users that didn't exist

We had to add the following to our base classes to reset the request context after tests where we used the Django test client with a logged in user

 def tearDown(self) -> None:
        FieldHistoryTracker.thread.request = None
matthewslaney commented 4 years ago

Thanks @twschiller

I had this exact issue and your solution worked for me too.

For others looking here, for me that error was:

django.db.utils.IntegrityError: insert or update on table "field_history_fieldhistory" violates foreign key constraint "field_history_fieldhistory_user_id_81e3dc7d_fk_auth_user_id" DETAIL: Key (user_id)=(7) is not present in table "auth_user".