1
1
from unittest .mock import patch
2
+ import json
2
3
import pytest
3
4
from fastapi .testclient import TestClient
4
5
from fastapi import HTTPException
17
18
18
19
@pytest .fixture
19
20
def 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 )
21
24
22
25
23
26
@pytest .fixture
24
27
def 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 )
26
31
27
32
28
- def test_create_env ():
33
+ def test_create_env (stub_env ):
29
34
with patch .object (
30
35
EnvController , "create_env" , return_value = EnvCreated (env_id = "123" )
31
36
) as mock_create_env :
32
- response = client .post (
33
- "/environments/" , json = {"latitude" : 0 , "longitude" : 0 }
34
- )
37
+ response = client .post ("/environments/" , json = stub_env )
35
38
assert response .status_code == 200
36
39
assert response .json () == {
37
40
"env_id" : "123" ,
38
41
"message" : "Environment successfully created" ,
39
42
}
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 ))
41
65
42
66
43
67
def test_create_env_invalid_input ():
@@ -47,14 +71,12 @@ def test_create_env_invalid_input():
47
71
assert response .status_code == 422
48
72
49
73
50
- def test_create_env_server_error ():
74
+ def test_create_env_server_error (stub_env ):
51
75
with patch .object (
52
76
EnvController , "create_env" , side_effect = Exception ("error" )
53
77
):
54
78
with pytest .raises (Exception ):
55
- response = client .post (
56
- "/environments/" , json = {"latitude" : 0 , "longitude" : 0 }
57
- )
79
+ response = client .post ("/environments/" , json = stub_env )
58
80
assert response .status_code == 500
59
81
assert response .json () == {
60
82
"detail" : "Failed to create environment: error"
@@ -63,13 +85,11 @@ def test_create_env_server_error():
63
85
64
86
def test_read_env (stub_env ):
65
87
with patch .object (
66
- EnvController , "get_env_by_id" , return_value = stub_env
88
+ EnvController , "get_env_by_id" , return_value = Env ( ** stub_env )
67
89
) as mock_read_env :
68
90
response = client .get ("/environments/123" )
69
91
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
73
93
mock_read_env .assert_called_once_with ("123" )
74
94
75
95
@@ -99,23 +119,19 @@ def test_read_env_server_error():
99
119
}
100
120
101
121
102
- def test_update_env ():
122
+ def test_update_env (stub_env ):
103
123
with patch .object (
104
124
EnvController ,
105
125
"update_env_by_id" ,
106
126
return_value = EnvUpdated (env_id = "123" ),
107
127
) 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 )
111
129
assert response .status_code == 200
112
130
assert response .json () == {
113
131
"env_id" : "123" ,
114
132
"message" : "Environment successfully updated" ,
115
133
}
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 ))
119
135
120
136
121
137
def test_update_env_invalid_input ():
@@ -125,34 +141,27 @@ def test_update_env_invalid_input():
125
141
assert response .status_code == 422
126
142
127
143
128
- def test_update_env_not_found ():
144
+ def test_update_env_not_found (stub_env ):
129
145
with patch .object (
130
146
EnvController ,
131
147
"update_env_by_id" ,
132
148
side_effect = HTTPException (
133
149
status_code = 404 , detail = "Environment not found"
134
150
),
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 )
139
153
assert response .status_code == 404
140
154
assert response .json () == {"detail" : "Environment not found" }
141
- mock_update_env .assert_called_once_with (
142
- "123" , Env (latitude = 1 , longitude = 1 )
143
- )
144
155
145
156
146
- def test_update_env_server_error ():
157
+ def test_update_env_server_error (stub_env ):
147
158
with patch .object (
148
159
EnvController ,
149
160
"update_env_by_id" ,
150
161
side_effect = Exception ("error" ),
151
162
):
152
163
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 )
156
165
assert response .status_code == 500
157
166
assert response .json () == {
158
167
"detail" : "Failed to update environment: error"
@@ -205,19 +214,13 @@ def test_delete_env_server_error():
205
214
206
215
def test_simulate_env (stub_env_summary ):
207
216
with patch .object (
208
- EnvController , "simulate_env" , return_value = stub_env_summary
217
+ EnvController ,
218
+ "simulate_env" ,
219
+ return_value = EnvSummary (** stub_env_summary ),
209
220
) as mock_simulate_env :
210
221
response = client .get ("/environments/123/summary" )
211
222
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
221
224
mock_simulate_env .assert_called_once_with ("123" )
222
225
223
226
0 commit comments