forked from pydanny/django-uni-form
/
tests.py
148 lines (112 loc) · 5.19 KB
/
tests.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from django import forms
from django.template import Context, Template
from django.template.loader import get_template_from_string
from django.test import TestCase
from uni_form.helpers import FormHelper, Submit, Reset, Hidden, Button
class TestForm(forms.Form):
is_company = forms.CharField(label="company", required=False, widget=forms.CheckboxInput())
email = forms.CharField(label="email", max_length=30, required=True, widget=forms.TextInput())
password1 = forms.CharField(label="password", max_length=30, required=True, widget=forms.PasswordInput())
password2 = forms.CharField(label="re-enter password", max_length=30, required=True, widget=forms.PasswordInput())
first_name = forms.CharField(label="first name", max_length=30, required=True, widget=forms.TextInput())
last_name = forms.CharField(label="last name", max_length=30, required=True, widget=forms.TextInput())
class TestBasicFunctionalityTags(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_as_uni_form(self):
# build the context
c = Context({'form':TestForm()})
# Simple form templare
template = get_template_from_string("""
{% load uni_form_tags %}
{{ form|as_uni_form }}
""")
# render the form template
html = template.render(c)
self.assertTrue("<td>" not in html)
self.assertTrue("id_is_company" in html)
def test_uni_form_setup(self):
c = Context()
template = get_template_from_string("""
{% load uni_form_tags %}
{% uni_form_setup %}
""")
html = template.render(c)
# Just look for file names because locations and names can change.
self.assertTrue('uni-form-generic.css' in html)
self.assertTrue('uni-form.css' in html)
self.assertTrue('uni-form.jquery.js' in html)
class TestFormHelpers(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_uni_form_helper_inputs(self):
form_helper = FormHelper()
submit = Submit('my-submit', 'Submit')
reset = Reset('my-reset', 'Reset')
hidden = Hidden('my-hidden', 'Hidden')
button = Button('my-button', 'Button')
form_helper.add_input(submit)
form_helper.add_input(reset)
form_helper.add_input(hidden)
form_helper.add_input(button)
c = Context({'form':TestForm(),'form_helper':form_helper})
template = get_template_from_string("""
{% load uni_form_tags %}
{% uni_form form form_helper %}
""")
html = template.render(c)
'''
self.assertTrue('class="submit submitButton"' in html)
self.assertTrue('id="submit-id-my-submit"' in html)
self.assertTrue('class="reset resetButton"' in html)
self.assertTrue('id="reset-id-my-reset"' in html)
self.assertTrue('name="my-hidden"' in html)
self.assertTrue('class="button"' in html)
self.assertTrue('id="button-id-my-button"' in html)
'''
def test_uni_form_helper_form_attributes(self):
template = get_template_from_string("""
{% load uni_form_tags %}
{% uni_form form form_helper %}
""")
# First we build a standard form helper
form_helper = FormHelper()
form_helper.form_id = 'this-form-rocks'
form_helper.form_class = 'forms-that-rock'
form_helper.form_method = 'GET'
# now we render it
c = Context({'form':TestForm(),'form_helper':form_helper})
html = template.render(c)
# Lets make sure everything loads right
self.assertTrue("""<form""" in html)
self.assertTrue("""class="uniForm forms-that-rock" """ in html)
self.assertTrue("""method="get" """ in html)
self.assertTrue("""id="this-form-rocks">""" in html)
# now lets remove the form tag and render it again. All the True items above
# should now be false because the form tag is removed.
form_helper.form_tag = False
c = Context({'form':TestForm(),'form_helper':form_helper})
html = template.render(c)
self.assertFalse("""<form""" in html)
self.assertFalse("""id="this-form-rocks">""" in html)
self.assertFalse("""class="uniForm forms-that-rock" """ in html)
self.assertFalse("""method="get" """ in html)
self.assertFalse("""id="this-form-rocks">""" in html)
"""
def test_csrf_token(self):
template = get_template_from_string("""
{% load uni_form_tags %}
{% uni_form form form_helper %}
""")
# First we build a standard form helper
form_helper = FormHelper()
# Now we set the CSRF token to bew true
form_helper.use_csrf_protection = True
# Render the text
c = Context({'form':TestForm(),'form_helper':form_helper})
html = template.render(c)
"""