11from unittest .mock import patch
22import pytest
33from fastapi .testclient import TestClient
4+ from fastapi import HTTPException
45from lib .models .environment import Env
56from lib .controllers .environment import EnvController
67from lib .views .environment import (
1516
1617
1718@pytest .fixture
18- def mock_env ():
19+ def stub_env ():
1920 return Env (latitude = 0 , longitude = 0 )
2021
2122
2223@pytest .fixture
23- def mock_env_summary ():
24+ def stub_env_summary ():
2425 return EnvSummary ()
2526
2627
@@ -38,18 +39,49 @@ def test_create_env():
3839 }
3940 mock_create_env .assert_called_once_with (Env (latitude = 0 , longitude = 0 ))
4041
42+ def test_create_env_invalid_input ():
43+ response = client .post ("/environments/" , json = {"latitude" : "foo" , "longitude" : "bar" })
44+ assert response .status_code == 422
4145
42- def test_read_env ( mock_env ):
46+ def test_create_env_server_error ( ):
4347 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
4560 ) as mock_read_env :
4661 response = client .get ("/environments/123" )
4762 assert response .status_code == 200
48- expected_content = mock_env .model_dump ()
63+ expected_content = stub_env .model_dump ()
4964 expected_content ["date" ] = expected_content ["date" ].isoformat ()
5065 assert response .json () == expected_content
5166 mock_read_env .assert_called_once_with ("123" )
5267
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" }
5385
5486def test_update_env ():
5587 with patch .object (
@@ -69,6 +101,37 @@ def test_update_env():
69101 "123" , Env (latitude = 1 , longitude = 1 )
70102 )
71103
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" }
72135
73136def test_delete_env ():
74137 with patch .object (
@@ -84,14 +147,38 @@ def test_delete_env():
84147 }
85148 mock_delete_env .assert_called_once_with ("123" )
86149
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" )
87163
88- def test_simulate_env ( mock_env_summary ):
164+ def test_delete_env_server_error ( ):
89165 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
91178 ) as mock_simulate_env :
92179 response = client .get ("/environments/123/summary" )
93180 assert response .status_code == 200
94- expected_content = mock_env_summary .model_dump ()
181+ expected_content = stub_env_summary .model_dump ()
95182 expected_content ["date" ] = expected_content ["date" ].isoformat ()
96183 expected_content ["local_date" ] = expected_content [
97184 "local_date"
@@ -102,6 +189,27 @@ def test_simulate_env(mock_env_summary):
102189 assert response .json () == expected_content
103190 mock_simulate_env .assert_called_once_with ("123" )
104191
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" }
105213
106214def test_read_rocketpy_env ():
107215 with patch .object (
@@ -112,3 +220,25 @@ def test_read_rocketpy_env():
112220 assert response .content == b'rocketpy'
113221 assert response .headers ["content-type" ] == "application/octet-stream"
114222 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