Opened 7 years ago

Closed 7 years ago

#27444 closed Bug (invalid)

Django StaticLiveServerTestCase: view cannot query test-database properly

Reported by: amstart Owned by: nobody
Component: Testing framework Version: 1.10
Severity: Normal Keywords: selenium, test database, select2
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by amstart)

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

Attachments (1)

mysite.zip (20.3 KB ) - added by amstart 7 years ago.
the whole project

Download all attachments as: .zip

Change History (4)

comment:1 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: newclosed

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 asking for help on our support channels.

by amstart, 7 years ago

Attachment: mysite.zip added

the whole project

comment:2 by amstart, 7 years ago

Description: modified (diff)
Resolution: needsinfo
Status: closednew

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

comment:3 by Tim Graham, 7 years ago

Resolution: invalid
Status: newclosed

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

Note: See TracTickets for help on using tickets.
Back to Top