#27444: Django StaticLiveServerTestCase: view cannot query test-database
properly
-------------------------------------+-------------------------------------
Reporter: amstart | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.10
Severity: Normal | Resolution:
Keywords: selenium, test | Triage Stage:
database, select2 | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by amstart):
* status: closed => new
* resolution: needsinfo =>
Old description:
> I have the problem that I cannot test my page via Selenium when using
> LiveServerTestCase (there seems to be a problem with querying the
> database, I am using PostgreSQL). Please excuse that I did not try other
> TestCases, but I wouldnt even know how
>
> The relevant third party apps I am loading: dal and dal_select2. I used
> https://github.com/pydanny/cookiecutter-django to kickstart django.
>
> The input form for my model
> {{{
> from dal import autocomplete
> from django import forms
> from django.core.urlresolvers import reverse_lazy
> from .models import MyModel
>
> class MyModelCreateForm(forms.ModelForm):
> class Meta:
> model = MyModel
> fields = ['my_other_model']
> widgets = {'my_other_model': autocomplete.ModelSelect2(url =
> reverse_lazy('autocomplete'))}
> }}}
>
> The create view:
> {{{
> class CreateMyModelView(CreateView):
> template_name = 'create_my_model.html'
> success_url = 'admin/'
> model = MyModel
>
> def get_form_class(self):
> return MyModelCreateForm
> }}}
>
> The view template:
> {{{
> {% load static %}
> <link href="{% static 'autocomplete_light/select2.css' %}"
> rel="stylesheet">
> <link rel="stylesheet" type="text/css" href="{% static
> 'css/bootstrap.min.css' %}" />
>
> <h1>New MyModel Object</h1>
> <form class="new_MyModel"
> id="new_MyModel_form"
> method="post"
> action="{{ request.get_full_path }}">
> {% csrf_token %}
> {{ form }}
> <button class="btn btn-primary" type="submit"> submit MyModel
> Object</button>
> </form>
> <script
> src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"
> integrity="sha384-THPy051/pYDQGanwU6poAc/hOdQxjnOEXzbT+OuUAFqNqFjL+4IGLBgCJC3ZOShY"
> crossorigin="anonymous"></script>
> <script src="{% static 'autocomplete_light/select2.js' %}"></script>
> {{ form.media }}
> }}}
>
> This is the view the select2 widget uses:
> {{{
> class AutocompleteView(autocomplete.Select2QuerySetView):
> def get_queryset(self):
> queryset = MyOtherModel.objects.all()
> if self.q:
> queryset = queryset.filter(foo__contains=self.q)
> print('This is what the autocomplete view sees:' +
> str(queryset)) #this is for debugging
> return queryset
> }}}
>
> The test:
>
> {{{
> from selenium import webdriver
> from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
>
> from django.contrib.staticfiles.testing import StaticLiveServerTestCase
>
> from .models import MyOtherModel
>
> class LiveServerTest(StaticLiveServerTestCase):
> def test_create_my_model(self):
> MyOtherModel.objects.create(foo = "find me pleease!")
> self.browser = webdriver.Firefox(firefox_binary=FirefoxBinary(
> firefox_path='C:\\Program Files\\Mozilla
> FirefoxESR\\firefox.exe'))
> self.browser.get(self.live_server_url + '/create')
> self.browser.implicitly_wait(0.5)
> print("This is what the test sees:" +
> str(MyOtherModel.objects.all()))
> self.browser.find_element_by_css_selector('#id_my_other_model +
> span').click()
> }}}
>
> Running the Test:
>
> > Creating test database for alias 'default'... This is what the test
> > sees:<QuerySet [<MyOtherModel: find me pleease!>]> .This is what the
> > autocomplete view sees:<QuerySet []>
>
> The select2 list in the browser says "no results found", which is of
> course expected. For some reason this particular view does not seem to
> have access to the test database.
>
> Note: This is not that bad since I can test my view with this doc test:
> {{{
> from selenium import webdriver
> from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
> import os
> import django
> os.environ['DJANGO_SETTINGS_MODULE'] = "config.settings.local"
> django.setup()
>
> from demoslogic.arguments.models import MyOtherModel
>
> MyOtherModel.objects.create(foo = "find me pleease!")
> #...test the page
> }}}
New description:
I have the problem that I cannot test my page via Selenium when using
LiveServerTestCase (there seems to be a problem with querying the
database, I am using PostgreSQL). Please excuse that I did not try other
TestCases, but I wouldnt even know how.
The relevant third party apps I am loading: dal and dal_select2. I used
https://github.com/pydanny/cookiecutter-django to kickstart django.
The input form for my model
{{{
from dal import autocomplete
from django import forms
from django.core.urlresolvers import reverse_lazy
from .models import MyModel
class MyModelCreateForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ['my_other_model']
widgets = {'my_other_model': autocomplete.ModelSelect2(url =
reverse_lazy('autocomplete'))}
}}}
The create view:
{{{
class CreateMyModelView(CreateView):
template_name = 'create_my_model.html'
success_url = 'admin/'
model = MyModel
def get_form_class(self):
return MyModelCreateForm
}}}
The view template:
{{{
{% load static %}
<link href="{% static 'autocomplete_light/select2.css' %}"
rel="stylesheet">
<link rel="stylesheet" type="text/css" href="{% static
'css/bootstrap.min.css' %}" />
<h1>New MyModel Object</h1>
<form class="new_MyModel"
id="new_MyModel_form"
method="post"
action="{{ request.get_full_path }}">
{% csrf_token %}
{{ form }}
<button class="btn btn-primary" type="submit"> submit MyModel
Object</button>
</form>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"
integrity="sha384-THPy051/pYDQGanwU6poAc/hOdQxjnOEXzbT+OuUAFqNqFjL+4IGLBgCJC3ZOShY"
crossorigin="anonymous"></script>
<script src="{% static 'autocomplete_light/select2.js' %}"></script>
{{ form.media }}
}}}
This is the view the select2 widget uses:
{{{
class AutocompleteView(autocomplete.Select2QuerySetView):
def get_queryset(self):
queryset = MyOtherModel.objects.all()
if self.q:
queryset = queryset.filter(foo__contains=self.q)
print('This is what the autocomplete view sees:' +
str(queryset)) #this is for debugging
return queryset
}}}
The test:
{{{
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from .models import MyOtherModel
class LiveServerTest(StaticLiveServerTestCase):
def test_create_my_model(self):
MyOtherModel.objects.create(foo = "find me pleease!")
self.browser = webdriver.Firefox(firefox_binary=FirefoxBinary(
firefox_path='C:\\Program Files\\Mozilla
FirefoxESR\\firefox.exe'))
self.browser.get(self.live_server_url + '/create')
self.browser.implicitly_wait(0.5)
print("This is what the test sees:" +
str(MyOtherModel.objects.all()))
self.browser.find_element_by_css_selector('#id_my_other_model +
span').click()
}}}
Running the Test:
> Creating test database for alias 'default'... This is what the test
> sees:<QuerySet [<MyOtherModel: find me pleease!>]> .This is what the
> autocomplete view sees:<QuerySet []>
The select2 list in the browser says "no results found", which is of
course expected. For some reason this particular view does not seem to
have access to the test database.
Note: This is not that bad since I can test my view with this doc test:
{{{
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = "config.settings.local"
django.setup()
from demoslogic.arguments.models import MyOtherModel
MyOtherModel.objects.create(foo = "find me pleease!")
#...test the page
}}}
--
Comment:
Thanks for the quick answer :)
So I added a file with a minimal project created with django-admin
startproject mysite (the only change to the settings were the additions of
the app modules).
I also removed the bootstrap css from the template
The thing still does not work as I would expect.
Cheers
--
Ticket URL: <https://code.djangoproject.com/ticket/27444#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.19b1ca4502d1a46d4dfe1aa132a2d380%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.