Package turbogears :: Package tests :: Module test_form_controllers
[hide private]

Source Code for Module turbogears.tests.test_form_controllers

  1  from unittest import TestCase 
  2  from datetime import datetime 
  3  import cherrypy 
  4  from turbogears import widgets, config, controllers, expose, mochikit, \ 
  5      validate, validators, testutil 
  6   
  7   
8 -class MyFormFields(widgets.WidgetsList):
9 #XXX: Since allow_extra_fields should be removed from validators.Schema, 10 # we need a validator for every input-expecting widget 11 name = widgets.TextField(validator=validators.String()) 12 age = widgets.TextField(validator=validators.Int(), default=0) 13 date = widgets.CalendarDatePicker(validator=validators.DateConverter( 14 if_empty=datetime.now()))
15 16 myform = widgets.TableForm(fields=MyFormFields()) 17 18
19 -class MyRoot(controllers.RootController):
20 21 [expose(html="turbogears.tests.form")]
22 - def index(self):
23 return dict(form=myform)
24 25 [expose(html="turbogears.tests.form")]
26 - def usemochi(self):
27 return dict(mochi=mochikit, form=myform)
28 29 [expose(html="turbogears.tests.othertemplate")] 30 [validate(form=myform)]
31 - def testform(self, name, date, age, tg_errors=None):
32 if tg_errors: 33 self.has_errors = True 34 self.name = name 35 self.age = age 36 self.date = date
37 38 [expose()] 39 [validate(form=myform)]
40 - def testform_new_style(self, name, date, age):
41 if cherrypy.request.validation_errors: 42 self.has_errors = True 43 self.name = name 44 self.age = age 45 self.date = date
46
47 -def test_form_translation():
48 """Form input is translated into properly converted parameters""" 49 root = MyRoot() 50 cherrypy.root = root 51 testutil.create_request("/testform?name=ed&date=11/05/2005&age=5") 52 assert root.name == "ed" 53 assert root.age == 5
54
55 -def test_form_translation_new_style():
56 """Form input is translated into properly converted parameters""" 57 root = MyRoot() 58 cherrypy.root = root 59 testutil.create_request("/testform_new_style?name=ed&date=11/05/2005&age=5&") 60 assert root.name == "ed" 61 assert root.age == 5
62
63 -def test_invalid_form_with_error_handling():
64 """Invalid forms can be handled by the method""" 65 root = cherrypy.root 66 testutil.create_request("/testform?name=ed&age=edalso&date=11/05/2005") 67 assert root.has_errors
68
69 -def test_css_should_appear():
70 """CSS should appear when asked for""" 71 testutil.create_request("/") 72 assert "calendar-system.css" in cherrypy.response.body[0]
73
74 -def test_javascript_should_appear():
75 """JavaScript should appear when asked for""" 76 testutil.create_request("/") 77 assert "calendar.js" in cherrypy.response.body[0]
78
79 -def test_include_mochikit():
80 """JSLinks (and MochiKit especially) can be included easily""" 81 testutil.create_request("/usemochi") 82 assert "MochiKit.js" in cherrypy.response.body[0]
83
84 -def test_suppress_mochikit():
85 """MochiKit inclusion can be suppressed""" 86 config.update({"global": {"tg.mochikit_suppress": True}}) 87 testutil.create_request("/usemochi") 88 suppressed_body = cherrypy.response.body[0] 89 # repair the fixture 90 config.update({"global": {"tg.mochikit_suppress": False}}) 91 testutil.create_request("/usemochi") 92 included_body = cherrypy.response.body[0] 93 assert "MochiKit.js" not in suppressed_body 94 assert "MochiKit.js" in included_body
95
96 -def test_mochikit_everywhere():
97 """MochiKit can be included everywhere by setting tg.mochikit_all""" 98 config.update({"global": {"tg.mochikit_all": True}}) 99 testutil.create_request("/") 100 config.update({"global": {"tg.mochikit_all": False}}) 101 assert "MochiKit.js" in cherrypy.response.body[0]
102
103 -def test_mochikit_nowhere():
104 """Setting tg.mochikit_suppress will prevent including it everywhere""" 105 config.update({"global": {"tg.mochikit_all": True}}) 106 config.update({"global": {"tg.mochikit_suppress": True}}) 107 testutil.create_request("/") 108 config.update({"global": {"tg.mochikit_all": False}}) 109 config.update({"global": {"tg.mochikit_suppress": False}}) 110 assert "MochiKit.js" not in cherrypy.response.body[0]
111
112 -def test_include_widgets():
113 """Any widget can be included everywhere by setting tg.include_widgets""" 114 config.update({"global": {"tg.include_widgets": ["mochikit"]}}) 115 testutil.create_request("/") 116 config.update({"global": {"tg.include_widgets": []}}) 117 assert "MochiKit.js" in cherrypy.response.body[0]
118 119
120 -class State(object):
121 counter = 0
122
123 -class AddingValidator(validators.FancyValidator):
124 - def _to_python(self, value, state=None):
125 state.counter += 1 126 return value
127
128 -class AddingSchema(validators.Schema):
129 a = AddingValidator() 130 b = AddingValidator()
131
132 -class AddingNestedSchema(AddingSchema):
133 c = AddingSchema()
134 135
136 -class TestValidationState(TestCase):
137
138 - class Controller(controllers.RootController):
139 140 [expose()] 141 [validate(validators=AddingNestedSchema(), state_factory=State)]
142 - def validation(self, a, b, c):
143 return 'counter: %d' % cherrypy.request.validation_state.counter
144
145 - def __init__(self, *args, **kw):
146 super(TestValidationState, self).