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

Support for form fields #83

Closed
jayvdb opened this issue Jun 20, 2020 · 1 comment
Closed

Support for form fields #83

jayvdb opened this issue Jun 20, 2020 · 1 comment

Comments

@jayvdb
Copy link
Contributor

jayvdb commented Jun 20, 2020

Currently model-bakery seems to mostly support db fields.

Would it be ok to also add support for form fields, specifically ChoiceField?

The use case is explained at alsur/django-admin-auto-tests#6, which is using model bakery (c.f. alsur/django-admin-auto-tests#4) to populate admin forms for smoke tests.

@berinhard
Copy link
Member

berinhard commented Jun 23, 2020

Hi @jayvdb, thanks for opening the issue.

Unfortunately the model bakery's main goal is to work as an easier way to populate your database with test data than Django's fixtures. That's why we don't support form fields at all.

In my personal understanding about testing on Django, you shouldn't use tools to generate data to test your forms, because the main goal of forms are data cleaning and validation. So, if you use dynamic data, you can mislead your test code to follow the form code changes and this can introduce bugs in your system if you don't have an integration test.

The way I'm used to test forms is by defining an initial set of valid data for that form via setUp or pytest fixtures. By doing that, the unit tests can only modify part of the data and the tests are more direct and easier to read. Here's a example:

class UserInfoFormTests(TestCase):

    def setUp(self):
        self.data = {'age': 18, 'first_name': 'Charles', 'last_name': 'McDonald'}

    def test_validate_data(self):
        form = UserInfoForm(data=self.data)
        assert form.is_valid()

    def test_first_name_can_not_have_spaces(self):
        self.data['first_name'] = 'Charles JR'
        form = UserInfoForm(data=self.data)
        assert not form.is_valid()
        assert 'first_name' in form.errors

I hope this was useful for you somehow, but I don't think that generating dynamic values for form field is one of the project's purposes =/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants