1
1
from unittest .mock import patch
2
2
import pytest
3
3
from fastapi .testclient import TestClient
4
+ from fastapi import HTTPException
4
5
from lib .models .environment import Env
5
6
from lib .controllers .environment import EnvController
6
7
from lib .views .environment import (
15
16
16
17
17
18
@pytest .fixture
18
- def mock_env ():
19
+ def stub_env ():
19
20
return Env (latitude = 0 , longitude = 0 )
20
21
21
22
22
23
@pytest .fixture
23
- def mock_env_summary ():
24
+ def stub_env_summary ():
24
25
return EnvSummary ()
25
26
26
27
@@ -38,18 +39,49 @@ def test_create_env():
38
39
}
39
40
mock_create_env .assert_called_once_with (Env (latitude = 0 , longitude = 0 ))
40
41
42
+ def test_create_env_invalid_input ():
43
+ response = client .post ("/environments/" , json = {"latitude" : "foo" , "longitude" : "bar" })
44
+ assert response .status_code == 422
41
45
42
- def test_read_env ( mock_env ):
46
+ def test_create_env_server_error ( ):
43
47
with patch .object (
44
- EnvController , "get_env_by_id" , return_value = mock_env
48
+ EnvController , "create_env" , side_effect = Exception ("error" )
49
+ ) as mock_create_env :
50
+ with pytest .raises (Exception ):
51
+ response = client .post (
52
+ "/environments/" , json = {"latitude" : 0 , "longitude" : 0 }
53
+ )
54
+ assert response .status_code == 500
55
+ assert response .json () == {"detail" : "Failed to create environment: error" }
56
+
57
+ def test_read_env (stub_env ):
58
+ with patch .object (
59
+ EnvController , "get_env_by_id" , return_value = stub_env
45
60
) as mock_read_env :
46
61
response = client .get ("/environments/123" )
47
62
assert response .status_code == 200
48
- expected_content = mock_env .model_dump ()
63
+ expected_content = stub_env .model_dump ()
49
64
expected_content ["date" ] = expected_content ["date" ].isoformat ()
50
65
assert response .json () == expected_content
51
66
mock_read_env .assert_called_once_with ("123" )
52
67
68
+ def test_read_env_not_found ():
69
+ with patch .object (
70
+ EnvController , "get_env_by_id" , side_effect = HTTPException (status_code = 404 , detail = "Environment not found" )
71
+ ) as mock_read_env :
72
+ response = client .get ("/environments/123" )
73
+ assert response .status_code == 404
74
+ assert response .json () == {"detail" : "Environment not found" }
75
+ mock_read_env .assert_called_once_with ("123" )
76
+
77
+ def test_read_env_server_error ():
78
+ with patch .object (
79
+ EnvController , "get_env_by_id" , side_effect = Exception ("error" )
80
+ ) as mock_read_env :
81
+ with pytest .raises (Exception ):
82
+ response = client .get ("/environments/123" )
83
+ assert response .status_code == 500
84
+ assert response .json () == {"detail" : "Failed to read environment: error" }
53
85
54
86
def test_update_env ():
55
87
with patch .object (
@@ -69,6 +101,37 @@ def test_update_env():
69
101
"123" , Env (latitude = 1 , longitude = 1 )
70
102
)
71
103
104
+ def test_update_env_invalid_input ():
105
+ response = client .put ("/environments/123" , json = {"latitude" : "foo" , "longitude" : "bar" })
106
+ assert response .status_code == 422
107
+
108
+ def test_update_env_not_found ():
109
+ with patch .object (
110
+ EnvController ,
111
+ "update_env_by_id" ,
112
+ side_effect = HTTPException (status_code = 404 , detail = "Environment not found" ),
113
+ ) as mock_update_env :
114
+ response = client .put (
115
+ "/environments/123" , json = {"longitude" : 1 , "latitude" : 1 }
116
+ )
117
+ assert response .status_code == 404
118
+ assert response .json () == {"detail" : "Environment not found" }
119
+ mock_update_env .assert_called_once_with (
120
+ "123" , Env (latitude = 1 , longitude = 1 )
121
+ )
122
+
123
+ def test_update_env_server_error ():
124
+ with patch .object (
125
+ EnvController ,
126
+ "update_env_by_id" ,
127
+ side_effect = Exception ("error" ),
128
+ ) as mock_update_env :
129
+ with pytest .raises (Exception ):
130
+ response = client .put (
131
+ "/environments/123" , json = {"longitude" : 1 , "latitude" : 1 }
132
+ )
133
+ assert response .status_code == 500
134
+ assert response .json () == {"detail" : "Failed to update environment: error" }
72
135
73
136
def test_delete_env ():
74
137
with patch .object (
@@ -84,14 +147,38 @@ def test_delete_env():
84
147
}
85
148
mock_delete_env .assert_called_once_with ("123" )
86
149
150
+ def test_delete_env_not_found ():
151
+ with patch .object (
152
+ EnvController ,
153
+ "delete_env_by_id" ,
154
+ return_value = EnvDeleted (env_id = "123" ),
155
+ ) as mock_delete_env :
156
+ response = client .delete ("/environments/123" )
157
+ assert response .status_code == 200
158
+ assert response .json () == {
159
+ "env_id" : "123" ,
160
+ "message" : "Environment successfully deleted" ,
161
+ }
162
+ mock_delete_env .assert_called_once_with ("123" )
87
163
88
- def test_simulate_env ( mock_env_summary ):
164
+ def test_delete_env_server_error ( ):
89
165
with patch .object (
90
- EnvController , "simulate_env" , return_value = mock_env_summary
166
+ EnvController ,
167
+ "delete_env_by_id" ,
168
+ side_effect = Exception ("error" ),
169
+ ) as mock_delete_env :
170
+ with pytest .raises (Exception ):
171
+ response = client .delete ("/environments/123" )
172
+ assert response .status_code == 500
173
+ assert response .json () == {"detail" : "Failed to delete environment: error" }
174
+
175
+ def test_simulate_env (stub_env_summary ):
176
+ with patch .object (
177
+ EnvController , "simulate_env" , return_value = stub_env_summary
91
178
) as mock_simulate_env :
92
179
response = client .get ("/environments/123/summary" )
93
180
assert response .status_code == 200
94
- expected_content = mock_env_summary .model_dump ()
181
+ expected_content = stub_env_summary .model_dump ()
95
182
expected_content ["date" ] = expected_content ["date" ].isoformat ()
96
183
expected_content ["local_date" ] = expected_content [
97
184
"local_date"
@@ -102,6 +189,27 @@ def test_simulate_env(mock_env_summary):
102
189
assert response .json () == expected_content
103
190
mock_simulate_env .assert_called_once_with ("123" )
104
191
192
+ def test_simulate_env_not_found ():
193
+ with patch .object (
194
+ EnvController ,
195
+ "simulate_env" ,
196
+ side_effect = HTTPException (status_code = 404 , detail = "Environment not found" ),
197
+ ) as mock_simulate_env :
198
+ response = client .get ("/environments/123/summary" )
199
+ assert response .status_code == 404
200
+ assert response .json () == {"detail" : "Environment not found" }
201
+ mock_simulate_env .assert_called_once_with ("123" )
202
+
203
+ def test_simulate_env_server_error ():
204
+ with patch .object (
205
+ EnvController ,
206
+ "simulate_env" ,
207
+ side_effect = Exception ("error" ),
208
+ ) as mock_simulate_env :
209
+ with pytest .raises (Exception ):
210
+ response = client .get ("/environments/123/summary" )
211
+ assert response .status_code == 500
212
+ assert response .json () == {"detail" : "Failed to simulate environment: error" }
105
213
106
214
def test_read_rocketpy_env ():
107
215
with patch .object (
@@ -112,3 +220,25 @@ def test_read_rocketpy_env():
112
220
assert response .content == b'rocketpy'
113
221
assert response .headers ["content-type" ] == "application/octet-stream"
114
222
mock_read_rocketpy_env .assert_called_once_with ("123" )
223
+
224
+ def test_read_rocketpy_env_not_found ():
225
+ with patch .object (
226
+ EnvController ,
227
+ "get_rocketpy_env_binary" ,
228
+ side_effect = HTTPException (status_code = 404 , detail = "Environment not found" ),
229
+ ) as mock_read_rocketpy_env :
230
+ response = client .get ("/environments/123/rocketpy" )
231
+ assert response .status_code == 404
232
+ assert response .json () == {"detail" : "Environment not found" }
233
+ mock_read_rocketpy_env .assert_called_once_with ("123" )
234
+
235
+ def test_read_rocketpy_env_server_error ():
236
+ with patch .object (
237
+ EnvController ,
238
+ "get_rocketpy_env_binary" ,
239
+ side_effect = Exception ("error" ),
240
+ ) as mock_read_rocketpy_env :
241
+ with pytest .raises (Exception ):
242
+ response = client .get ("/environments/123/rocketpy" )
243
+ assert response .status_code == 500
244
+ assert response .json () == {"detail" : "Failed to read rocketpy environment: error" }
0 commit comments