Skip to content

Commit

Permalink
Refs #32559 -- Added selenium test for FloatField client-side validat…
Browse files Browse the repository at this point in the history
…ion.

step="any" is required for non-integer values. See:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number#step

Covers behaviour added in 7ec2a21.
  • Loading branch information
carltongibson authored and felixxm committed Feb 3, 2022
1 parent 832adb3 commit 4b8e949
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
23 changes: 23 additions & 0 deletions tests/forms_tests/field_tests/test_floatfield.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.core.exceptions import ValidationError
from django.forms import FloatField, NumberInput
from django.test import SimpleTestCase
from django.test.selenium import SeleniumTestCase
from django.test.utils import ignore_warnings, override_settings
from django.urls import reverse
from django.utils import formats, translation
from django.utils.deprecation import RemovedInDjango50Warning

Expand Down Expand Up @@ -111,3 +113,24 @@ def test_decimalfield_support_thousands_separator(self):
msg = "'Enter a number.'"
with self.assertRaisesMessage(ValidationError, msg):
f.clean('1,001.1')


@override_settings(ROOT_URLCONF='forms_tests.urls')
class FloatFieldHTMLTest(SeleniumTestCase):

available_apps = ['forms_tests']

def test_float_field_rendering_passes_client_side_validation(self):
"""
Rendered widget allows non-integer value with the client-side
validation.
"""
from selenium.webdriver.common.by import By

self.selenium.get(self.live_server_url + reverse('form_view'))
number_input = self.selenium.find_element(By.ID, 'id_number')
number_input.send_keys('0.5')
is_valid = self.selenium.execute_script(
"return document.getElementById('id_number').checkValidity()"
)
self.assertTrue(is_valid)
3 changes: 2 additions & 1 deletion tests/forms_tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path

from .views import ArticleFormView
from .views import ArticleFormView, form_view

urlpatterns = [
path('form_view/', form_view, name='form_view'),
path('model_form/<int:pk>/', ArticleFormView.as_view(), name='article_form'),
]
11 changes: 11 additions & 0 deletions tests/forms_tests/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django import forms
from django.http import HttpResponse
from django.template import Context, Template
from django.views.generic.edit import UpdateView

from .models import Article
Expand All @@ -16,3 +18,12 @@ class ArticleFormView(UpdateView):
model = Article
success_url = '/'
form_class = ArticleForm


def form_view(request):
class Form(forms.Form):
number = forms.FloatField()

template = Template('<html>{{ form }}</html>')
context = Context({'form': Form()})
return HttpResponse(template.render(context))

0 comments on commit 4b8e949

Please sign in to comment.