如何为表单编辑用户数据的ModelForm字段分配新值,以测试数据是否将被编辑 Django测试TestCase
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
我正在测试 Django 表单处理并尝试测试 ModelForm,该表单编辑用户数据。我尝试以多种方式编写测试,但每种方式都会引发不同的错误。所有的错误都与我尝试分配给表格的数据有关。我创建了一个用户和个人资料,因为在我的应用程序中,用户模型被扩展并试图将 profile.first_name 从 'Test' 更改为 'Edited'。
这是测试:
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)
这是我尝试将 form_data 分配给表单实例时发生的错误。
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'
当我尝试按以下方式更改 form.data.first_name 时,也会出现上述错误。
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'
更新:经过长时间的搜索我找到了解决方案,这是通过的测试:
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)
我正在测试 Django 表单处理并尝试测试 ModelForm,该表单编辑用户数据。我尝试以多种方式编写测试,但每种方式都会引发不同的错误。所有的错误都与我尝试分配给表格的数据有关。我创建了一个用户和个人资料,因为在我的应用程序中,用户模型被扩展并试图将 profile.first_name 从 'Test' 更改为 'Edited'。
这是测试:
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)
这是我尝试将 form_data 分配给表单实例时发生的错误。
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'
当我尝试按以下方式更改 form.data.first_name 时,也会出现上述错误。
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'
更新:经过长时间的搜索我找到了解决方案,这是通过的测试:
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)