11from unittest .mock import patch , Mock , AsyncMock
2+ import copy
23import json
34import pytest
45from fastapi .testclient import TestClient
56from fastapi import HTTPException , status
67from src .models .environment import EnvironmentModel
7- from src .models .flight import FlightModel
8+ from src .models .flight import FlightModel , FlightWithReferencesRequest
89from src .models .rocket import RocketModel
910from src .views .motor import MotorView
1011from src .views .rocket import RocketView
@@ -54,9 +55,23 @@ def mock_controller_instance():
5455 mock_controller_instance .get_rocketpy_flight_binary = Mock ()
5556 mock_controller_instance .update_environment_by_flight_id = Mock ()
5657 mock_controller_instance .update_rocket_by_flight_id = Mock ()
58+ mock_controller_instance .create_flight_from_references = Mock ()
59+ mock_controller_instance .update_flight_from_references = Mock ()
5760 yield mock_controller_instance
5861
5962
63+ @pytest .fixture
64+ def stub_flight_reference_payload (stub_flight_dump ):
65+ partial_flight = copy .deepcopy (stub_flight_dump )
66+ partial_flight .pop ('environment' , None )
67+ partial_flight .pop ('rocket' , None )
68+ return {
69+ 'environment_id' : 'env-123' ,
70+ 'rocket_id' : 'rocket-456' ,
71+ 'flight' : partial_flight ,
72+ }
73+
74+
6075def test_create_flight (stub_flight_dump , mock_controller_instance ):
6176 mock_response = AsyncMock (return_value = FlightCreated (flight_id = '123' ))
6277 mock_controller_instance .post_flight = mock_response
@@ -71,6 +86,50 @@ def test_create_flight(stub_flight_dump, mock_controller_instance):
7186 )
7287
7388
89+ def test_create_flight_from_references (
90+ stub_flight_reference_payload , mock_controller_instance
91+ ):
92+ mock_response = AsyncMock (return_value = FlightCreated (flight_id = '123' ))
93+ mock_controller_instance .create_flight_from_references = mock_response
94+ response = client .post (
95+ '/flights/from-references' , json = stub_flight_reference_payload
96+ )
97+ assert response .status_code == 201
98+ assert response .json () == {
99+ 'flight_id' : '123' ,
100+ 'message' : 'Flight successfully created' ,
101+ }
102+ mock_controller_instance .create_flight_from_references .assert_called_once_with (
103+ FlightWithReferencesRequest (** stub_flight_reference_payload )
104+ )
105+
106+
107+ def test_create_flight_from_references_not_found (
108+ stub_flight_reference_payload , mock_controller_instance
109+ ):
110+ mock_controller_instance .create_flight_from_references .side_effect = (
111+ HTTPException (status_code = status .HTTP_404_NOT_FOUND )
112+ )
113+ response = client .post (
114+ '/flights/from-references' , json = stub_flight_reference_payload
115+ )
116+ assert response .status_code == 404
117+ assert response .json () == {'detail' : 'Not Found' }
118+
119+
120+ def test_create_flight_from_references_server_error (
121+ stub_flight_reference_payload , mock_controller_instance
122+ ):
123+ mock_controller_instance .create_flight_from_references .side_effect = (
124+ HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR )
125+ )
126+ response = client .post (
127+ '/flights/from-references' , json = stub_flight_reference_payload
128+ )
129+ assert response .status_code == 500
130+ assert response .json () == {'detail' : 'Internal Server Error' }
131+
132+
74133def test_create_flight_optional_params (
75134 stub_flight_dump , mock_controller_instance
76135):
@@ -173,6 +232,49 @@ def test_update_flight_by_id(stub_flight_dump, mock_controller_instance):
173232 )
174233
175234
235+ def test_update_flight_from_references (
236+ stub_flight_reference_payload , mock_controller_instance
237+ ):
238+ mock_response = AsyncMock (return_value = None )
239+ mock_controller_instance .update_flight_from_references = mock_response
240+ response = client .put (
241+ '/flights/123/from-references' ,
242+ json = stub_flight_reference_payload ,
243+ )
244+ assert response .status_code == 204
245+ mock_controller_instance .update_flight_from_references .assert_called_once_with (
246+ '123' , FlightWithReferencesRequest (** stub_flight_reference_payload )
247+ )
248+
249+
250+ def test_update_flight_from_references_not_found (
251+ stub_flight_reference_payload , mock_controller_instance
252+ ):
253+ mock_controller_instance .update_flight_from_references .side_effect = (
254+ HTTPException (status_code = status .HTTP_404_NOT_FOUND )
255+ )
256+ response = client .put (
257+ '/flights/123/from-references' ,
258+ json = stub_flight_reference_payload ,
259+ )
260+ assert response .status_code == 404
261+ assert response .json () == {'detail' : 'Not Found' }
262+
263+
264+ def test_update_flight_from_references_server_error (
265+ stub_flight_reference_payload , mock_controller_instance
266+ ):
267+ mock_controller_instance .update_flight_from_references .side_effect = (
268+ HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR )
269+ )
270+ response = client .put (
271+ '/flights/123/from-references' ,
272+ json = stub_flight_reference_payload ,
273+ )
274+ assert response .status_code == 500
275+ assert response .json () == {'detail' : 'Internal Server Error' }
276+
277+
176278def test_update_environment_by_flight_id (
177279 stub_environment_dump , mock_controller_instance
178280):
0 commit comments