@@ -83,6 +83,71 @@ def test_backfill_datasets(mock_get, mock_storage_client):
8383 mock_session .commit .assert_called_once ()
8484
8585
86+ @patch ("google.cloud.storage.Client" , autospec = True )
87+ @patch ("requests.get" )
88+ def test_backfill_datasets_service_date_range_swap (mock_get , mock_storage_client ):
89+ # Mock the storage client and bucket
90+ mock_bucket = MagicMock ()
91+ mock_client_instance = mock_storage_client .return_value
92+ mock_client_instance .bucket .return_value = mock_bucket
93+ mock_blob = MagicMock ()
94+ mock_blob .exists .return_value = False
95+ mock_bucket .blob .return_value = mock_blob
96+
97+ mock_session = MagicMock ()
98+ mock_dataset = Mock (spec = Gtfsdataset )
99+ mock_dataset .id = 1
100+ mock_dataset .stable_id = "mdb-392-202406181921"
101+ mock_dataset .service_date_range_end = None
102+ mock_dataset .service_date_range_start = None
103+ mock_dataset .validation_reports = [
104+ MagicMock (
105+ validator_version = "6.0.0" ,
106+ validated_at = "2022-01-01T00:00:00Z" ,
107+ json_report = "http://example-2.com/report.json" ,
108+ ),
109+ MagicMock (
110+ validator_version = "5.0.0" ,
111+ validated_at = "2023-01-01T00:00:00Z" ,
112+ json_report = "http://example-3.com/report.json" ,
113+ ),
114+ MagicMock (
115+ validator_version = "6.0.0" ,
116+ validated_at = "2024-01-01T00:00:00Z" ,
117+ json_report = "http://example-1.com/report.json" ,
118+ ),
119+ ]
120+
121+ mock_query = MagicMock ()
122+ mock_query .options .return_value = mock_query
123+ mock_query .filter .return_value = mock_query
124+ mock_query .all .return_value = [mock_dataset ]
125+ mock_session .query .return_value = mock_query
126+
127+ # Mock the requests.get response
128+ mock_response = Mock ()
129+ mock_response .status_code = 200
130+ mock_response .json .return_value = {
131+ "summary" : {
132+ "feedInfo" : {
133+ "feedServiceWindowStart" : "2023-12-31" ,
134+ "feedServiceWindowEnd" : "2023-01-01" ,
135+ }
136+ }
137+ }
138+ mock_get .return_value = mock_response
139+
140+ changes_count = backfill_datasets (mock_session )
141+
142+ assert changes_count == 1
143+ assert mock_dataset .service_date_range_start == "2023-01-01"
144+ assert mock_dataset .service_date_range_end == "2023-12-31"
145+ mock_get .assert_called_once_with (
146+ "http://example-1.com/report.json"
147+ ) # latest validation report
148+ mock_session .commit .assert_called_once ()
149+
150+
86151@patch ("logging.error" , autospec = True )
87152@patch ("google.cloud.storage.Client" , autospec = True )
88153@patch ("requests.get" )
0 commit comments