def test_save_returns_instance(self):

        model_instance = FakeModel()

        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)
    def test_save_calls_member_saves(self):

        form_data = {
            'group-name-first_name': 'John',
            'group-name-last_name': 'Doe',
            'group-email-email': '*****@*****.**',
        }
        form_group = ContactFormGroup(
            data=form_data,
            instance=FakeModel(),
        )

        form_group.save()

        self.assertTrue(form_group.name.called['save'])
        self.assertTrue(form_group.email.called['save'])
    def test_save_calls_instance_save_once(self):

        with patch.object(FakeModel, 'save', autospec=True) as save_mock:

            save_mock.side_effect = lambda s: setattr(s, 'id', 42)
            form_data = {
                'group-name-first_name': 'John',
                'group-name-last_name': 'Doe',
                'group-email-email': '*****@*****.**',
            }
            form_group = ContactFormGroup(
                data=form_data,
                instance=FakeModel(),
            )

            form_group.save()

            save_mock.assert_called_once_with(ANY)
    def test_save_calls_save_m2m_after_parent_saved(self):

        form_data = flatten_to_dict(MultiEmailFormGroup())
        form_data.update({
            'group-name-first_name': 'Joe',
            'group-name-last_name': 'Smith',
            'group-email-0-email': '*****@*****.**',
            'group-email-1-email': '*****@*****.**',
            'group-email-TOTAL_FORMS': '2',
        })
        formgroup = MultiEmailFormGroup(
            data=form_data,
            instance=FakeModel(),
        )

        self.assertTrue(formgroup.is_valid())

        formgroup.save()

        self.assertTrue(formgroup.name.called['save_m2m'])
    def test_inline_formsets_saved_after_parent_saved(self):

        form_data = flatten_to_dict(MultiEmailFormGroup())
        form_data.update({
            'group-name-first_name': 'Joe',
            'group-name-last_name': 'Smith',
            'group-email-0-email': '*****@*****.**',
            'group-email-1-email': '*****@*****.**',
            'group-email-TOTAL_FORMS': '2',
        })
        instance = FakeModel()
        self.assertEqual(instance.id, None)

        formgroup = MultiEmailFormGroup(
            data=form_data,
            instance=instance,
        )

        self.assertTrue(formgroup.is_valid())
        self.assertEqual(len(formgroup.email), 2)

        formgroup.save()

        self.assertEqual(instance.id, 42)
    def test_instance_passed_to_members(self):

        form_group = ContactFormGroup(instance=FakeModel())
        self.assertEqual(form_group.instance, form_group.name.instance)
        self.assertEqual(form_group.instance, form_group.email.instance)