11from unittest .mock import patch
2+ import json
23import pytest
34from fastapi .testclient import TestClient
45from fastapi import HTTPException
1718
1819@pytest .fixture
1920def stub_env ():
20- return Env (latitude = 0 , longitude = 0 )
21+ env = Env (latitude = 0 , longitude = 0 )
22+ env_json = env .model_dump_json ()
23+ return json .loads (env_json )
2124
2225
2326@pytest .fixture
2427def stub_env_summary ():
25- return EnvSummary ()
28+ env_summary = EnvSummary ()
29+ env_summary_json = env_summary .model_dump_json ()
30+ return json .loads (env_summary_json )
2631
2732
28- def test_create_env ():
33+ def test_create_env (stub_env ):
2934 with patch .object (
3035 EnvController , "create_env" , return_value = EnvCreated (env_id = "123" )
3136 ) as mock_create_env :
32- response = client .post (
33- "/environments/" , json = {"latitude" : 0 , "longitude" : 0 }
34- )
37+ response = client .post ("/environments/" , json = stub_env )
3538 assert response .status_code == 200
3639 assert response .json () == {
3740 "env_id" : "123" ,
3841 "message" : "Environment successfully created" ,
3942 }
40- mock_create_env .assert_called_once_with (Env (latitude = 0 , longitude = 0 ))
43+ mock_create_env .assert_called_once_with (Env (** stub_env ))
44+
45+
46+ def test_create_env_optional_params ():
47+ test_object = {
48+ "latitude" : 0 ,
49+ "longitude" : 0 ,
50+ "elevation" : 1 ,
51+ "atmospheric_model_type" : "STANDARD_ATMOSPHERE" ,
52+ "atmospheric_model_file" : None ,
53+ "date" : "2021-01-01T00:00:00" ,
54+ }
55+ with patch .object (
56+ EnvController , "create_env" , return_value = EnvCreated (env_id = "123" )
57+ ) as mock_create_env :
58+ response = client .post ("/environments/" , json = test_object )
59+ assert response .status_code == 200
60+ assert response .json () == {
61+ "env_id" : "123" ,
62+ "message" : "Environment successfully created" ,
63+ }
64+ mock_create_env .assert_called_once_with (Env (** test_object ))
4165
4266
4367def test_create_env_invalid_input ():
@@ -47,14 +71,12 @@ def test_create_env_invalid_input():
4771 assert response .status_code == 422
4872
4973
50- def test_create_env_server_error ():
74+ def test_create_env_server_error (stub_env ):
5175 with patch .object (
5276 EnvController , "create_env" , side_effect = Exception ("error" )
5377 ):
5478 with pytest .raises (Exception ):
55- response = client .post (
56- "/environments/" , json = {"latitude" : 0 , "longitude" : 0 }
57- )
79+ response = client .post ("/environments/" , json = stub_env )
5880 assert response .status_code == 500
5981 assert response .json () == {
6082 "detail" : "Failed to create environment: error"
@@ -63,13 +85,11 @@ def test_create_env_server_error():
6385
6486def test_read_env (stub_env ):
6587 with patch .object (
66- EnvController , "get_env_by_id" , return_value = stub_env
88+ EnvController , "get_env_by_id" , return_value = Env ( ** stub_env )
6789 ) as mock_read_env :
6890 response = client .get ("/environments/123" )
6991 assert response .status_code == 200
70- expected_content = stub_env .model_dump ()
71- expected_content ["date" ] = expected_content ["date" ].isoformat ()
72- assert response .json () == expected_content
92+ assert response .json () == stub_env
7393 mock_read_env .assert_called_once_with ("123" )
7494
7595
@@ -99,23 +119,19 @@ def test_read_env_server_error():
99119 }
100120
101121
102- def test_update_env ():
122+ def test_update_env (stub_env ):
103123 with patch .object (
104124 EnvController ,
105125 "update_env_by_id" ,
106126 return_value = EnvUpdated (env_id = "123" ),
107127 ) as mock_update_env :
108- response = client .put (
109- "/environments/123" , json = {"longitude" : 1 , "latitude" : 1 }
110- )
128+ response = client .put ("/environments/123" , json = stub_env )
111129 assert response .status_code == 200
112130 assert response .json () == {
113131 "env_id" : "123" ,
114132 "message" : "Environment successfully updated" ,
115133 }
116- mock_update_env .assert_called_once_with (
117- "123" , Env (latitude = 1 , longitude = 1 )
118- )
134+ mock_update_env .assert_called_once_with ("123" , Env (** stub_env ))
119135
120136
121137def test_update_env_invalid_input ():
@@ -125,34 +141,27 @@ def test_update_env_invalid_input():
125141 assert response .status_code == 422
126142
127143
128- def test_update_env_not_found ():
144+ def test_update_env_not_found (stub_env ):
129145 with patch .object (
130146 EnvController ,
131147 "update_env_by_id" ,
132148 side_effect = HTTPException (
133149 status_code = 404 , detail = "Environment not found"
134150 ),
135- ) as mock_update_env :
136- response = client .put (
137- "/environments/123" , json = {"longitude" : 1 , "latitude" : 1 }
138- )
151+ ):
152+ response = client .put ("/environments/123" , json = stub_env )
139153 assert response .status_code == 404
140154 assert response .json () == {"detail" : "Environment not found" }
141- mock_update_env .assert_called_once_with (
142- "123" , Env (latitude = 1 , longitude = 1 )
143- )
144155
145156
146- def test_update_env_server_error ():
157+ def test_update_env_server_error (stub_env ):
147158 with patch .object (
148159 EnvController ,
149160 "update_env_by_id" ,
150161 side_effect = Exception ("error" ),
151162 ):
152163 with pytest .raises (Exception ):
153- response = client .put (
154- "/environments/123" , json = {"longitude" : 1 , "latitude" : 1 }
155- )
164+ response = client .put ("/environments/123" , json = stub_env )
156165 assert response .status_code == 500
157166 assert response .json () == {
158167 "detail" : "Failed to update environment: error"
@@ -205,19 +214,13 @@ def test_delete_env_server_error():
205214
206215def test_simulate_env (stub_env_summary ):
207216 with patch .object (
208- EnvController , "simulate_env" , return_value = stub_env_summary
217+ EnvController ,
218+ "simulate_env" ,
219+ return_value = EnvSummary (** stub_env_summary ),
209220 ) as mock_simulate_env :
210221 response = client .get ("/environments/123/summary" )
211222 assert response .status_code == 200
212- expected_content = stub_env_summary .model_dump ()
213- expected_content ["date" ] = expected_content ["date" ].isoformat ()
214- expected_content ["local_date" ] = expected_content [
215- "local_date"
216- ].isoformat ()
217- expected_content ["datetime_date" ] = expected_content [
218- "datetime_date"
219- ].isoformat ()
220- assert response .json () == expected_content
223+ assert response .json () == stub_env_summary
221224 mock_simulate_env .assert_called_once_with ("123" )
222225
223226
0 commit comments