1717 update_s3_object ,
1818 get_dict_value ,
1919 get_config_file ,
20+
2021)
2122
23+ # Mock API response
24+ api_response = {
25+ "download_links" : [
26+ "https://example.com/organisation_history_api_response.json"
27+ ]
28+ # There are other fields in the API response, but we don't need them for this test
29+ }
30+
31+ # Mock usage data returned from GitHub API
32+ fetched_usage_data = {"day_totals" : [
33+ {"day" : "2024-01-01" , "usage" : 10 },
34+ {"day" : "2024-01-02" , "usage" : 20 },
35+ ]}
36+
2237
2338class TestUpdateS3Object :
2439 def test_update_s3_object_success (self , caplog ):
@@ -124,20 +139,6 @@ def teardown_method(self):
124139 def test_get_and_update_historic_usage_success (self ):
125140 s3 = MagicMock ()
126141 gh = MagicMock ()
127-
128- # Mock API response
129- api_response = {
130- "download_links" : [
131- "https://example.com/organisation_history_api_response.json"
132- ]
133- # There are other fields in the API response, but we don't need them for this test
134- }
135-
136- # Mock usage data returned from GitHub API
137- fetched_usage_data = {"day_totals" : [
138- {"day" : "2024-01-01" , "usage" : 10 },
139- {"day" : "2024-01-02" , "usage" : 20 },
140- ]}
141142
142143 gh .get .return_value .json .return_value = api_response
143144
@@ -150,8 +151,8 @@ def test_get_and_update_historic_usage_success(self):
150151 # Mock requests.get returns usage data from download_links
151152 # We always patch dependencies imported inside the function we're testing.
152153 # Test environment initialisation ends here.
153- with patch ("src.main.requests. get" ) as mock_requests_get :
154- mock_requests_get .return_value .json .return_value = fetched_usage_data
154+ with patch ("src.main.get" ) as mock_get :
155+ mock_get .return_value .json .return_value = fetched_usage_data
155156 result , dates_added = get_and_update_historic_usage (s3 , gh , False )
156157
157158 assert result == [
@@ -169,14 +170,6 @@ def test_get_and_update_historic_usage_success(self):
169170 def test_get_and_update_historic_usage_no_existing_data (self , caplog ):
170171 s3 = MagicMock ()
171172 gh = MagicMock ()
172- api_response = {
173- "download_links" : [
174- "https://example.com/organisation_history_api_response.json"
175- ]
176- }
177- fetched_usage_data = {"day_totals" : [
178- {"day" : "2024-01-01" , "usage" : 10 },
179- ]}
180173
181174 gh .get .return_value .json .return_value = api_response
182175
@@ -186,12 +179,12 @@ def test_get_and_update_historic_usage_no_existing_data(self, caplog):
186179 operation_name = "GetObject" ,
187180 )
188181
189- with patch ("src.main.requests. get" ) as mock_requests_get :
190- mock_requests_get .return_value .json .return_value = fetched_usage_data
182+ with patch ("src.main.get" ) as mock_get :
183+ mock_get .return_value .json .return_value = fetched_usage_data
191184 result , dates_added = get_and_update_historic_usage (s3 , gh , False )
192185
193- assert result == [{"day" : "2024-01-01" , "usage" : 10 }]
194- assert dates_added == ["2024-01-01" ]
186+ assert result == [{"day" : "2024-01-01" , "usage" : 10 }, { "day" : "2024-01-02" , "usage" : 20 } ]
187+ assert dates_added == ["2024-01-01" , "2024-01-02" ]
195188 s3 .put_object .assert_called_once ()
196189 assert any (
197190 "Error getting organisation_history.json" in record .getMessage ()
@@ -201,41 +194,25 @@ def test_get_and_update_historic_usage_no_existing_data(self, caplog):
201194 def test_get_and_update_historic_usage_no_new_dates (self ):
202195 s3 = MagicMock ()
203196 gh = MagicMock ()
204- api_response = {
205- "download_links" : [
206- "https://example.com/organisation_history_api_response.json"
207- ]
208- }
209- fetched_usage_data = {"day_totals" : [
210- {"day" : "2024-01-01" , "usage" : 10 },
211- ]}
212197
213198 gh .get .return_value .json .return_value = api_response
214199
215200 # S3 get_object returns same date as usage_data
216- existing_usage = [{"day" : "2024-01-01" , "usage" : 10 }]
201+ existing_usage = [{"day" : "2024-01-01" , "usage" : 10 }, { "day" : "2024-01-02" , "usage" : 20 } ]
217202 s3 .get_object .return_value = {
218203 "Body" : BytesIO (json .dumps (existing_usage ).encode ("utf-8" ))
219204 }
220- with patch ("src.main.requests. get" ) as mock_requests_get :
221- mock_requests_get .return_value .json .return_value = fetched_usage_data
205+ with patch ("src.main.get" ) as mock_get :
206+ mock_get .return_value .json .return_value = fetched_usage_data
222207 result , dates_added = get_and_update_historic_usage (s3 , gh , False )
223208
224- assert result == [{"day" : "2024-01-01" , "usage" : 10 }]
209+ assert result == [{"day" : "2024-01-01" , "usage" : 10 }, { "day" : "2024-01-02" , "usage" : 20 } ]
225210 assert dates_added == []
226211 s3 .put_object .assert_called_once ()
227212
228213 def test_write_data_locally_creates_file (self , tmp_path ):
229214 s3 = MagicMock ()
230215 gh = MagicMock ()
231- api_response = {
232- "download_links" : [
233- "https://example.com/organisation_history_api_response.json"
234- ]
235- }
236- fetched_usage_data = {"day_totals" : [
237- {"day" : "2024-01-01" , "usage" : 10 },
238- ]}
239216
240217 gh .get .return_value .json .return_value = api_response
241218
@@ -248,11 +225,11 @@ def test_write_data_locally_creates_file(self, tmp_path):
248225 # Patch os.makedirs and open to use tmp_path
249226 with patch ("src.main.os.makedirs" ) as mock_makedirs , \
250227 patch ("src.main.open" , create = True ) as mock_open , \
251- patch ("src.main.requests. get" ) as mock_requests_get :
252- mock_requests_get .return_value .json .return_value = fetched_usage_data
228+ patch ("src.main.get" ) as mock_get :
229+ mock_get .return_value .json .return_value = fetched_usage_data
253230 result , dates_added = get_and_update_historic_usage (s3 , gh , True )
254- assert result == [{"day" : "2024-01-01" , "usage" : 10 }]
255- assert dates_added == ["2024-01-01" ]
231+ assert result == [{"day" : "2024-01-01" , "usage" : 10 }, { "day" : "2024-01-02" , "usage" : 20 } ]
232+ assert dates_added == ["2024-01-01" , "2024-01-02" ]
256233 mock_makedirs .assert_called_once_with ("output" , exist_ok = True )
257234 mock_open .assert_called_once ()
258235 s3 .put_object .assert_not_called ()
0 commit comments