Debian Codegraph @

tests/test_forms.py

raw
1 """
2 Tests form async behaviour of gavo.formal.
3 """
4
5 #c Copyright 2008-2025, the GAVO project <[email protected]>
6 #c
7 #c This program is free software, covered by the GNU GPL. See the
8 #c COPYING file in the source distribution.
9
10
11 from twisted.web import template
12 from twisted.web.template import tags as T
13
14 from gavo import formal
15 from gavo.formal import validation
16 from gavo.helpers import trialhelpers
17
18 FK = formal.FORMS_KEY
19
20 class FormalTestResource(formal.ResourceWithForm):
21 isLeaf = True
22 result = None
23
24 def form_borken(self, request):
25 if request.args.get(b"input")==[b"break early"]:
26 raise ValueError("Broken")
27
28 form = formal.Form()
29 form.addField("anint", formal.Integer(),
30 label="A test integer input")
31 form.addField("input", formal.String(),
32 label="A test text input")
33 form.addAction(self.evaluateBroken)
34 return form
35
36 def evaluateBroken(self, request, form, data):
37 if data["input"]=="break late":
38 return 1/0
39 if data["input"]=="invalid":
40 raise validation.FieldValidationError("Don't like that input",
41 fieldName="input")
42 self.result = "Ok: "+data["input"]
43
44 @template.renderer
45 def insertResult(self, request, tag):
46 if self.result is None:
47 return ""
48 else:
49 return tag[self.result]
50
51 loader = template.TagLoader(T.html[
52 T.body[T.p["Before form"],
53 T.p(class_="result")(render="insertResult"),
54 T.transparent(render="form borken"),
55 T.p["After form"]]])
56
57
58 class BasicFormalTests(trialhelpers.RenderTest):
59 @property
60 def renderer(self):
61 return FormalTestResource()
62
63 def testFormEvaluationWoks(self):
64 return self.assertPOSTHasStrings("/",
65 {FK: ["borken"], "input": ["123"]}, [
66 '<p class="result">Ok: 123'])
67
68 def testGETDoesNotEvaluate(self):
69 return self.assertGETLacksStrings("/",
70 {FK: ["borken"], "input": ["123"]}, [
71 "Ok: 123"])
72
73 def testFormEvaluationErrorReported(self):
74 return self.assertPOSTHasStrings("/",
75 {FK: ["borken"], "input": ["invalid"]}, [
76 '<p>Please correct the following error(s):',
77 "Don't like that input</li>",
78 'class="field string textinput error"',
79 'value="invalid"'])
80
81 def testFormRender(self):
82 return self.assertGETHasStrings("/", {}, [
83 '<form',
84 'class="nevow-form"',
85 'id="borken"',
86 'action=""', 'method="post"',
87 ' id="borken-input-field" ',
88 '<label', 'for="borken-input">',
89 'A test text input</label>',
90 'id="borken-action-submit"',
91 '<p>Before form</p>',
92 '<p>After form</p>'])
93
94 def testBrokenHandlerTerminates(self):
95 return self.assertPOSTHasStrings("/",
96 {FK: ["borken"], "input": ["break late"]}, [
97 "Unhandled exception while handling the form:"])
98
99 def testBrokenFormMakerTerminates(self):
100 return self.assertPOSTHasStrings("/",
101 {FK: ["borken"], "input": ["break early"]}, [
102 "\n\nBroken", "Unhandled exception while handling the form:"])
103
104 def testGETFillsEvenBrokenValues(self):
105 return self.assertGETHasStrings("/honk",
106 {FK: ["borken"], "input": ["break late"]}, [
107 'action="honk"', 'value="break late"'])
108
109 def testFormErrorInline(self):
110 return self.assertPOSTHasStrings("/",
111 {FK: ["borken"], "anint": ["invalid"]}, [
112 "<p>Please correct the following error(s):",
113 '<div class="message">Not a valid number</div>',
114 'value="invalid"'])
115
116
117 class GettableTestResource(FormalTestResource):
118 processOnGET = True
119
120
121 class ExtraGETTests(trialhelpers.RenderTest):
122 @property
123 def renderer(self):
124 return GettableTestResource()
125
126 def testFormEvaluationOnGET(self):
127 return self.assertPOSTHasStrings("/",
128 {FK: ["borken"], "input": ["123"]}, [
129 "Ok: 123"])
130
131 def testGETErrors(self):
132 return self.assertGETHasStrings("/",
133 {FK: ["borken"], "anint": ["invalid"]}, [
134 "<p>Please correct the following error(s):",
135 '<div class="message">Not a valid number</div>',
136 'value="invalid"'])