-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
139 lines (122 loc) · 4.6 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
import sys
from formstorm import FormTest, FormElement
from formstorm.iterhelpers import every_combo, dict_combo
from .forms import BookForm
from django.test import TestCase
from .models import Author, Genre
from django.db.models import Q
class BookFormTest(FormTest):
form = BookForm
title = FormElement(
good=["Moby Dick"],
bad=[None, "", "A"*101],
is_unique=True
)
subtitle = FormElement(
good=[None, "", "or The Whale"],
bad=["A"*101]
)
author = FormElement(
good=[Q(name="Herman Melville")],
bad=[None, "", -1]
)
is_fiction = FormElement(
good=[True, False, None, "", -1, "A"],
bad=[] # Boolean input is either truthy or falsy, so nothing is bad.
)
pages = FormElement(
good=[0, 10, 100],
bad=[None, "", "A"]
)
genre = FormElement(
good=every_combo([
Q(name="Mystery"),
Q(name="History"),
Q(name="Humor")
]),
bad=[None]
)
additional_values = [
({'title': "A"*100, 'subtitle': "A"*50}, True),
({'title': "A"*50, 'subtitle': "A"*100}, True),
({'title': "A"*100, 'subtitle': "A"*51}, False),
({'title': "A"*51, 'subtitle': "A"*100}, False),
]
class UtilTest(TestCase):
def test_every_combo(self):
x = every_combo([1, 2, 3])
assert list(x) == [(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
def test_dict_combo(self):
x = dict_combo({
"a": ["A", "B", "C"],
"b": [0, 1],
"c": [True, False, None],
})
y = [
{'a': 'A', 'b': 0, 'c': True},
{'a': 'A', 'b': 0, 'c': False},
{'a': 'A', 'b': 0, 'c': None},
{'a': 'A', 'b': 1, 'c': True},
{'a': 'A', 'b': 1, 'c': False},
{'a': 'A', 'b': 1, 'c': None},
{'a': 'B', 'b': 0, 'c': True},
{'a': 'B', 'b': 0, 'c': False},
{'a': 'B', 'b': 0, 'c': None},
{'a': 'B', 'b': 1, 'c': True},
{'a': 'B', 'b': 1, 'c': False},
{'a': 'B', 'b': 1, 'c': None},
{'a': 'C', 'b': 0, 'c': True},
{'a': 'C', 'b': 0, 'c': False},
{'a': 'C', 'b': 0, 'c': None},
{'a': 'C', 'b': 1, 'c': True},
{'a': 'C', 'b': 1, 'c': False},
{'a': 'C', 'b': 1, 'c': None},
]
x = list(x)
# Compensate for non-deterministic dict insertion if using python <3.7.
if sys.version_info[:2] < (3, 7):
x.sort()
y.sort()
assert x == y
def test_dict_combo_with_base_dict(self):
x = dict_combo({
"a": ["A", "B", "C"],
"b": [0, 1],
"c": [True, False, None],
}, {"d": "A", "e": 0, 'f': True})
y = [
{'a': 'A', 'b': 0, 'c': True, "d": "A", "e": 0, 'f': True},
{'a': 'A', 'b': 0, 'c': False, "d": "A", "e": 0, 'f': True},
{'a': 'A', 'b': 0, 'c': None, "d": "A", "e": 0, 'f': True},
{'a': 'A', 'b': 1, 'c': True, "d": "A", "e": 0, 'f': True},
{'a': 'A', 'b': 1, 'c': False, "d": "A", "e": 0, 'f': True},
{'a': 'A', 'b': 1, 'c': None, "d": "A", "e": 0, 'f': True},
{'a': 'B', 'b': 0, 'c': True, "d": "A", "e": 0, 'f': True},
{'a': 'B', 'b': 0, 'c': False, "d": "A", "e": 0, 'f': True},
{'a': 'B', 'b': 0, 'c': None, "d": "A", "e": 0, 'f': True},
{'a': 'B', 'b': 1, 'c': True, "d": "A", "e": 0, 'f': True},
{'a': 'B', 'b': 1, 'c': False, "d": "A", "e": 0, 'f': True},
{'a': 'B', 'b': 1, 'c': None, "d": "A", "e": 0, 'f': True},
{'a': 'C', 'b': 0, 'c': True, "d": "A", "e": 0, 'f': True},
{'a': 'C', 'b': 0, 'c': False, "d": "A", "e": 0, 'f': True},
{'a': 'C', 'b': 0, 'c': None, "d": "A", "e": 0, 'f': True},
{'a': 'C', 'b': 1, 'c': True, "d": "A", "e": 0, 'f': True},
{'a': 'C', 'b': 1, 'c': False, "d": "A", "e": 0, 'f': True},
{'a': 'C', 'b': 1, 'c': None, "d": "A", "e": 0, 'f': True},
]
x = list(x)
# See note above.
if sys.version_info[:2] < (3, 7):
x.sort()
y.sort()
assert x == y
class BookTestCase(TestCase):
def setUp(self):
Genre(name="Mystery").save()
Genre(name="History").save()
Genre(name="Humor").save()
Author(name="Herman Melville").save()
Author(name="Charles Dickens").save()
self.theBookFormTest = BookFormTest()
def test_book_form(self):
self.theBookFormTest.run()