/usr/lib/python2.7/dist-packages/zope/formlib/tests/test_functional_decimalwidget.py is in python-zope.formlib 4.3.0a2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | ##############################################################################
#
# Copyright (c) 2001, 2002, 2006 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Decimal Widget Functional Tests
"""
import unittest
import decimal
from zope.interface import Interface, implementer
from zope.schema import Decimal, Choice
from zope.formlib import form
from zope.publisher.browser import TestRequest
from zope.formlib.tests.support import patternExists
from zope.formlib.widgets import (
DecimalWidget,
DropdownWidget, ChoiceInputWidget)
from zope.formlib.tests.functionalsupport import FunctionalWidgetTestCase
import zope.schema.interfaces
class IDecimalTest(Interface):
f1 = Decimal(
required=False,
min=decimal.Decimal("1.1"),
max=decimal.Decimal("10.1"))
f2 = Decimal(
required=False)
f3 = Choice(
required=True,
values=(decimal.Decimal("0.0"), decimal.Decimal("1.1"),
decimal.Decimal("2.1"), decimal.Decimal("3.1"),
decimal.Decimal("5.1"), decimal.Decimal("7.1"),
decimal.Decimal("11.1")),
missing_value=0)
@implementer(IDecimalTest)
class DecimalTest(object):
def __init__(self):
self.f1 = None
self.f2 = decimal.Decimal("1.1")
self.f3 = decimal.Decimal("2.1")
class Form(form.EditForm):
form_fields = form.fields(IDecimalTest)
class Test(FunctionalWidgetTestCase):
widgets = [
(zope.schema.interfaces.IDecimal, DecimalWidget),
(zope.schema.interfaces.IChoice, ChoiceInputWidget),
((zope.schema.interfaces.IChoice, zope.schema.interfaces.IVocabularyTokenized),
DropdownWidget)]
def test_display_editform(self):
foo = DecimalTest()
request = TestRequest()
html = Form(foo, request)()
# f1 and f2 should be displayed in text fields
self.assert_(patternExists(
'<input .* name="form.f1".* value="".*>', html))
self.assert_(patternExists(
'<input .* name="form.f2".* value="1.1".*>', html))
# f3 should be in a dropdown
self.assert_(patternExists(
'<select .*name="form.f3".*>', html))
self.assert_(patternExists(
'<option selected="selected" value="2.1">2.1</option>',
html))
def test_submit_editform(self):
foo = DecimalTest()
request = TestRequest()
request.form['form.f1'] = '1.123'
request.form['form.f2'] = '2.23456789012345'
request.form['form.f3'] = '11.1'
request.form['form.actions.apply'] = u''
Form(foo, request)()
# check new values in object
self.assertEqual(foo.f1, decimal.Decimal("1.123"))
self.assertEqual(foo.f2, decimal.Decimal("2.23456789012345"))
self.assertEqual(foo.f3, decimal.Decimal("11.1"))
def test_missing_value(self):
foo = DecimalTest()
request = TestRequest()
request.form['form.f1'] = ''
request.form['form.f2'] = ''
request.form['form.f3'] = '1.1'
request.form['form.actions.apply'] = u''
Form(foo, request)()
# check new values in object
self.assertEqual(foo.f1, None)
self.assertEqual(foo.f2, None) # None is default missing_value
self.assertEqual(foo.f3, decimal.Decimal("1.1")) # 0 is from f3.missing_value=0
def test_required_validation(self):
foo = DecimalTest()
request = TestRequest()
request.form['form.f1'] = ''
request.form['form.f2'] = ''
request.form['form.f3'] = ''
request.form['form.actions.apply'] = u''
html = Form(foo, request)()
# confirm error msgs
f3_index = html.find('form.f3')
missing_index = html.find('Required input is missing')
self.assert_(missing_index > f3_index)
def test_invalid_allowed_value(self):
foo = DecimalTest()
request = TestRequest()
# submit a value for f3 that isn't allowed
request.form['form.f3'] = '10000'
request.form['form.actions.apply'] = u''
html = Form(foo, request)()
f3_index = html.find('form.f3')
invalid_index = html.find('Invalid')
self.assert_(invalid_index > f3_index)
def test_min_max_validation(self):
foo = DecimalTest()
request = TestRequest()
# submit value for f1 that is too low
request.form['form.f1'] = '-1'
request.form['form.actions.apply'] = u''
html = Form(foo, request)()
f1_index = html.find('form.f1')
f2_index = html.find('form.f2')
too_small_index = html.find('Value is too small')
self.assert_(too_small_index > f1_index)
self.assert_(html.find('Value is too small', f2_index) == -1)
# submit value for f1 that is too high
request.form['form.f1'] = '1000.2'
request.form['form.actions.apply'] = u''
html = Form(foo, request)()
f1_index = html.find('form.f1')
f2_index = html.find('form.f2')
too_high_index = html.find('Value is too big')
self.assert_(too_high_index > f1_index)
self.assert_(html.find('Value is too small', f2_index) == -1)
def test_omitted_value(self):
foo = DecimalTest()
request = TestRequest()
# confirm default values
self.assert_(foo.f1 is None)
self.assertEqual(foo.f2, decimal.Decimal("1.1"))
self.assertEqual(foo.f3, decimal.Decimal("2.1"))
# submit change with only f2 present -- note that required
# field f1 is omitted, which should not cause a validation error
request.form['form.f2'] = ''
request.form['form.actions.apply'] = u''
Form(foo, request)()
# check new value in object
self.assert_(foo.f1 is None)
self.assert_(foo.f2 is None)
self.assertEqual(foo.f3, decimal.Decimal("2.1"))
def test_conversion(self):
foo = DecimalTest()
request = TestRequest()
# submit value for f1 that cannot be convert to an float
request.form['form.f1'] = 'foo'
request.form['form.actions.apply'] = u''
html = Form(foo, request)()
f1_index = html.find('form.f1')
f2_index = html.find('form.f2')
invalid_index = html.find('Invalid decimal data')
self.assert_(invalid_index > f1_index)
self.assert_(html.find('Invalid decimal data', f2_index) == -1)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test))
return suite
|