File: test_describe_form.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (69 lines) | stat: -rw-r--r-- 2,247 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
from io import StringIO

from django.test import TestCase
from django.db import models
from django.core.management import CommandError, call_command


class DescribeFormExceptionsTests(TestCase):
    """Tests for describe_form command exceptions."""

    def test_should_raise_CommandError_if_invalid_arg(self):
        with self.assertRaisesRegex(
            CommandError, "Need application and model name in the form: appname.model"
        ):
            call_command("describe_form", "testapp")


class DescribeFormTests(TestCase):
    """Tests for describe_form command."""

    def setUp(self):
        self.out = StringIO()

        class BaseModel(models.Model):
            title = models.CharField(max_length=50)
            body = models.TextField()

            class Meta:
                app_label = "testapp"

        class NonEditableModel(models.Model):
            created_at = models.DateTimeField(auto_now_add=True)
            title = models.CharField(max_length=50)

            class Meta:
                app_label = "testapp"

    def test_should_print_form_definition_for_TestModel(self):
        expected_result = """from django import forms
from testapp.models import BaseModel

class BaseModelForm(forms.Form):
    title = forms.CharField(label='Title', max_length=50)
    body = forms.CharField(label='Body')"""

        call_command("describe_form", "testapp.BaseModel", stdout=self.out)

        self.assertIn(expected_result, self.out.getvalue())

    def test_should_print_form_definition_for_TestModel_with_non_editable_field(self):
        expected_result = """from django import forms
from testapp.models import NonEditableModel

class NonEditableModelForm(forms.Form):
    title = forms.CharField(label='Title', max_length=50)"""

        call_command("describe_form", "testapp.NonEditableModel", stdout=self.out)

        self.assertIn(expected_result, self.out.getvalue())

    def test_should_print_form_with_fields_for_TestModel(self):
        not_expected = """body = forms.CharField(label='Body')"""

        call_command(
            "describe_form", "testapp.BaseModel", "--fields=title", stdout=self.out
        )

        self.assertNotIn(not_expected, self.out.getvalue())