|
1 | | -from datetime import datetime |
2 | 1 | from typing import Any, Dict, List, Union |
3 | 2 |
|
4 | 3 | import pytest |
5 | | -from fastapi import status # Import status |
6 | 4 | from httpx import AsyncClient |
7 | 5 | from sqlmodel.ext.asyncio.session import AsyncSession |
8 | 6 |
|
9 | | -from app.models import Alert, AlertSequence, Camera, Organization, Pose, Sequence |
10 | | -from app.services.storage import s3_service |
11 | | - |
12 | 7 |
|
13 | 8 | @pytest.mark.parametrize( |
14 | 9 | ("user_idx", "payload", "status_code", "status_detail"), |
@@ -169,125 +164,6 @@ async def test_delete_organization( |
169 | 164 | assert response.json() is None |
170 | 165 |
|
171 | 166 |
|
172 | | -@pytest.mark.asyncio |
173 | | -async def test_delete_organization_with_alerts_and_sequences( |
174 | | - async_client: AsyncClient, |
175 | | - async_session: AsyncSession, |
176 | | -): |
177 | | - # Create a separate dummy organization for the dummy camera |
178 | | - dummy_cam_organization = Organization(id=98, name="dummy-cam-org", telegram_id=None, slack_hook=None) |
179 | | - async_session.add(dummy_cam_organization) |
180 | | - await async_session.commit() |
181 | | - await async_session.refresh(dummy_cam_organization) |
182 | | - |
183 | | - # Create dummy camera and pose for the sequence and alert, linked to the dummy_cam_organization |
184 | | - dummy_camera = Camera( |
185 | | - id=998, |
186 | | - organization_id=dummy_cam_organization.id, |
187 | | - name="dummy-cam", |
188 | | - angle_of_view=1.0, |
189 | | - elevation=1.0, |
190 | | - lat=1.0, |
191 | | - lon=1.0, |
192 | | - is_trustable=True, |
193 | | - last_active_at=datetime.utcnow(), |
194 | | - created_at=datetime.utcnow(), |
195 | | - ) |
196 | | - async_session.add(dummy_camera) |
197 | | - await async_session.commit() |
198 | | - await async_session.refresh(dummy_camera) |
199 | | - |
200 | | - dummy_pose = Pose( |
201 | | - id=997, |
202 | | - camera_id=dummy_camera.id, |
203 | | - azimuth=1.0, |
204 | | - patrol_id=1, |
205 | | - ) |
206 | | - async_session.add(dummy_pose) |
207 | | - await async_session.commit() |
208 | | - await async_session.refresh(dummy_pose) |
209 | | - |
210 | | - # Create a dummy sequence as AlertSequence needs a sequence_id |
211 | | - dummy_sequence = Sequence( |
212 | | - id=999, |
213 | | - camera_id=dummy_camera.id, |
214 | | - pose_id=dummy_pose.id, |
215 | | - camera_azimuth=1.0, |
216 | | - is_wildfire="wildfire_smoke", |
217 | | - sequence_azimuth=1.0, |
218 | | - cone_angle=1.0, |
219 | | - started_at=datetime.utcnow(), |
220 | | - last_seen_at=datetime.utcnow(), |
221 | | - ) |
222 | | - async_session.add(dummy_sequence) |
223 | | - await async_session.commit() |
224 | | - await async_session.refresh(dummy_sequence) |
225 | | - |
226 | | - # 1. Create the new organization to be deleted |
227 | | - new_organization = Organization(id=99, name="temp-organization-with-alerts", telegram_id=None, slack_hook=None) |
228 | | - async_session.add(new_organization) |
229 | | - await async_session.commit() |
230 | | - await async_session.refresh(new_organization) |
231 | | - |
232 | | - # Create S3 bucket for the new organization |
233 | | - s3_service.create_bucket(s3_service.resolve_bucket_name(new_organization.id)) |
234 | | - |
235 | | - # 2. Create an alert associated with the new organization |
236 | | - new_alert = Alert( |
237 | | - id=9999, |
238 | | - organization_id=new_organization.id, # This alert is linked to the organization to be deleted |
239 | | - event_at=datetime.utcnow(), |
240 | | - score=0.9, |
241 | | - latitude=10.0, |
242 | | - longitude=20.0, |
243 | | - camera_id=dummy_camera.id, # Link to the dummy camera (not to be deleted) |
244 | | - sensor_id=1, |
245 | | - started_at=datetime.utcnow(), |
246 | | - last_seen_at=datetime.utcnow(), |
247 | | - ) |
248 | | - async_session.add(new_alert) |
249 | | - await async_session.commit() |
250 | | - await async_session.refresh(new_alert) |
251 | | - |
252 | | - # 3. Create an alert sequence associated with the new alert |
253 | | - new_alert_sequence = AlertSequence( |
254 | | - alert_id=new_alert.id, |
255 | | - sequence_id=dummy_sequence.id, # Link to the dummy sequence (not to be deleted) |
256 | | - ) |
257 | | - async_session.add(new_alert_sequence) |
258 | | - await async_session.commit() |
259 | | - await async_session.refresh(new_alert_sequence) |
260 | | - |
261 | | - # Get an admin token for the organization |
262 | | - auth = pytest.get_token(pytest.user_table[0]["id"], pytest.user_table[0]["role"].split(), new_organization.id) |
263 | | - |
264 | | - # 4. Delete the organization |
265 | | - response = await async_client.delete(f"/organizations/{new_organization.id}", headers=auth) |
266 | | - assert response.status_code == 200, response.text |
267 | | - assert response.json() is None |
268 | | - |
269 | | - # 5. Verify that the organization is deleted from the API's perspective |
270 | | - get_response = await async_client.get(f"/organizations/{new_organization.id}", headers=auth) |
271 | | - assert get_response.status_code == status.HTTP_404_NOT_FOUND, get_response.text |
272 | | - |
273 | | - # Verify that the S3 bucket is also deleted |
274 | | - with pytest.raises(ValueError, match="unable to access bucket"): |
275 | | - s3_service.get_bucket(s3_service.resolve_bucket_name(new_organization.id)) |
276 | | - |
277 | | - # Assert that the dummy camera, pose, sequence, and their organization are NOT deleted |
278 | | - dummy_cam_org_in_db = await async_session.get(Organization, dummy_cam_organization.id) |
279 | | - assert dummy_cam_org_in_db is not None |
280 | | - |
281 | | - dummy_camera_in_db = await async_session.get(Camera, dummy_camera.id) |
282 | | - assert dummy_camera_in_db is not None |
283 | | - |
284 | | - dummy_pose_in_db = await async_session.get(Pose, dummy_pose.id) |
285 | | - assert dummy_pose_in_db is not None |
286 | | - |
287 | | - dummy_sequence_in_db = await async_session.get(Sequence, dummy_sequence.id) |
288 | | - assert dummy_sequence_in_db is not None |
289 | | - |
290 | | - |
291 | 167 | @pytest.mark.parametrize( |
292 | 168 | ("user_idx", "organization_id", "payload", "status_code", "status_detail"), |
293 | 169 | [ |
|
0 commit comments