-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmock_server_for_testing.py
54 lines (43 loc) · 1.51 KB
/
mock_server_for_testing.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
"""Helper for using mock server for high level testing."""
from __future__ import annotations
import typing as t
from unittest.mock import AsyncMock, Mock
from labone.core.session import NodeInfo, Session
from labone.mock import AutomaticLabOneServer
from labone.nodetree.entry_point import construct_nodetree
if t.TYPE_CHECKING:
from labone.core.value import AnnotatedValue
from labone.nodetree.node import Node
async def get_mocked_node(
nodes_to_info: dict[str, NodeInfo],
*,
hide_kernel_prefix: bool = False,
custom_parser: t.Callable[[AnnotatedValue], AnnotatedValue] | None = None,
) -> Node:
"""Uses custom mock implementation.
Use when testing calls to the server.
"""
session_mock = await AutomaticLabOneServer(nodes_to_info).start_pipe()
return (
await construct_nodetree(
session_mock,
hide_kernel_prefix=hide_kernel_prefix,
custom_parser=custom_parser,
)
).root
async def get_unittest_mocked_node(
nodes_to_info: dict[str, NodeInfo],
*,
hide_kernel_prefix: bool = False,
) -> Node:
"""Minimal unittest mock.
Use when no calls to the server are tested.
This way, the tests do not depend on the `labone`
mock server.
"""
session_mock = Mock(spec=Session)
session_mock.list_nodes_info = AsyncMock(return_value=nodes_to_info)
session_mock.get_with_expression = AsyncMock()
return (
await construct_nodetree(session_mock, hide_kernel_prefix=hide_kernel_prefix)
).root