Opened 8 years ago

Last modified 8 years ago

#27444 closed Bug

Django StaticLiveServerTestCase: view cannot query test-database properly — at Initial Version

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

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

Change History (0)

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