Skip to content

Commit efb448e

Browse files
committed
1.07 Demonstrate Hypothesis usage in Jupyter NB
1 parent f9bb5da commit efb448e

File tree

1 file changed

+330
-0
lines changed

1 file changed

+330
-0
lines changed
Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Hypothesis Demonstration"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import string\n",
17+
"from hypothesis.strategies import text, booleans"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 2,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"text_strategy = text()"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 3,
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"data": {
36+
"text/plain": [
37+
"'\\r$\\x19'"
38+
]
39+
},
40+
"execution_count": 3,
41+
"metadata": {},
42+
"output_type": "execute_result"
43+
}
44+
],
45+
"source": [
46+
"text_strategy.example()"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 4,
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"data": {
56+
"text/plain": [
57+
"'\\U000b1abd\\x14*\\U000d45a5\\x0c'"
58+
]
59+
},
60+
"execution_count": 4,
61+
"metadata": {},
62+
"output_type": "execute_result"
63+
}
64+
],
65+
"source": [
66+
"text_strategy.example()"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 5,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"'𥀧\\U00105eb8+\\x18\\U000f7711'"
78+
]
79+
},
80+
"execution_count": 5,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"text_strategy.example()"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 6,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"text_strategy = text(\n",
96+
" alphabet=(string.ascii_letters+string.digits),\n",
97+
" min_size=1,\n",
98+
" max_size=8,\n",
99+
")"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 7,
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"data": {
109+
"text/plain": [
110+
"'7'"
111+
]
112+
},
113+
"execution_count": 7,
114+
"metadata": {},
115+
"output_type": "execute_result"
116+
}
117+
],
118+
"source": [
119+
"text_strategy.example()"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 8,
125+
"metadata": {},
126+
"outputs": [
127+
{
128+
"data": {
129+
"text/plain": [
130+
"'0'"
131+
]
132+
},
133+
"execution_count": 8,
134+
"metadata": {},
135+
"output_type": "execute_result"
136+
}
137+
],
138+
"source": [
139+
"text_strategy.example()"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 9,
145+
"metadata": {},
146+
"outputs": [],
147+
"source": [
148+
"text_bool_strategy = text() | booleans()"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 10,
154+
"metadata": {},
155+
"outputs": [
156+
{
157+
"data": {
158+
"text/plain": [
159+
"'\\U000b5f50\\U0003108b\\U00038daa\\U00031fed鄯+'"
160+
]
161+
},
162+
"execution_count": 10,
163+
"metadata": {},
164+
"output_type": "execute_result"
165+
}
166+
],
167+
"source": [
168+
"text_bool_strategy.example()"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 12,
174+
"metadata": {
175+
"scrolled": true
176+
},
177+
"outputs": [
178+
{
179+
"data": {
180+
"text/plain": [
181+
"True"
182+
]
183+
},
184+
"execution_count": 12,
185+
"metadata": {},
186+
"output_type": "execute_result"
187+
}
188+
],
189+
"source": [
190+
"text_bool_strategy.example()"
191+
]
192+
},
193+
{
194+
"cell_type": "markdown",
195+
"metadata": {},
196+
"source": [
197+
"# Hypothesis for Django"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": 13,
203+
"metadata": {},
204+
"outputs": [],
205+
"source": [
206+
"from django.db.models import SlugField\n",
207+
"from django_extensions.db.fields import AutoSlugField\n",
208+
"\n",
209+
"from hypothesis.extra.django import (\n",
210+
" from_field,\n",
211+
" from_model,\n",
212+
" register_field_strategy,\n",
213+
")\n",
214+
"from organizer.models import Tag"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": 14,
220+
"metadata": {},
221+
"outputs": [],
222+
"source": [
223+
"# Hypothesis doesn't know anything about AutoSlugField\n",
224+
"# we tell it to use the same strategy as Django's SlugField\n",
225+
"register_field_strategy(\n",
226+
" AutoSlugField, from_field(SlugField())\n",
227+
")"
228+
]
229+
},
230+
{
231+
"cell_type": "code",
232+
"execution_count": 15,
233+
"metadata": {},
234+
"outputs": [],
235+
"source": [
236+
"tag_strategy = from_model(Tag)"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": 16,
242+
"metadata": {},
243+
"outputs": [],
244+
"source": [
245+
"tag = tag_strategy.example()"
246+
]
247+
},
248+
{
249+
"cell_type": "code",
250+
"execution_count": 17,
251+
"metadata": {},
252+
"outputs": [
253+
{
254+
"data": {
255+
"text/plain": [
256+
"organizer.models.Tag"
257+
]
258+
},
259+
"execution_count": 17,
260+
"metadata": {},
261+
"output_type": "execute_result"
262+
}
263+
],
264+
"source": [
265+
"type(tag)"
266+
]
267+
},
268+
{
269+
"cell_type": "code",
270+
"execution_count": 18,
271+
"metadata": {},
272+
"outputs": [
273+
{
274+
"data": {
275+
"text/plain": [
276+
"'𝠗'"
277+
]
278+
},
279+
"execution_count": 18,
280+
"metadata": {},
281+
"output_type": "execute_result"
282+
}
283+
],
284+
"source": [
285+
"tag.name"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": 19,
291+
"metadata": {},
292+
"outputs": [
293+
{
294+
"data": {
295+
"text/plain": [
296+
"'-33'"
297+
]
298+
},
299+
"execution_count": 19,
300+
"metadata": {},
301+
"output_type": "execute_result"
302+
}
303+
],
304+
"source": [
305+
"tag.slug"
306+
]
307+
}
308+
],
309+
"metadata": {
310+
"kernelspec": {
311+
"display_name": "Django Shell-Plus",
312+
"language": "python",
313+
"name": "django_extensions"
314+
},
315+
"language_info": {
316+
"codemirror_mode": {
317+
"name": "ipython",
318+
"version": 3
319+
},
320+
"file_extension": ".py",
321+
"mimetype": "text/x-python",
322+
"name": "python",
323+
"nbconvert_exporter": "python",
324+
"pygments_lexer": "ipython3",
325+
"version": "3.7.4"
326+
}
327+
},
328+
"nbformat": 4,
329+
"nbformat_minor": 2
330+
}

0 commit comments

Comments
 (0)