Skip to content

Commit 5acfdd3

Browse files
committed
add tests for different types of ships
1 parent ea0912f commit 5acfdd3

File tree

4 files changed

+203
-11
lines changed

4 files changed

+203
-11
lines changed

antares-python/src/antares/cli.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ def add_ship( # noqa: PLR0913
122122

123123
except (ValidationError, ValueError, TypeError) as e:
124124
handle_error(f"Invalid ship parameters: {e}", code=2, json_output=json_output)
125-
return
126125

127126
try:
128127
client.add_ship(ship)

antares-python/tests/client/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from antares.client import AntaresClient
4-
from antares.models.ship import ShipConfig
4+
from antares.models.ship import StationaryShip
55

66

77
def test_reset_simulation_delegates(mocker):
@@ -14,7 +14,7 @@ def test_reset_simulation_delegates(mocker):
1414
def test_add_ship_delegates(mocker):
1515
mock_add = mocker.patch("antares.client.rest.RestClient.add_ship")
1616
client = AntaresClient(base_url="http://localhost")
17-
ship = ShipConfig(initial_position=(1.0, 2.0))
17+
ship = StationaryShip(initial_position=(1.0, 2.0))
1818
client.add_ship(ship)
1919
mock_add.assert_called_once_with(ship)
2020

antares-python/tests/client/test_rest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from antares.client.rest import RestClient
55
from antares.errors import ConnectionError, ShipConfigError, SimulationError
6-
from antares.models.ship import ShipConfig
6+
from antares.models.ship import CircleShip, LineShip, RandomShip
77

88

99
def test_reset_simulation_success(mocker):
@@ -24,7 +24,7 @@ def test_reset_simulation_failure(mocker):
2424
def test_add_ship_success(mocker):
2525
mock_request = httpx.Request("POST", "http://localhost/simulation/ships")
2626
mock_post = mocker.patch("httpx.post", return_value=httpx.Response(200, request=mock_request))
27-
ship = ShipConfig(initial_position=(0, 0))
27+
ship = LineShip(initial_position=(0, 0), angle=0, speed=1)
2828
client = RestClient(base_url="http://localhost")
2929
client.add_ship(ship)
3030
mock_post.assert_called_once()
@@ -40,7 +40,7 @@ def test_add_ship_invalid_response(mocker):
4040
request=mock_request,
4141
),
4242
)
43-
ship = ShipConfig(initial_position=(0, 0))
43+
ship = CircleShip(initial_position=(0, 0), radius=1, speed=1)
4444
client = RestClient(base_url="http://localhost")
4545
with pytest.raises(ShipConfigError):
4646
client.add_ship(ship)
@@ -75,7 +75,7 @@ def test_add_ship_request_error(mocker):
7575
),
7676
)
7777

78-
ship = ShipConfig(initial_position=(0, 0))
78+
ship = RandomShip(initial_position=(0, 0), max_speed=1)
7979
client = RestClient(base_url="http://localhost")
8080

8181
with pytest.raises(ConnectionError) as exc:

antares-python/tests/test_cli.py

Lines changed: 197 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,95 @@ def test_cli_reset(mocker, fake_config):
3232
mock_reset.assert_called_once()
3333

3434

35-
def test_cli_add_ship(mocker, fake_config):
35+
def test_cli_add_stationary_ship_success(mocker, fake_config):
3636
mock_add = mocker.patch("antares.client.rest.RestClient.add_ship")
37-
result = runner.invoke(app, ["add-ship", "--x", "5.0", "--y", "6.0", "--config", fake_config])
37+
38+
result = runner.invoke(
39+
app,
40+
["add-ship", "--type", "stationary", "--x", "5.0", "--y", "6.0", "--config", fake_config],
41+
)
42+
43+
assert result.exit_code == 0
44+
assert "Added stationary ship at (5.0, 6.0)" in result.output
45+
mock_add.assert_called_once()
46+
47+
48+
def test_cli_add_line_ship_success(mocker, fake_config):
49+
mock_add = mocker.patch("antares.client.rest.RestClient.add_ship")
50+
51+
result = runner.invoke(
52+
app,
53+
[
54+
"add-ship",
55+
"--type",
56+
"line",
57+
"--x",
58+
"10.0",
59+
"--y",
60+
"20.0",
61+
"--angle",
62+
"0.5",
63+
"--speed",
64+
"3.0",
65+
"--config",
66+
fake_config,
67+
],
68+
)
69+
70+
assert result.exit_code == 0
71+
assert "Added line ship at (10.0, 20.0)" in result.output
72+
mock_add.assert_called_once()
73+
74+
75+
def test_cli_add_circle_ship_success(mocker, fake_config):
76+
mock_add = mocker.patch("antares.client.rest.RestClient.add_ship")
77+
78+
result = runner.invoke(
79+
app,
80+
[
81+
"add-ship",
82+
"--type",
83+
"circle",
84+
"--x",
85+
"30.0",
86+
"--y",
87+
"40.0",
88+
"--radius",
89+
"15.0",
90+
"--speed",
91+
"2.5",
92+
"--config",
93+
fake_config,
94+
],
95+
)
96+
97+
assert result.exit_code == 0
98+
assert "Added circle ship at (30.0, 40.0)" in result.output
99+
mock_add.assert_called_once()
100+
101+
102+
def test_cli_add_random_ship_success(mocker, fake_config):
103+
mock_add = mocker.patch("antares.client.rest.RestClient.add_ship")
104+
105+
result = runner.invoke(
106+
app,
107+
[
108+
"add-ship",
109+
"--type",
110+
"random",
111+
"--x",
112+
"0.0",
113+
"--y",
114+
"0.0",
115+
"--max-speed",
116+
"12.0",
117+
"--config",
118+
fake_config,
119+
],
120+
)
121+
38122
assert result.exit_code == 0
39-
assert "Added ship at (5.0, 6.0)" in result.output
123+
assert "Added random ship at (0.0, 0.0)" in result.output
40124
mock_add.assert_called_once()
41125

42126

@@ -82,12 +166,121 @@ def test_cli_add_ship_error_handling(mocker, fake_config):
82166
mocker.patch(
83167
"antares.client.rest.RestClient.add_ship", side_effect=SimulationError("ship rejected")
84168
)
85-
result = runner.invoke(app, ["add-ship", "--x", "1", "--y", "2", "--config", fake_config])
169+
170+
result = runner.invoke(
171+
app,
172+
["add-ship", "--type", "stationary", "--x", "1", "--y", "2", "--config", fake_config],
173+
)
174+
86175
expected_exit_code = 2
87176
assert result.exit_code == expected_exit_code
88177
assert "ship rejected" in result.output
89178

90179

180+
def test_cli_add_ship_invalid_type(mocker, fake_config):
181+
result = runner.invoke(
182+
app,
183+
[
184+
"add-ship",
185+
"--type",
186+
"invalid_type",
187+
"--x",
188+
"10.0",
189+
"--y",
190+
"20.0",
191+
"--config",
192+
fake_config,
193+
],
194+
)
195+
196+
expected_exit_code = 2
197+
assert result.exit_code == expected_exit_code
198+
assert "Invalid ship type" in result.output
199+
200+
201+
def test_cli_add_stationary_ship_missing_args(fake_config):
202+
result = runner.invoke(
203+
app,
204+
[
205+
"add-ship",
206+
"--type",
207+
"stationary",
208+
"--x",
209+
"5.0",
210+
"--config",
211+
fake_config,
212+
],
213+
)
214+
215+
expected_exit_code = 2
216+
assert result.exit_code == expected_exit_code
217+
218+
219+
def test_cli_add_line_ship_missing_args(fake_config):
220+
result = runner.invoke(
221+
app,
222+
[
223+
"add-ship",
224+
"--type",
225+
"line",
226+
"--x",
227+
"10.0",
228+
"--y",
229+
"20.0",
230+
"--config",
231+
fake_config,
232+
],
233+
)
234+
235+
expected_exit_code = 2
236+
assert result.exit_code == expected_exit_code
237+
assert "Invalid ship parameters" in result.output
238+
239+
240+
def test_cli_add_circle_missing_radius(mocker, fake_config):
241+
result = runner.invoke(
242+
app,
243+
[
244+
"add-ship",
245+
"--type",
246+
"circle",
247+
"--x",
248+
"10.0",
249+
"--y",
250+
"20.0",
251+
"--speed",
252+
"2.0",
253+
"--config",
254+
fake_config,
255+
],
256+
)
257+
258+
expected_exit_code = 2
259+
assert result.exit_code == expected_exit_code
260+
assert "Invalid ship parameters" in result.output
261+
262+
263+
def test_cli_add_random_missing_max_speed(mocker, fake_config):
264+
result = runner.invoke(
265+
app,
266+
[
267+
"add-ship",
268+
"--type",
269+
"random",
270+
"--x",
271+
"0.0",
272+
"--y",
273+
"0.0",
274+
"--config",
275+
fake_config,
276+
],
277+
)
278+
279+
expected_exit_code = 2
280+
assert result.exit_code == expected_exit_code
281+
assert "Invalid ship parameters" in result.output
282+
283+
91284
def test_cli_subscribe_error(monkeypatch, fake_config):
92285
class FailingAsyncGenerator:
93286
def __aiter__(self):

0 commit comments

Comments
 (0)