Skip to content

Commit bc14b8b

Browse files
committed
Refactor motor tests to use a spy instead of a custom mock motor
1 parent c65278e commit bc14b8b

File tree

4 files changed

+198
-338
lines changed

4 files changed

+198
-338
lines changed

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)