test_forms.py

 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from django.test import TestCase
from mywebsite.todos.models import List, Todo
from mywebsite.todos.forms import (
    TodoForm,
    EditForm,
    EditTodoForm,
    ListForm,
    EMPTY_ITEM_ERROR,
    DUPLICATE_ITEM_ERROR,
)
from unittest.mock import patch
from mywebsite.users.models import User


class FormTestMixin(object):
    form_class = None
    placeholder_text: str = None

    class EditFormTestMixin(object):
        def setUp(self):
            self.form = self.form_class(data={"text": ""})

    def setUp(self):
        self.form = self.form_class(queryset=[], data={"text": ""})

    def test_form_renders_attributes(self):
        as_p = self.form.as_p()
        self.assertIn(f'placeholder="{self.placeholder_text}"', as_p)
        self.assertIn('class="form-control input-lg"', as_p)

    def test_form_validation_for_blank_items(self):
        self.assertFalse(self.form.is_valid())
        self.assertEqual(self.form.errors["text"], [EMPTY_ITEM_ERROR])


class ListFormTest(FormTestMixin, TestCase):
    form_class = ListForm
    placeholder_text = "Enter a title for list. Press enter to submit."

    def test_form_validation_for_duplicate_lists(self):
        user = User.objects.create()
        text = "a list"
        List.create_new(text, owner=user)
        form = self.form_class(queryset=List.objects.all(), data={"text": text})

        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors["__all__"], [DUPLICATE_ITEM_ERROR])


class TodoFormTest(FormTestMixin, TestCase):
    form_class = TodoForm
    placeholder_text = "Enter a todo"

    def setUp(self):
        self.list_ = List.objects.create()
        self.form = self.form_class(for_list=self.list_)
        self.form_data = self.form_class(for_list=self.list_, data={"text": ""})

    def test_form_validation_for_duplicate_todos(self):
        list_ = List.objects.create()
        text = "a list name"
        Todo.objects.create(text="unrelated text", list=list_, todo_id="anotherid1")
        Todo.objects.create(text=text, list=list_, todo_id="aunqiueid")
        form = self.form_class(for_list=list_, data={"text": text})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors["__all__"], [DUPLICATE_ITEM_ERROR])

    def test_form_validation_for_blank_items(self):
        user = User.objects.create()
        text = "list_name"
        list_ = List.create_new(text, owner=user)

        form = self.form_class(for_list=list_, data={"text": ""})
        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors["text"], [EMPTY_ITEM_ERROR])


class EditFormTest(FormTestMixin.EditFormTestMixin, TestCase):
    form_class = EditForm
    placeholder_text = "Enter a new title"

    def test_edit_form_has_id_attr(self):
        self.assertIn('id="edit-form"', self.form.as_p())


class EditTodoFormTest(FormTestMixin.EditFormTestMixin, TestCase):
    form_class = EditTodoForm
    placeholder_text = "Edit Todo entry"

    def test_edit_form_has_id_attr(self):
        form = self.form_class()
        # care with the quotes
        self.assertIn('id="edit-todo-form"', form.as_p())