generated from ntoll/codespaces-project-template-pyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
umock.py
702 lines (607 loc) · 25.4 KB
/
umock.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
"""
uMock (MicroMock)
A very simple, small and limited module for mocking objects in MicroPython with
PyScript.
Copyright (c) 2024 Nicholas H.Tollervey
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import sys
import inspect
__all__ = ["Mock", "AsyncMock", "patch"]
#: A flag to show if MicroPython is the current Python interpreter.
is_micropython = "micropython" in sys.version.lower()
#: Attributes of the Mock/AsyncMock classes that should be handled as "normal"
#: attributes rather than treated as mocked attributes.
_RESERVED_ATTRIBUTES = ("side_effect", "return_value")
_RESERVED_MOCK_ATTRIBUTES = _RESERVED_ATTRIBUTES + (
"call_count",
"called",
"call_args",
"call_args_list",
)
_RESERVED_ASYNCMOCK_ATTRIBUTES = _RESERVED_ATTRIBUTES + (
"await_count",
"awaited",
"await_args",
"await_args_list",
)
def is_awaitable(obj):
"""
Returns a boolean indication if the passed in obj is an awaitable
function. (MicroPython treats awaitables as generator functions, and if
the object is a closure containing an async function we need to tread
carefully.)
"""
if is_micropython:
# MicroPython doesn't appear to have a way to determine if a closure is
# an async function except via the repr. This is a bit hacky.
if "<closure <generator>" in repr(obj):
return True
return inspect.isgeneratorfunction(obj)
return inspect.iscoroutinefunction(obj)
def import_module(module_path):
"""
Import the referenced module in a way that works with both MicroPython and
Pyodide. The module_path should be a dotted string representing the module
to import.
"""
if is_micropython:
file_path = module_path.replace(".", "/")
module = __import__(file_path)
return module
else:
module_name = module_path.split(".")[-1]
module = __import__(
module_path,
fromlist=[
module_name,
],
)
return module
class Mock:
"""
A simple class for creating mock objects. Inspired by (but not the same as)
Python's own unittest.mock.Mock class.
Differences between this Mock class and Python's unittest.mock.Mock class:
* Instantiation of the object only allows use of the spec, side_effect and
return_value keyword arguments (no name, spec_set, wraps or unsafe
arguments). However, arbitrary keyword arguments can be passed to be set
as attributes on the mock object.
* Calls are recorded in a list of tuples in the form (args, kwargs) rather
than a list of special Call objects.
* Mock objects do NOT record nor reveal call information relating to thier
child mock objects (i.e. calls are not propagated to parent mocks).
* None of the following methods exist in this implementation:
mock_add_spec, attach_mock, configure_mock, _get_child_mock,
method_calls, mock_calls.
"""
def __init__(
self,
spec=None,
side_effect=None,
return_value=None,
**kwargs,
):
"""
Create a new Mock object. Mock takes several optional arguments that
specify the behaviour of the Mock object:
* spec: This can be either a list of strings or an existing object (a
class or instance) that acts as the specification for the mock
object. If you pass in an object then a list of strings is formed by
calling dir on the object (excluding unsupported magic attributes and
methods). Accessing any attribute not in this list will raise an
AttributeError.
If spec is an object (rather than a list of strings) then __class__
returns the class of the spec object. This allows mocks to pass
isinstance() tests.
* side_effect: A function to be called whenever the Mock is called.
Useful for raising exceptions or dynamically changing return values.
The function is called with the same arguments as the mock, and the
return value of this function is used as the mock's return value.
Alternatively side_effect can be an exception class or instance. In
this case the exception will be raised when the mock is called.
If side_effect is an iterable then each call to the mock will return
the next value from the iterable.
A side_effect can be cleared by setting it to None.
* return_value: The value returned when the mock is called. By default
this is a new Mock (created on first access).
"""
if spec:
if isinstance(spec, list):
self._spec = spec
else:
self._spec = [
name
for name in dir(spec)
if not name.startswith("_")
and not (
callable(getattr(spec, name)) # no methods
or is_awaitable(getattr(spec, name)) # no awaitables
)
]
for name in self._spec:
# Create a new mock object for each attribute in the spec.
setattr(self, name, Mock())
self.return_value = return_value
if side_effect:
if type(side_effect) in (str, list, tuple, set, dict):
# If side_effect is an iterable then make it an iterator.
self.side_effect = iter(side_effect)
else:
self.side_effect = side_effect
# The return_value is used to ensure the same result is always returned
# when calling the mock object and if no side_effect is specified.
self.return_value = return_value
self.reset_mock()
for key, value in kwargs.items():
setattr(self, key, value)
def reset_mock(self):
"""
Reset the mock object (self._calls) to a clean state.
This is useful for when you want to reuse a mock object.
The self._calls is a list of tuples, each of which represents a
call to the mock object. Each tuple is in the form: (args, kwargs)
"""
self._calls = []
@property
def call_count(self):
"""
Return the number of calls made to the mock object.
"""
return len(self._calls)
@property
def called(self):
"""
Return True if the mock object was called at least once.
"""
return self.call_count > 0
@property
def call_args(self):
"""
Return the arguments of the last call to the mock object.
"""
if self.call_count:
return self._calls[-1]
@property
def call_args_list(self):
"""
Return a list of the arguments of each call to the mock object.
"""
return self._calls
def assert_called(self):
"""
Assert that the mock object was called at least once.
"""
assert self.call_count > 0, "Expected at least one call."
def assert_called_once(self):
"""
Assert that the mock object was called once.
"""
assert self.call_count == 1, f"Expected 1 call, got {self.call_count}."
def assert_called_with(self, *args, **kwargs):
"""
Assert that the mock object was last called with the specified
arguments.
"""
self.assert_called()
assert (args, kwargs) == self._calls[
-1
], f"Expected {args} and {kwargs}, got {self._calls[-1]}."
def assert_called_once_with(self, *args, **kwargs):
"""
Assert that the mock object was called once with the given arguments.
"""
self.assert_called_once()
self.assert_called_with(*args, **kwargs)
def assert_any_call(self, *args, **kwargs):
"""
Assert that the mock object was called at least once with the given
arguments.
"""
assert (
args,
kwargs,
) in self._calls, (
f"Expected at least one call with {args} and {kwargs}."
)
def assert_has_calls(self, calls, any_order=False):
"""
Assert the mock has been called with the specified calls.
If any_order is false then the calls must be sequential.
If any_order is true then the calls can be in any order, but they must
all appear in call_args_list.
"""
assert len(calls) <= len(
self._calls
), f"Expected {len(calls)} call[s], got {len(self._calls)}."
if any_order:
for call in calls:
assert (
call in self._calls
), f"Expected {call} in {self._calls}."
else:
for i, call in enumerate(calls):
assert (
self._calls[i] == call
), f"Expected {call} at index {i}, got {self._calls[i]}."
def assert_never_called(self):
"""
Assert that the mock object was never called.
"""
assert (
self.call_count == 0
), f"Expected no calls, got {self.call_count}."
def __call__(self, *args, **kwargs):
"""
Record the call and return the specified result. Calls to the mock
object are recorded in the self._calls list. Each call is a tuple in
the form: (args, kwargs).
In order of precedence, the return value is determined as follows:
If a side_effect is specified then that is used to determine the
return value. If a return_value is specified then that is used. If
neither are specified then the same Mock object is returned each time.
"""
self._calls.append((args, kwargs))
if "side_effect" in self.__dict__:
if type(self.side_effect) is type and issubclass(
self.side_effect, BaseException
):
raise self.side_effect()
elif isinstance(self.side_effect, BaseException):
raise self.side_effect
elif hasattr(self.side_effect, "__next__"):
return next(self.side_effect)
elif callable(self.side_effect):
return self.side_effect(*args, **kwargs)
raise TypeError("The mock object has an invalid side_effect.")
# Return the return_value or a mock object (ensuring it's the same one
# each time).
if self.return_value is None:
self.return_value = Mock()
return self.return_value
def __getattr__(self, name):
"""
Retrieve the value of the attribute `name` if it exists in the
instance's dictionary. If the attribute does not exist, a new `Mock`
object is created, assigned to the attribute, and returned.
Raises an AttributeError if the attribute does not exist and the
instance has a `_spec` attribute that does not contain the attribute
name.
"""
if name.startswith("_") or name in _RESERVED_MOCK_ATTRIBUTES:
# Special attributes are handled as normal attributes.
return self.__dict__.get(name)
elif name in self.__dict__:
# Existing attributes are returned as is.
return self.__dict__[name]
elif "_spec" in self.__dict__ and name not in self._spec:
# If the attribute is not in the spec then raise an AttributeError.
raise AttributeError(f"Mock object has no attribute '{name}'.")
else:
# Otherwise, create a new mock object for the attribute.
new_mock = Mock()
setattr(self, name, new_mock)
return new_mock
class AsyncMock:
"""
A simple class for creating async (awaitable) mock objects. Inspired by
(but not the same as) Python's own unittest.mock.AsyncMock class.
Differences between this Mock class and Python's unittest.mock.AsyncMock
class:
* Instantiation of the object only allows use of the spec, side_effect and
return_value keyword arguments (no name, spec_set, wraps or unsafe
arguments). However, arbitrary keyword arguments can be passed to be set
as attributes on the mock object.
* Awaits are recorded in a list of tuples in the form (args, kwargs) rather
than a list of special awaited Call objects.
* Mock objects do NOT record nor reveal await related information relating
to thier child mock objects (i.e. awaits are not propagated to parent
mocks).
* None of the following methods exist in this implementation:
mock_add_spec, attach_mock, configure_mock, _get_child_mock,
method_calls, mock_calls.
"""
def __init__(
self,
spec=None,
side_effect=None,
return_value=None,
**kwargs,
):
"""
Create a new AsyncMock object. It takes several optional arguments that
specify the behaviour of the asynchronous Mock object:
* spec: This can be either a list of strings or an existing object (a
class or instance) that acts as the specification for the mock
object. If you pass in an object then a list of strings is formed by
calling dir on the object (excluding unsupported magic attributes and
methods). Accessing any attribute not in this list will raise an
AttributeError.
If spec is an object (rather than a list of strings) then __class__
returns the class of the spec object. This allows mocks to pass
isinstance() tests.
* side_effect: A function to be called whenever the Mock is awaited.
Useful for raising exceptions or dynamically changing return values.
The function is called with the same arguments as the mock, and the
return value of this function is used as the awaited mock's return
value.
Alternatively side_effect can be an exception class or instance. In
this case the exception will be raised when the mock is awaited.
If side_effect is an iterable then each await to the mock will yield
the next value from the iterable.
A side_effect can be cleared by setting it to None.
* return_value: The value returned when the mock is awaited. By default
this is a new AsyncMock (created on first access).
"""
if spec:
if isinstance(spec, list):
self._spec = spec
else:
self._spec = [
name
for name in dir(spec)
if not name.startswith("_")
and not (
callable(getattr(spec, name)) # no methods
or is_awaitable(getattr(spec, name)) # no awaitables
)
]
for name in self._spec:
# Create a new mock object for each attribute in the spec.
setattr(self, name, Mock())
if side_effect:
if type(side_effect) in (str, list, tuple, set, dict):
# If side_effect is an iterable then make it an iterator.
self.side_effect = iter(side_effect)
else:
self.side_effect = side_effect
# The return_value is used to ensure the same result is always returned
# when calling the mock object and if no side_effect is specified.
self.return_value = return_value
self.reset_mock()
for key, value in kwargs.items():
setattr(self, key, value)
def reset_mock(self):
"""
Reset the mock object (self._awaits) to a clean state.
This is useful for when you want to reuse a mock object.
The self._awaits is a list of tuples, each of which represents an
await to the mock object. Each tuple is in the form: (args, kwargs)
"""
self._awaits = []
@property
def await_count(self):
"""
Return the number of times the mock object has been awaited.
"""
return len(self._awaits)
@property
def awaited(self):
"""
Return True if the mock object was awaited at least once.
"""
return self.await_count > 0
@property
def await_args(self):
"""
Return the arguments of the last await on the mock object.
"""
if self.await_count:
return self._awaits[-1]
@property
def await_args_list(self):
"""
Return a list of the arguments of each await on the mock object.
"""
return self._awaits
def assert_awaited(self):
"""
Assert that the mock object was awaited at least once.
"""
assert self.await_count > 0, "Expected at least one await."
def assert_awaited_once(self):
"""
Assert that the mock object was awaited once.
"""
assert (
self.await_count == 1
), f"Expected 1 await, got {self.await_count}."
def assert_awaited_with(self, *args, **kwargs):
"""
Assert that the mock object was last awaited with the specified
arguments.
"""
self.assert_awaited()
assert (args, kwargs) == self._awaits[
-1
], f"Expected {args} and {kwargs}, got {self._awaits[-1]}."
def assert_awaited_once_with(self, *args, **kwargs):
"""
Assert that the mock object was awaited once with the given arguments.
"""
self.assert_awaited_once()
self.assert_awaited_with(*args, **kwargs)
def assert_any_await(self, *args, **kwargs):
"""
Assert that the mock object was awaited at least once with the given
arguments.
"""
assert (
args,
kwargs,
) in self._awaits, (
f"Expected at least one await with {args} and {kwargs}."
)
def assert_has_awaits(self, awaits, any_order=False):
"""
Assert the mock has been awaited with the specified awaits.
If any_order is false then the awaits must be sequential.
If any_order is true then the awaits can be in any order, but they must
all appear in await_args_list.
"""
assert len(awaits) <= len(
self._awaits
), f"Expected {len(awaits)} await[s], got {len(self._awaits)}."
if any_order:
for awaited in awaits:
assert (
awaited in self._awaits
), f"Expected {awaited} in {self._awaits}."
else:
for i, awaited in enumerate(awaits):
assert (
self._awaits[i] == awaited
), f"Expected {awaited} at index {i}, got {self._awaits[i]}."
def assert_not_awaited(self):
"""
Assert that the mock object was never awaited.
"""
assert (
self.await_count == 0
), f"Expected no awaits, got {self.await_count}."
async def __call__(self, *args, **kwargs):
"""
Record the await and return the specified result. Awaits on the mock
object are recorded in the self._awaits list. Each await is a tuple in
the form: (args, kwargs).
In order of precedence, the return value is determined as follows:
If a side_effect is specified then that is used to determine the
return value. If a return_value is specified then that is used. If
neither are specified then the same AsyncMock object is returned each
time.
"""
self._awaits.append((args, kwargs))
if "side_effect" in self.__dict__:
if type(self.side_effect) is type and issubclass(
self.side_effect, BaseException
):
raise self.side_effect()
elif isinstance(self.side_effect, BaseException):
raise self.side_effect
elif hasattr(self.side_effect, "__next__"):
return next(self.side_effect)
elif callable(self.side_effect):
return self.side_effect(*args, **kwargs)
raise TypeError("The mock object has an invalid side_effect.")
# Return the return_value or a mock object (ensuring it's the same one
# each time).
if self.return_value is None:
self.return_value = AsyncMock()
return self.return_value
def __getattr__(self, name):
"""
Retrieve the value of the attribute `name` if it exists in the
instance's dictionary. If the attribute does not exist, a new
`AsyncMock` object is created, assigned to the attribute, and returned.
Raises an AttributeError if the attribute does not exist and the
instance has a `_spec` attribute that does not contain the attribute
name.
"""
if name.startswith("_") or name in _RESERVED_ASYNCMOCK_ATTRIBUTES:
# Special attributes are handled as normal attributes.
return self.__dict__.get(name)
elif name in self.__dict__:
# Existing attributes are returned as is.
return self.__dict__[name]
elif "_spec" in self.__dict__ and name not in self._spec:
# If the attribute is not in the spec then raise an AttributeError.
raise AttributeError(
f"AsyncMock object has no attribute '{name}'."
)
else:
# Otherwise, create a new async mock object for the attribute.
new_mock = AsyncMock()
setattr(self, name, new_mock)
return new_mock
class patch:
"""
patch() acts as a function decorator or a context manager. Inside the body
of the function or with statement, the target is patched with a new object.
When the function/with statement exits the patch is undone.
"""
def __init__(self, target, new=None, **kwargs):
"""
Create a new patch object that will replace the target with new.
The target should be in the form (note the colon ":"):
"module.submodule:object_name.method_name"
If no new object is provided, a new Mock object is created with the
supplied kwargs.
"""
self.target = target
self.new = new
self.kwargs = kwargs
def __call__(self, func, *args, **kwargs):
"""
Decorate a function with the patch class, and calling the wrapped
function with the resulting mock object.
"""
def wrapper(*args, **kwargs):
with patch(self.target, self.new, **self.kwargs) as new:
# Ensure the resulting mock object is passed to the function.
args = args + (new,)
return func(*args, **kwargs)
return wrapper
def __enter__(self):
"""
Replace the target attribute with self.new.
If self.new is None, a new Mock object is created with the supplied
kwargs and bound to self.new before the target is replaced.
"""
if self.new is None:
self.new = Mock(**self.kwargs)
self._old = patch_target(self.target, self.new)
return self.new
def __exit__(self, exc_type, exc_value, traceback):
"""
Restore the target attribute to its original state.
"""
patch_target(self.target, self._old)
return False
def patch_target(target, replacement):
"""
Patch the referenced target path, with the given replacement. Return
the original object that was replaced.
The target should be in the form (note the colon ":"):
"module.submodule:object_name.method_name"
"""
if ":" not in target:
# No colon in the target, so assume the target is just a module.
if target in sys.modules:
old_module = sys.modules[target]
else:
old_module = import_module(target)
sys.modules[target] = replacement
return old_module
# There IS a colon in the target, so split the target into module and
# attribute parts.
module_name, attributes = target.split(":")
# The target_name is the attribute we eventually want to replace.
target_name = attributes.rsplit(".", 1)[-1]
# Get the parent module of the target.
parent = sys.modules.get(module_name)
if not parent:
parent = import_module(module_name)
# Traverse the module path to get the parent object of the target.
parts = attributes.split(".")
for part in parts[:-1]:
parent = getattr(parent, part)
# Get the original target object that we're going to replace (so we can
# return it).
old_object = getattr(parent, target_name)
# Replace the target with the replacement.
setattr(parent, target_name, replacement)
return old_object