source: ralphm-patches/findform.patch @ 27:a9704141033a

Last change on this file since 27:a9704141033a was 27:a9704141033a, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Ensure all modified code has unittests, add setOptions to PubSubClient?.

File size: 5.4 KB
RevLine 
[26]1Move findForm to wokkel.data_form
2
[27]3diff -r ff3064cad1af wokkel/data_form.py
4--- a/wokkel/data_form.py       Fri Jan 01 16:16:57 2010 +0100
5+++ b/wokkel/data_form.py       Sat Jan 02 13:44:05 2010 +0100
6@@ -657,3 +657,25 @@
[21]7 
8         for field in filtered:
9             self.removeField(field)
10+
11+
[27]12+
[21]13+def findForm(element, formNamespace):
14+    """
15+    Find a Data Form.
16+
17+    Look for an element that represents a Data Form with the specified
18+    form namespace as a child element of the given element.
19+    """
20+    if not element:
21+        return None
22+
23+    for child in element.elements():
24+        if (child.uri, child.name) == ((NS_X_DATA, 'x')):
25+            form = Form.fromElement(child)
26+
27+            if (form.formNamespace == formNamespace or
28+                not form.formNamespace and form.formType=='cancel'):
29+                return form
30+
31+    return None
[27]32diff -r ff3064cad1af wokkel/pubsub.py
33--- a/wokkel/pubsub.py  Fri Jan 01 16:16:57 2010 +0100
34+++ b/wokkel/pubsub.py  Sat Jan 02 13:44:05 2010 +0100
[21]35@@ -252,30 +252,6 @@
36         self.verb = verb
37 
38 
39-    @staticmethod
40-    def _findForm(element, formNamespace):
41-        """
42-        Find a Data Form.
43-
44-        Look for an element that represents a Data Form with the specified
45-        form namespace as a child element of the given element.
46-        """
47-        if not element:
48-            return None
49-
50-        form = None
51-        for child in element.elements():
52-            try:
53-                form = data_form.Form.fromElement(child)
54-            except data_form.Error:
55-                continue
56-
57-            if form.formNamespace != NS_PUBSUB_NODE_CONFIG:
58-                continue
59-
60-        return form
61-
62-
63     def _parse_node(self, verbElement):
64         """
65         Parse the required node identifier out of the verbElement.
66@@ -366,7 +342,7 @@
67         """
68         Parse node type out of a request for the default node configuration.
69         """
70-        form = PubSubRequest._findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
71+        form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
72         if form and form.formType == 'submit':
73             values = form.getValues()
74             self.nodeType = values.get('pubsub#node_type', 'leaf')
75@@ -378,7 +354,7 @@
76         """
77         Parse options out of a request for setting the node configuration.
78         """
79-        form = PubSubRequest._findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
80+        form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
81         if form:
[26]82             if form.formType == 'submit':
83                 self.options = form.getValues()
84@@ -437,7 +413,7 @@
[21]85 
86 
87     def _parse_options(self, verbElement):
88-        form = PubSubRequest._findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS)
89+        form = data_form.findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS)
90         if form:
[26]91             if form.formType == 'submit':
92                 self.options = form.getValues()
[27]93diff -r ff3064cad1af wokkel/test/test_data_form.py
94--- a/wokkel/test/test_data_form.py     Fri Jan 01 16:16:57 2010 +0100
95+++ b/wokkel/test/test_data_form.py     Sat Jan 02 13:44:05 2010 +0100
96@@ -1064,3 +1064,74 @@
97         self.assertEqual([], checked)
98 
99 
100+
101+class FindFormTest(unittest.TestCase):
102+    """
103+    Tests for L{data_form.findForm}.
104+    """
105+
106+    def test_findForm(self):
107+        element = domish.Element((None, 'test'))
108+        theForm = data_form.Form('submit', formNamespace='myns')
109+        element.addChild(theForm.toElement())
110+        form = data_form.findForm(element, 'myns')
111+        self.assertEqual('myns', form.formNamespace)
112+
113+
114+    def test_noFormType(self):
115+        element = domish.Element((None, 'test'))
116+        otherForm = data_form.Form('submit')
117+        element.addChild(otherForm.toElement())
118+        form = data_form.findForm(element, 'myns')
119+        self.assertIdentical(None, form)
120+
121+
122+    def test_noFormTypeCancel(self):
123+        """
124+        Cancelled forms don't have a FORM_TYPE field, the first is returned.
125+        """
126+        element = domish.Element((None, 'test'))
127+        cancelledForm = data_form.Form('cancel')
128+        element.addChild(cancelledForm.toElement())
129+        form = data_form.findForm(element, 'myns')
130+        self.assertEqual('cancel', form.formType)
131+
132+
133+    def test_otherFormType(self):
134+        """
135+        Forms with other FORM_TYPEs are ignored.
136+        """
137+        element = domish.Element((None, 'test'))
138+        otherForm = data_form.Form('submit', formNamespace='otherns')
139+        element.addChild(otherForm.toElement())
140+        form = data_form.findForm(element, 'myns')
141+        self.assertIdentical(None, form)
142+
143+
144+    def test_otherFormTypeCancel(self):
145+        """
146+        Cancelled forms with another FORM_TYPE are ignored.
147+        """
148+        element = domish.Element((None, 'test'))
149+        cancelledForm = data_form.Form('cancel', formNamespace='otherns')
150+        element.addChild(cancelledForm.toElement())
151+        form = data_form.findForm(element, 'myns')
152+        self.assertIdentical(None, form)
153+
154+
155+    def test_noElement(self):
156+        """
157+        When None is passed as element, None is returned.
158+        """
159+        element = None
160+        form = data_form.findForm(element, 'myns')
161+        self.assertIdentical(None, form)
162+
163+
164+    def test_noForm(self):
165+        """
166+        When no child element is a form, None is returned.
167+        """
168+        element = domish.Element((None, 'test'))
169+        form = data_form.findForm(element, 'myns')
170+        self.assertIdentical(None, form)
Note: See TracBrowser for help on using the repository browser.