def test_converter_RelationList(self):
        from plone.app.z3cform.converters import RelatedItemsDataConverter
        from z3c.relationfield.interfaces import IRelationList
        field = List()
        alsoProvides(field, IRelationList)
        brain1 = Mock(getObject=Mock(return_value='obj1'), UID='id1')
        brain2 = Mock(getObject=Mock(return_value='obj2'), UID='id2')
        portal_catalog = Mock(return_value=[brain1, brain2])
        widget = Mock(separator=';')
        converter = RelatedItemsDataConverter(field, widget)

        self.assertEqual(converter.toWidgetValue(None), None)
        with mock.patch(
            'plone.app.z3cform.converters.IUUID',
            side_effect=['id1', 'id2']
        ):
            self.assertEqual(
                converter.toWidgetValue(['obj1', 'obj2']), 'id1;id2')

        self.assertEqual(converter.toFieldValue(None), None)
        with mock.patch(
                'plone.app.z3cform.converters.getToolByName',
                return_value=portal_catalog):
            self.assertEqual(
                converter.toFieldValue('id1;id2'), ['obj1', 'obj2'])
    def test_converter_List_of_Choice(self):
        from plone.app.z3cform.converters import RelatedItemsDataConverter
        field = List()
        widget = Mock(separator=';')
        converter = RelatedItemsDataConverter(field, widget)

        self.assertEqual(converter.toWidgetValue(None), None)
        self.assertEqual(
            converter.toWidgetValue(['id1', 'id2']), 'id1;id2')

        self.assertEqual(converter.toFieldValue(None), None)
        self.assertEqual(
            converter.toFieldValue('id1;id2'), ['id1', 'id2'])
    def test_converter_List_of_Choice(self):
        from plone.app.z3cform.converters import RelatedItemsDataConverter
        fields = (
            List(),
            List(value_type=TextLine()),
            List(value_type=BytesLine()),
            )
        for field in fields:
            expected_value_type = getattr(field.value_type, '_type', unicode)
            widget = Mock(separator=';')
            converter = RelatedItemsDataConverter(field, widget)

            self.assertEqual(converter.toWidgetValue(None), None)
            self.assertEqual(
                converter.toWidgetValue(['id1', 'id2']), 'id1;id2')

            self.assertEqual(converter.toFieldValue(None), None)
            self.assertEqual(
                converter.toFieldValue('id1;id2'), ['id1', 'id2'])

            self.assertEqual(converter.toFieldValue(None), None)
            self.assertEqual(
                type(converter.toFieldValue('id1;id2')[0]),
                expected_value_type
                )
Exemple #4
0
    def test_converter_List_of_Choice(self):
        from plone.app.z3cform.converters import RelatedItemsDataConverter
        field = List()
        widget = Mock(separator=';')
        converter = RelatedItemsDataConverter(field, widget)

        self.assertEqual(converter.toWidgetValue(None), None)
        self.assertEqual(converter.toWidgetValue(['id1', 'id2']), 'id1;id2')

        self.assertEqual(converter.toFieldValue(None), None)
        self.assertEqual(converter.toFieldValue('id1;id2'), ['id1', 'id2'])
Exemple #5
0
    def test_converter_RelationList(self):
        from plone.app.z3cform.converters import RelatedItemsDataConverter
        from z3c.relationfield.interfaces import IRelationList
        field = List()
        alsoProvides(field, IRelationList)
        brain1 = Mock(getObject=Mock(return_value='obj1'), UID='id1')
        brain2 = Mock(getObject=Mock(return_value='obj2'), UID='id2')
        portal_catalog = Mock(return_value=[brain1, brain2])
        widget = Mock(separator=';')
        converter = RelatedItemsDataConverter(field, widget)

        self.assertEqual(converter.toWidgetValue(None), None)
        with mock.patch('plone.app.z3cform.converters.IUUID',
                        side_effect=['id1', 'id2']):
            self.assertEqual(converter.toWidgetValue(['obj1', 'obj2']),
                             'id1;id2')

        self.assertEqual(converter.toFieldValue(None), None)
        with mock.patch('plone.app.z3cform.converters.getToolByName',
                        return_value=portal_catalog):
            self.assertEqual(converter.toFieldValue('id1;id2'),
                             ['obj1', 'obj2'])