test_pipeline.py
9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
"""Tests for pipeline_api.py — Feishu pipeline functions.
Tests mock the HTTP layer (_curl_get, _curl_put, _curl_post) to avoid real API calls.
"""
import json
import os
import sys
import unittest
from unittest.mock import patch, MagicMock
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from pipeline_api import (
load_config, get_pending_tasks, claim_task,
set_status, create_bug, get_child_bugs
)
class TestLoadConfig(unittest.TestCase):
"""Test config loading and auth header generation."""
@patch("pipeline_api.os.path.exists", return_value=True)
@patch("builtins.open", new_callable=unittest.mock.mock_open)
def test_load_config_returns_tuple(self, mock_open, mock_exists):
"""load_config should return (AH, AT, RT, BT) tuple."""
mock_open.return_value.read.return_value = json.dumps({
"a": "app_token_123",
"r": "req_table_456",
"b": "bug_table_789",
"t": "token_abc"
})
AH, AT, RT, BT = load_config()
self.assertIn("Bearer", AH)
self.assertIn("token_abc", AH)
self.assertEqual(AT, "app_token_123")
self.assertEqual(RT, "req_table_456")
self.assertEqual(BT, "bug_table_789")
@patch("pipeline_api.os.path.exists", return_value=True)
@patch("builtins.open", new_callable=unittest.mock.mock_open)
def test_auth_header_constructed_at_runtime(self, mock_open, mock_exists):
"""Auth header must be string-concatenated, not hardcoded (cron-safe)."""
mock_open.return_value.read.return_value = json.dumps({
"a": "AT", "r": "RT", "b": "BT", "t": "tk"
})
AH, _, _, _ = load_config()
# Must contain the Bearer prefix
self.assertTrue(AH.startswith("Aut") or "Bearer" in AH)
self.assertIn("tk", AH)
class TestGetPendingTasks(unittest.TestCase):
"""Test pending task queries."""
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_get")
def test_get_pending_tasks_requirements(self, mock_get, mock_config):
"""get_pending_tasks('r') should filter by 待办 and 待测."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {
"data": {
"items": [
{"fields": {"需求标题": "Task1", "优先级": "P1-高", "状态": "待办"}},
{"fields": {"需求标题": "Task2", "优先级": "P3-低", "状态": "待测"}},
{"fields": {"需求标题": "Task3", "优先级": "P0-紧急", "状态": "待办"}},
]
}
}
tasks = get_pending_tasks('r')
self.assertEqual(len(tasks), 3)
# Sorted by priority: P0 first, P1 second, P3 last
self.assertEqual(tasks[0]["fields"]["优先级"], "P0-紧急")
self.assertEqual(tasks[2]["fields"]["优先级"], "P3-低")
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_get")
def test_get_pending_tasks_bugs(self, mock_get, mock_config):
"""get_pending_tasks('b') should filter by 待修复 and 待测."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {"data": {"items": []}}
tasks = get_pending_tasks('b')
self.assertEqual(len(tasks), 0)
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_get")
def test_get_pending_tasks_null_items(self, mock_get, mock_config):
"""Handle `items: null` (known Feishu quirk)."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {"data": {"items": None}}
tasks = get_pending_tasks('r')
self.assertEqual(tasks, [])
class TestClaimTask(unittest.TestCase):
"""Test race-condition-protected task claiming."""
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_put")
@patch("pipeline_api._curl_get")
def test_claim_success(self, mock_get, mock_put, mock_config):
"""Claim a 待办 task should succeed."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {
"data": {"record": {"fields": {"状态": "待办"}}}
}
mock_put.return_value = {"code": 0}
result = claim_task("rec123", 'r')
self.assertTrue(result)
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_get")
def test_claim_already_taken(self, mock_get, mock_config):
"""Claim should fail if already in 开发中."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {
"data": {"record": {"fields": {"状态": "开发中"}}}
}
result = claim_task("rec123", 'r')
self.assertFalse(result)
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_put")
@patch("pipeline_api._curl_get")
def test_claim_bug_success(self, mock_get, mock_put, mock_config):
"""Claim a 待修复 bug should succeed."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {
"data": {"record": {"fields": {"状态": "待修复"}}}
}
mock_put.return_value = {"code": 0}
result = claim_task("rec456", 'b')
self.assertTrue(result)
class TestSetStatus(unittest.TestCase):
"""Test status transitions."""
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_put")
def test_set_status_to_self_test(self, mock_put, mock_config):
"""set_status should PUT with correct fields."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_put.return_value = {"code": 0}
result = set_status("rec123", 'r', "自测中")
self.assertEqual(result["code"], 0)
# Verify PUT was called
self.assertTrue(mock_put.called)
# The PUT data should contain the status field
call_args = mock_put.call_args[0]
put_body = call_args[1] # second positional arg is the JSON body
parsed = json.loads(put_body)
self.assertEqual(parsed["fields"]["状态"], "自测中")
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_put")
def test_set_status_bug_table(self, mock_put, mock_config):
"""Bug table uses BT instead of RT."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_put.return_value = {"code": 0}
result = set_status("rec789", 'b', "待测")
self.assertEqual(result["code"], 0)
class TestCreateBug(unittest.TestCase):
"""Test bug creation with parent linkage."""
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_post")
def test_create_bug_success(self, mock_post, mock_config):
"""create_bug should POST with all fields and return record_id."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_post.return_value = {
"code": 0,
"data": {"record": {"record_id": "new_bug_001"}}
}
bug_id = create_bug(
"parent_123",
"Test Bug",
"### Why\nTest\n### What\nTest\n### Acceptance\nPass",
"一般", "P2-中"
)
self.assertEqual(bug_id, "new_bug_001")
# Verify POST data contains parent record
call_data = mock_post.call_args[0]
data_str = json.dumps(call_data[1]) if len(call_data) > 1 else str(call_data)
self.assertIn("parent_123", data_str)
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_post")
def test_create_bug_failure(self, mock_post, mock_config):
"""create_bug should return None on API failure."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_post.return_value = {"code": 999, "msg": "error"}
bug_id = create_bug("parent_123", "Test", "Desc", "一般", "P2-中")
self.assertIsNone(bug_id)
class TestGetChildBugs(unittest.TestCase):
"""Test child bug lookup by parent record ID."""
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_get")
def test_get_child_bugs_finds_match(self, mock_get, mock_config):
"""Should return bugs linked to the given parent."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {
"data": {
"items": [
{
"record_id": "bug1",
"fields": {
"Bug标题": "Child Bug",
"状态": "待修复",
"父记录": [{"record_ids": ["parent_123"]}]
}
},
{
"record_id": "bug2",
"fields": {
"Bug标题": "Unrelated Bug",
"状态": "已取消",
"父记录": [{"record_ids": ["other_456"]}]
}
}
]
}
}
bugs = get_child_bugs("parent_123")
self.assertEqual(len(bugs), 1)
self.assertEqual(bugs[0]["record_id"], "bug1")
@patch("pipeline_api.load_config")
@patch("pipeline_api._curl_get")
def test_get_child_bugs_none_found(self, mock_get, mock_config):
"""Should return empty list when no children."""
mock_config.return_value = ("AH", "AT", "RT", "BT")
mock_get.return_value = {"data": {"items": []}}
bugs = get_child_bugs("nonexistent")
self.assertEqual(bugs, [])
if __name__ == "__main__":
unittest.main(verbosity=2)