When running tests, Django accesses the database referred to in settings
|Reported by:||EvilDMP||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Tests that require a database (namely, model tests) will not use your "real" (production) database. Separate, blank databases are created for the tests.
There seem to be circumstances in which Django will access the real database that is referred to in settings.
Say I have a model called Entity.
I find I can't directly access the real database's Entity objects in tests, and in most of the application code, as expected, Django doesn't find anything in Entity.objects.all(). This is what the documentation tells me to expect.
However, in other parts of the code, Entity.objects.all() does return the Entity objects from the real database, which surprised me. The real database is accessed before Django gets to "Creating test database for alias 'default'..."
If the real database is removed, then things behave as I would expect.
If it is to be expected that Django code when running tests will sometimes access the real database, then the documents need to explain when or how this will happen - but I don't think it should happen.
Change History (15)
comment:1 follow-up: ↓ 2 Changed 3 years ago by aaugustin
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:8 Changed 3 years ago by ptone
- Component changed from Uncategorized to Documentation
- Triage Stage changed from Unreviewed to Accepted
comment:14 Changed 2 years ago by Tim Graham <timograham@…>
- Resolution set to fixed
- Status changed from new to closed