Opened 4 years ago
Closed 4 years ago
#31886 closed Bug (invalid)
Django test hits default database while running test - expecting `test_<defaultdatabasename>`
Reported by: | Carlos Leite | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | 3.1 |
Severity: | Normal | Keywords: | settings database test |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
# models.py
class Disco(models.Model): nome = models.CharField('disco', max_length=50) class Musica(models.Model): disco = models.ForeignKey(Disco, on_delete=models.CASCADE) nome = models.CharField('nome do disco', max_length=50)
# forms.py
class MusicForm(forms.Form): ''' ... like a search form, I need option from database ''' disco = forms.ChoiceField(choices=Disco.objects.all()) # this cause the error, I dont like it , but supposed its a fair code to have choices.
# tests.py
# in the tests.py I just need to import the form to have the odd bahaviour
from django.test import TestCase from .forms import MusicForm
... I just need to import the MusicForm
to have the erros
# the error
... conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: FATAL: database "disccollection" does not exist. <<<- database should be `test_dis...` <test_something>
I was trying to run tests, so the database to hit should be test_disccollection
but not disccollection
(in my settings.py).
That may happends because django test framework only changes the settings.database.default alias after my form was initialized I supposed.
The problem I have was in Django 1.8 (Iknow its deprecated) ... then I tried django 3.1 , and error remains
I got some sample code here -> https://github.com/cadu-leite/django18py2testdb/tree/dj33py3test
Thanks for this ticket, however you should use
ModelChoiceField(queryset=Disco.objects.all())
instead ofChoiceField(choices=...)
.Closing per TicketClosingReasons/UseSupportChannels.