Changeset 26:59ace4d1df68 in ralphm-patches for pubsub-forms.patch


Ignore:
Timestamp:
Jan 1, 2010, 4:18:52 PM (12 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Clean up patches, more tests, adjust copyright statements to new year.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pubsub-forms.patch

    r25 r26  
    1 diff -r bf2fb205fe5a wokkel/data_form.py
    2 --- a/wokkel/data_form.py       Fri Jan 01 01:28:11 2010 +0100
    3 +++ b/wokkel/data_form.py       Fri Jan 01 01:29:23 2010 +0100
     1Move makeFields to wokkel.data_form, add form type checking.
     2
     3diff -r ba58db23e8b9 wokkel/data_form.py
     4--- a/wokkel/data_form.py       Fri Jan 01 15:54:26 2010 +0100
     5+++ b/wokkel/data_form.py       Fri Jan 01 16:16:57 2010 +0100
    46@@ -76,6 +76,7 @@
    57             option['label'] = self.label
     
    2729         """
    2830         Return the DOM representation of this Field.
    29 @@ -322,16 +324,16 @@
     31@@ -322,16 +324,24 @@
    3032 
    3133 
     
    3436-        kwargs = dictionary.copy()
    3537+    def fromDict(fieldDict):
    36 +        kwargs = fieldDict.copy()
     38+        """
     39+        Create a field from a dictionary.
    3740 
    3841-        if 'type' in dictionary:
    3942-            kwargs['fieldType'] = dictionary['type']
     43+        This is a short hand for passing arguments directly on Field object
     44+        creation. The field type is represented by the C{'type'} key. For
     45+        C{'options'} the value is not a list of L{Option}s, but a dictionary
     46+        keyed by value, with an optional label as value.
     47+        """
     48+        kwargs = fieldDict.copy()
     49+
    4050+        if 'type' in fieldDict:
    4151+            kwargs['fieldType'] = fieldDict['type']
     
    5060             kwargs['options'] = options
    5161 
    52 @@ -415,7 +417,7 @@
     62@@ -415,7 +425,7 @@
    5363         """
    5464         Add a field to this form.
     
    5969         adding new fields. Multiple fields with the same name are disallowed.
    6070         """
    61 @@ -428,7 +430,71 @@
     71@@ -428,7 +438,85 @@
    6272         self.fieldList.append(field)
    6373 
     
    7888+
    7989+        This creates fields from a mapping of name to value(s) and adds them to
    80 +        this form. If C{fieldDefs} is not C{None}, this is used to fill in
     90+        this form. It is typically used for generating outgoing forms.
     91+
     92+        If C{fieldDefs} is not C{None}, this is used to fill in
    8193+        additional properties of fields, like the field types, labels and
    82 +        possible options. If C{filterUnknown} is C{True} and C{fieldDefs} is
    83 +        not C{None}, fields will only be created from C{values} with a
    84 +        corresponding entry in C{fieldDefs}.
     94+        possible options.
     95+
     96+        If C{filterUnknown} is C{True} and C{fieldDefs} is not C{None}, fields
     97+        will only be created from C{values} with a corresponding entry in
     98+        C{fieldDefs}.
     99+
     100+        If the field type is unknown, the field type is derived from the field
     101+        value. For single values, if the value is a C{bool}, C{'boolean'} s
     102+        used, for L{JID} C{'jid-single'}, and C{'text-single'} otherwise. For
     103+        multiple values, if the first item is a L{JID} C{'jid-multi'} is used.
     104+        Otherwise C{'list-multi'}.
     105+
     106+        Note that the values are used as given, no type coersion or checking
     107+        is performed. Use L{typeCheck} to check types and coerce values of
     108+        fields added to a form.
    85109+
    86110+        @param values: Values to create fields from.
     
    110134+                        fieldDict['type'] = 'jid-multi'
    111135+                    else:
    112 +                        fieldDict['type'] = 'text-multi'
     136+                        fieldDict['type'] = 'list-multi'
    113137+            else:
    114138+                fieldDict['value'] = value
     
    131155         form['type'] = self.formType
    132156 
    133 @@ -516,3 +582,48 @@
     157@@ -516,3 +604,56 @@
    134158             values[name] = value
    135159 
     
    142166+
    143167+        This method walks all named fields to check their values against their
    144 +        type. The field definition in C{fieldDefs} is used to check the field
    145 +        type. Unknown fields are ignored.
    146 +
    147 +        If the field type is C{None} (when not set by the receiving entity),
    148 +        C{'text-single'} is assumed, as this is the default value.
     168+        type, and is typically used for forms received from other entities.The
     169+        field definition in C{fieldDefs} is used to check the field type.
     170+
     171+        If C{filterUnknown} is C{True}, fields that are not present in
     172+        C{fieldDefs} are removed from the form.
     173+
     174+        If the field type is C{None} (when not set by the sending entity),
     175+        the type from the field definitition is used, or C{'text-single'} if
     176+        that is not set.
    149177+
    150178+        @param fieldDefs: Field definitions as a dictionary. See
     
    161189+        for name, field in self.fields.iteritems():
    162190+            if name in fieldDefs:
     191+                fieldDef = fieldDefs[name]
     192+                if 'type' not in fieldDef:
     193+                    fieldDef['type'] = 'text-single'
     194+
    163195+                if field.fieldType is None:
    164 +                    field.fieldType = fieldDefs[name]['type']
    165 +                elif field.fieldType != fieldDefs[name]['type']:
     196+                    field.fieldType = fieldDef['type']
     197+                elif field.fieldType != fieldDef['type']:
    166198+                    raise TypeError("Field type for %r is %r, expected %r" %
    167199+                                    (name,
    168200+                                     field.fieldType,
    169 +                                     fieldDefs[name]['type']))
     201+                                     fieldDef['type']))
    170202+                else:
    171203+                    # Field type is correct
     
    180212+        for field in filtered:
    181213+            self.removeField(field)
    182 diff -r bf2fb205fe5a wokkel/pubsub.py
    183 --- a/wokkel/pubsub.py  Fri Jan 01 01:28:11 2010 +0100
    184 +++ b/wokkel/pubsub.py  Fri Jan 01 01:29:23 2010 +0100
     214diff -r ba58db23e8b9 wokkel/pubsub.py
     215--- a/wokkel/pubsub.py  Fri Jan 01 15:54:26 2010 +0100
     216+++ b/wokkel/pubsub.py  Fri Jan 01 16:16:57 2010 +0100
     217@@ -1,6 +1,6 @@
     218 # -*- test-case-name: wokkel.test.test_pubsub -*-
     219 #
     220-# Copyright (c) 2003-2009 Ralph Meijer
     221+# Copyright (c) 2003-2010 Ralph Meijer
     222 # See LICENSE for details.
     223 
     224 """
    185225@@ -1012,29 +1012,11 @@
    186226             return None
     
    216256 
    217257 
    218 diff -r bf2fb205fe5a wokkel/test/test_data_form.py
    219 --- a/wokkel/test/test_data_form.py     Fri Jan 01 01:28:11 2010 +0100
    220 +++ b/wokkel/test/test_data_form.py     Fri Jan 01 01:29:23 2010 +0100
    221 @@ -782,6 +782,45 @@
     258diff -r ba58db23e8b9 wokkel/test/test_data_form.py
     259--- a/wokkel/test/test_data_form.py     Fri Jan 01 15:54:26 2010 +0100
     260+++ b/wokkel/test/test_data_form.py     Fri Jan 01 16:16:57 2010 +0100
     261@@ -787,6 +787,128 @@
    222262         self.assertRaises(data_form.Error, form.addField, field2)
    223263 
     
    245285+
    246286+
    247 +    def test_makeFieldsJID(self):
     287+    def test_makeField(self):
     288+        """
     289+        Fields can be created from a dict of values and a dict of field defs.
     290+        """
     291+        fieldDefs = {
     292+                "pubsub#persist_items":
     293+                    {"type": "boolean",
     294+                     "label": "Persist items to storage"},
     295+                "pubsub#deliver_payloads":
     296+                    {"type": "boolean",
     297+                     "label": "Deliver payloads with event notifications"},
     298+                "pubsub#creator":
     299+                    {"type": "jid-single",
     300+                     "label": "The JID of the node creator"},
     301+                "pubsub#description":
     302+                    {"type": "text-single",
     303+                     "label": "A description of the node"},
     304+                "pubsub#owner":
     305+                    {"type": "jid-single",
     306+                     "label": "Owner of the node"},
     307+                }
     308+        values = {'pubsub#deliver_payloads': '0',
     309+                  'pubsub#persist_items': True,
     310+                  'pubsub#description': 'a great node',
     311+                  'pubsub#owner': jid.JID('user@example.org'),
     312+                  'x-myfield': ['a', 'b']}
     313+
     314+        form = data_form.Form('submit')
     315+        form.makeFields(values, fieldDefs)
     316+
     317+        # Check that the expected fields have been created
     318+        self.assertIn('pubsub#deliver_payloads', form.fields)
     319+        self.assertIn('pubsub#persist_items', form.fields)
     320+        self.assertIn('pubsub#description', form.fields)
     321+        self.assertIn('pubsub#owner', form.fields)
     322+
     323+        # This field is not created because there is no value for it.
     324+        self.assertNotIn('pubsub#creator', form.fields)
     325+
     326+        # This field is not created because it does not appear in fieldDefs
     327+        self.assertNotIn('x-myfield', form.fields)
     328+
     329+        # Check properties the created fields
     330+        self.assertEqual('boolean',
     331+                         form.fields['pubsub#deliver_payloads'].fieldType)
     332+        self.assertEqual('0',
     333+                         form.fields['pubsub#deliver_payloads'].value)
     334+        self.assertEqual('Deliver payloads with event notifications',
     335+                         form.fields['pubsub#deliver_payloads'].label)
     336+        self.assertEqual(True,
     337+                         form.fields['pubsub#persist_items'].value)
     338+
     339+
     340+
     341+
     342+    def test_makeFieldsUnknownTypeJID(self):
     343+        """
     344+        Without type, a single JID value sets field type jid-single.
     345+        """
    248346+        values = {'pubsub#creator': jid.JID('user@example.org')}
    249347+        form = data_form.Form('result')
     
    253351+
    254352+
    255 +    def test_makeFieldsJIDMulti(self):
     353+    def test_makeFieldsUnknownTypeJIDMulti(self):
     354+        """
     355+        Without type, multiple JID values sets field type jid-multi.
     356+        """
    256357+        values = {'pubsub#contact': [jid.JID('user@example.org'),
    257358+                                     jid.JID('other@example.org')]}
     
    262363+
    263364+
     365+    def test_makeFieldsUnknownTypeBoolean(self):
     366+        """
     367+        Without type, a boolean value sets field type boolean.
     368+        """
     369+        values = {'pubsub#persist_items': True}
     370+        form = data_form.Form('result')
     371+        form.makeFields(values)
     372+        field = form.fields['pubsub#persist_items']
     373+        self.assertEqual('boolean', field.fieldType)
     374+
     375+
     376+    def test_makeFieldsUnknownTypeListMulti(self):
     377+        """
     378+        Without type, multiple values sets field type list-multi.
     379+        """
     380+        values = {'pubsub#show-values': ['chat', 'online', 'away']}
     381+        form = data_form.Form('result')
     382+        form.makeFields(values)
     383+        field = form.fields['pubsub#show-values']
     384+        self.assertEqual('list-multi', field.fieldType)
     385+
     386+
    264387     def test_getValues(self):
    265388         """
    266389         Each named field is represented in the values, keyed by name.
     390@@ -834,3 +956,111 @@
     391         form = data_form.Form('submit', fields=fields)
     392         values = form.getValues()
     393         self.assertEqual({'features': 'news'}, values)
     394+
     395+
     396+    def test_typeCheckKnownFieldChecked(self):
     397+        """
     398+        Known fields are type checked.
     399+        """
     400+        checked = []
     401+        fieldDefs = {"pubsub#description":
     402+                        {"type": "text-single",
     403+                         "label": "A description of the node"}}
     404+        form = data_form.Form('submit')
     405+        form.addField(data_form.Field(var='pubsub#description',
     406+                                      value='a node'))
     407+        field = form.fields['pubsub#description']
     408+        field.typeCheck = lambda : checked.append(None)
     409+        form.typeCheck(fieldDefs)
     410+
     411+        self.assertEqual([None], checked)
     412+
     413+
     414+    def test_typeCheckKnownFieldNoType(self):
     415+        """
     416+        Known fields without a type get the type of the field definition.
     417+        """
     418+        checked = []
     419+        fieldDefs = {"pubsub#description":
     420+                        {"type": "text-single",
     421+                         "label": "A description of the node"}}
     422+        form = data_form.Form('submit')
     423+        form.addField(data_form.Field(None, var='pubsub#description',
     424+                                            value='a node'))
     425+        field = form.fields['pubsub#description']
     426+        field.typeCheck = lambda : checked.append(None)
     427+        form.typeCheck(fieldDefs)
     428+
     429+        self.assertEqual('text-single', field.fieldType)
     430+        self.assertEqual([None], checked)
     431+
     432+
     433+    def test_typeCheckWrongFieldType(self):
     434+        """
     435+        A field should have the same type as the field definition.
     436+        """
     437+        checked = []
     438+        fieldDefs = {"pubsub#description":
     439+                        {"type": "text-single",
     440+                         "label": "A description of the node"}}
     441+        form = data_form.Form('submit')
     442+        form.addField(data_form.Field('list-single', var='pubsub#description',
     443+                                                     value='a node'))
     444+        field = form.fields['pubsub#description']
     445+        field.typeCheck = lambda : checked.append(None)
     446+
     447+        self.assertRaises(TypeError, form.typeCheck, fieldDefs)
     448+        self.assertEqual([], checked)
     449+
     450+
     451+    def test_typeCheckDefaultTextSingle(self):
     452+        """
     453+        If a field definition has no type, use text-single.
     454+        """
     455+        checked = []
     456+        fieldDefs = {"pubsub#description":
     457+                        {"label": "A description of the node"}}
     458+        form = data_form.Form('submit')
     459+        form.addField(data_form.Field('text-single', var='pubsub#description',
     460+                                                     value='a node'))
     461+        field = form.fields['pubsub#description']
     462+        field.typeCheck = lambda : checked.append(None)
     463+        form.typeCheck(fieldDefs)
     464+
     465+        self.assertEqual([None], checked)
     466+
     467+
     468+    def test_typeCheckUnknown(self):
     469+        """
     470+        Unkown fields are not checked, but not removed if filterUnknown False.
     471+        """
     472+        checked = []
     473+        fieldDefs = {}
     474+        form = data_form.Form('submit')
     475+        form.addField(data_form.Field('list-single', var='pubsub#description',
     476+                                                     value='a node'))
     477+        field = form.fields['pubsub#description']
     478+        field.typeCheck = lambda : checked.append(None)
     479+        form.typeCheck(fieldDefs, filterUnknown=False)
     480+
     481+        self.assertIn('pubsub#description', form.fields)
     482+        self.assertEqual([], checked)
     483+
     484+
     485+    def test_typeCheckUnknownRemoved(self):
     486+        """
     487+        Unkown fields are not checked, and removed if filterUnknown True.
     488+        """
     489+        checked = []
     490+        fieldDefs = {}
     491+        form = data_form.Form('submit')
     492+        form.addField(data_form.Field('list-single', var='pubsub#description',
     493+                                                     value='a node'))
     494+        field = form.fields['pubsub#description']
     495+        field.typeCheck = lambda : checked.append(None)
     496+        form.typeCheck(fieldDefs, filterUnknown=True)
     497+
     498+        self.assertNotIn('pubsub#description', form.fields)
     499+        self.assertEqual([], checked)
     500+
     501+
Note: See TracChangeset for help on using the changeset viewer.