• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LostInDarkMath / pedantic-python-decorators / 10283603338

07 Aug 2024 11:40AM UTC coverage: 96.385% (+0.9%) from 95.502%
10283603338

push

github

LostInDarkMath
improve test coverage

2063 of 2084 branches covered (98.99%)

Branch coverage included in aggregate %.

42 of 42 new or added lines in 4 files covered. (100.0%)

1 existing line in 1 file now uncovered.

6763 of 7073 relevant lines covered (95.62%)

1.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.6
/pedantic/tests/tests_small_method_decorators.py
1
import asyncio
2✔
2
import unittest
2✔
3
import warnings
2✔
4
from abc import abstractmethod
2✔
5
from unittest import IsolatedAsyncioTestCase
2✔
6

7
from pedantic import overrides, timer, count_calls, trace, trace_if_returns, does_same_as_function, deprecated, \
2✔
8
    unimplemented, mock, require_kwargs
9
from pedantic.exceptions import NotImplementedException, PedanticOverrideException, PedanticCallWithArgsException
2✔
10

11

12
class TestSmallDecoratorMethods(unittest.TestCase):
2✔
13
    def test_overrides_parent_has_no_such_method(self):
2✔
14
        class MyClassA:
2✔
15
            pass
2✔
16

17
        with self.assertRaises(expected_exception=PedanticOverrideException):
2✔
18
            class MyClassB(MyClassA):
2✔
19
                @overrides(MyClassA)
2✔
20
                def operation(self): pass
2✔
21

22
    def test_overrides_all_good(self):
2✔
23
        class MyClassA:
2✔
24
            def operation(self): pass
2✔
25

26
        class MyClassB(MyClassA):
2✔
27
            @overrides(MyClassA)
2✔
28
            def operation(self):
2✔
29
                return 42
2✔
30

31
        b = MyClassB()
2✔
32
        b.operation()
2✔
33

34
    def test_overrides_static_method(self):
2✔
35
        class MyClassA:
2✔
36
            @staticmethod
2✔
37
            def operation(): pass
2✔
38

39
        class MyClassB(MyClassA):
2✔
40
            @staticmethod
2✔
41
            @overrides(MyClassA)
2✔
42
            def operation():
2✔
43
                return 42
2✔
44

45
        b = MyClassB()
2✔
46
        self.assertEqual(b.operation(), 42)
2✔
47
        self.assertEqual(MyClassB.operation(), 42)
2✔
48

49
    def test_overrides_below_property(self):
2✔
50
        class MyClassA:
2✔
51
            @property
2✔
52
            @abstractmethod
2✔
53
            def operation(self): pass
2✔
54

55
        class MyClassB(MyClassA):
2✔
56
            @property
2✔
57
            @overrides(MyClassA)   # Note: it does not work the other way around
2✔
58
            def operation(self):
2✔
59
                return 43
2✔
60

61
        b = MyClassB()
2✔
62
        self.assertEqual(b.operation, 43)
2✔
63

64
    def test_overrides_function(self):
2✔
65
        class MyClassA:
2✔
66
            pass
2✔
67

68
        with self.assertRaises(expected_exception=PedanticOverrideException):
2✔
69
            @overrides(MyClassA)
2✔
70
            def operation(): return 42
2✔
71

72
    def test_deprecated_1(self):
2✔
73
        @deprecated
2✔
74
        def old_method(i: int) -> str: return str(i)
2✔
75

76
        with warnings.catch_warnings(record=True) as w:
2✔
77
            warnings.simplefilter("always")
2✔
78
            old_method(42)
2✔
79
            assert len(w) == 1
2✔
80
            assert issubclass(w[-1].category, DeprecationWarning)
2✔
81
            assert "deprecated" in str(w[-1].message)
2✔
82

83
    def test_deprecated_2(self):
2✔
84
        def old_method(i: int) -> str:
2✔
85
            return str(i)
2✔
86

87
        with warnings.catch_warnings(record=True) as w:
2✔
88
            warnings.simplefilter("always")
2✔
89
            old_method(42)
2✔
90
            assert not len(w) == 1
2✔
91

92
    def test_unimplemented(self):
2✔
93
        @unimplemented
2✔
94
        def dirt(i: int) -> str:
2✔
UNCOV
95
            return str(i)
×
96

97
        with self.assertRaises(expected_exception=NotImplementedException):
2✔
98
            dirt(42)
2✔
99

100
    def test_timer(self):
2✔
101
        @timer
2✔
102
        def operation(i: int) -> str:
2✔
103
            return str(i)
2✔
104

105
        operation(42)
2✔
106

107
    def test_count_calls(self):
2✔
108
        @count_calls
2✔
109
        def operation(i: int) -> str:
2✔
110
            return str(i)
2✔
111

112
        operation(42)
2✔
113

114
    def test_trace(self):
2✔
115
        def some_method(x, y):
2✔
116
            return x + y
2✔
117

118
        traced_method = trace(some_method)
2✔
119
        self.assertEqual(some_method(42, 99), traced_method(42, 99))
2✔
120

121
    def test_trace_if_returns(self):
2✔
122
        def some_method(x, y):
2✔
123
            return x + y
2✔
124
        traced_method = trace_if_returns(100)(some_method)
2✔
125
        self.assertEqual(some_method(42, 99), traced_method(42, 99))
2✔
126
        self.assertEqual(some_method(42, 58), traced_method(42, 58))
2✔
127

128
    def test_does_same_as_function(self):
2✔
129
        def some_method(x, y, z):
2✔
130
            return x * (y + z)
2✔
131

132
        @does_same_as_function(some_method)
2✔
133
        def other_method(x, y, z):
2✔
134
            return x * y + x * z
2✔
135

136
        other_method(1, 2, 3)
2✔
137
        other_method(4, 5, 6)
2✔
138

139
    def test_does_same_as_function_wrong(self):
2✔
140
        def some_method(x, y, z):
2✔
141
            return x * (y + z)
2✔
142

143
        @does_same_as_function(some_method)
2✔
144
        def other_method(x, y, z):
2✔
145
            return x * y + z
2✔
146

147
        other_method(0, 2, 0)
2✔
148
        with self.assertRaises(expected_exception=AssertionError):
2✔
149
            other_method(4, 5, 6)
2✔
150

151

152
class AsyncSmallDecoratorTests(IsolatedAsyncioTestCase):
2✔
153
    async def test_overrides_async_instance_method(self) -> None:
2✔
154
        class MyClassA:
2✔
155
            async def operation(self): pass
2✔
156

157
        class MyClassB(MyClassA):
2✔
158
            @overrides(MyClassA)
2✔
159
            async def operation(self):
2✔
160
                await asyncio.sleep(0)
2✔
161
                return 42
2✔
162

163
        b = MyClassB()
2✔
164
        await b.operation()
2✔
165

166
    async def test_overrides_parent_has_no_such_method_async(self):
2✔
167
        class MyClassA:
2✔
168
            pass
2✔
169

170
        with self.assertRaises(expected_exception=PedanticOverrideException):
2✔
171
            class MyClassB(MyClassA):
2✔
172
                @overrides(MyClassA)
2✔
173
                async def operation(self): return 42
2✔
174

175
    async def test_count_calls_async(self):
2✔
176
        @count_calls
2✔
177
        async def operation(i: int) -> str:
2✔
178
            await asyncio.sleep(0)
2✔
179
            return str(i)
2✔
180

181
        res = await operation(42)
2✔
182
        self.assertEqual('42', res)
2✔
183

184
    async def test_trace_async(self):
2✔
185
        async def some_method(x, y):
2✔
186
            await asyncio.sleep(0)
2✔
187
            return x + y
2✔
188

189
        traced_method = trace(some_method)
2✔
190
        self.assertEqual(await some_method(42, 99), await traced_method(42, 99))
2✔
191

192
    async def test_trace_if_returns_async(self):
2✔
193
        async def some_method(x, y):
2✔
194
            await asyncio.sleep(0)
2✔
195
            return x + y
2✔
196

197
        traced_method = trace_if_returns(100)(some_method)
2✔
198
        self.assertEqual(await some_method(42, 99), await traced_method(42, 99))
2✔
199
        self.assertEqual(await some_method(42, 58), await traced_method(42, 58))
2✔
200

201
    async def test_timer_async(self):
2✔
202
        @timer
2✔
203
        async def operation(i: int) -> str:
2✔
204
            await asyncio.sleep(0.05)
2✔
205
            return str(i)
2✔
206

207
        await operation(42)
2✔
208

209
    async def test_deprecated_async(self):
2✔
210
        @deprecated
2✔
211
        async def old_method(i: int) -> str:
2✔
212
            return str(i)
2✔
213

214
        with warnings.catch_warnings(record=True) as w:
2✔
215
            warnings.simplefilter("always")
2✔
216
            await old_method(42)
2✔
217
            assert len(w) == 1
2✔
218
            assert issubclass(w[-1].category, DeprecationWarning)
2✔
219
            assert "deprecated" in str(w[-1].message)
2✔
220

221
    async def test_does_same_as_function_async(self):
2✔
222
        async def some_method(x, y, z):
2✔
223
            await asyncio.sleep(0)
2✔
224
            return x * (y + z)
2✔
225

226
        @does_same_as_function(some_method)
2✔
227
        async def other_method(x, y, z):
2✔
228
            await asyncio.sleep(0)
2✔
229
            return x * y + x * z
2✔
230

231
        await other_method(1, 2, 3)
2✔
232
        await other_method(4, 5, 6)
2✔
233

234
    async def test_does_same_as_function_async_and_sync(self):
2✔
235
        def some_method(x, y, z):
2✔
236
            return x * (y + z)
2✔
237

238
        @does_same_as_function(some_method)
2✔
239
        async def other_method(x, y, z):
2✔
240
            await asyncio.sleep(0)
2✔
241
            return x * y + x * z
2✔
242

243
        await other_method(1, 2, 3)
2✔
244
        await other_method(4, 5, 6)
2✔
245

246
    async def test_does_same_as_function_wrong(self):
2✔
247
        async def some_method(x, y, z):
2✔
248
            await asyncio.sleep(0)
2✔
249
            return x * (y + z)
2✔
250

251
        @does_same_as_function(some_method)
2✔
252
        async def other_method(x, y, z):
2✔
253
            await asyncio.sleep(0)
2✔
254
            return x * y + z
2✔
255

256
        await other_method(0, 2, 0)
2✔
257

258
        with self.assertRaises(expected_exception=AssertionError):
2✔
259
            await other_method(4, 5, 6)
2✔
260

261
    async def test_mock_async(self) -> None:
2✔
262
        @mock(return_value=42)
2✔
263
        async def my_function(a, b, c): return a + b + c
2✔
264

265
        assert await my_function(1, 2, 3) == 42
2✔
266
        assert await my_function(100, 200, 300) == 42
2✔
267

268
    async def test_require_kwargs(self):
2✔
269
        @require_kwargs
2✔
270
        async def calc(n: int, m: int, i: int) -> int:
2✔
271
            return n + m + i
2✔
272

273
        await calc(n=1, m=2, i=3)
2✔
274

275
        with self.assertRaises(expected_exception=PedanticCallWithArgsException):
2✔
276
            await calc(1, m=2, i=3)
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc