-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_run.py
243 lines (191 loc) · 7.83 KB
/
test_run.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
# Copyright (c) 2022-2024 Contributors to the Eclipse Foundation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
import json
import os
import sys
from unittest import mock
from unittest.mock import AsyncMock, MagicMock
import pytest
from google.protobuf.timestamp_pb2 import Timestamp
from vehicle import vehicle # type: ignore
from velocitas_sdk.vdb.reply import DataPointReply
from velocitas_sdk.vdb.types import TypedDataPointResult
# Get the app directory path
app_dir_path = os.path.abspath(os.path.join(__file__, "../../.."))
# Add the src directory to the sys.path
src_dir = os.path.join(app_dir_path, "src")
sys.path.insert(0, src_dir)
# Import the SeatAdjusterApp class from vapp.py
from vapp import SeatAdjusterApp # type: ignore # noqa: E402
@pytest.mark.asyncio
async def test_on_seat_position_changed():
seat_adjuster_app = SeatAdjusterApp(vehicle)
# Create a MagicMock instance to mock the value attribute
position_data_point_value_mock = MagicMock(value=5)
# Create a MagicMock instance to mock the data point
position_data_point_mock = MagicMock()
position_data_point_mock.get.return_value = position_data_point_value_mock
# Create a MagicMock instance to mock the DataPointReply class
position_data_point_reply_mock = MagicMock(spec=DataPointReply)
position_data_point_reply_mock.return_value = position_data_point_mock
expected_data = {"position": position_data_point_value_mock.value}
# Mock the publish_event method
seat_adjuster_app.publish_event = AsyncMock()
# Call the method under test
await seat_adjuster_app.on_seat_position_changed(position_data_point_reply_mock())
# Assert that publish_event was called with the correct arguments
seat_adjuster_app.publish_event.assert_called_once_with(
"seatadjuster/currentPosition",
json.dumps(expected_data),
)
@pytest.mark.asyncio
async def test_on_set_position_request_received_vehicle_not_moving():
seat_adjuster_app = SeatAdjusterApp(vehicle)
vehicle_speed_data_point = TypedDataPointResult[float](
"foo", 0, Timestamp(seconds=10, nanos=0)
)
request_data_str = get_valid_request_data_str()
request_data = json.loads(request_data_str)
expected_response_data = {
"requestId": request_data["requestId"],
"result": {
"status": 0,
"message": f"Set Seat position to: {request_data['position']}",
},
}
with mock.patch.object(
vehicle.Speed,
"get",
new_callable=mock.AsyncMock,
return_value=vehicle_speed_data_point,
):
called_data_point = (
seat_adjuster_app.Vehicle.Cabin.Seat.Row1.DriverSide.Position
)
called_data_point.set = AsyncMock()
seat_adjuster_app.publish_event = AsyncMock()
await seat_adjuster_app.on_set_position_request_received(request_data_str)
called_data_point.set.assert_called_once_with(request_data["position"])
seat_adjuster_app.publish_event.assert_called_once_with(
"seatadjuster/setPosition/response",
json.dumps(expected_response_data),
)
@pytest.mark.asyncio
async def test_on_set_position_request_received_vehicle_moving():
seat_adjuster_app = SeatAdjusterApp(vehicle)
vehicle_speed_data_point = TypedDataPointResult[float](
"foo", 5, Timestamp(seconds=10, nanos=0)
)
request_data_str = get_valid_request_data_str()
request_data = json.loads(request_data_str)
error_msg = f"""Not allowed to move seat because vehicle speed
is {vehicle_speed_data_point.value} and not 0"""
expected_response_data = {
"requestId": request_data["requestId"],
"result": {
"status": 1,
"message": error_msg,
},
}
with mock.patch.object(
vehicle.Speed,
"get",
new_callable=mock.AsyncMock,
return_value=vehicle_speed_data_point,
):
seat_adjuster_app.publish_event = AsyncMock()
await seat_adjuster_app.on_set_position_request_received(request_data_str)
seat_adjuster_app.publish_event.assert_called_once_with(
"seatadjuster/setPosition/response",
json.dumps(expected_response_data),
)
@pytest.mark.asyncio
async def test_on_set_position_request_received_error_path():
seat_adjuster_app = SeatAdjusterApp(vehicle)
vehicle_speed_data_point = TypedDataPointResult[float](
"foo", 0, Timestamp(seconds=10, nanos=0)
)
request_data_str = get_valid_request_data_str()
request_data = json.loads(request_data_str)
expected_response_data = {
"requestId": request_data["requestId"],
"result": {
"status": 1,
"message": "Exception on set Seat position",
},
}
with mock.patch.object(
vehicle.Speed,
"get",
new_callable=mock.AsyncMock,
return_value=vehicle_speed_data_point,
):
called_data_point = (
seat_adjuster_app.Vehicle.Cabin.Seat.Row1.DriverSide.Position
)
called_data_point.set = AsyncMock(side_effect=async_raise_exception)
seat_adjuster_app.publish_event = AsyncMock()
await seat_adjuster_app.on_set_position_request_received(request_data_str)
called_data_point.set.assert_called_once_with(request_data["position"])
seat_adjuster_app.publish_event.assert_called_once_with(
"seatadjuster/setPosition/response",
json.dumps(expected_response_data),
)
@pytest.mark.asyncio
async def test_on_set_position_request_received_high_position():
seat_adjuster_app = SeatAdjusterApp(vehicle)
vehicle_speed_data_point = TypedDataPointResult[float](
"foo", 0, Timestamp(seconds=10, nanos=0)
)
request_data_str = get_invalid_request_data_str()
request_data = json.loads(request_data_str)
error_msg = f"""Provided position {request_data["position"]} \
should not be Greater than 1000 (Max)"""
expected_response_data = {
"requestId": request_data["requestId"],
"result": {
"status": 1,
"message": f"Failed to set the position \
{request_data['position']}, error: {error_msg}",
},
}
with mock.patch.object(
vehicle.Speed,
"get",
new_callable=mock.AsyncMock,
return_value=vehicle_speed_data_point,
):
called_data_point = (
seat_adjuster_app.Vehicle.Cabin.Seat.Row1.DriverSide.Position
)
called_data_point.set = AsyncMock(side_effect=async_raise_value_error)
seat_adjuster_app.publish_event = AsyncMock()
await seat_adjuster_app.on_set_position_request_received(request_data_str)
called_data_point.set.assert_called_once_with(request_data["position"])
seat_adjuster_app.publish_event.assert_called_once_with(
"seatadjuster/setPosition/response",
json.dumps(expected_response_data),
)
def get_valid_request_data_str():
return '{"requestId": 123, "position": 10}'
def get_invalid_request_data_str():
return '{"requestId": 123, "position": 1001}'
async def async_raise_exception(*args):
raise Exception("Unknown error")
async def async_raise_value_error(*args):
data = json.loads(get_invalid_request_data_str())
raise ValueError(
f"""Provided position {data['position']} \
should not be Greater than 1000 (Max)"""
)