'How to assign new value to a ModelForm field which form edits user data, to test if the data will be edited Django test TestCase

I'm testing Django forms handling and trying to test a ModelForm, which form edits the user's data. I tried to write the test in several ways, but each one throws different mistakes. And all the mistakes are related to the data, that I try to assign to the form. I create a user and profile because in my app the user model is extended and trying to change profile.first_name from 'Test' to 'Edited'.

This is the test:

 class ProfileEditFormTests(TestCase):
  

def test_if_form__can_change_passed_data__expect_success(self):
    """
       Test ProfileEditForm if can edit a valid data:
        -Change profile.first_name from 'Test' to 'Edited'
    """
    user, profile = self.__create_valid_user_and_profile()
    form_data = {
        'first_name': profile.first_name,
        'last_name': profile.last_name,
        'image': profile.image,
        'description': profile.description,
        'phone_number': profile.phone_number,
        'email': profile.email,
    }
    updated_form_data = {
        'first_name': 'Edited',
    }
    form = ProfileEditForm(data=updated_form_data, instance=profile)
    # Error:
    #  self.assertTrue(form.is_valid())
    # AssertionError: False is not true
    # on debug, the form have only data.first_name = 'Edited'
    # and the another fields are empty

    self.assertTrue(form.is_valid())
    form.save()
    self.assertEqual(updated_form_data['first_name'], profile.first_name)

This is the error that occurs when I try to assign a form_data to the form instance.

 form = ProfileEditForm(data=updated_form_data, instance=form_data)
    # Error
    # Traceback (most recent call last):
    # object_data = model_to_dict(instance, opts.fields, opts.exclude)
    #  opts = instance._meta
    # AttributeError: 'dict' object has no attribute '_meta'

The above error occurs also when I try to change form.data.first_name in the following way.

form = ProfileEditForm(instance=form_data)
    form.data.first_name = updated_form_data['first_name']
    # Error
    # Traceback (most recent call last):
    # object_data = model_to_dict(instance, opts.fields, opts.exclude)
    #  opts = instance._meta
    # AttributeError: 'dict' object has no attribute '_meta'


Solution 1:[1]

Update: After a long search I found the solution, this is the test that passes:

def test_if_form__receive_data_properly__expect_success(self):
""" Test ProfileEditForm with valid data """
user, profile = self.__create_valid_user_and_profile()

form_data = {
    'first_name': profile.first_name,
    'last_name': profile.last_name,
    'image': profile.image,
    'description': profile.description,
    'phone_number': profile.phone_number,
    'email': profile.email,
}
form = ProfileEditForm(data=form_data)
form.instance.user = user

self.assertTrue(form.is_valid())
obj = form.save()
self.assertEqual(obj.first_name, profile.first_name)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Radoslav Hadzhiev