Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing tests for action forms #5

Open
jayqi opened this issue Nov 30, 2024 · 2 comments
Open

Writing tests for action forms #5

jayqi opened this issue Nov 30, 2024 · 2 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@jayqi
Copy link

jayqi commented Nov 30, 2024

When using these action forms, what is the right way to write tests?

I tried to follow some of the examples from this StackOverflow post for testing admin actions generically, and test for a redirect to the admin/django_admin_action_forms/action_form.html template, but I got some weird errors like:

>       action = request.POST.getlist("action")[int(request.POST.get("index"))]
E       TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

Just for experimentation, I hard-coded a {index: 0} into my POST data and got it to pass, but I don't really understand what that would be doing or if I'm actually testing anything correctly.

Then, separately, what would be the right way to test the form submission?

@michalpokusa
Copy link
Owner

michalpokusa commented Dec 1, 2024

Thanks for posting this.

Exception with missing index in POST

So, the index is used when there are multiple AdminForms (Django ones aka django.contrib.admin.helpers.AdminForm) on single change list page, which contain the information about a selected action. But when there are multiple of them, e.g. when one uses actions_on_bottom on a ModelAdmin, the POST data that is being sent might look like this:

image

Then, we can determine which "Go" button was clicked (the index POST value) and by that get the action name which was selected.

That saying, it seems like Django also accepts when the index is not set and uses 0 by default.
https://github.com/django/django/blob/3.2/django/contrib/admin/options.py#L1363-L1366

This looks like an oversight on my part, as the line you provided raises exception if the index is not in payload.

I will look into it and fix that, if that is a bug, which it seems it is.

Testing form submission

Regarding this, the post you linked correctly explains how to test actions, and this stays true when it comes to actions with forms.

The only difference, is that you would have to add:

  • the action_form field to you POST data (explained later)
  • values that would satisfy the ActionForm (from the lib), that is used on your action
data = {
    'action': 'action_name',
    '_selected_action': [1, 2, 3, ...], # e.g.
    'action_form': "",
    'field1': ...,
    'field2': ...
}

Why is action_form field necessary? It is used to determine whether user is displaying the intermediate page for the first time (thus before entering any data to the form), without it, all the fields would show errors on the intermediate page, as technically speaking the form is not valid at that point.
https://github.com/michalpokusa/django-admin-action-forms/blob/1.2.4/django_admin_action_forms/decorators.py#L39-L43

@michalpokusa michalpokusa added bug Something isn't working documentation Improvements or additions to documentation labels Dec 1, 2024
@michalpokusa
Copy link
Owner

Please test the newly released version 1.3.0, it should work fine without the need to explicitly pass the index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants