def test_custom_field():
    # a custom field that returns a list
    # it doesn't inherits from SelectMultipleField
    class SuperPowersField(SelectFieldBase):
        POWERS = [
            ('fly', ''),
            ('super strength', ''),
            ('regeneration', ''),
            ('stamina', ''),
            ('agility', ''),
        ]

        widget = widgets.Select(multiple=True)

        def iter_choices(self):
            if self.allow_blank:
                yield (u'__None', self.blank_text, self.data is None)
            for item in self.POWERS:
                selected = item[0] in self.data
                yield (item[0], item[1], selected)

        def process_formdata(self, valuelist):
            if valuelist:
                if valuelist[0] == '__None':
                    self.data = None
                else:
                    self.data = [
                        item[0] for item in self.POWERS
                        if str(item[0]) in valuelist]
                    if not len(self.data):
                        self.data = None

        def _is_selected(self, item):
            return item in self.data

    class SuperHeroForm(Form):
        name = TextField()
        powers = SuperPowersField(
            validators=[validators.required()]
        )

    fixtures = [
        {'name': 'Juggernaut', 'powers': ['super strength'], '__result': True},
        {
            'name': 'Wolverine',
            'powers': ['stamina', 'agility', 'regeneration'],
            '__result': True
        },
        {'name': 'Beast', 'powers': ['agility'], '__result': True},
        {'name': 'Rocket Rackoon', 'powers': [], '__result': False}
    ]

    for fixture in fixtures:
        result = fixture.pop('__result')
        assert SuperHeroForm.from_json(fixture).validate() == result
dataset/ETHPy150Open kvesteri/wtforms-json/tests/test_select_multiple_field.py/test_custom_field/original