PlanOpticon

planopticon / tests / test_callbacks.py
Blame History Raw 115 lines
1
"""Tests for video_processor.utils.callbacks.WebhookCallback."""
2
3
import json
4
from unittest.mock import patch
5
6
import pytest
7
8
from video_processor.utils.callbacks import WebhookCallback
9
10
11
@pytest.fixture()
12
def callback():
13
return WebhookCallback(url="https://example.com/webhook")
14
15
16
# --- Constructor ---
17
18
19
def test_default_headers():
20
cb = WebhookCallback(url="https://example.com/hook")
21
assert cb.headers == {"Content-Type": "application/json"}
22
23
24
def test_custom_headers():
25
headers = {"Authorization": "Bearer tok", "Content-Type": "application/json"}
26
cb = WebhookCallback(url="https://example.com/hook", headers=headers)
27
assert cb.headers["Authorization"] == "Bearer tok"
28
29
30
def test_custom_timeout():
31
cb = WebhookCallback(url="https://example.com/hook", timeout=5.0)
32
assert cb.timeout == 5.0
33
34
35
# --- _post ---
36
37
38
@patch("urllib.request.urlopen")
39
@patch("urllib.request.Request")
40
def test_post_sends_json_payload(mock_request_cls, mock_urlopen, callback):
41
callback._post({"event": "test"})
42
43
mock_request_cls.assert_called_once()
44
call_args = mock_request_cls.call_args
45
data = json.loads(call_args[1]["data"] if "data" in call_args[1] else call_args[0][1])
46
assert data["event"] == "test"
47
mock_urlopen.assert_called_once()
48
49
50
@patch("urllib.request.urlopen", side_effect=Exception("Connection refused"))
51
@patch("urllib.request.Request")
52
def test_post_logs_failure_does_not_raise(mock_request_cls, mock_urlopen, callback):
53
# Should not raise
54
callback._post({"event": "fail_test"})
55
56
57
# --- on_step_start ---
58
59
60
@patch.object(WebhookCallback, "_post")
61
def test_on_step_start_payload(mock_post, callback):
62
callback.on_step_start("transcription", 1, 5)
63
64
mock_post.assert_called_once_with(
65
{
66
"event": "step_start",
67
"step": "transcription",
68
"index": 1,
69
"total": 5,
70
}
71
)
72
73
74
# --- on_step_complete ---
75
76
77
@patch.object(WebhookCallback, "_post")
78
def test_on_step_complete_payload(mock_post, callback):
79
callback.on_step_complete("analysis", 3, 5)
80
81
mock_post.assert_called_once_with(
82
{
83
"event": "step_complete",
84
"step": "analysis",
85
"index": 3,
86
"total": 5,
87
}
88
)
89
90
91
# --- on_progress ---
92
93
94
@patch.object(WebhookCallback, "_post")
95
def test_on_progress_payload(mock_post, callback):
96
callback.on_progress("transcription", 42.5, "Processing chunk 3/7")
97
98
mock_post.assert_called_once_with(
99
{
100
"event": "progress",
101
"step": "transcription",
102
"percent": 42.5,
103
"message": "Processing chunk 3/7",
104
}
105
)
106
107
108
@patch.object(WebhookCallback, "_post")
109
def test_on_progress_default_message(mock_post, callback):
110
callback.on_progress("extraction", 100.0)
111
112
payload = mock_post.call_args[0][0]
113
assert payload["message"] == ""
114
assert payload["percent"] == 100.0
115

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button