Dubious code in multiple_database tests
|Reported by:||aaugustin||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
QueryTestCase.test_o2o_cross_database_protection contains code that seems invalid to me.
In that test, UserProfile has a nullable one-to-one relation to User.
An unsaved instance of User called charlie is created at line 655, and a profile is attached to this instance at line 664: charlie.userprofile = bob_profile.
But after saving everything, that doesn't work as expected: the profile isn't linked to the user!
This is easy to understand: this line basically performs bob_profile.user_id = charlie.id (and populates relation caches); but since charlie is unsaved, charlie.id is None, hence bob_profile.used_id remains None.
I'm attaching a patch for this test case that reloads the objects and tests that charlie.userprofile == bob_profile ; with this patch, the test fails, proving my point.
In #18153, I tried to catch such programming errors by raising an exception. And that breaks that test.
I think this code needs to fixed, while preserving as much of the testing behavior as possible.