def test_formgroup_length_is_number_of_members(self):

        formgroup = formgroup_factory((
            NameForm,
            EmailForm,
        ), )()
        self.assertEqual(len(formgroup), 2)

        formgroup = formgroup_factory((NameForm, ), )()
        self.assertEqual(len(formgroup), 1)
Exemple #2
0
    def test_formgroup_length_is_number_of_members(self):

        formgroup = formgroup_factory(
            (NameForm,
             EmailForm,
            ),
        )()
        self.assertEqual(len(formgroup), 2)

        formgroup = formgroup_factory(
            (NameForm,
            ),
        )()
        self.assertEqual(len(formgroup), 1)
    def test_validation(self):

        FormGroupClass = formgroup_factory([
            (TestForm,  'test1'),
            (TestForm2, 'test2'),
            ])

        # create some form data -- missing a required field
        data = {
            'group-test1-name' : '',
            'group-test2-name' : 'Anita Man',
            }

        fg = FormGroupClass(data, instance=CallSentinel())
        self.assertFalse(fg.is_valid())
        self.assert_(fg.forms[0].called.get('is_valid', False))
        self.assert_(fg.forms[1].called.get('is_valid', False))

        # formgroup.errors is a dict of error dicts
        # -- TestForm2 is valid
        self.assertFalse(fg.errors[1])
        # -- TestForm is not valid
        self.assert_(fg.errors[0])

        # create some form data that passes validation
        data = {
            'group-test1-name' : 'Anita Man',
            'group-test2-name' : 'Mike Rotch',
            }

        fg = FormGroupClass(data, instance=CallSentinel())
        self.assert_(fg.is_valid())
        self.assert_(fg.forms[0].called.get('is_valid', False))
        self.assert_(fg.forms[1].called.get('is_valid', False))
    def test_form_group_factory_takes_member_sequence(self):

        fg_class = formgroup_factory((
            NameForm,
            EmailForm,
        ), )

        self.assertTrue(issubclass(fg_class, FormGroup))
    def test_formgroup_member_name_configurable(self):

        formgroup = formgroup_factory(((
            NameForm,
            'named_form',
        ), ), )()

        self.assertIsInstance(formgroup.named_form, NameForm)
Exemple #6
0
    def test_formgroup_member_name_configurable(self):

        formgroup = formgroup_factory(
            (
                (NameForm, 'named_form',),
            ),
        )()

        self.assertIsInstance(formgroup.named_form, NameForm)
Exemple #7
0
    def test_formgroup_factory_baseclass_must_subclass_formgroup(self):

        with self.assertRaises(TypeError):
            fg_class = formgroup_factory(
                (NameForm,
                 EmailForm,
                ),
                formgroup=object,
            )
    def test_initial_not_passed_to_formgroups(self):

        fg_class = formgroup_factory((
            (NameForm, 'name'),
            (formset_factory(EmailForm), 'emails'),
        ), )
        form_group = fg_class(initial={'last_name': 'Jones'})

        self.assertFalse(form_group.emails.initial)
Exemple #9
0
    def test_initial_not_passed_to_formgroups(self):

        fg_class = formgroup_factory(
            ((NameForm, 'name'),
             (formset_factory(EmailForm), 'emails'),
             ),
        )
        form_group = fg_class(initial={'last_name': 'Jones'})

        self.assertFalse(form_group.emails.initial)
    def test_formgroup_factory_baseclass_must_subclass_formgroup(self):

        with self.assertRaises(TypeError):
            fg_class = formgroup_factory(
                (
                    NameForm,
                    EmailForm,
                ),
                formgroup=object,
            )
    def test_form_group_factory_takes_form_classes_kwarg(self):

        # this was existing behavior, retaining for compatibility.

        fg_class = formgroup_factory(form_classes=(
            NameForm,
            EmailForm,
        ), )

        self.assertTrue(issubclass(fg_class, FormGroup))
Exemple #12
0
    def get_form_class(self, forms=None):
        """ Create FormGroup instance using django-rebar's formgroup_factory.

            Args:
                forms (list): use instead of self.forms   
        """
        try:
            return formgroup_factory(*[forms or self.forms])
        except NameError:
            raise ImproperlyConfigured(
                "No forms to display. Provide forms attribute.")
Exemple #13
0
    def test_form_group_factory_takes_member_sequence(self):

        fg_class = formgroup_factory(
            (NameForm,
             EmailForm,
            ),
        )

        self.assertTrue(
            issubclass(fg_class, FormGroup)
        )
    def test_formgroup_factory_can_take_baseclass(self):
        class MyFormGroup(FormGroup):
            pass

        fg_class = formgroup_factory(
            (
                NameForm,
                EmailForm,
            ),
            formgroup=MyFormGroup,
        )

        self.assertTrue(issubclass(fg_class, MyFormGroup))
Exemple #15
0
    def test_specifying_state_validators_uses_mixin_baseclass(self):

        fg_class = formgroup_factory(
            (NameForm,
             EmailForm,
            ),
            state_validators={
                'testing': {
                    'first_name': (lambda x: x,),
                },
            },
        )

        self.assertTrue(issubclass(fg_class, StateValidatorFormGroup))
Exemple #16
0
    def test_form_group_factory_takes_form_classes_kwarg(self):

        # this was existing behavior, retaining for compatibility.

        fg_class = formgroup_factory(
            form_classes = (
                NameForm,
                EmailForm,
            ),
        )

        self.assertTrue(
            issubclass(fg_class, FormGroup)
        )
    def test_pass_seperate_initial(self):
        """
        This test is related to bug in which the same initial was made
        available to the __init__ for each subform. Elements of initial that
        happened to have the same key were being overwritten.
        """
        class OtherNameForm(NameForm):
            def __init__(self, *args, **kwargs):
                kwargs['initial']['first_name'] = 'Steve'

        fg_class = formgroup_factory((OtherNameForm, NameForm), )

        formgroup = fg_class()

        self.assertNotEqual(formgroup.name.initial.get('first_name'), 'Steve')
    def test_specifying_state_validators_uses_mixin_baseclass(self):

        fg_class = formgroup_factory(
            (
                NameForm,
                EmailForm,
            ),
            state_validators={
                'testing': {
                    'first_name': (lambda x: x, ),
                },
            },
        )

        self.assertTrue(issubclass(fg_class, StateValidatorFormGroup))
Exemple #19
0
    def test_formgroup_factory_can_take_baseclass(self):

        class MyFormGroup(FormGroup):
            pass

        fg_class = formgroup_factory(
            (NameForm,
             EmailForm,
            ),
            formgroup=MyFormGroup,
        )

        self.assertTrue(
            issubclass(fg_class, MyFormGroup)
        )
    def test_form_access(self):
        """You can access individual Forms as properties."""

        FormGroupClass = formgroup_factory([
            (TestForm,  'test1'),
            (TestForm2, 'test2'),
            ])

        fg = FormGroupClass(instance=CallSentinel())

        self.assert_(len(fg), 2)
        self.assert_(isinstance(fg.test1, TestForm))
        self.assert_(isinstance(fg.test2, TestForm2))

        self.assert_(fg.test1 == fg.forms[0])
        self.assert_(fg.test2 == fg.forms[1])
    def test_form_prefixes(self):

        FormGroupClass = formgroup_factory([
            (TestForm,  'test1'),
            (TestForm2, 'test2'),
            ])

        instance = CallSentinel()
        fg = FormGroupClass(instance=instance)

        # members have different prefixes
        self.assert_(fg.forms[0].prefix != fg.forms[1].prefix)

        # the prefixes all start with the same string
        self.assert_(fg.forms[0].prefix.find(fg.prefix) == 0)
        self.assert_(fg.forms[1].prefix.find(fg.prefix) == 0)
Exemple #22
0
    def test_pass_seperate_initial(self):
        """
        This test is related to bug in which the same initial was made
        available to the __init__ for each subform. Elements of initial that
        happened to have the same key were being overwritten.
        """
        class OtherNameForm(NameForm):
            def __init__(self, *args, **kwargs):
                kwargs['initial']['first_name'] = 'Steve'

        fg_class = formgroup_factory(
            (OtherNameForm,
             NameForm),
        )

        formgroup = fg_class()

        self.assertNotEqual(formgroup.name.initial.get('first_name'), 'Steve')
Exemple #23
0
    def test_state_validators_added_to_fg_class(self):

        fg_class = formgroup_factory(
            (NameForm,
             EmailForm,
            ),
            state_validators={
                'testing': {
                    'first_name': (lambda x: x,),
                },
            },
        )

        self.assertTrue(
            hasattr(fg_class, 'state_validators'),
            'formgroup_factory did not add state_validators property to class.',
        )
        self.assertTrue(fg_class.state_validators)
    def test_state_validators_added_to_fg_class(self):

        fg_class = formgroup_factory(
            (
                NameForm,
                EmailForm,
            ),
            state_validators={
                'testing': {
                    'first_name': (lambda x: x, ),
                },
            },
        )

        self.assertTrue(
            hasattr(fg_class, 'state_validators'),
            'formgroup_factory did not add state_validators property to class.',
        )
        self.assertTrue(fg_class.state_validators)
    def test_save(self):
        """Calling .save() calls save on all elements."""

        FormGroupClass = formgroup_factory([
            (TestForm,  'test1'),
            (TestForm2, 'test2'),
            ])

        instance = CallSentinel()
        fg = FormGroupClass(instance=instance)

        # assert our save sentinel values is False to start with
        self.assertFalse(instance.called.get('save', False))
        self.assertFalse(fg.forms[0].called.get('save', False))
        self.assertFalse(fg.forms[1].called.get('save', False))

        # calling .save() will call .save() on both Forms, flipping the flag
        fg.save()
        self.assert_(fg.forms[0].called.get('save', False))
        self.assert_(fg.forms[1].called.get('save', False))

        # this also calls save() on the instance
        self.assert_(instance.called.get('save', False))
        form_data = {
            'group-name-first_name': 'John',
            'group-name-last_name': 'Doe',
            'group-email-email': '*****@*****.**',
        }
        form_group = ContactFormGroup(
            data=form_data,
            instance=model_instance,
        )

        self.assertEqual(form_group.save(), model_instance)


ContactFormGroup = formgroup_factory((
    NameForm,
    EmailForm,
), )


class TestingFormSet(BaseFormSet):
    def __init__(self, instance=None, *args, **kwargs):
        self.instance = instance
        super(TestingFormSet, self).__init__(*args, **kwargs)

    def save(self, commit=True):

        if self.instance.id is None:
            raise AssertionError("Expected instance.id to be previously set.")


MultiEmailFormGroup = formgroup_factory(
    def test_form_group_factory_returns_formgroup_subclass(self):

        fg_class = formgroup_factory([])
        self.assertTrue(issubclass(fg_class, FormGroup))
    def test_factory(self):

        FormGroupClass = formgroup_factory([])
        self.assert_(issubclass(FormGroupClass, FormGroup))
Exemple #29
0
        form_data = {
            'group-name-first_name': 'John',
            'group-name-last_name': 'Doe',
            'group-email-email': '*****@*****.**',
        }
        form_group = ContactFormGroup(
            data=form_data,
            instance=model_instance,
        )

        self.assertEqual(form_group.save(), model_instance)


ContactFormGroup = formgroup_factory(
    (
        NameForm,
        EmailForm,
    ),
)

class TestingFormSet(BaseFormSet):

    def __init__(self, instance=None, *args, **kwargs):
        self.instance = instance
        super(TestingFormSet, self).__init__(*args, **kwargs)

    def save(self, commit=True):

        if self.instance.id is None:
            raise AssertionError(
                "Expected instance.id to be previously set."
            )
Exemple #30
0
    def test_form_group_factory_returns_formgroup_subclass(self):

        fg_class = formgroup_factory([])
        self.assertTrue(
            issubclass(fg_class, FormGroup)
        )