Skip to content

Commit a8de6f4

Browse files
committed
update test_order_fail.py
1 parent ecb92cf commit a8de6f4

File tree

9 files changed

+608
-403
lines changed

9 files changed

+608
-403
lines changed

notebooks/ExpectingTheUnexpected.ipynb

Lines changed: 52 additions & 54 deletions
Large diffs are not rendered by default.

notebooks/Functions-DecoratorPattern.ipynb

Lines changed: 75 additions & 230 deletions
Large diffs are not rendered by default.

notebooks/OperatorOverloading.ipynb

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"cell_type": "code",
17-
"execution_count": 1,
17+
"execution_count": null,
1818
"id": "b6f773eb",
1919
"metadata": {},
2020
"outputs": [],
@@ -57,18 +57,10 @@
5757
},
5858
{
5959
"cell_type": "code",
60-
"execution_count": 2,
60+
"execution_count": null,
6161
"id": "33a52de0",
6262
"metadata": {},
63-
"outputs": [
64-
{
65-
"name": "stdout",
66-
"output_type": "stream",
67-
"text": [
68-
"12:26:05\n"
69-
]
70-
}
71-
],
63+
"outputs": [],
7264
"source": [
7365
"# now let's use MyTime class and its methods again\n",
7466
"current_time = MyTime(9, 50, 45)\n",
@@ -79,7 +71,7 @@
7971
},
8072
{
8173
"cell_type": "code",
82-
"execution_count": 3,
74+
"execution_count": null,
8375
"id": "727ee39a",
8476
"metadata": {},
8577
"outputs": [],
@@ -124,18 +116,10 @@
124116
},
125117
{
126118
"cell_type": "code",
127-
"execution_count": 4,
119+
"execution_count": null,
128120
"id": "a9c637b1",
129121
"metadata": {},
130-
"outputs": [
131-
{
132-
"name": "stdout",
133-
"output_type": "stream",
134-
"text": [
135-
"12:26:05\n"
136-
]
137-
}
138-
],
122+
"outputs": [],
139123
"source": [
140124
"current_time = MyTime(9, 50, 45)\n",
141125
"bread_time = MyTime(2, 35, 20)\n",
@@ -200,11 +184,13 @@
200184
},
201185
{
202186
"cell_type": "code",
203-
"execution_count": 5,
187+
"execution_count": 8,
204188
"id": "4efd7cf8",
205189
"metadata": {},
206190
"outputs": [],
207191
"source": [
192+
"from typing import Any\n",
193+
"\n",
208194
"class Point:\n",
209195
" \"\"\"\n",
210196
" Point class represents and manipulates x,y coords\n",
@@ -229,18 +215,18 @@
229215
" self.x = xx\n",
230216
" self.y = yy\n",
231217
" \n",
232-
" def __add__(self, other):\n",
218+
" def __add__(self, other: \"Point\"):\n",
233219
" x = self.x + other.x\n",
234220
" y = self.y + other.y\n",
235221
" return Point(x, y)\n",
236222
" \n",
237-
" def __mul__(self, other):\n",
223+
" def __mul__(self, other: \"Point\"):\n",
238224
" \"\"\"\n",
239225
" computes the dot product of two points\n",
240226
" \"\"\"\n",
241227
" return self.x * other.x + self.y * other.y\n",
242228
" \n",
243-
" def __rmul__(self, other):\n",
229+
" def __rmul__(self, other: Any):\n",
244230
" \"\"\"\n",
245231
" if the left operand is a primitive type (int or float) \n",
246232
" and the right operand is a Point, Python invokes __rmul__\n",
@@ -276,7 +262,7 @@
276262
},
277263
{
278264
"cell_type": "code",
279-
"execution_count": 7,
265+
"execution_count": 9,
280266
"id": "0c9a8ac4",
281267
"metadata": {},
282268
"outputs": [
@@ -287,8 +273,8 @@
287273
"traceback": [
288274
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
289275
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
290-
"Cell \u001b[0;32mIn[7], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# not allowed as it calls __mul__\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mp1\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m5\u001b[39;49m)\n",
291-
"Cell \u001b[0;32mIn[5], line 34\u001b[0m, in \u001b[0;36mPoint.__mul__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21m__mul__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other):\n\u001b[1;32m 31\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;124;03m computes the dot product of two points\u001b[39;00m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 34\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mx \u001b[38;5;241m*\u001b[39m \u001b[43mother\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mx\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39my \u001b[38;5;241m*\u001b[39m other\u001b[38;5;241m.\u001b[39my\n",
276+
"Cell \u001b[0;32mIn[9], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# not allowed as it calls __mul__\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mp1\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m5\u001b[39;49m)\n",
277+
"Cell \u001b[0;32mIn[5], line 34\u001b[0m, in \u001b[0;36mPoint.__mul__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21m__mul__\u001b[39m(\u001b[38;5;28mself\u001b[39m, other: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPoint\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 31\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;124;03m computes the dot product of two points\u001b[39;00m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 34\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mx \u001b[38;5;241m*\u001b[39m \u001b[43mother\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mx\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39my \u001b[38;5;241m*\u001b[39m other\u001b[38;5;241m.\u001b[39my\n",
292278
"\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'x'"
293279
]
294280
}
@@ -309,7 +295,7 @@
309295
],
310296
"metadata": {
311297
"kernelspec": {
312-
"display_name": "Python 3 (ipykernel)",
298+
"display_name": "Python 3",
313299
"language": "python",
314300
"name": "python3"
315301
},
@@ -323,7 +309,7 @@
323309
"name": "python",
324310
"nbconvert_exporter": "python",
325311
"pygments_lexer": "ipython3",
326-
"version": "3.10.8"
312+
"version": "3.9.6"
327313
}
328314
},
329315
"nbformat": 4,

notebooks/TestDataGeneration.ipynb

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"@settings(max_examples=100, verbosity=Verbosity.verbose, derandomize=True)\n",
114114
"def test_ints_are_commutative(x, y):\n",
115115
" #print(x, y)\n",
116-
" assert x + y == y + x"
116+
" assert x + y == y - x"
117117
]
118118
},
119119
{
@@ -238,9 +238,10 @@
238238
"source": [
239239
"# can compose types such as list, tuple, etc...\n",
240240
"# list with at most 100 integers with min value of 1\n",
241-
"@given(some.lists(some.integers(min_value=1), min_size=1, max_size=100))\n",
241+
"@given(some.lists(some.integers(min_value=1), min_size=1, max_size=50))\n",
242242
"def test_func1(nums):\n",
243-
" print(nums)"
243+
" print(nums)\n",
244+
" assert len(nums) >= 1 <= 50"
244245
]
245246
},
246247
{
@@ -267,7 +268,7 @@
267268
},
268269
{
269270
"cell_type": "code",
270-
"execution_count": null,
271+
"execution_count": 31,
271272
"id": "365cbafd",
272273
"metadata": {},
273274
"outputs": [],
@@ -278,12 +279,16 @@
278279
"\n",
279280
"def int_sqrt(n: int) -> float:\n",
280281
" # Is this the correct implementation?\n",
282+
"\n",
283+
" if not isinstance(n, int):\n",
284+
" raise AssertionError\n",
285+
" assert 1 <= n <= 100\n",
281286
" return n**0.5"
282287
]
283288
},
284289
{
285290
"cell_type": "code",
286-
"execution_count": null,
291+
"execution_count": 32,
287292
"id": "0a0a7d84",
288293
"metadata": {},
289294
"outputs": [],
@@ -293,24 +298,37 @@
293298
" assert int_sqrt(9) == 3, 'sqrt(9) != 3'\n",
294299
" assert int_sqrt(4) == 2, 'sqrt(4) != 2'\n",
295300
" assert int_sqrt(10) == math.sqrt(10)\n",
296-
" # assert int_sqrt(100) == 10, 'sqrt(100) != 10'\n",
301+
" #assert int_sqrt(100)\n",
297302
" # any problem here...?\n",
303+
" try:\n",
304+
" assert int_sqrt(100)\n",
305+
" assert int_sqrt(\"abc\")\n",
306+
" except AssertionError:\n",
307+
" pass\n",
298308
" print('all tests PASS...')"
299309
]
300310
},
301311
{
302312
"cell_type": "code",
303-
"execution_count": null,
313+
"execution_count": 33,
304314
"id": "9468407f",
305315
"metadata": {},
306-
"outputs": [],
316+
"outputs": [
317+
{
318+
"name": "stdout",
319+
"output_type": "stream",
320+
"text": [
321+
"all tests PASS...\n"
322+
]
323+
}
324+
],
307325
"source": [
308326
"test_int_sqrt()"
309327
]
310328
},
311329
{
312330
"cell_type": "code",
313-
"execution_count": null,
331+
"execution_count": 19,
314332
"id": "445fee60",
315333
"metadata": {},
316334
"outputs": [],
@@ -329,7 +347,28 @@
329347
},
330348
{
331349
"cell_type": "code",
332-
"execution_count": 38,
350+
"execution_count": 20,
351+
"id": "5fc01fbb",
352+
"metadata": {},
353+
"outputs": [
354+
{
355+
"data": {
356+
"text/plain": [
357+
"TestData(int_value=integers(min_value=1, max_value=10))"
358+
]
359+
},
360+
"execution_count": 20,
361+
"metadata": {},
362+
"output_type": "execute_result"
363+
}
364+
],
365+
"source": [
366+
"test_data"
367+
]
368+
},
369+
{
370+
"cell_type": "code",
371+
"execution_count": 21,
333372
"id": "e56044f1",
334373
"metadata": {},
335374
"outputs": [],
@@ -341,7 +380,7 @@
341380
" an_int = data.draw(test_data.int_value)\n",
342381
" root = int_sqrt(an_int)\n",
343382
" # TODO: uncomment to see the test data\n",
344-
" #print(an_int, root) \n",
383+
" print(an_int, root) \n",
345384
"\n",
346385
" assert isinstance(an_int, int)\n",
347386
" assert 1 <= an_int <= 10\n",
@@ -352,24 +391,15 @@
352391
},
353392
{
354393
"cell_type": "code",
355-
"execution_count": 39,
394+
"execution_count": 34,
356395
"id": "136c9737",
357396
"metadata": {},
358397
"outputs": [
359398
{
360399
"name": "stdout",
361400
"output_type": "stream",
362401
"text": [
363-
"all answer correct\n",
364-
"all answer correct\n",
365-
"all answer correct\n",
366-
"all answer correct\n",
367-
"all answer correct\n",
368-
"all answer correct\n",
369-
"all answer correct\n",
370-
"all answer correct\n",
371-
"all answer correct\n",
372-
"all answer correct\n"
402+
"all tests PASS...\n"
373403
]
374404
}
375405
],
@@ -379,7 +409,7 @@
379409
},
380410
{
381411
"cell_type": "code",
382-
"execution_count": null,
412+
"execution_count": 35,
383413
"id": "67a5fbe6",
384414
"metadata": {},
385415
"outputs": [],
@@ -402,7 +432,7 @@
402432
},
403433
{
404434
"cell_type": "code",
405-
"execution_count": 40,
435+
"execution_count": 36,
406436
"id": "b791a591",
407437
"metadata": {},
408438
"outputs": [
@@ -519,13 +549,13 @@
519549
},
520550
{
521551
"cell_type": "code",
522-
"execution_count": null,
552+
"execution_count": 37,
523553
"id": "32708e6d",
524554
"metadata": {},
525555
"outputs": [],
526556
"source": [
527557
"# let's test for larger than 10 values\n",
528-
"@given(some.integers(min_value=11, max_value=100_000))\n",
558+
"@given(some.integers(min_value=11, max_value=1_000_000))\n",
529559
"def test_int_sqrt_larger_positives(n: int):\n",
530560
" # This should throw AssertionError, but does it...?\n",
531561
" try:\n",
@@ -541,22 +571,24 @@
541571
},
542572
{
543573
"cell_type": "code",
544-
"execution_count": 41,
574+
"execution_count": 38,
545575
"id": "22953b5e",
546576
"metadata": {},
547577
"outputs": [
548578
{
549579
"name": "stdout",
550580
"output_type": "stream",
551581
"text": [
582+
"root of 11 is 3.3166247903554\n",
583+
"FAIL\n",
552584
"AssertionError thrown... PASS\n",
553585
"AssertionError thrown... PASS\n",
554586
"AssertionError thrown... PASS\n",
555587
"AssertionError thrown... PASS\n",
556588
"AssertionError thrown... PASS\n",
557589
"AssertionError thrown... PASS\n",
558-
"AssertionError thrown... PASS\n",
559-
"AssertionError thrown... PASS\n",
590+
"root of 12 is 3.4641016151377544\n",
591+
"FAIL\n",
560592
"AssertionError thrown... PASS\n",
561593
"AssertionError thrown... PASS\n",
562594
"AssertionError thrown... PASS\n",
@@ -658,7 +690,7 @@
658690
},
659691
{
660692
"cell_type": "code",
661-
"execution_count": null,
693+
"execution_count": 39,
662694
"id": "d0d7bc54",
663695
"metadata": {},
664696
"outputs": [],
@@ -680,7 +712,7 @@
680712
},
681713
{
682714
"cell_type": "code",
683-
"execution_count": 42,
715+
"execution_count": 40,
684716
"id": "59a2c78f",
685717
"metadata": {},
686718
"outputs": [
@@ -797,7 +829,7 @@
797829
},
798830
{
799831
"cell_type": "code",
800-
"execution_count": null,
832+
"execution_count": 41,
801833
"id": "815f0f2a",
802834
"metadata": {},
803835
"outputs": [],
@@ -819,7 +851,7 @@
819851
},
820852
{
821853
"cell_type": "code",
822-
"execution_count": 43,
854+
"execution_count": 42,
823855
"id": "e3658b87",
824856
"metadata": {},
825857
"outputs": [
@@ -1012,7 +1044,7 @@
10121044
"name": "python",
10131045
"nbconvert_exporter": "python",
10141046
"pygments_lexer": "ipython3",
1015-
"version": "3.12.1"
1047+
"version": "3.9.6"
10161048
}
10171049
},
10181050
"nbformat": 4,

0 commit comments

Comments
 (0)