-
-
Notifications
You must be signed in to change notification settings - Fork 489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unset middleware request #213
Conversation
Nice. I've been a little worried about the middleware, cleanup should help a little in preventing nasty surprises. In your added test, did you verify the middleware is being called? I have a feeling there should be a constraint failure in what you're trying to demonstrate. Another approach would be to set the request attribute on the thread local explicitly, instead of trying to mock the settings. You would then have the added advantage of giving it a ridiculous id that would never exist normally, like -1. |
Thanks! Do you know what's happening with the travis build? The failures superficially appear unrelated to my changes.
Yes, and I did see the constraint error, as hoped. The middleware is definitely being called, since I wrote the tests first in the git history. Running
Since the tests pass on 4ecf837, and that commit only modifies the middleware, it must be getting called.
It seems like that would lose the benefit of testing the middleware's relationship to Django's request-response cycle.
I'm not sure I see how that would work. Note that this doesn't test that saving a history model will work even if the user on the request object is invalid (it won't), but rather that the request is cleared at the end of the request-response cycle, so the data never has a chance to be invalid. |
Basically, when you delete things in Django it goes out to all the related models to either cascade the delete or perform some 'on delete' action. The missing tables are because those models were created inside a test case. I'm not sure what the right solution would be to allow deleting users without incident. That was part of why I suggested attaching an unsaved user with an abnormal PK to the request instance... You wouldn't need to delete the user, and the strange errors would go away. |
I don't see a good way to set the That didn't completely fix the issue, since the travis build is now failing on the |
One option would be to use something like this stackoverflow post to dynamically add the app containing the model and sync it to the database. Getting it to work in both 1.6/south and 1.7+ might be a bit tricky. If getting a working solution for 1.6 proves difficult, what are your thoughts about dropping support for Django 1.6? Note that 1.6 (and 1.7) have hit end-of-life, and aren't even receiving security updates anymore. |
Cool. I'll look at it a bit and see if I can shake a solution out. Maybe we
|
@lucaswiman: Don't forget to add yourself to the AUTHORS file before we get this merged in! |
Middleware testing suggestions
Done, and I also updated the version and changelog. I think this is good to go on my end. |
Build failed with the same |
Weird. And I used to be able to restart specific portions of the matrix build, but I don't seem to see that now. I'll work on resolving the test issues, thanks for getting this pr together! |
@treyhunner This pull request adds a
process_response
method toHistoryRequestMiddleware
to unset the threadlocal request attribute when a response is processed. See also jedie/django-tools#7, where I did something similar. This behavior means there are no side-effects on global state which are not cleared by the djangoTestCase
class.Testing
I added tests for the desired behavior, including a regression test (
test_rolled_back_user_does_not_lead_to_foreign_key_error
) simulating the following scenario:U
and simulates that user saving a model instance in an admin form. This sets theHistoricalRecords.thread.request
variable to a request instance referencing the user.U
doesn't exist in the database anymore. (Or, the user entry is deleted.)history_user
foreign key is set to aUser
that does not exist in the database, failing with the following error: