droboticflow/tests/backend/wjzh_operator_regression_test.py
2026-06-23 13:02:36 +08:00

298 lines
12 KiB
Python

import importlib.util
import json
import pathlib
import sys
import unittest
from unittest import mock
ROOT = pathlib.Path(__file__).resolve().parents[2]
BASE_PATH = ROOT / "scripts" / "wjzh_operator_regression.py"
MULTI_PATH = ROOT / "scripts" / "wjzh_operator_multi_project_regression.py"
BASE_SPEC = importlib.util.spec_from_file_location("wjzh_operator_regression", BASE_PATH)
BASE = importlib.util.module_from_spec(BASE_SPEC)
assert BASE_SPEC.loader is not None
sys.modules[BASE_SPEC.name] = BASE
BASE_SPEC.loader.exec_module(BASE)
MULTI_SPEC = importlib.util.spec_from_file_location("wjzh_operator_multi_project_regression", MULTI_PATH)
MULTI = importlib.util.module_from_spec(MULTI_SPEC)
assert MULTI_SPEC.loader is not None
sys.modules[MULTI_SPEC.name] = MULTI
MULTI_SPEC.loader.exec_module(MULTI)
VALID_RULES_JSON = json.dumps(
[
{
"id": "clarity-gate",
"field": "quality.clarity",
"operator": "gte",
"value": 0.3,
"samplePercent": 100,
}
]
)
def valid_config(**overrides):
values = {
"api_base_url": "http://api.example.test",
"api_token": "token",
"acceptance_email": "",
"acceptance_password": "",
"source_path": "bos://bucket/input",
"upload_root_prefix": "bos://bucket/output",
"project_prefix": "真实数据算子回归",
"storage_connection_id": "storage-1",
"storage_connection_name": "",
"create_connection_if_missing": False,
"mongo_connection_id": "",
"mongo_collection_prefix": "wjzh_operator_regression",
"bos_endpoint": "",
"bos_bucket": "",
"bos_access_key_id": "",
"bos_secret_access_key": "",
"delete_all_projects": False,
"delete_regression_projects": False,
"preflight_only": True,
"skip_runtime_acceptance": False,
"max_wait_seconds": 60,
"audit_video": "audit.mp4",
"medium_video": "medium.mp4",
"huge_video": "huge.mp4",
"huge_window_seconds": 120,
"route_device_id": "device-1",
"filter_date_start": "2026-06-01",
"filter_date_end": "2026-06-16",
"lerobot_source_path": "/workspace/datasets/lerobot",
"rosbag_source_path": "/workspace/datasets/sample.bag",
"incremental_place": "office",
"incremental_camera_model": "camera-x",
"incremental_video_subdir": "videos",
"sampling_rules_json": BASE.normalize_sampling_rules_json(VALID_RULES_JSON),
"quality_min_clarity": "0.35",
"quality_max_shake": "0.2",
"quality_max_hand_occlusion": "0.25",
"medium_window_seconds": "45",
"medium_segment_duration_seconds": "45",
"huge_segment_duration_seconds": "120",
"batch_window_seconds": "45",
"batch_quality_window_seconds": "90",
"batch_segment_duration_seconds": "30",
"incremental_window_seconds": "30",
"incremental_segment_duration_seconds": "30",
"annotation_frame_interval_seconds": "1.0",
}
values.update(overrides)
return BASE.Config(**values)
def operator(name):
return {
"name": name,
"category": "ACCEPTANCE",
"description": name,
"inputs": [],
"outputs": [{"id": "out-1", "name": "out", "type": "json"}],
"hyperparams": [],
}
class WJZHOperatorRegressionTest(unittest.TestCase):
def test_validate_config_requires_explicit_sampling_rules(self):
config = valid_config(sampling_rules_json="")
with self.assertRaisesRegex(BASE.RegressionError, "SAMPLING_RULES_JSON is required"):
BASE.validate_config(config)
def test_validate_config_requires_explicit_quality_thresholds(self):
config = valid_config(quality_min_clarity="", quality_max_shake="-1")
with self.assertRaisesRegex(BASE.RegressionError, "QUALITY_MIN_CLARITY"):
BASE.validate_config(config)
with self.assertRaisesRegex(BASE.RegressionError, "QUALITY_MAX_SHAKE"):
BASE.validate_config(config)
def test_validate_config_rejects_out_of_range_quality_thresholds(self):
config = valid_config(quality_min_clarity="1.1", quality_max_shake="2", quality_max_hand_occlusion="-0.1")
with self.assertRaisesRegex(BASE.RegressionError, "QUALITY_MIN_CLARITY must be in the range"):
BASE.validate_config(config)
with self.assertRaisesRegex(BASE.RegressionError, "QUALITY_MAX_SHAKE must be in the range"):
BASE.validate_config(config)
with self.assertRaisesRegex(BASE.RegressionError, "QUALITY_MAX_HAND_OCCLUSION must be in the range"):
BASE.validate_config(config)
def test_validate_config_rejects_non_finite_quality_and_timing_values(self):
config = valid_config(
quality_min_clarity="nan",
annotation_frame_interval_seconds="inf",
)
with self.assertRaises(BASE.RegressionError) as context:
BASE.validate_config(config)
message = str(context.exception)
self.assertIn("QUALITY_MIN_CLARITY must be finite", message)
self.assertIn("ANNOTATION_FRAME_INTERVAL_SECONDS must be finite", message)
def test_validate_config_requires_explicit_video_timing(self):
config = valid_config(batch_segment_duration_seconds="", annotation_frame_interval_seconds="0")
with self.assertRaisesRegex(BASE.RegressionError, "BATCH_SEGMENT_DURATION_SECONDS"):
BASE.validate_config(config)
with self.assertRaisesRegex(BASE.RegressionError, "ANNOTATION_FRAME_INTERVAL_SECONDS"):
BASE.validate_config(config)
def test_validate_config_rejects_lossy_integer_timing(self):
config = valid_config(medium_segment_duration_seconds="01")
with self.assertRaisesRegex(BASE.RegressionError, "MEDIUM_SEGMENT_DURATION_SECONDS must be an exact positive integer"):
BASE.validate_config(config)
def test_validate_config_rejects_too_small_max_wait(self):
config = valid_config(max_wait_seconds=30)
with self.assertRaisesRegex(BASE.RegressionError, "MAX_WAIT_SECONDS must be greater than or equal to 60"):
BASE.validate_config(config)
def test_normalize_sampling_rules_json_validates_rule_shape(self):
with self.assertRaisesRegex(BASE.RegressionError, "samplePercent"):
BASE.normalize_sampling_rules_json(
json.dumps(
[
{
"id": "broken",
"field": "quality.clarity",
"operator": "gte",
"value": 0.3,
"samplePercent": 0,
}
]
)
)
with self.assertRaisesRegex(BASE.RegressionError, "samplePercent must be finite"):
BASE.normalize_sampling_rules_json(
json.dumps(
[
{
"id": "broken",
"field": "quality.clarity",
"operator": "gte",
"value": 0.3,
"samplePercent": "nan",
}
]
)
)
with self.assertRaisesRegex(BASE.RegressionError, "samplePercent must be numeric"):
BASE.normalize_sampling_rules_json(
json.dumps(
[
{
"id": "broken",
"field": "quality.clarity",
"operator": "gte",
"value": 0.3,
"samplePercent": True,
}
]
)
)
def test_normalize_sampling_rules_json_keeps_caller_defined_rules(self):
normalized = BASE.normalize_sampling_rules_json(VALID_RULES_JSON)
self.assertEqual(json.loads(normalized), json.loads(VALID_RULES_JSON))
def test_normalize_sampling_rules_json_allows_exists_without_value(self):
normalized = BASE.normalize_sampling_rules_json(
json.dumps(
[
{
"id": "has-collection",
"field": "collectionId",
"operator": "exists",
"samplePercent": 100,
}
],
ensure_ascii=False,
)
)
self.assertEqual(json.loads(normalized)[0]["operator"], "exists")
def test_manual_sampling_workflow_uses_registered_operator_and_configured_rules(self):
config = valid_config()
operators = {
name: operator(name)
for name in (
"BOS 增量扫描",
"X 平台数据集索引",
"采集目录校验",
"Shell 命令执行",
"视频质检",
"视频切片",
"视频标注",
"成品打包",
"抽检采样",
"结果上传",
)
}
created_payloads = []
def fake_create_workflow(config_arg, project_id, name, description, nodes, edges, **kwargs):
self.assertIs(config_arg, config)
created_payloads.append(
{
"project_id": project_id,
"name": name,
"description": description,
"nodes": nodes,
"edges": edges,
"kwargs": kwargs,
}
)
return {"id": "workflow-1", "name": name, "nodes": nodes}
with mock.patch.object(MULTI.base, "create_workflow", side_effect=fake_create_workflow), mock.patch.object(
MULTI,
"refresh_workflow_runner_image",
side_effect=lambda _config, _workflow_id, _runner_image: {"id": _workflow_id},
):
MULTI.create_manual_sampling_workflow(
config,
"project-1",
operators,
"connection-1",
"bos://bucket/output",
"bos://bucket/input",
)
self.assertEqual(created_payloads[0]["name"], "11B-人工抽检采样")
sampling_node = next(node for node in created_payloads[0]["nodes"] if node["id"] == "manual_sample")
self.assertEqual(sampling_node["data"]["label"], "人工抽检采样")
rule_param = next(item for item in sampling_node["data"]["hyperparams"] if item["key"] == "RULES_JSON")
self.assertEqual(json.loads(rule_param["value"]), json.loads(config.sampling_rules_json))
quality_node = next(node for node in created_payloads[0]["nodes"] if node["id"] == "quality")
quality_params = {item["key"]: item["value"] for item in quality_node["data"]["hyperparams"]}
self.assertEqual(quality_params["MIN_CLARITY"], "0.35")
self.assertEqual(quality_params["MAX_SHAKE"], "0.2")
self.assertEqual(quality_params["MAX_HAND_OCCLUSION"], "0.25")
slice_node = next(node for node in created_payloads[0]["nodes"] if node["id"] == "slice")
slice_params = {item["key"]: item["value"] for item in slice_node["data"]["hyperparams"]}
self.assertEqual(slice_params["SEGMENT_DURATION_SECONDS"], "30")
annotate_node = next(node for node in created_payloads[0]["nodes"] if node["id"] == "annotate")
annotate_params = {item["key"]: item["value"] for item in annotate_node["data"]["hyperparams"]}
self.assertEqual(annotate_params["FRAME_INTERVAL_SECONDS"], "1.0")
if __name__ == "__main__":
unittest.main()