Skip to content

Commit 59b816d

Browse files
authored
Refactor motor tests to use a spy instead of a custom mock motor (#1067)
1 parent a73825c commit 59b816d

File tree

5 files changed

+226
-359
lines changed

5 files changed

+226
-359
lines changed

doc/dependency_decisions.yml

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
---
22
- - :permit
33
- Apache 2.0
4-
- :who:
5-
:why:
4+
- :who:
5+
:why:
66
:versions: []
77
:when: 2022-08-12 14:31:48.226163463 Z
88
- - :permit
99
- MIT
10-
- :who:
11-
:why:
10+
- :who:
11+
:why:
1212
:versions: []
1313
:when: 2022-08-12 14:31:52.953331422 Z
1414
- - :permit
1515
- BSD-3-Clause
16-
- :who:
17-
:why:
16+
- :who:
17+
:why:
1818
:versions: []
1919
:when: 2022-08-15 12:18:01.493210046 Z
2020
- - :permit
2121
- BSD
22-
- :who:
23-
:why:
22+
- :who:
23+
:why:
2424
:versions: []
2525
:when: 2022-08-15 12:18:10.367754569 Z
2626
- - :permit
2727
- Simplified BSD
28-
- :who:
29-
:why:
28+
- :who:
29+
:why:
3030
:versions: []
3131
:when: 2022-08-15 12:18:30.799597748 Z
3232
- - :permit
3333
- ISC License (ISCL)
34-
- :who:
35-
:why:
34+
- :who:
35+
:why:
3636
:versions: []
3737
:when: 2022-08-15 12:30:07.716433134 Z
3838
- - :license
3939
- matplotlib-inline
4040
- BSD-3-Clause
41-
- :who:
42-
:why: 'TODO(RSDK-583): why is license finder unable to detect this license automatically?'
41+
- :who:
42+
:why: "TODO(RSDK-583): why is license finder unable to detect this license automatically?"
4343
:license_links: https://github.com/ipython/matplotlib-inline/blob/fbf0ab8f2d81993ff8e319a003f58896c77f2443/LICENSE
4444
:versions: []
4545
:when: 2022-09-01 18:14:44.253208011 Z
4646
- - :permit
4747
- Historical Permission Notice and Disclaimer (HPND)
48-
- :who:
49-
:why:
48+
- :who:
49+
:why:
5050
:versions: []
5151
:when: 2022-09-16 15:36:22.534210576 Z
5252
- - :permit
5353
- Python Software Foundation License
54-
- :who:
55-
:why:
54+
- :who:
55+
:why:
5656
:versions: []
5757
:when: 2022-09-16 15:36:35.544888808 Z
5858
- - :approve
5959
- protobuf
60-
- :who:
61-
:why:
60+
- :who:
61+
:why:
6262
:versions: []
6363
:when: 2022-11-17 17:47:26.832844000 Z
6464
- - :permit
@@ -70,7 +70,14 @@
7070
- - :ignore
7171
- typing-extensions
7272
- :who: ethan
73-
:why: it's part of python and subject to the same license as the language, see
73+
:why:
74+
it's part of python and subject to the same license as the language, see
7475
https://github.com/python/typing_extensions/blob/main/LICENSE
7576
:versions: []
7677
:when: 2025-11-17 15:54:33.095491000 Z
78+
- - :approve
79+
- pymongo
80+
- :who: njooma
81+
:why: Apache 2.0
82+
:versions: []
83+
:when: 2026-01-07 21:58:05.061701000 Z

pyproject.toml

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ dependencies = [
1717
"typing-extensions>=4.12.2",
1818
"pymongo>=4.10.1",
1919
]
20+
[dependency-groups]
21+
dev = [
22+
"coverage>=7.6.1",
23+
"mypy-protobuf>=3.6.0",
24+
"myst-nb<1.0.0; python_version<'3.9'",
25+
"myst-nb>=1.0.0; python_version>='3.9'",
26+
"nbmake>=1.5.4",
27+
"numpy<1.25.0; python_version<'3.9'",
28+
"numpy>=1.26.2; python_version>='3.9'",
29+
"pillow>=10.4.0",
30+
"pyright>=1.1.382.post1",
31+
"pytest-asyncio>=0.24.0",
32+
"pytest>=8.3.3",
33+
"ruff>=0.6.8",
34+
"sphinx-autoapi<3.0.0; python_version<'3.9'",
35+
"sphinx-autoapi>=3.0.0; python_version>='3.9'",
36+
"sphinx-rtd-theme>=2.0.0",
37+
"types-pillow>=10.2.0.20240822",
38+
]
2039

2140
[project.urls]
2241
Homepage = "https://www.viam.com"
@@ -39,27 +58,6 @@ path = "src/viam/version_metadata.py"
3958
packages = ["src/viam"]
4059
artifacts = ["src/viam/rpc/libviam_rust_utils.*"]
4160

42-
[tool.uv]
43-
dev-dependencies = [
44-
"coverage>=7.6.1",
45-
"mypy-protobuf>=3.6.0",
46-
"myst-nb<1.0.0; python_version<'3.9'",
47-
"myst-nb>=1.0.0; python_version>='3.9'",
48-
"nbmake>=1.5.4",
49-
"numpy<1.25.0; python_version<'3.9'",
50-
"numpy>=1.26.2; python_version>='3.9'",
51-
"pillow>=10.4.0",
52-
"pyright>=1.1.382.post1",
53-
"pytest-asyncio>=0.24.0",
54-
"pytest-mock>=3.14.0",
55-
"pytest>=8.3.3",
56-
"ruff>=0.6.8",
57-
"sphinx-autoapi<3.0.0; python_version<'3.9'",
58-
"sphinx-autoapi>=3.0.0; python_version>='3.9'",
59-
"sphinx-rtd-theme>=2.0.0",
60-
"types-pillow>=10.2.0.20240822",
61-
]
62-
6361
[tool.pytest.ini_options]
6462
addopts = "-ra"
6563
testpaths = "tests"

tests/mocks/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import inspect
2+
from typing import Dict, List, Sequence, Set, Tuple, Union, get_args, get_origin, get_type_hints
3+
from unittest.mock import AsyncMock, create_autospec
4+
5+
6+
def create_mock_subclass(abstract_class):
7+
abstracts = getattr(abstract_class, "__abstractmethods__", set())
8+
9+
namespace = {}
10+
for method_name in abstracts:
11+
original_method = getattr(abstract_class, method_name)
12+
13+
if inspect.iscoroutinefunction(original_method):
14+
mock = AsyncMock(spec=f"{abstract_class.__name__}.{method_name}")
15+
mock.__name__ = method_name
16+
mock.__signature__ = inspect.signature(original_method)
17+
else:
18+
mock = create_autospec(original_method, instance=True)
19+
20+
namespace[method_name] = mock
21+
22+
base_methods = [("do_command", {}), ("get_geometries", [])]
23+
for method_name, return_value in base_methods:
24+
original_method = getattr(abstract_class, method_name, None)
25+
if original_method:
26+
mock = AsyncMock(spec=f"{abstract_class.__name__}.{method_name}")
27+
mock.__name__ = method_name
28+
mock.__signature__ = inspect.signature(original_method)
29+
mock.return_value = return_value
30+
namespace[method_name] = mock
31+
32+
return type(f"Mock{abstract_class.__name__}", (abstract_class,), namespace)

0 commit comments

Comments
 (0)