Changeset 24:8116c2eaa96a in ralphm-patches


Ignore:
Timestamp:
Jan 1, 2010, 1:24:04 AM (10 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

More tests, integrate form-coerce-values.patch.

Files:
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • form-missing-tests.patch

    r23 r24  
    11diff -r 3ef9bc7a0d70 wokkel/data_form.py
    22--- a/wokkel/data_form.py       Wed Dec 30 16:53:05 2009 +0100
    3 +++ b/wokkel/data_form.py       Thu Dec 31 16:20:55 2009 +0100
     3+++ b/wokkel/data_form.py       Fri Jan 01 01:21:56 2010 +0100
     4@@ -251,10 +251,10 @@
     5             field['var'] = self.var
     6 
     7         for value in self.values:
     8-            if self.fieldType == 'boolean':
     9+            if isinstance(value, bool):
     10                 value = unicode(value).lower()
     11-            elif self.fieldType in ('jid-single', 'jid-multi'):
     12-                value = value.full()
     13+            else:
     14+                value = unicode(value)
     15 
     16             field.addElement('value', content=value)
     17 
    418@@ -343,7 +343,7 @@
    519     """
     
    5064 
    5165     def __init__(self, formType, title=None, instructions=None,
     66@@ -392,7 +404,7 @@
     67         if self.formNamespace:
     68             r.append(", formNamespace=")
     69             r.append(repr(self.formNamespace))
     70-        if self.fields:
     71+        if self.fieldList:
     72             r.append(", fields=")
     73             r.append(repr(self.fieldList))
     74         r.append(")")
    5275@@ -424,7 +436,7 @@
    5376             form.addElement('title', content=self.title)
     
    5982         if self.formNamespace is not None:
    6083             field = Field('hidden', 'FORM_TYPE', self.formNamespace)
     84@@ -477,11 +489,26 @@
     85 
     86         return form
     87 
     88+
     89     def getValues(self):
     90+        """
     91+        Extract values from the named form fields.
     92+
     93+        For all named fields, the corresponding value or values are
     94+        returned in a dictionary keyed by the field name. For multi-value
     95+        fields, the dictionary value is a list, otherwise a single value.
     96+
     97+        If a field has no type, and the field has multiple values, the value of
     98+        the dictionary entry is the list of values. Otherwise, it will be a
     99+        single value.
     100+
     101+        @rtype: C{dict}
     102+        """
     103         values = {}
     104 
     105         for name, field in self.fields.iteritems():
     106-            if len(field.values) > 1:
     107+            if (field.fieldType in ('jid-multi', 'list-multi', 'text-multi') or
     108+                (field.fieldType is None and len(field.values) > 1)):
     109                 value = field.values
     110             else:
     111                 value = field.value
    61112diff -r 3ef9bc7a0d70 wokkel/test/test_data_form.py
    62113--- a/wokkel/test/test_data_form.py     Wed Dec 30 16:53:05 2009 +0100
    63 +++ b/wokkel/test/test_data_form.py     Thu Dec 31 16:20:55 2009 +0100
     114+++ b/wokkel/test/test_data_form.py     Fri Jan 01 01:21:56 2010 +0100
    64115@@ -19,14 +19,67 @@
    65116     """
     
    136187 
    137188 
    138 @@ -250,6 +303,7 @@
     189@@ -44,6 +97,21 @@
     190         self.assertEqual('test', field.var)
     191 
     192 
     193+    def test_repr(self):
     194+        """
     195+        The repr of a field should be equal to its initialization.
     196+        """
     197+        field = data_form.Field('list-single', var='test', label='label',
     198+                               desc='desc', required=True, value='test',
     199+                               options=[data_form.Option('test')])
     200+        self.assertEqual("""Field(fieldType='list-single', """
     201+                         """var='test', label='label', """
     202+                         """desc='desc', required=True, """
     203+                         """values=['test'], """
     204+                         """options=[Option('test')])""",
     205+                         repr(field))
     206+
     207+
     208     def test_toElement(self):
     209         """
     210         Test rendering to a DOM.
     211@@ -130,12 +198,44 @@
     212 
     213 
     214     def test_toElementJID(self):
     215+        """
     216+        A JID value should render to text.
     217+        """
     218         field = data_form.Field(fieldType='jid-single', var='test',
     219                                 value=jid.JID(u'test@example.org'))
     220         element = field.toElement()
     221         self.assertEqual(u'test@example.org', unicode(element.value))
     222 
     223 
     224+    def test_toElementJIDTextSingle(self):
     225+        """
     226+        A JID value should render to text if field type is text-single.
     227+        """
     228+        field = data_form.Field(fieldType='text-single', var='test',
     229+                                value=jid.JID(u'test@example.org'))
     230+        element = field.toElement()
     231+        self.assertEqual(u'test@example.org', unicode(element.value))
     232+
     233+
     234+    def test_toElementBoolean(self):
     235+        """
     236+        A boolean value should render to text.
     237+        """
     238+        field = data_form.Field(fieldType='boolean', var='test',
     239+                                value=True)
     240+        element = field.toElement()
     241+        self.assertEqual(u'true', unicode(element.value))
     242+
     243+
     244+    def test_toElementBooleanTextSingle(self):
     245+        """
     246+        A boolean value should render to text if the field type is text-single.
     247+        """
     248+        field = data_form.Field(var='test', value=True)
     249+        element = field.toElement()
     250+        self.assertEqual(u'true', unicode(element.value))
     251+
     252+
     253     def test_typeCheckNoFieldName(self):
     254         """
     255         A field not of type fixed must have a var.
     256@@ -250,6 +350,7 @@
    139257         field = data_form.Field.fromElement(element)
    140258         self.assertEquals(u'user@example.org', field.value)
     
    144262         """
    145263         Parsed jid-single field values should be of type C{unicode}.
    146 @@ -275,6 +329,49 @@
     264@@ -275,6 +376,85 @@
    147265         self.assertEquals(u'false', field.value)
    148266 
     
    191309+
    192310+
     311+    def test_fromDict(self):
     312+        """
     313+        A named field with a value can be created by providing a dictionary.
     314+        """
     315+        fieldDict = {'var': 'test', 'value': 'text'}
     316+        field = data_form.Field.fromDict(fieldDict)
     317+        self.assertEqual('test', field.var)
     318+        self.assertEqual('text', field.value)
     319+
     320+
     321+    def test_fromDictFieldType(self):
     322+        """
     323+        The field type is set using the key 'type'.
     324+        """
     325+        fieldDict = {'type': 'boolean'}
     326+        field = data_form.Field.fromDict(fieldDict)
     327+        self.assertEqual('boolean', field.fieldType)
     328+
     329+
     330+    def test_fromDictOptions(self):
     331+        """
     332+        The field options are set using the key 'options'.
     333+
     334+        The options are represented as a dictionary keyed by option,
     335+        with the optional label as value.
     336+        """
     337+        fieldDict = {'options': {'value1': 'label1',
     338+                                 'value2': 'label2'}}
     339+        field = data_form.Field.fromDict(fieldDict)
     340+        self.assertEqual(2, len(field.options))
     341+        options = {}
     342+        for option in field.options:
     343+            options[option.value] = option.label
     344+
     345+        self.assertEqual(options, fieldDict['options'])
     346+
    193347 
    194348 class FormTest(unittest.TestCase):
    195349     """
    196 @@ -289,11 +386,13 @@
     350@@ -289,11 +469,13 @@
    197351         form = data_form.Form('result')
    198352         self.assertEqual('result', form.formType)
     
    208362 
    209363         self.assertTrue(domish.IElement.providedBy(element))
    210 @@ -303,6 +402,105 @@
     364@@ -303,6 +485,105 @@
    211365         self.assertEquals([], element.children)
    212366 
     
    314468         """
    315469         C{fromElement} creates a L{data_form.Form} from a DOM representation.
    316 @@ -348,6 +546,7 @@
     470@@ -348,6 +629,7 @@
    317471 
    318472         self.assertEquals(['instruction'], form.instructions)
     
    322476         element = domish.Element((NS_X_DATA, 'x'))
    323477         element.addElement('instructions', content='instruction 1')
    324 @@ -377,3 +576,109 @@
     478@@ -377,3 +659,173 @@
    325479         self.assertEquals('field1', form.fieldList[0].var)
    326480         self.assertIn('field2', form.fields)
     
    369523+
    370524+
     525+    def test_repr(self):
     526+        """
     527+        The repr of a form should be equal to its initialization.
     528+        """
     529+        form = data_form.Form('form', title='title', instructions=['instr'],
     530+                                      formNamespace='myns',
     531+                                      fields=[data_form.Field('fixed',
     532+                                                              value='test')])
     533+        self.assertEqual("""Form(formType='form', title='title', """
     534+                         """instructions=['instr'], formNamespace='myns', """
     535+                         """fields=[Field(fieldType='fixed', """
     536+                         """values=['test'])])""",
     537+                         repr(form))
     538+
     539+
    371540+    def test_addField(self):
    372541+        """
     
    432601+        form.addField(field1)
    433602+        self.assertRaises(data_form.Error, form.addField, field2)
     603+
     604+
     605+    def test_getValues(self):
     606+        """
     607+        Each named field is represented in the values, keyed by name.
     608+        """
     609+        fields = [data_form.Field(var='botname', value='The Jabber Bot'),
     610+                  data_form.Field('boolean', var='public', value=True),
     611+                  data_form.Field('list-multi', var='features',
     612+                                                values=['news', 'search'])]
     613+        form = data_form.Form('submit', fields=fields)
     614+        values = form.getValues()
     615+        self.assertEqual({'botname': 'The Jabber Bot',
     616+                          'public': True,
     617+                          'features': ['news', 'search']},
     618+                         values)
     619+
     620+
     621+    def test_getValuesOneValueTypeMulti(self):
     622+        """
     623+        A single value for a multi-value field type is returned in a list.
     624+        """
     625+        fields = [data_form.Field('list-multi', var='features',
     626+                                                values=['news'])]
     627+        form = data_form.Form('submit', fields=fields)
     628+        values = form.getValues()
     629+        self.assertEqual({'features': ['news']}, values)
     630+
     631+
     632+    def test_getValuesMultipleValuesNoType(self):
     633+        """
     634+        Multiple values for a field without type are returned in a list.
     635+        """
     636+        fields = [data_form.Field(None, var='features',
     637+                                        values=['news', 'search'])]
     638+        form = data_form.Form('submit', fields=fields)
     639+        values = form.getValues()
     640+        self.assertEqual({'features': ['news', 'search']}, values)
     641+
     642+
     643+    def test_getValuesMultipleValuesTypeSingle(self):
     644+        """
     645+        Multiple values for a single-value field type returns the first value.
     646+        """
     647+        fields = [data_form.Field('text-single', var='features',
     648+                                        values=['news', 'search'])]
     649+        form = data_form.Form('submit', fields=fields)
     650+        values = form.getValues()
     651+        self.assertEqual({'features': 'news'}, values)
  • pubsub-forms.patch

    r22 r24  
    1 diff -r 5ccaacff8028 wokkel/data_form.py
    2 --- a/wokkel/data_form.py       Thu Dec 31 14:34:48 2009 +0100
    3 +++ b/wokkel/data_form.py       Thu Dec 31 14:36:22 2009 +0100
     1diff -r 7ec54fd678fc wokkel/data_form.py
     2--- a/wokkel/data_form.py       Fri Jan 01 01:21:56 2010 +0100
     3+++ b/wokkel/data_form.py       Fri Jan 01 01:22:24 2010 +0100
    44@@ -76,6 +76,7 @@
    55             option['label'] = self.label
     
    2828         Return the DOM representation of this Field.
    2929@@ -256,7 +258,7 @@
    30              elif self.fieldType in ('jid-single', 'jid-multi'):
    31                  value = value.full()
     30             else:
     31                 value = unicode(value)
    3232 
    3333-            field.addElement('value', content=value)
     
    140140         form['type'] = self.formType
    141141 
    142 @@ -489,7 +555,17 @@
    143  
    144          return form
    145  
    146 +
    147      def getValues(self):
    148 +        """
    149 +        Extract values from the named form fields.
    150 +
    151 +        For all named fields, the corresponding value or values are
    152 +        returned in a dictionary keyed by the field name. For multi-value
    153 +        fields, the dictionary value is a list, otherwise a single value.
    154 +
    155 +        @rtype: C{dict}
    156 +        """
    157          values = {}
    158  
    159          for name, field in self.fields.iteritems():
    160 @@ -501,3 +577,48 @@
     142@@ -516,3 +582,48 @@
    161143             values[name] = value
    162144 
     
    207189+        for field in filtered:
    208190+            self.removeField(field)
    209 diff -r 5ccaacff8028 wokkel/pubsub.py
    210 --- a/wokkel/pubsub.py  Thu Dec 31 14:34:48 2009 +0100
    211 +++ b/wokkel/pubsub.py  Thu Dec 31 14:36:22 2009 +0100
     191diff -r 7ec54fd678fc wokkel/pubsub.py
     192--- a/wokkel/pubsub.py  Fri Jan 01 01:21:56 2010 +0100
     193+++ b/wokkel/pubsub.py  Fri Jan 01 01:22:24 2010 +0100
    212194@@ -1012,29 +1012,11 @@
    213195             return None
     
    243225 
    244226 
    245 diff -r 5ccaacff8028 wokkel/test/test_data_form.py
    246 --- a/wokkel/test/test_data_form.py     Thu Dec 31 14:34:48 2009 +0100
    247 +++ b/wokkel/test/test_data_form.py     Thu Dec 31 14:36:22 2009 +0100
    248 @@ -544,3 +544,42 @@
    249          field2 = data_form.Field(var='test', value='value2')
    250          form.addField(field1)
     227diff -r 7ec54fd678fc wokkel/test/test_data_form.py
     228--- a/wokkel/test/test_data_form.py     Fri Jan 01 01:21:56 2010 +0100
     229+++ b/wokkel/test/test_data_form.py     Fri Jan 01 01:22:24 2010 +0100
     230@@ -782,6 +782,45 @@
    251231         self.assertRaises(data_form.Error, form.addField, field2)
    252 +
    253 +
     232 
     233 
    254234+    def test_removeField(self):
    255235+        """
     
    289269+        field = form.fields['pubsub#contact']
    290270+        self.assertEqual('jid-multi', field.fieldType)
     271+
     272+
     273     def test_getValues(self):
     274         """
     275         Each named field is represented in the values, keyed by name.
  • series

    r22 r24  
    33pubsub-request-options-form.patch
    44findform.patch
    5 form-coerce-values.patch
    65pubsub-subscription-options.patch
    76pubsub-create-configure.patch
Note: See TracChangeset for help on using the changeset viewer.