-
Notifications
You must be signed in to change notification settings - Fork 127
/
test_server.py
174 lines (136 loc) · 7.18 KB
/
test_server.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
from unittest import mock
from ros_tcp_endpoint import TcpServer
from ros_tcp_endpoint.server import SysCommands
import importlib
import rospy
import sys
import ros_tcp_endpoint
@mock.patch("socket.socket")
@mock.patch("ros_tcp_endpoint.server.rospy")
def test_server_constructor(mock_ros, mock_socket):
mock_ros.get_param = mock.Mock(return_value="127.0.0.1")
server = TcpServer("test-tcp-server")
assert server.node_name == "test-tcp-server"
assert server.tcp_ip == "127.0.0.1"
assert server.buffer_size == 1024
assert server.connections == 10
def test_start_server():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
assert server.tcp_ip == "127.0.0.1"
assert server.tcp_port == 10000
assert server.connections == 10
server.start()
def test_unity_service_empty_topic_should_return_none():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
system_cmds = SysCommands(server)
result = system_cmds.unity_service("", "test message")
assert result is None
def test_unity_service_resolve_message_name_failure():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
system_cmds = SysCommands(server)
result = system_cmds.unity_service("get_pos", "unresolvable message")
assert result is None
@mock.patch.object(rospy, "Service")
@mock.patch.object(
SysCommands, "resolve_message_name", return_value="unity_interfaces.msg/RosUnitySrvMessage"
)
def test_unity_service_resolve_news_service(mock_resolve_message, mock_ros_service):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
assert server.ros_services_table == {}
system_cmds = SysCommands(server)
result = system_cmds.unity_service("get_pos", "unity_interfaces.msg/RosUnitySrvMessage")
mock_ros_service.assert_called_once
assert result is None
@mock.patch.object(rospy, "Service")
@mock.patch.object(
SysCommands, "resolve_message_name", return_value="unity_interfaces.msg/RosUnitySrvMessage"
)
def test_unity_service_resolve_existing_service(mock_resolve_message, mock_ros_service):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
server.ros_services = {"get_pos": mock.Mock()}
system_cmds = SysCommands(server)
result = system_cmds.unity_service("get_pos", "unity_interfaces.msg/RosUnitySrvMessage")
mock_ros_service.assert_called_once
assert result is None
@mock.patch.object(sys, "modules", return_value="unity_interfaces.msg")
@mock.patch.object(importlib, "import_module")
def test_resolve_message_name(mock_import_module, mock_sys_modules):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
msg_name = "unity_interfaces.msg/UnityColor.msg"
result = SysCommands(server).resolve_message_name(msg_name)
mock_import_module.assert_called_once
mock_sys_modules.assert_called_once
assert result is not None
@mock.patch.object(rospy, "Publisher")
def test_publish_add_new_topic(mock_ros_publisher):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).publish("object_pos_topic", "std_msgs/Bool")
assert server.publishers_table != {}
mock_ros_publisher.assert_called_once
@mock.patch.object(rospy, "Publisher")
def test_publish_existing_topic(mock_ros_publisher):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
server.publishers_table = {"object_pos_topic": mock.Mock()}
result = SysCommands(server).publish("object_pos_topic", "std_msgs/Bool")
assert server.publishers_table["object_pos_topic"] is not None
mock_ros_publisher.assert_called_once
def test_publish_empty_topic_should_return_none():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).publish("", "pos")
assert result is None
assert server.publishers_table == {}
def test_publish_empty_message_should_return_none():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).publish("test-topic", "")
assert result is None
assert server.publishers_table == {}
@mock.patch.object(rospy, "Subscriber")
@mock.patch.object(SysCommands, "resolve_message_name", return_value="unity_interfaces.msg/Pos")
def test_subscribe_to_new_topic(mock_resolve_msg, mock_ros_subscriber):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).subscribe("object_pos_topic", "pos")
assert server.subscribers_table != {}
mock_ros_subscriber.assert_called_once
@mock.patch.object(rospy, "Subscriber")
@mock.patch.object(SysCommands, "resolve_message_name", return_value="unity_interfaces.msg/Pos")
def test_subscribe_to_existing_topic(mock_resolve_msg, mock_ros_subscriber):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
server.subscribers_table = {"object_pos_topic": mock.Mock()}
result = SysCommands(server).subscribe("object_pos_topic", "pos")
assert server.subscribers_table["object_pos_topic"] is not None
mock_ros_subscriber.assert_called_once
def test_subscribe_to_empty_topic_should_return_none():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).subscribe("", "pos")
assert result is None
assert server.subscribers_table == {}
def test_subscribe_to_empty_message_should_return_none():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).subscribe("test-topic", "")
assert result is None
assert server.subscribers_table == {}
@mock.patch.object(rospy, "ServiceProxy")
@mock.patch.object(SysCommands, "resolve_message_name")
def test_ros_service_new_topic(mock_resolve_msg, mock_ros_service):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).ros_service("object_pos_topic", "pos")
assert server.ros_services_table != {}
mock_ros_service.assert_called_once
@mock.patch.object(rospy, "ServiceProxy")
@mock.patch.object(SysCommands, "resolve_message_name")
def test_ros_service_existing_topic(mock_resolve_msg, mock_ros_service):
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
server.ros_services_table = {"object_pos_topic": mock.Mock()}
result = SysCommands(server).ros_service("object_pos_topic", "pos")
assert server.ros_services_table["object_pos_topic"] is not None
mock_ros_service.assert_called_once
def test_ros_service_empty_topic_should_return_none():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).ros_service("", "pos")
assert result is None
assert server.ros_services_table == {}
def test_ros_service_empty_message_should_return_none():
server = TcpServer(node_name="test-tcp-server", tcp_ip="127.0.0.1", tcp_port=10000)
result = SysCommands(server).ros_service("test-topic", "")
assert result is None
assert server.ros_services_table == {}