source: ralphm-patches/form-missing-tests.patch @ 23:a689746cd2b3

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

Even more tests.

File size: 15.2 KB
  • wokkel/data_form.py

    diff -r 3ef9bc7a0d70 wokkel/data_form.py
    a b  
    343343    """
    344344    Data Form.
    345345
    346     There are two similarly named properties of forms. The L{formType} is the
     346    There are two similarly named properties of forms. The C{formType} is the
    347347    the so-called type of the form, and is set as the C{'type'} attribute
    348348    on the form's root element.
    349349
     
    351351    provide a context for the field names used in this form, by setting a
    352352    special hidden field named C{'FORM_TYPE'}, to put the names of all
    353353    other fields in the namespace of the value of that field. This namespace
    354     is recorded in the L{formNamespace} instance variable.
     354    is recorded in the C{formNamespace} instance variable.
    355355
    356356    @ivar formType: Type of form. One of C{'form'}, C{'submit'}, {'cancel'},
    357357                    or {'result'}.
    358     @type formType: C{str}.
     358    @type formType: C{str}
     359
     360    @ivar title: Natural language title of the form.
     361    @type title: C{unicode}
     362
     363    @ivar instructions: Natural language instructions as a list of C{unicode}
     364        strings without line breaks.
     365    @type instructions: C{list}
     366
    359367    @ivar formNamespace: The optional namespace of the field names for this
    360                          form. This goes in the special field named
    361                          C{'FORM_TYPE'}, if set.
     368        form. This goes in the special field named C{'FORM_TYPE'}, if set.
    362369    @type formNamespace: C{str}.
    363     @ivar fields: Dictionary of fields that have a name. Note that this is
    364                   meant to be used for reading, only. One should use
    365                   L{addField} for adding fields.
     370
     371    @ivar fields: Dictionary of named fields. Note that this is meant to be
     372        used for reading, only. One should use L{addField} or L{makeFields} and
     373        L{removeField} for adding and removing fields.
    366374    @type fields: C{dict}
     375
     376    @ivar fieldList: List of all fields, in the order they are added. Like
     377        C{fields}, this is meant to be used for reading, only.
     378    @type fieldList: C{list}
    367379    """
    368380
    369381    def __init__(self, formType, title=None, instructions=None,
     
    424436            form.addElement('title', content=self.title)
    425437
    426438        for instruction in self.instructions:
    427             form.addElement('instruction', content=instruction)
     439            form.addElement('instructions', content=instruction)
    428440
    429441        if self.formNamespace is not None:
    430442            field = Field('hidden', 'FORM_TYPE', self.formNamespace)
  • wokkel/test/test_data_form.py

    diff -r 3ef9bc7a0d70 wokkel/test/test_data_form.py
    a b  
    1919    """
    2020
    2121    def test_toElement(self):
     22        option = data_form.Option('value')
     23        element = option.toElement()
     24
     25        self.assertEqual('option', element.name)
     26        self.assertEqual(NS_X_DATA, element.uri)
     27        self.assertEqual(NS_X_DATA, element.value.uri)
     28        self.assertEqual('value', unicode(element.value))
     29        self.assertFalse(element.hasAttribute('label'))
     30
     31
     32    def test_toElementLabel(self):
    2233        option = data_form.Option('value', 'label')
    2334        element = option.toElement()
    24         self.assertEquals('option', element.name)
    25         self.assertEquals(NS_X_DATA, element.uri)
    26         self.assertEquals('label', element['label'])
    27         self.assertEquals('value', element.value.name)
    28         self.assertEquals(NS_X_DATA, element.value.uri)
    29         self.assertEquals('value', unicode(element.value))
     35
     36        self.assertEqual('option', element.name)
     37        self.assertEqual(NS_X_DATA, element.uri)
     38        self.assertEqual(NS_X_DATA, element.value.uri)
     39        self.assertEqual('value', unicode(element.value))
     40        self.assertEqual('label', element['label'])
     41
     42
     43    def test_fromElement(self):
     44        """
     45        An option has a child element with the option value.
     46        """
     47        element = domish.Element((NS_X_DATA, 'option'))
     48        element.addElement('value', content='value')
     49        option = data_form.Option.fromElement(element)
     50
     51        self.assertEqual('value', option.value)
     52        self.assertIdentical(None, option.label)
     53
     54
     55    def test_fromElementLabel(self):
     56        """
     57        An option label is an attribute on the option element.
     58        """
     59
     60        element = domish.Element((NS_X_DATA, 'option'))
     61        element.addElement('value', content='value')
     62        element['label'] = 'label'
     63        option = data_form.Option.fromElement(element)
     64
     65        self.assertEqual('label', option.label)
     66
     67
     68    def test_fromElementNoValue(self):
     69        """
     70        An option MUST have a value.
     71        """
     72        element = domish.Element((NS_X_DATA, 'option'))
     73        self.assertRaises(data_form.Error,
     74                          data_form.Option.fromElement, element)
     75
     76
     77    def test_repr(self):
     78        """
     79        The representation of an Option is equal to how it is created.
     80        """
     81        option = data_form.Option('value', 'label')
     82        self.assertEqual("""Option('value', 'label')""", repr(option))
    3083
    3184
    3285
     
    250303        field = data_form.Field.fromElement(element)
    251304        self.assertEquals(u'user@example.org', field.value)
    252305
     306
    253307    def test_fromElementValueJIDMalformed(self):
    254308        """
    255309        Parsed jid-single field values should be of type C{unicode}.
     
    275329        self.assertEquals(u'false', field.value)
    276330
    277331
     332    def test_fromElementDesc(self):
     333        """
     334        Field descriptions are in a desc child element.
     335        """
     336        element = domish.Element((NS_X_DATA, 'field'))
     337        element.addElement('desc', content=u'My description')
     338        field = data_form.Field.fromElement(element)
     339        self.assertEqual(u'My description', field.desc)
     340
     341
     342    def test_fromElementOption(self):
     343        """
     344        Field descriptions are in a desc child element.
     345        """
     346        element = domish.Element((NS_X_DATA, 'field'))
     347        element.addElement('option').addElement('value', content=u'option1')
     348        element.addElement('option').addElement('value', content=u'option2')
     349        field = data_form.Field.fromElement(element)
     350        self.assertEqual(2, len(field.options))
     351
     352
     353    def test_fromElementRequired(self):
     354        """
     355        Required fields have a required child element.
     356        """
     357        element = domish.Element((NS_X_DATA, 'field'))
     358        element.addElement('required')
     359        field = data_form.Field.fromElement(element)
     360        self.assertTrue(field.required)
     361
     362
     363    def test_fromElementChildOtherNamespace(self):
     364        """
     365        Child elements from another namespace are ignored.
     366        """
     367        element = domish.Element((NS_X_DATA, 'field'))
     368        element['var'] = 'test'
     369        child = element.addElement(('myns', 'value'))
     370        field = data_form.Field.fromElement(element)
     371
     372        self.assertIdentical(None, field.value)
     373
     374
    278375
    279376class FormTest(unittest.TestCase):
    280377    """
     
    289386        form = data_form.Form('result')
    290387        self.assertEqual('result', form.formType)
    291388
     389
    292390    def test_toElement(self):
    293391        """
    294392        The toElement method returns a form's DOM representation.
    295393        """
    296394        form = data_form.Form('result')
     395
    297396        element = form.toElement()
    298397
    299398        self.assertTrue(domish.IElement.providedBy(element))
     
    303402        self.assertEquals([], element.children)
    304403
    305404
     405    def test_toElementTitle(self):
     406        """
     407        A title is rendered as a child element with the title as CDATA.
     408        """
     409        form = data_form.Form('form', title='Bot configuration')
     410        element = form.toElement()
     411
     412        elements = list(element.elements())
     413        self.assertEqual(1, len(elements))
     414        title = elements[0]
     415        self.assertEqual('title', title.name)
     416        self.assertEqual(NS_X_DATA, title.uri)
     417        self.assertEqual('Bot configuration', unicode(title))
     418
     419
     420    def test_toElementInstructions(self):
     421        """
     422        Instructions are rendered as child elements with CDATA.
     423        """
     424        form = data_form.Form('form', instructions=['Fill out this form!'])
     425        element = form.toElement()
     426
     427        elements = list(element.elements())
     428        self.assertEqual(1, len(elements))
     429        instructions = elements[0]
     430        self.assertEqual('instructions', instructions.name)
     431        self.assertEqual(NS_X_DATA, instructions.uri)
     432        self.assertEqual('Fill out this form!', unicode(instructions))
     433
     434
     435    def test_toElementInstructionsMultiple(self):
     436        """
     437        Instructions render as one element per instruction, in order.
     438        """
     439        form = data_form.Form('form', instructions=['Fill out this form!',
     440                                                    'no really'])
     441        element = form.toElement()
     442
     443        elements = list(element.elements())
     444        self.assertEqual(2, len(elements))
     445        instructions1 = elements[0]
     446        instructions2 = elements[1]
     447        self.assertEqual('instructions', instructions1.name)
     448        self.assertEqual(NS_X_DATA, instructions1.uri)
     449        self.assertEqual('Fill out this form!', unicode(instructions1))
     450        self.assertEqual('instructions', instructions2.name)
     451        self.assertEqual(NS_X_DATA, instructions2.uri)
     452        self.assertEqual('no really', unicode(instructions2))
     453
     454
     455    def test_toElementFormType(self):
     456        """
     457        The form type is rendered as a hidden field with name FORM_TYPE.
     458        """
     459        form = data_form.Form('form', formNamespace='jabber:bot')
     460        element = form.toElement()
     461
     462        elements = list(element.elements())
     463        self.assertEqual(1, len(elements))
     464        formTypeField = elements[0]
     465        self.assertEqual('field', formTypeField.name)
     466        self.assertEqual(NS_X_DATA, formTypeField.uri)
     467        self.assertEqual('FORM_TYPE', formTypeField['var'])
     468        self.assertEqual('hidden', formTypeField['type'])
     469        self.assertEqual('jabber:bot', unicode(formTypeField.value))
     470
     471
     472    def test_toElementFields(self):
     473        """
     474        Fields are rendered as child elements, in order.
     475        """
     476        fields = [data_form.Field('fixed', value='Section 1'),
     477                  data_form.Field('text-single',
     478                                  var='botname',
     479                                  label='The name of your bot'),
     480                  data_form.Field('text-multi',
     481                                  var='description',
     482                                  label='Helpful description of your bot'),
     483                  data_form.Field('boolean',
     484                                  var='public',
     485                                  label='Public bot?',
     486                                  required=True)
     487                 ]
     488        form = data_form.Form('form', fields=fields)
     489        element = form.toElement()
     490
     491        elements = list(element.elements())
     492        self.assertEqual(4, len(elements))
     493        for field in elements:
     494            self.assertEqual('field', field.name)
     495            self.assertEqual(NS_X_DATA, field.uri)
     496
     497        # Check order
     498        self.assertEqual('fixed', elements[0]['type'])
     499        self.assertEqual('botname', elements[1]['var'])
     500        self.assertEqual('description', elements[2]['var'])
     501        self.assertEqual('public', elements[3]['var'])
     502
     503
    306504    def test_fromElement(self):
    307505        """
    308506        C{fromElement} creates a L{data_form.Form} from a DOM representation.
     
    348546
    349547        self.assertEquals(['instruction'], form.instructions)
    350548
     549
    351550    def test_fromElementInstructions2(self):
    352551        element = domish.Element((NS_X_DATA, 'x'))
    353552        element.addElement('instructions', content='instruction 1')
     
    377576        self.assertEquals('field1', form.fieldList[0].var)
    378577        self.assertIn('field2', form.fields)
    379578        self.assertEquals('field2', form.fieldList[1].var)
     579
     580
     581    def test_fromElementFormType(self):
     582        """
     583        The form type is a hidden field named FORM_TYPE.
     584        """
     585        element = domish.Element((NS_X_DATA, 'x'))
     586        field = element.addElement('field')
     587        field['var'] = 'FORM_TYPE'
     588        field['type'] = 'hidden'
     589        field.addElement('value', content='myns')
     590        form = data_form.Form.fromElement(element)
     591
     592        self.assertNotIn('FORM_TYPE', form.fields)
     593        self.assertEqual('myns', form.formNamespace)
     594
     595    def test_fromElementFormTypeNotHidden(self):
     596        """
     597        A non-hidden field named FORM_TYPE does not set the form type.
     598        """
     599        element = domish.Element((NS_X_DATA, 'x'))
     600        field = element.addElement('field')
     601        field['var'] = 'FORM_TYPE'
     602        field.addElement('value', content='myns')
     603        form = data_form.Form.fromElement(element)
     604
     605        self.assertIn('FORM_TYPE', form.fields)
     606        self.assertIdentical(None, form.formNamespace)
     607
     608
     609    def test_fromElementChildOtherNamespace(self):
     610        """
     611        Child elements from another namespace are ignored.
     612        """
     613        element = domish.Element((NS_X_DATA, 'x'))
     614        element['type'] = 'result'
     615        field = element.addElement(('myns', 'field'))
     616        field['var'] = 'test'
     617        form = data_form.Form.fromElement(element)
     618
     619        self.assertEqual(0, len(form.fields))
     620
     621
     622    def test_addField(self):
     623        """
     624        A field should occur in fieldList.
     625        """
     626        form = data_form.Form('result')
     627        field = data_form.Field('fixed', value='Section 1')
     628        form.addField(field)
     629        self.assertEqual([field], form.fieldList)
     630
     631
     632    def test_addFieldTwice(self):
     633        """
     634        Fields occur in fieldList in the order they were added.
     635        """
     636        form = data_form.Form('result')
     637        field1 = data_form.Field('fixed', value='Section 1')
     638        field2 = data_form.Field('fixed', value='Section 2')
     639        form.addField(field1)
     640        form.addField(field2)
     641        self.assertEqual([field1, field2], form.fieldList)
     642
     643
     644    def test_addFieldNotNamed(self):
     645        """
     646        A non-named field should not occur in fields.
     647        """
     648        form = data_form.Form('result')
     649        field = data_form.Field('fixed', value='Section 1')
     650        form.addField(field)
     651        self.assertEqual({}, form.fields)
     652
     653
     654    def test_addFieldNamed(self):
     655        """
     656        A named field should occur in fields.
     657        """
     658        form = data_form.Form('result')
     659        field = data_form.Field(var='test')
     660        form.addField(field)
     661        self.assertEqual({'test': field}, form.fields)
     662
     663
     664    def test_addFieldTwiceNamed(self):
     665        """
     666        A second named field should occur in fields.
     667        """
     668        form = data_form.Form('result')
     669        field1 = data_form.Field(var='test')
     670        field2 = data_form.Field(var='test2')
     671        form.addField(field2)
     672        form.addField(field1)
     673        self.assertEqual({'test': field1, 'test2': field2}, form.fields)
     674
     675
     676    def test_addFieldSameName(self):
     677        """
     678        A named field cannot occur twice.
     679        """
     680        form = data_form.Form('result')
     681        field1 = data_form.Field(var='test', value='value')
     682        field2 = data_form.Field(var='test', value='value2')
     683        form.addField(field1)
     684        self.assertRaises(data_form.Error, form.addField, field2)
Note: See TracBrowser for help on using the repository browser.