Christian Hammond got a fish trophy!
Fix a model contamination issue with TestModelsLoaderMixin.
Review Request #9229 - Created Sept. 28, 2017 and submitted
A recent unit test was affected by a bug in
causing a models module to remain in Django's cache after the test suite
concluded. This only happened if a subclass defined a tests app that
already contained a
models.pyfile. If it didn't define a specific
tests app or didn't include a
models.py, then the cleanup would work
correctly, but if both were present then it would fail.
The reason for this inconsistency is that if a valid
exist, we'd create one and store a reference to it, which allowed us to
later remove it from the module cache. If one already existed, then
there wouldn't be a reference, so it never got removed.
The logic for this is now a bit more careful to explicitly check for a
modelsmodule and to store it. If we can't import, we then proceed to
create a module and store that. This ensures we always have a module to
work with and to clean up at the end, fixing all tests.
All unit tests pass. Before this change, some failed with unknown app
errors involving models from prior test suites.