source: wokkel/test/test_data_form.py @ 29:dd4e908b9d12

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

Implement type checking in data forms and incoming pubsub node config.

Author: ralphm.
Fixes #15.

File size: 5.3 KB
Line 
1# Copyright (c) 2003-2008 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        field = Field(var='test')
41        self.assertEqual('text-single', field.fieldType)
42        self.assertEqual('test', field.var)
43
44        element = field.toElement()
45
46        self.assertTrue(domish.IElement.providedBy(element))
47        self.assertEquals('field', element.name)
48        self.assertEquals(NS_X_DATA, element.uri)
49        self.assertEquals('text-single', element['type'])
50        self.assertEquals('test', element['var'])
51        self.assertEquals([], element.children)
52
53
54    def test_noFieldName(self):
55        field = Field()
56        self.assertRaises(FieldNameRequiredError, field.toElement)
57
58
59    def test_fromElementType(self):
60        element = domish.Element((NS_X_DATA, 'field'))
61        element['type'] = 'fixed'
62        field = Field.fromElement(element)
63        self.assertEquals('fixed', field.fieldType)
64
65
66    def test_fromElementNoType(self):
67        element = domish.Element((NS_X_DATA, 'field'))
68        field = Field.fromElement(element)
69        self.assertEquals(None, field.fieldType)
70
71
72    def test_fromElementValue(self):
73        element = domish.Element((NS_X_DATA, 'field'))
74        element.addElement("value", content="text")
75        field = Field.fromElement(element)
76        self.assertEquals('text', field.value)
77
78
79
80class FormTest(unittest.TestCase):
81    """
82    Tests for L{Form}.
83    """
84
85    def test_formType(self):
86        """
87        A form has a type.
88        """
89
90        form = Form('result')
91        self.assertEqual('result', form.formType)
92
93    def test_toElement(self):
94        """
95        The toElement method returns a form's DOM representation.
96        """
97        form = Form('result')
98        element = form.toElement()
99
100        self.assertTrue(domish.IElement.providedBy(element))
101        self.assertEquals('x', element.name)
102        self.assertEquals(NS_X_DATA, element.uri)
103        self.assertEquals('result', element['type'])
104        self.assertEquals([], element.children)
105
106
107    def test_fromElement(self):
108        """
109        The fromElement static method creates a L{Form} from a L{DOM.
110        """
111        element = domish.Element((NS_X_DATA, 'x'))
112        element['type'] = 'result'
113        form = Form.fromElement(element)
114
115        self.assertEquals('result', form.formType)
116        self.assertEquals(None, form.title)
117        self.assertEquals([], form.instructions)
118        self.assertEquals({}, form.fields)
119
120
121    def test_fromElementInvalidElementName(self):
122        """
123        Bail if the passed element does not have the correct name.
124        """
125        element = domish.Element((NS_X_DATA, 'form'))
126        self.assertRaises(Exception, Form.fromElement, element)
127
128
129    def test_fromElementInvalidElementURI(self):
130        """
131        Bail if the passed element does not have the correct namespace.
132        """
133        element = domish.Element(('myns', 'x'))
134        self.assertRaises(Exception, Form.fromElement, element)
135
136
137    def test_fromElementTitle(self):
138        element = domish.Element((NS_X_DATA, 'x'))
139        element.addElement('title', content='My title')
140        form = Form.fromElement(element)
141
142        self.assertEquals('My title', form.title)
143
144
145    def test_fromElementInstructions(self):
146        element = domish.Element((NS_X_DATA, 'x'))
147        element.addElement('instructions', content='instruction')
148        form = Form.fromElement(element)
149
150        self.assertEquals(['instruction'], form.instructions)
151
152    def test_fromElementInstructions2(self):
153        element = domish.Element((NS_X_DATA, 'x'))
154        element.addElement('instructions', content='instruction 1')
155        element.addElement('instructions', content='instruction 2')
156        form = Form.fromElement(element)
157
158        self.assertEquals(['instruction 1', 'instruction 2'], form.instructions)
159
160
161    def test_fromElementOneField(self):
162        element = domish.Element((NS_X_DATA, 'x'))
163        element.addElement('field')
164        form = Form.fromElement(element)
165
166        self.assertEquals(1, len(form.fieldList))
167        self.assertNotIn('field', form.fields)
168
169
170    def test_fromElementTwoFields(self):
171        element = domish.Element((NS_X_DATA, 'x'))
172        element.addElement('field')['var'] = 'field1'
173        element.addElement('field')['var'] = 'field2'
174        form = Form.fromElement(element)
175
176        self.assertEquals(2, len(form.fieldList))
177        self.assertIn('field1', form.fields)
178        self.assertEquals('field1', form.fieldList[0].var)
179        self.assertIn('field2', form.fields)
180        self.assertEquals('field2', form.fieldList[1].var)
Note: See TracBrowser for help on using the repository browser.