|
1 | 1 | import unittest
|
| 2 | +from typing import List |
2 | 3 |
|
3 |
| -from exception_with_retry.source import ExceptionWithRetry |
| 4 | +from exception_with_retry.source import ExceptionWithRetry, exception_with_retry |
4 | 5 |
|
5 | 6 | __author__ = "Doru Irimescu"
|
6 | 7 | __copyright__ = "Doru Irimescu"
|
@@ -31,39 +32,88 @@ def method_caller(arg):
|
31 | 32 | method(arg)
|
32 | 33 |
|
33 | 34 |
|
| 35 | +def wrapper_test_method(n: int): |
| 36 | + if n < 0: |
| 37 | + raise Exception("Not gonna happen") |
| 38 | + else: |
| 39 | + return 15 |
| 40 | + |
| 41 | + |
34 | 42 | class TestExceptionWithRetry(unittest.TestCase):
|
35 | 43 | def test_1(self):
|
36 |
| - ewr = ExceptionWithRetry(method, 5, 0.0) |
37 |
| - arg = [0] |
38 |
| - ewr.run([arg]) |
39 |
| - self.assertEqual(arg, [0, 1, 2, 3, 4, 5]) |
| 44 | + with self.assertRaises(Exception): |
| 45 | + ewr = ExceptionWithRetry(method, 5, 0.0) |
| 46 | + arg = [0] |
| 47 | + ewr.run(arg) |
| 48 | + self.assertEqual(arg, [0, 1, 2, 3, 4, 5]) |
40 | 49 |
|
41 | 50 | def test_2(self):
|
42 |
| - ewr = ExceptionWithRetry(method, 0, 0.0) |
43 |
| - arg = [0] |
44 |
| - ewr.run([arg]) |
45 |
| - self.assertEqual(arg, [0]) |
| 51 | + with self.assertRaises(Exception): |
| 52 | + ewr = ExceptionWithRetry(method, 0, 0.0) |
| 53 | + arg = [0] |
| 54 | + ewr.run(arg) |
| 55 | + self.assertEqual(arg, [0]) |
46 | 56 |
|
47 | 57 | def test_3(self):
|
48 |
| - ewr = ExceptionWithRetry(method2, 2, 0.0) |
49 |
| - arg = [0] |
50 |
| - ewr.run([arg, 1]) |
51 |
| - self.assertEqual(arg, [0, 1, 2]) |
| 58 | + with self.assertRaises(Exception): |
| 59 | + ewr = ExceptionWithRetry(method2, 2, 0.0) |
| 60 | + arg = [0] |
| 61 | + ewr.run(arg, 1) |
| 62 | + self.assertEqual(arg, [0, 1, 2]) |
52 | 63 |
|
53 | 64 | def test_4(self):
|
54 |
| - ewr = ExceptionWithRetry(method_caller, 2, 0.0) |
55 |
| - arg = [1, 2] |
56 |
| - ewr.run([arg]) |
57 |
| - self.assertEqual(arg, [1, 2, 3, 4]) |
| 65 | + with self.assertRaises(Exception): |
| 66 | + ewr = ExceptionWithRetry(method_caller, 2, 0.0) |
| 67 | + arg = [1, 2] |
| 68 | + ewr.run(arg) |
| 69 | + self.assertEqual(arg, [1, 2, 3, 4]) |
58 | 70 |
|
59 | 71 | def test_5(self):
|
60 |
| - t = TestMethods() |
61 |
| - ewr = ExceptionWithRetry(t.method, 5, 0.0) |
62 |
| - arg = [0] |
63 |
| - ewr.run([arg]) |
64 |
| - self.assertEqual(arg, [0, 20, 40, 60, 80, 100]) |
| 72 | + with self.assertRaises(Exception): |
| 73 | + t = TestMethods() |
| 74 | + ewr = ExceptionWithRetry(t.method, 5, 0.0) |
| 75 | + arg = [0] |
| 76 | + ewr.run(arg) |
| 77 | + self.assertEqual(arg, [0, 20, 40, 60, 80, 100]) |
65 | 78 |
|
66 | 79 | def test_6(self):
|
67 | 80 | ewr = ExceptionWithRetry(method3, 5, 0.0)
|
68 |
| - result = ewr.run([]) |
| 81 | + result = ewr.run() |
69 | 82 | self.assertEqual(result, 25)
|
| 83 | + |
| 84 | + |
| 85 | +@exception_with_retry(3, 0.1) |
| 86 | +def wrapped_method(number_1: int, number_2: int = 0, calls: List = []): |
| 87 | + calls.append(1) |
| 88 | + if number_1 < 0: |
| 89 | + raise Exception("Not gonna happen") |
| 90 | + else: |
| 91 | + return number_1 + number_2 |
| 92 | + |
| 93 | + |
| 94 | +class TestExceptionWithRetryDecorator(unittest.TestCase): |
| 95 | + def test_no_exception(self): |
| 96 | + result = wrapped_method(number_1=1) |
| 97 | + self.assertEqual(1, result) |
| 98 | + |
| 99 | + calls = [] |
| 100 | + result = wrapped_method(number_1=1, number_2=10, calls=calls) |
| 101 | + self.assertEqual(11, result) |
| 102 | + self.assertEqual(1, len(calls)) |
| 103 | + |
| 104 | + def test_throws_exception(self): |
| 105 | + with self.assertRaises(Exception): |
| 106 | + calls = [] |
| 107 | + result = wrapped_method(-1, 0, calls) |
| 108 | + self.assertEqual(15, result) |
| 109 | + self.assertEqual(3, len(calls)) |
| 110 | + |
| 111 | + with self.assertRaises(Exception): |
| 112 | + calls = [] |
| 113 | + wrapped_method(number_1=-1, number_2=2) |
| 114 | + self.assertEqual(3, len(calls)) |
| 115 | + |
| 116 | + with self.assertRaises(Exception): |
| 117 | + calls = [] |
| 118 | + wrapped_method(-1, 2) |
| 119 | + self.assertEqual(3, len(calls)) |
0 commit comments