[Django] #27444: Django StaticLiveServerTestCase: view cannot query test-database properly

14 views
Skip to first unread message

Django

unread,
Nov 5, 2016, 9:50:54 AM11/5/16
to django-...@googlegroups.com
#27444: Django StaticLiveServerTestCase: view cannot query test-database properly
-------------------------------------+-------------------------------------
Reporter: amstart | Owner: nobody
Type: Bug | Status: new
Component: Testing | Version: 1.10
framework | Keywords: selenium, test
Severity: Normal | database, select2
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27444>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 5, 2016, 11:30:18 AM11/5/16
to django-...@googlegroups.com
#27444: Django StaticLiveServerTestCase: view cannot query test-database properly
-------------------------------------+-------------------------------------
Reporter: amstart | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 1.10
Severity: Normal | Resolution: needsinfo
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 Tim Graham):

* status: new => closed
* resolution: => needsinfo


Comment:

Hi, this report is a bit too complicated. To confirm that Django is at
fault, could you please create a minimal project without any third-party
dependencies that reproduces the issue?

I think it's more likely that you've made a mistake somewhere. I'd suggest
[wiki:TicketClosingReasons/UseSupportChannels asking for help on our
support channels].

--
Ticket URL: <https://code.djangoproject.com/ticket/27444#comment:1>

Django

unread,
Nov 5, 2016, 7:41:12 PM11/5/16
to django-...@googlegroups.com
#27444: Django StaticLiveServerTestCase: view cannot query test-database properly
-------------------------------------+-------------------------------------
Reporter: amstart | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 1.10
Severity: Normal | Resolution: needsinfo
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):

* Attachment "mysite.zip" added.

the whole project

Django

unread,
Nov 5, 2016, 7:43:52 PM11/5/16
to django-...@googlegroups.com
#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:

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.

def get_form_class(self):
return MyModelCreateForm
}}}

The test:

from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from .models import MyOtherModel


Running the Test:

from demoslogic.arguments.models import MyOtherModel

--

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

unread,
Nov 6, 2016, 1:09:09 AM11/6/16
to django-...@googlegroups.com
#27444: Django StaticLiveServerTestCase: view cannot query test-database properly
-------------------------------------+-------------------------------------
Reporter: amstart | Owner: nobody
Type: Bug | Status: closed

Component: Testing framework | Version: 1.10
Severity: Normal | Resolution: invalid

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 Tim Graham):

* status: new => closed

* resolution: => invalid


Comment:

It's working fine for me (tested with Chrome rather than Firefox as
geckodriver is a bit unstable these days).

--
Ticket URL: <https://code.djangoproject.com/ticket/27444#comment:3>

Reply all
Reply to author
Forward
0 new messages