source: wokkel/test/test_data_form.py @ 50:a37c65be8203

Last change on this file since 50:a37c65be8203 was 50:a37c65be8203, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Selectively render Data Forms fields depending on form type.

Author: ralphm.
Reviewer: tofu.
Fixes #40.

File size: 7.5 KB
Line 
1# Copyright (c) 2003-2009 Ralph Meijer
2# See LICENSE for details.
3
4"""
5Tests for {wokkel.data_form}.
6"""
7
8from twisted.trial import unittest
9from twisted.words.xish import domish
10
11from wokkel.data_form import Field, Form, Option, FieldNameRequiredError
12
13NS_X_DATA = 'jabber:x:data'
14
15
16
17class OptionTest(unittest.TestCase):
18    """
19    Tests for L{Option}.
20    """
21
22    def test_toElement(self):
23        option = Option('value', 'label')
24        element = option.toElement()
25        self.assertEquals('option', element.name)
26        self.assertEquals(NS_X_DATA, element.uri)
27        self.assertEquals('label', element['label'])
28        self.assertEquals('value', element.value.name)
29        self.assertEquals(NS_X_DATA, element.value.uri)
30        self.assertEquals('value', unicode(element.value))
31
32
33
34class FieldTest(unittest.TestCase):
35    """
36    Tests for L{Field}.
37    """
38
39    def test_basic(self):
40        """
41        Test basic field initialization.
42        """
43        field = Field(var='test')
44        self.assertEqual('text-single', field.fieldType)
45        self.assertEqual('test', field.var)
46
47
48    def test_noFieldName(self):
49        field = Field()
50        self.assertRaises(FieldNameRequiredError, field.toElement)
51
52
53    def test_toElement(self):
54        """
55        Test rendering to a DOM.
56        """
57        field = Field(var='test')
58        element = field.toElement()
59
60        self.assertTrue(domish.IElement.providedBy(element))
61        self.assertEquals('field', element.name)
62        self.assertEquals(NS_X_DATA, element.uri)
63        self.assertEquals('text-single',
64                          element.getAttribute('type', 'text-single'))
65        self.assertEquals('test', element['var'])
66        self.assertEquals([], element.children)
67
68
69    def test_toElementTypeNotListSingle(self):
70        """
71        Always render the field type, if different from list-single.
72        """
73        field = Field('hidden', var='test')
74        element = field.toElement()
75
76        self.assertEquals('hidden', element.getAttribute('type'))
77
78
79    def test_toElementAsForm(self):
80        """
81        Always render the field type, if asForm is True.
82        """
83        field = Field(var='test')
84        element = field.toElement(True)
85
86        self.assertEquals('text-single', element.getAttribute('type'))
87
88
89    def test_toElementOptions(self):
90        """
91        Test rendering to a DOM with options.
92        """
93        field = Field('list-single', var='test')
94        field.options = [Option(u'option1'), Option(u'option2')]
95        element = field.toElement(True)
96
97        self.assertEqual(2, len(element.children))
98
99
100    def test_toElementLabel(self):
101        """
102        Test rendering to a DOM with a label.
103        """
104        field = Field(var='test', label=u'my label')
105        element = field.toElement(True)
106
107        self.assertEqual(u'my label', element.getAttribute('label'))
108
109
110    def test_toElementDescription(self):
111        """
112        Test rendering to a DOM with options.
113        """
114        field = Field(var='test', desc=u'My desc')
115        element = field.toElement(True)
116
117        self.assertEqual(1, len(element.children))
118        child = element.children[0]
119        self.assertEqual('desc', child.name)
120        self.assertEqual(NS_X_DATA, child.uri)
121        self.assertEqual(u'My desc', unicode(child))
122
123
124    def test_toElementRequired(self):
125        """
126        Test rendering to a DOM with options.
127        """
128        field = Field(var='test', required=True)
129        element = field.toElement(True)
130
131        self.assertEqual(1, len(element.children))
132        child = element.children[0]
133        self.assertEqual('required', child.name)
134        self.assertEqual(NS_X_DATA, child.uri)
135
136
137    def test_fromElementType(self):
138        element = domish.Element((NS_X_DATA, 'field'))
139        element['type'] = 'fixed'
140        field = Field.fromElement(element)
141        self.assertEquals('fixed', field.fieldType)
142
143
144    def test_fromElementNoType(self):
145        element = domish.Element((NS_X_DATA, 'field'))
146        field = Field.fromElement(element)
147        self.assertEquals(None, field.fieldType)
148
149
150    def test_fromElementValue(self):
151        element = domish.Element((NS_X_DATA, 'field'))
152        element.addElement("value", content="text")
153        field = Field.fromElement(element)
154        self.assertEquals('text', field.value)
155
156
157
158class FormTest(unittest.TestCase):
159    """
160    Tests for L{Form}.
161    """
162
163    def test_formType(self):
164        """
165        A form has a type.
166        """
167
168        form = Form('result')
169        self.assertEqual('result', form.formType)
170
171    def test_toElement(self):
172        """
173        The toElement method returns a form's DOM representation.
174        """
175        form = Form('result')
176        element = form.toElement()
177
178        self.assertTrue(domish.IElement.providedBy(element))
179        self.assertEquals('x', element.name)
180        self.assertEquals(NS_X_DATA, element.uri)
181        self.assertEquals('result', element['type'])
182        self.assertEquals([], element.children)
183
184
185    def test_fromElement(self):
186        """
187        The fromElement static method creates a L{Form} from a L{DOM.
188        """
189        element = domish.Element((NS_X_DATA, 'x'))
190        element['type'] = 'result'
191        form = Form.fromElement(element)
192
193        self.assertEquals('result', form.formType)
194        self.assertEquals(None, form.title)
195        self.assertEquals([], form.instructions)
196        self.assertEquals({}, form.fields)
197
198
199    def test_fromElementInvalidElementName(self):
200        """
201        Bail if the passed element does not have the correct name.
202        """
203        element = domish.Element((NS_X_DATA, 'form'))
204        self.assertRaises(Exception, Form.fromElement, element)
205
206
207    def test_fromElementInvalidElementURI(self):
208        """
209        Bail if the passed element does not have the correct namespace.
210        """
211        element = domish.Element(('myns', 'x'))
212        self.assertRaises(Exception, Form.fromElement, element)
213
214
215    def test_fromElementTitle(self):
216        element = domish.Element((NS_X_DATA, 'x'))
217        element.addElement('title', content='My title')
218        form = Form.fromElement(element)
219
220        self.assertEquals('My title', form.title)
221
222
223    def test_fromElementInstructions(self):
224        element = domish.Element((NS_X_DATA, 'x'))
225        element.addElement('instructions', content='instruction')
226        form = Form.fromElement(element)
227
228        self.assertEquals(['instruction'], form.instructions)
229
230    def test_fromElementInstructions2(self):
231        element = domish.Element((NS_X_DATA, 'x'))
232        element.addElement('instructions', content='instruction 1')
233        element.addElement('instructions', content='instruction 2')
234        form = Form.fromElement(element)
235
236        self.assertEquals(['instruction 1', 'instruction 2'], form.instructions)
237
238
239    def test_fromElementOneField(self):
240        element = domish.Element((NS_X_DATA, 'x'))
241        element.addElement('field')
242        form = Form.fromElement(element)
243
244        self.assertEquals(1, len(form.fieldList))
245        self.assertNotIn('field', form.fields)
246
247
248    def test_fromElementTwoFields(self):
249        element = domish.Element((NS_X_DATA, 'x'))
250        element.addElement('field')['var'] = 'field1'
251        element.addElement('field')['var'] = 'field2'
252        form = Form.fromElement(element)
253
254        self.assertEquals(2, len(form.fieldList))
255        self.assertIn('field1', form.fields)
256        self.assertEquals('field1', form.fieldList[0].var)
257        self.assertIn('field2', form.fields)
258        self.assertEquals('field2', form.fieldList[1].var)
Note: See TracBrowser for help on using the repository browser.