| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import sys
- import os
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- import numpy as np
- import pytest
- from event_pusher import EventPusher
- def test_upload_numpy_image(monkeypatch):
- config = {"device_id": "test-device", "base_url": "http://localhost"}
- pusher = EventPusher(config)
- called = {"path": None, "existed": False}
- def fake_upload(path):
- called["path"] = path
- called["existed"] = os.path.exists(path)
- return "http://example.com/image.jpg"
- monkeypatch.setattr(pusher, "_upload_image", fake_upload)
- img = np.zeros((100, 100, 3), dtype=np.uint8)
- url = pusher.upload_numpy_image(img)
- assert url == "http://example.com/image.jpg"
- assert called["path"] is not None
- assert called["existed"]
- def test_push_tracking_capture(monkeypatch):
- config = {"device_id": "test-device", "base_url": "http://localhost"}
- pusher = EventPusher(config)
- captured = {}
- def fake_post(url, json):
- captured["url"] = url
- captured["json"] = json
- class Resp:
- status_code = 200
- return Resp()
- monkeypatch.setattr(pusher, "_post", fake_post)
- pusher.push_tracking_capture(
- batch_time=1234567890.0,
- captures=[{"track_id": 1, "ptz_image_url": "url1"}]
- )
- assert captured["url"] == "http://localhost/api/system/event"
- assert captured["json"]["eventType"] == "TRACKING_CAPTURE"
- assert captured["json"]["data"]["captureCount"] == 1
- def test_upload_numpy_image_cleans_temp_file_on_success(monkeypatch):
- config = {"device_id": "test-device", "base_url": "http://localhost"}
- pusher = EventPusher(config)
- temp_paths = []
- def fake_upload(path):
- temp_paths.append(path)
- assert os.path.exists(path)
- return "http://example.com/image.jpg"
- monkeypatch.setattr(pusher, "_upload_image", fake_upload)
- img = np.zeros((100, 100, 3), dtype=np.uint8)
- url = pusher.upload_numpy_image(img)
- assert url == "http://example.com/image.jpg"
- assert len(temp_paths) == 1
- assert not os.path.exists(temp_paths[0])
- def test_upload_numpy_image_cleans_temp_file_on_upload_failure(monkeypatch):
- config = {"device_id": "test-device", "base_url": "http://localhost"}
- pusher = EventPusher(config)
- temp_paths = []
- def fake_upload(path):
- temp_paths.append(path)
- assert os.path.exists(path)
- raise RuntimeError("upload failed")
- monkeypatch.setattr(pusher, "_upload_image", fake_upload)
- img = np.zeros((100, 100, 3), dtype=np.uint8)
- url = pusher.upload_numpy_image(img)
- assert url is None
- assert len(temp_paths) == 1
- assert not os.path.exists(temp_paths[0])
|