-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
168 lines (144 loc) · 4.53 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pytest
from .models import User
pytestmark = pytest.mark.django_db
def test_formset_get(client):
response = client.get("/bulk_create_form/")
assert response.status_code == 200
assert response.context["formset"].management_form["TOTAL_FORMS"].value() == 1
assert response.context["formset"].management_form["MIN_NUM_FORMS"].value() == 1
def test_formset_post__min_required__1_form(client):
response = client.post(
"/bulk_create_form/",
data={
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 0,
"form-MIN_NUM_FORMS": 1,
"form-MAX_NUM_FORMS": 1000,
"form-0-name": "",
"form-0-email": "",
},
)
assert response.status_code == 200
assert not response.context["formset"].is_valid()
assert response.context["formset"].errors == [
{
"name": ["This field is required."],
"email": ["This field is required."],
}
]
def test_formset_post__min_required__5_forms(client):
response = client.post(
"/bulk_create_form/",
data={
"form-TOTAL_FORMS": 5,
"form-INITIAL_FORMS": 0,
"form-MIN_NUM_FORMS": 1,
"form-MAX_NUM_FORMS": 1000,
"form-0-name": "",
"form-0-email": "",
},
)
assert response.status_code == 200
assert not response.context["formset"].is_valid()
assert response.context["formset"].errors == [
{
"name": ["This field is required."],
"email": ["This field is required."],
},
{},
{},
{},
{},
]
def test_formset_post__min_required__midway__valid(client):
# make sure that a record posted midway down the formset is still valid
response = client.post(
"/bulk_create_form/",
data={
"form-TOTAL_FORMS": 5,
"form-INITIAL_FORMS": 0,
"form-MIN_NUM_FORMS": 1,
"form-MAX_NUM_FORMS": 1000,
"form-0-name": "",
"form-0-email": "",
"form-1-name": "valid",
"form-1-email": "valid@valid.com",
},
)
assert response.status_code == 200
assert list(User.objects.values("name", "email").order_by("name")) == [
{
"name": "valid",
"email": "valid@valid.com",
},
]
def test_formset_post__invalid_email(client):
response = client.post(
"/bulk_create_form/",
data={
"form-TOTAL_FORMS": 2,
"form-INITIAL_FORMS": 0,
"form-MIN_NUM_FORMS": 1,
"form-MAX_NUM_FORMS": 1000,
"form-0-name": "invalid",
"form-0-email": "invalid",
"form-1-name": "valid",
"form-1-email": "valid@valid.com",
},
)
assert response.status_code == 200
assert not response.context["formset"].is_valid()
assert response.context["formset"].errors == [
{
"email": ["Enter a valid email address."],
},
{},
]
def test_formset_post__duplicate_email(client):
User.objects.create(name="duplicate", email="duplicate@duplicate.com")
response = client.post(
"/bulk_create_form/",
data={
"form-TOTAL_FORMS": 2,
"form-INITIAL_FORMS": 0,
"form-MIN_NUM_FORMS": 1,
"form-MAX_NUM_FORMS": 1000,
"form-0-name": "duplicate",
"form-0-email": "duplicate@duplicate.com",
"form-1-name": "valid",
"form-1-email": "valid@valid.com",
},
)
assert response.status_code == 200
assert not response.context["formset"].is_valid()
assert response.context["formset"].errors == [
{
"email": ["User with this Email already exists."],
},
{},
]
def test_formset_post__valid(client):
response = client.post(
"/bulk_create_form/",
data={
"form-TOTAL_FORMS": 2,
"form-INITIAL_FORMS": 0,
"form-MIN_NUM_FORMS": 1,
"form-MAX_NUM_FORMS": 1000,
"form-0-name": "valid_1",
"form-0-email": "valid_1@valid.com",
"form-1-name": "valid_2",
"form-1-email": "valid_2@valid.com",
},
)
assert response.status_code == 200
assert list(User.objects.values("name", "email").order_by("name")) == [
{
"name": "valid_1",
"email": "valid_1@valid.com",
},
{
"name": "valid_2",
"email": "valid_2@valid.com",
},
]