示例#1
0
    def test_choices03(self):
        'From dict.'
        help = 'Stronger than word'
        field = EnhancedMultipleChoiceField(
            choices=[
                {'value': 1, 'label': 'Sword'},
                {'value': 2, 'label': 'Axes', 'help': help},
                {'value': 3, 'label': 'Spear'},
            ],
        )

        choices = [*field.choices]
        choice1 = choices[0]
        id1, label1 = choice1
        self.assertEqual('Sword', label1)
        self.assertEqual(1,       id1.value)
        self.assertEqual('',      id1.help)

        choice2 = choices[1]
        id2, label2 = choice2
        self.assertEqual('Axes', label2)
        self.assertEqual(2,    id2.value)
        self.assertEqual(help, id2.help)

        wchoice2 = [*field.widget.choices][1]
        self.assertEqual('Axes', wchoice2[1])
        wid2 = wchoice2[0]
        self.assertEqual(2,    wid2.value)
        self.assertEqual(help, wid2.help)
示例#2
0
    def test_choices04(self):
        'Dict + callable.'
        help = 'Stronger than word'

        def _choices():
            return [
                {'value': 1, 'label': 'Sword'},
                {'value': 2, 'label': 'Axes', 'help': help},
                {'value': 3, 'label': 'Spear'},
            ]

        field = EnhancedMultipleChoiceField(choices=_choices)

        choices = [*field.choices]
        choice1 = choices[0]
        id1, label1 = choice1
        self.assertEqual('Sword', label1)
        self.assertEqual(1,       id1.value)
        self.assertEqual('',      id1.help)
        self.assertEqual(False,   id1.readonly)

        choice2 = choices[1]
        id2, label2 = choice2
        self.assertEqual('Axes', label2)
        self.assertEqual(2,    id2.value)
        self.assertEqual(help, id2.help)

        wchoice2 = [*field.widget.choices][1]
        self.assertEqual('Axes', wchoice2[1])
        wid2 = wchoice2[0]
        self.assertEqual(2,    wid2.value)
        self.assertEqual(help, wid2.help)
示例#3
0
    def test_choices02(self):
        "Callable."
        def _choices():
            return [
                (1, 'Sword'),
                (2, 'Axes'),
                (3, 'Spear'),
            ]

        field = EnhancedMultipleChoiceField(choices=_choices)

        choices = [*field.choices]
        choice1 = choices[0]
        id1, label1 = choice1
        self.assertEqual('Sword', label1)
        self.assertEqual(1,       id1.value)
        self.assertEqual('',      id1.help)

        choice2 = choices[1]
        id2, label2 = choice2
        self.assertEqual('Axes', label2)
        self.assertEqual(2,      id2.value)

        wchoice = [*field.widget.choices][0]
        self.assertEqual('Sword', wchoice[1])
        self.assertEqual(1,       wchoice[0].value)
示例#4
0
    def test_choices01(self):
        "From tuples."
        field = EnhancedMultipleChoiceField(
            choices=[(1, 'Sword'),
                     (2, 'Axes'),
                     (3, 'Spear'),
                    ],
        )

        choices = [*field.choices]
        choice1 = choices[0]
        id1, label1 = choice1
        self.assertEqual('Sword', label1)
        self.assertEqual(1,       id1.value)
        self.assertEqual('',      id1.help)
        self.assertFalse(id1.readonly)

        choice2 = choices[1]
        id2, label2 = choice2
        self.assertEqual('Axes', label2)
        self.assertEqual(2,      id2.value)

        wchoice = [*field.widget.choices][0]
        self.assertEqual('Sword', wchoice[1])
        self.assertEqual(1,       wchoice[0].value)

        self.assertFalse(field.forced_values)
示例#5
0
    def test_forced_values02(self):
        "Dict + callable."
        def _choices():
            return [
                {'value': 1, 'label': 'Sword'},
                {'value': 2, 'label': 'Axes'},
                {'value': 3, 'label': 'Spear'},
                {'value': 4, 'label': 'Bow'},
            ]

        field = EnhancedMultipleChoiceField(choices=_choices)

        field.forced_values = [2, 3]
        self.assertEqual(frozenset([2, 3]), field.forced_values)
        self.assertSetEqual({2, 3}, field.initial)

        choices = [*field.choices]
        self.assertFalse(choices[0][0].readonly)
        self.assertTrue(choices[1][0].readonly)

        self.assertCountEqual(['1', '2', '3'], field.clean(['1', '2']))
示例#6
0
    def test_not_required(self):
        field = EnhancedMultipleChoiceField(
            choices=[(1, 'Sword'),
                     (2, 'Axes'),
                     (3, 'Spear'),
                    ],
            required=False,
        )

        clean = field.clean
        self.assertListEqual(['2'], [*clean(['2'])])
        self.assertFalse([], clean(''))
        self.assertFalse([], clean([]))
示例#7
0
    def test_required(self):
        choices = [(1, 'Sword'),
                   (2, 'Axes'),
                   (3, 'Spear'),
                  ]
        field = EnhancedMultipleChoiceField(choices=choices)
        self.assertTrue(field.required)
        self.assertIsInstance(field.widget, UnorderedMultipleChoiceWidget)
        self.assertSetEqual(set(), field.initial)

        clean = field.clean
        self.assertSetEqual({'1', '3'}, {*clean([1, 3])})
        self.assertSetEqual({'1', '3'}, {*clean(['1', '3'])})

        # NB: we need a 0-argument constructor
        field_builder = partial(EnhancedMultipleChoiceField, choices=choices)
        self.assertFieldValidationError(field_builder, 'required', clean, '')
        self.assertFieldValidationError(field_builder, 'required', clean, [])
        self.assertFieldValidationError(field_builder, 'required', clean, None)
示例#8
0
    def test_iterator(self):
        class CustomIterator(EnhancedChoiceIterator):
            def __iter__(self):
                for x in self.choices:
                    label = x[1]

                    yield (
                        self.choice_cls(value=x[0],
                                        help='The "{}" weapon'.format(label),
                                       ),
                        label,
                    )

        field = EnhancedMultipleChoiceField(
            choices=[(1, 'Sword'), (2, 'Axes')],
            iterator=CustomIterator,
        )

        choice = [*field.choices][0]
        self.assertEqual('The "Sword" weapon', choice[0].help)
示例#9
0
    def test_initial(self):
        field = EnhancedMultipleChoiceField(choices=[
            (1, 'Sword'),
            (2, 'Axes'),
            (3, 'Spear'),
        ])

        field.initial = [2, 1]
        self.assertSetEqual({1, 2}, field.initial)

        field.initial = None
        self.assertFalse(field.initial)

        field.forced_values = [1]
        field.initial = [2]
        self.assertSetEqual({1, 2}, field.initial)