@@ -62,3 +62,138 @@ def test_finished_notification(
6262 headers = {"x-api-key" : internal_api_key_secret },
6363 )
6464 mock_email_sender .send_email .assert_called_once ()
65+
66+
67+ def test_date_started_set_on_pulled_status (
68+ client : TestClient ,
69+ db_session : Session ,
70+ internal_api_key_secret : str ,
71+ jobs : list [Job ],
72+ ) -> None :
73+ """Test that date_started is set when job status changes to 'pulled'."""
74+ job = jobs [0 ]
75+ assert job .date_started is None # Initially no start date
76+
77+ response = client .put (
78+ ENDPOINT ,
79+ json = {"job_id" : job .id , "status" : "pulled" },
80+ headers = {"x-api-key" : internal_api_key_secret },
81+ )
82+ assert response .status_code == 200
83+
84+ # Refresh job from database
85+ db_session .refresh (job )
86+ assert job .status == "pulled"
87+ assert job .date_started is not None # Should now have a start date
88+ assert job .date_finished is None # Should not have finished date yet
89+
90+
91+ def test_date_started_not_overwritten (
92+ client : TestClient ,
93+ db_session : Session ,
94+ internal_api_key_secret : str ,
95+ jobs : list [Job ],
96+ ) -> None :
97+ """Test that date_started is not overwritten if already set."""
98+ job = jobs [0 ]
99+
100+ # First update to 'pulled' to set date_started
101+ client .put (
102+ ENDPOINT ,
103+ json = {"job_id" : job .id , "status" : "pulled" },
104+ headers = {"x-api-key" : internal_api_key_secret },
105+ )
106+ db_session .refresh (job )
107+ original_start_date = job .date_started
108+ assert original_start_date is not None
109+
110+ # Update to another status and then back to pulled
111+ client .put (
112+ ENDPOINT ,
113+ json = {"job_id" : job .id , "status" : "running" },
114+ headers = {"x-api-key" : internal_api_key_secret },
115+ )
116+ client .put (
117+ ENDPOINT ,
118+ json = {"job_id" : job .id , "status" : "pulled" },
119+ headers = {"x-api-key" : internal_api_key_secret },
120+ )
121+
122+ db_session .refresh (job )
123+ assert job .date_started == original_start_date # Should not change
124+
125+
126+ def test_date_finished_set_on_finished_status (
127+ client : TestClient ,
128+ db_session : Session ,
129+ internal_api_key_secret : str ,
130+ jobs : list [Job ],
131+ ) -> None :
132+ """Test that date_finished is set when job status changes to 'finished'."""
133+ job = jobs [0 ]
134+ assert job .date_finished is None # Initially no finish date
135+
136+ response = client .put (
137+ ENDPOINT ,
138+ json = {"job_id" : job .id , "status" : "finished" },
139+ headers = {"x-api-key" : internal_api_key_secret },
140+ )
141+ assert response .status_code == 200
142+
143+ # Refresh job from database
144+ db_session .refresh (job )
145+ assert job .status == "finished"
146+ assert job .date_finished is not None # Should now have a finish date
147+
148+
149+ def test_date_finished_set_on_error_status (
150+ client : TestClient ,
151+ db_session : Session ,
152+ internal_api_key_secret : str ,
153+ jobs : list [Job ],
154+ ) -> None :
155+ """Test that date_finished is set when job status changes to 'error'."""
156+ job = jobs [0 ]
157+ assert job .date_finished is None # Initially no finish date
158+
159+ response = client .put (
160+ ENDPOINT ,
161+ json = {"job_id" : job .id , "status" : "error" },
162+ headers = {"x-api-key" : internal_api_key_secret },
163+ )
164+ assert response .status_code == 200
165+
166+ # Refresh job from database
167+ db_session .refresh (job )
168+ assert job .status == "error"
169+ assert job .date_finished is not None # Should now have a finish date
170+
171+
172+ def test_date_finished_not_overwritten (
173+ client : TestClient ,
174+ db_session : Session ,
175+ internal_api_key_secret : str ,
176+ jobs : list [Job ],
177+ ) -> None :
178+ """Test that date_finished is not overwritten if already set."""
179+ job = jobs [0 ]
180+
181+ # First update to 'finished' to set date_finished
182+ client .put (
183+ ENDPOINT ,
184+ json = {"job_id" : job .id , "status" : "finished" },
185+ headers = {"x-api-key" : internal_api_key_secret },
186+ )
187+ db_session .refresh (job )
188+ original_finish_date = job .date_finished
189+ assert original_finish_date is not None
190+
191+ # Update to error status (should not overwrite date_finished)
192+ client .put (
193+ ENDPOINT ,
194+ json = {"job_id" : job .id , "status" : "error" },
195+ headers = {"x-api-key" : internal_api_key_secret },
196+ )
197+
198+ db_session .refresh (job )
199+ assert job .date_finished == original_finish_date # Should not change
0 commit comments