Django 表单集单元测试

发布于 2024-08-09 05:49:20 字数 3970 浏览 5 评论 0原文

我无法使用表单集运行单元测试。

我尝试做一个测试:

class NewClientTestCase(TestCase):
    
    def setUp(self):
        self.c = Client()

    def test_0_create_individual_with_same_adress(self):
        
        post_data =  {
            'ctype': User.CONTACT_INDIVIDUAL,
            'username': 'dupond.f',        
            'email': '[email protected]', 
            'password': 'pwd', 
            'password2': 'pwd', 
            'civility': User.CIVILITY_MISTER, 
            'first_name': 'François', 
            'last_name': 'DUPOND', 
            'phone': '+33 1 34 12 52 30', 
            'gsm': '+33 6 34 12 52 30', 
            'fax': '+33 1 34 12 52 30', 
            'form-0-address1': '33 avenue Gambetta', 
            'form-0-address2': 'apt 50', 
            'form-0-zip_code': '75020', 
            'form-0-city': 'Paris', 
            'form-0-country': 'FRA', 
            'same_for_billing': True,            
        }
        
        response = self.c.post(reverse('client:full_account'), post_data, follow=True)   

        self.assertRedirects(response, '%s?created=1' % reverse('client:dashboard'))

但出现以下错误:

ValidationError: [u'ManagementForm 数据丢失或已丢失 被篡改']

我的观点:

def full_account(request, url_redirect=''):    
    from forms import NewUserFullForm,  AddressForm,  BaseArticleFormSet
    
    fields_required = []
    fields_notrequired = []
    
    AddressFormSet = formset_factory(AddressForm, extra=2,  formset=BaseArticleFormSet)
    
    if request.method == 'POST':        
        form = NewUserFullForm(request.POST)        
        objforms = AddressFormSet(request.POST)            
       
        if objforms.is_valid() and form.is_valid():            
            user = form.save()            
            address = objforms.forms[0].save()

            
            if url_redirect=='':
                url_redirect = '%s?created=1' % reverse('client:dashboard')
                logon(request, form.instance)            
            return HttpResponseRedirect(url_redirect)
    else:
        form = NewUserFullForm()
        objforms = AddressFormSet()   
    
    return direct_to_template(request, 'clients/full_account.html', {
        'form':form,
        'formset': objforms, 
        'tld_fr':False, 
    })

和我的表单文件:

class BaseArticleFormSet(BaseFormSet):

    def clean(self):        
        
        msg_err = _('Ce champ est obligatoire.')
        non_errors = True
        
        if 'same_for_billing' in self.data and self.data['same_for_billing'] == 'on':
            same_for_billing = True
        else:            
            same_for_billing = False
        
        for i in [0, 1]:
            
            form = self.forms[i]           
            
            for field in form.fields:                                
                name_field = 'form-%d-%s' % (i, field )
                value_field = self.data[name_field].strip()                
                
                if i == 0 and self.forms[0].fields[field].required and value_field =='':                    
                    form.errors[field] = msg_err                    
                    non_errors = False
                    
                elif i == 1 and not same_for_billing and self.forms[1].fields[field].required and value_field =='':
                    form.errors[field] = msg_err                    
                    non_errors = False
        
        return non_errors

class AddressForm(forms.ModelForm):

    class Meta:
        model = Address

    address1 = forms.CharField()
    address2 = forms.CharField(required=False)
    zip_code = forms.CharField()
    city = forms.CharField()
    country = forms.ChoiceField(choices=CountryField.COUNTRIES,  initial='FRA')

I can't run a unit test with formset.

I try to do a test:

class NewClientTestCase(TestCase):
    
    def setUp(self):
        self.c = Client()

    def test_0_create_individual_with_same_adress(self):
        
        post_data =  {
            'ctype': User.CONTACT_INDIVIDUAL,
            'username': 'dupond.f',        
            'email': '[email protected]', 
            'password': 'pwd', 
            'password2': 'pwd', 
            'civility': User.CIVILITY_MISTER, 
            'first_name': 'François', 
            'last_name': 'DUPOND', 
            'phone': '+33 1 34 12 52 30', 
            'gsm': '+33 6 34 12 52 30', 
            'fax': '+33 1 34 12 52 30', 
            'form-0-address1': '33 avenue Gambetta', 
            'form-0-address2': 'apt 50', 
            'form-0-zip_code': '75020', 
            'form-0-city': 'Paris', 
            'form-0-country': 'FRA', 
            'same_for_billing': True,            
        }
        
        response = self.c.post(reverse('client:full_account'), post_data, follow=True)   

        self.assertRedirects(response, '%s?created=1' % reverse('client:dashboard'))

and I have this error:

ValidationError: [u'ManagementForm data is missing or has been
tampered with']

My view :

def full_account(request, url_redirect=''):    
    from forms import NewUserFullForm,  AddressForm,  BaseArticleFormSet
    
    fields_required = []
    fields_notrequired = []
    
    AddressFormSet = formset_factory(AddressForm, extra=2,  formset=BaseArticleFormSet)
    
    if request.method == 'POST':        
        form = NewUserFullForm(request.POST)        
        objforms = AddressFormSet(request.POST)            
       
        if objforms.is_valid() and form.is_valid():            
            user = form.save()            
            address = objforms.forms[0].save()

            
            if url_redirect=='':
                url_redirect = '%s?created=1' % reverse('client:dashboard')
                logon(request, form.instance)            
            return HttpResponseRedirect(url_redirect)
    else:
        form = NewUserFullForm()
        objforms = AddressFormSet()   
    
    return direct_to_template(request, 'clients/full_account.html', {
        'form':form,
        'formset': objforms, 
        'tld_fr':False, 
    })

and my form file :

class BaseArticleFormSet(BaseFormSet):

    def clean(self):        
        
        msg_err = _('Ce champ est obligatoire.')
        non_errors = True
        
        if 'same_for_billing' in self.data and self.data['same_for_billing'] == 'on':
            same_for_billing = True
        else:            
            same_for_billing = False
        
        for i in [0, 1]:
            
            form = self.forms[i]           
            
            for field in form.fields:                                
                name_field = 'form-%d-%s' % (i, field )
                value_field = self.data[name_field].strip()                
                
                if i == 0 and self.forms[0].fields[field].required and value_field =='':                    
                    form.errors[field] = msg_err                    
                    non_errors = False
                    
                elif i == 1 and not same_for_billing and self.forms[1].fields[field].required and value_field =='':
                    form.errors[field] = msg_err                    
                    non_errors = False
        
        return non_errors

class AddressForm(forms.ModelForm):

    class Meta:
        model = Address

    address1 = forms.CharField()
    address2 = forms.CharField(required=False)
    zip_code = forms.CharField()
    city = forms.CharField()
    country = forms.ChoiceField(choices=CountryField.COUNTRIES,  initial='FRA')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

奈何桥上唱咆哮 2024-08-16 05:49:20

特别是,我发现 ManagmentForm 验证器正在寻找要发布的以下项目:

form_data = {
            'form-TOTAL_FORMS': 1, 
            'form-INITIAL_FORMS': 0 
}

In particular, I've found that the ManagmentForm validator is looking for the following items to be POSTed:

form_data = {
            'form-TOTAL_FORMS': 1, 
            'form-INITIAL_FORMS': 0 
}
娜些时光,永不杰束 2024-08-16 05:49:20

每个 Django 表单集都带有一个需要包含在帖子中的管理表单。 官方文档很好地解释了这一点。要在单元测试中使用它,您需要自己编写它。 (我提供的链接显示了一个示例),或调用输出数据的 formset.management_form

Every Django formset comes with a management form that needs to be included in the post. The official docs explain it pretty well. To use it within your unit test, you either need to write it out yourself. (The link I provided shows an example), or call formset.management_form which outputs the data.

颜漓半夏 2024-08-16 05:49:20

事实上,通过检查响应的上下文可以很容易地重现表单集中的内容。

考虑下面的代码(其中 self.client 是常规的 测试客户端):

url = "some_url"

response = self.client.get(url)
self.assertEqual(response.status_code, 200)

# data will receive all the forms field names
# key will be the field name (as "formx-fieldname"), value will be the string representation.
data = {}

# global information, some additional fields may go there
data['csrf_token'] = response.context['csrf_token']

# management form information, needed because of the formset
management_form = response.context['form'].management_form
for i in 'TOTAL_FORMS', 'INITIAL_FORMS', 'MIN_NUM_FORMS', 'MAX_NUM_FORMS':
    data['%s-%s' % (management_form.prefix, i)] = management_form[i].value()

for i in range(response.context['form'].total_form_count()):
    # get form index 'i'
    current_form = response.context['form'].forms[i]

    # retrieve all the fields
    for field_name in current_form.fields:
        value = current_form[field_name].value()
        data['%s-%s' % (current_form.prefix, field_name)] = value if value is not None else ''

# flush out to stdout
print '#' * 30
for i in sorted(data.keys()):
    print i, '\t:', data[i]

# post the request without any change
response = self.client.post(url, data)

重要提示

如果您在调用self.client.post之前修改data,则可能会改变数据库。因此,对 self.client.get 的后续调用可能不会产生相同的数据,特别是对于管理表单和表单集中表单的顺序(因为它们可以以不同的方式排序,取决于底层查询集)。这意味着,

  • 如果您修改 data[form-3-somefield] 并调用 self.client.get,相同的字段可能会出现在 data[form- 8-somefield]
  • 如果您在 self.client.post 之前修改 data,则无法调用 self.client.post > 再次使用相同的数据:您必须调用self.client.get并再次重建数据

It is in fact easy to reproduce whatever is in the formset by inspecting the context of the response.

Consider the code below (with self.client being a regular test client):

url = "some_url"

response = self.client.get(url)
self.assertEqual(response.status_code, 200)

# data will receive all the forms field names
# key will be the field name (as "formx-fieldname"), value will be the string representation.
data = {}

# global information, some additional fields may go there
data['csrf_token'] = response.context['csrf_token']

# management form information, needed because of the formset
management_form = response.context['form'].management_form
for i in 'TOTAL_FORMS', 'INITIAL_FORMS', 'MIN_NUM_FORMS', 'MAX_NUM_FORMS':
    data['%s-%s' % (management_form.prefix, i)] = management_form[i].value()

for i in range(response.context['form'].total_form_count()):
    # get form index 'i'
    current_form = response.context['form'].forms[i]

    # retrieve all the fields
    for field_name in current_form.fields:
        value = current_form[field_name].value()
        data['%s-%s' % (current_form.prefix, field_name)] = value if value is not None else ''

# flush out to stdout
print '#' * 30
for i in sorted(data.keys()):
    print i, '\t:', data[i]

# post the request without any change
response = self.client.post(url, data)

Important note

If you modify data prior to calling the self.client.post, you are likely mutating the DB. As a consequence, subsequent call to self.client.get might not yield to the same data, in particular for the management form and the order of the forms in the formset (because they can be ordered differently, depending on the underlying queryset). This means that

  • if you modify data[form-3-somefield] and call self.client.get, this same field might appear in say data[form-8-somefield],
  • if you modify data prior to a self.client.post, you cannot call self.client.post again with the same data: you have to call a self.client.get and reconstruct data again.
酒中人 2024-08-16 05:49:20

Django formset 单元测试

您可以将以下测试辅助方法添加到您的测试类 [Python 3 代码]

def build_formset_form_data(self, form_number, **data):
    form = {}
    for key, value in data.items():
        form_key = f"form-{form_number}-{key}"
        form[form_key] = value
    return form

def build_formset_data(self, forms, **common_data):
    formset_dict = {
        "form-TOTAL_FORMS": f"{len(forms)}",
        "form-MAX_NUM_FORMS": "1000",
        "form-INITIAL_FORMS": "1"
    }
    formset_dict.update(common_data)
    for i, form_data in enumerate(forms):
        form_dict = self.build_formset_form_data(form_number=i, **form_data)
        formset_dict.update(form_dict)
    return formset_dict

并在测试中使用它们

def test_django_formset_post(self):
    forms = [{"key1": "value1", "key2": "value2"}, {"key100": "value100"}]
    payload = self.build_formset_data(forms=forms, global_param=100)
    print(payload)
    # self.client.post(url=url, data=payload)

您将 >获取正确的有效负载,这使 Django ManagementForm 满意

{
    "form-INITIAL_FORMS": "1",
    "form-TOTAL_FORMS": "2",
    "form-MAX_NUM_FORMS": "1000",
    "global_param": 100,
    "form-0-key1": "value1",
    "form-0-key2": "value2",
    "form-1-key100": "value100",
}

利润

Django formset unit test

You can add following test helper methods to your test class [Python 3 code]

def build_formset_form_data(self, form_number, **data):
    form = {}
    for key, value in data.items():
        form_key = f"form-{form_number}-{key}"
        form[form_key] = value
    return form

def build_formset_data(self, forms, **common_data):
    formset_dict = {
        "form-TOTAL_FORMS": f"{len(forms)}",
        "form-MAX_NUM_FORMS": "1000",
        "form-INITIAL_FORMS": "1"
    }
    formset_dict.update(common_data)
    for i, form_data in enumerate(forms):
        form_dict = self.build_formset_form_data(form_number=i, **form_data)
        formset_dict.update(form_dict)
    return formset_dict

And use them in test

def test_django_formset_post(self):
    forms = [{"key1": "value1", "key2": "value2"}, {"key100": "value100"}]
    payload = self.build_formset_data(forms=forms, global_param=100)
    print(payload)
    # self.client.post(url=url, data=payload)

You will get correct payload which makes Django ManagementForm happy

{
    "form-INITIAL_FORMS": "1",
    "form-TOTAL_FORMS": "2",
    "form-MAX_NUM_FORMS": "1000",
    "global_param": 100,
    "form-0-key1": "value1",
    "form-0-key2": "value2",
    "form-1-key100": "value100",
}

Profit

小嗲 2024-08-16 05:49:20

这里有几个非常有用的答案,例如 pymenRaffi,展示了如何使用测试客户端为表单集帖子构建格式正确的有效负载。

然而,所有这些仍然需要至少对前缀进行一些手工编码、处理现有对象等,这并不理想。

作为替代方案,我们可以为 post() 使用 get() 请求获得的响应

def create_formset_post_data(response, new_form_data=None):
    if new_form_data is None:
        new_form_data = []
    csrf_token = response.context['csrf_token']
    formset = response.context['formset']
    prefix_template = formset.empty_form.prefix  # default is 'form-__prefix__'
    # extract initial formset data
    management_form_data = formset.management_form.initial
    form_data_list = formset.initial  # this is a list of dict objects
    # add new form data and update management form data
    form_data_list.extend(new_form_data)
    management_form_data['TOTAL_FORMS'] = len(form_data_list)
    # initialize the post data dict...
    post_data = dict(csrf_token=csrf_token)
    # add properly prefixed management form fields
    for key, value in management_form_data.items():
        prefix = prefix_template.replace('__prefix__', '')
        post_data[prefix + key] = value
    # add properly prefixed data form fields
    for index, form_data in enumerate(form_data_list):
        for key, value in form_data.items():
            prefix = prefix_template.replace('__prefix__', f'{index}-')
            post_data[prefix + key] = value
    return post_data

输出 (post_data) 还将包括任何现有对象的表单字段。

以下是您在 Django TestCase 中使用它的方法:

def test_post_formset_data(self):
    url_path = '/my/post/url/'
    user = User.objects.create()
    self.client.force_login(user)
    # first GET the form content
    response = self.client.get(url_path)
    self.assertEqual(HTTPStatus.OK, response.status_code)
    # specify form data for test
    test_data = [
        dict(first_name='someone', email='[email protected]', ...),
        ...
    ]
    # convert test_data to properly formatted dict
    post_data = create_formset_post_data(response, new_form_data=test_data)
    # now POST the data
    response = self.client.post(url_path, data=post_data, follow=True)
    # some assertions here
    ...

一些注意事项:

  • 我们可以导入 TOTAL_FORM_COUNTTOTAL_FORM_COUNT 字符串文字,而不是使用 'TOTAL_FORMS' 字符串文字code> 来自 django.forms.formsets,但这似乎不是公开的(至少在 Django 2.2 中)。

  • 另请注意,如果 can_deleteTrue。要测试现有项目的删除,您可以在测试中执行以下操作:

    <前><代码> ...
    post_data = create_formset_post_data(响应)
    post_data['form-0-DELETE'] = True
    # 然后 POST 等
    ...

  • 来自 source,我们可以看到我们的测试数据中不需要包含 MIN_NUM_FORM_COUNTMAX_NUM_FORM_COUNT

    <块引用>

    MIN_NUM_FORM_COUNT 和 MAX_NUM_FORM_COUNT 与管理表单的其余部分一起输出,但这只是为了方便客户端代码。不检查客户端返回的POST值。

There are several very useful answers here, e.g. pymen's and Raffi's, that show how to construct properly formatted payload for a formset post using the test client.

However, all of them still require at least some hand-coding of prefixes, dealing with existing objects, etc., which is not ideal.

As an alternative, we could create the payload for a post() using the response obtained from a get() request:

def create_formset_post_data(response, new_form_data=None):
    if new_form_data is None:
        new_form_data = []
    csrf_token = response.context['csrf_token']
    formset = response.context['formset']
    prefix_template = formset.empty_form.prefix  # default is 'form-__prefix__'
    # extract initial formset data
    management_form_data = formset.management_form.initial
    form_data_list = formset.initial  # this is a list of dict objects
    # add new form data and update management form data
    form_data_list.extend(new_form_data)
    management_form_data['TOTAL_FORMS'] = len(form_data_list)
    # initialize the post data dict...
    post_data = dict(csrf_token=csrf_token)
    # add properly prefixed management form fields
    for key, value in management_form_data.items():
        prefix = prefix_template.replace('__prefix__', '')
        post_data[prefix + key] = value
    # add properly prefixed data form fields
    for index, form_data in enumerate(form_data_list):
        for key, value in form_data.items():
            prefix = prefix_template.replace('__prefix__', f'{index}-')
            post_data[prefix + key] = value
    return post_data

The output (post_data) will also include form fields for any existing objects.

Here's how you might use this in a Django TestCase:

def test_post_formset_data(self):
    url_path = '/my/post/url/'
    user = User.objects.create()
    self.client.force_login(user)
    # first GET the form content
    response = self.client.get(url_path)
    self.assertEqual(HTTPStatus.OK, response.status_code)
    # specify form data for test
    test_data = [
        dict(first_name='someone', email='[email protected]', ...),
        ...
    ]
    # convert test_data to properly formatted dict
    post_data = create_formset_post_data(response, new_form_data=test_data)
    # now POST the data
    response = self.client.post(url_path, data=post_data, follow=True)
    # some assertions here
    ...

Some notes:

  • Instead of using the 'TOTAL_FORMS' string literal, we could import TOTAL_FORM_COUNT from django.forms.formsets, but that does not seem to be public (at least in Django 2.2).

  • Also note that the formset adds a 'DELETE' field to each form if can_delete is True. To test deletion of existing items, you can do something like this in your test:

      ...
      post_data = create_formset_post_data(response)
      post_data['form-0-DELETE'] = True
      # then POST, etc.
      ...
    
  • From the source, we can see that there is no need include MIN_NUM_FORM_COUNT and MAX_NUM_FORM_COUNT in our test data:

    MIN_NUM_FORM_COUNT and MAX_NUM_FORM_COUNT are output with the rest of the management form, but only for the convenience of client-side code. The POST value of them returned from the client is not checked.

一片旧的回忆 2024-08-16 05:49:20

这似乎根本不是一个表单集。表单集在每个 POSTed 值以及 Bartek 提到的 ManagementForm 上总是有某种前缀。如果您发布要测试的视图的代码及其使用的表单/表单集,可能会有所帮助。

This doesn't seem to be a formset at all. Formsets will always have some sort of prefix on every POSTed value, as well as the ManagementForm that Bartek mentions. It might have helped if you posted the code of the view you're trying to test, and the form/formset it uses.

惜醉颜 2024-08-16 05:49:20

我的情况可能是异常值,但某些实例实际上缺少库存“contrib”管理表单/模板中设置的字段,从而导致错误

“管理表单数据丢失或已被篡改”

保存时显示

。问题出在 unicode 方法(SomeModel:[错误的 Unicode 数据])上,我发现该方法正在调查丢失的内联。

我想,吸取的教训是不要使用 MS 字符映射表。我的问题是粗俗分数(1/4、1/2、3/4),但我认为它可能会以多种不同的方式发生。对于特殊字符,从 w3 utf-8 页面复制/粘贴可以修复它。

postscript-utf-8

My case may be an outlier, but some instances were actually missing a field set in the stock "contrib" admin form/template leading to the error

"ManagementForm data is missing or has been tampered with"

when saved.

The issue was with the unicode method (SomeModel: [Bad Unicode data]) which I found investigating the inlines that were missing.

The lesson learned is to not use the MS Character Map, I guess. My issue was with vulgar fractions (¼, ½, ¾), but I'd assume it could occur many different ways. For special characters, copying/pasting from the w3 utf-8 page fixed it.

postscript-utf-8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文