forked from longtao.wu/droboticflow
275 lines
10 KiB
Python
275 lines
10 KiB
Python
import importlib.util
|
|
import json
|
|
import pathlib
|
|
import sys
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
|
|
MODULE_PATH = pathlib.Path(__file__).resolve().parents[2] / "scripts" / "xplatform-live-acceptance.py"
|
|
SPEC = importlib.util.spec_from_file_location("xplatform_live_acceptance", MODULE_PATH)
|
|
MODULE = importlib.util.module_from_spec(SPEC)
|
|
assert SPEC.loader is not None
|
|
sys.modules[SPEC.name] = MODULE
|
|
SPEC.loader.exec_module(MODULE)
|
|
|
|
|
|
def valid_config(**overrides):
|
|
values = {
|
|
"api_base_url": "http://api.example.test",
|
|
"api_token": "token",
|
|
"acceptance_email": "",
|
|
"acceptance_password": "",
|
|
"source_path": "/workspace/datasets/xplatform/real-collection",
|
|
"storage_connection_id": "",
|
|
"storage_connection_name": "",
|
|
"storage_connection_bucket": "",
|
|
"upload_output_path": "",
|
|
"project_prefix": "acceptance",
|
|
"min_clarity": "0.3",
|
|
"max_shake": "0.6",
|
|
"max_hand_occlusion": "0.2",
|
|
"clarity_scale": "35",
|
|
"date_start": "",
|
|
"date_end": "",
|
|
"place": "",
|
|
"dataset_type": "",
|
|
"segment_duration_seconds": "2",
|
|
"frame_interval_seconds": "0.5",
|
|
"rules_json": '[{"id":"clarity","field":"quality.clarity","operator":"gte","value":0.3,"samplePercent":100}]',
|
|
"min_collection_count": 1,
|
|
"min_valid_collection_count": 1,
|
|
"min_video_count": 1,
|
|
"min_filtered_count": 1,
|
|
"min_clip_count": 1,
|
|
"min_annotation_count": 1,
|
|
"min_package_count": 1,
|
|
"min_sampled_count": 1,
|
|
"preflight_only": False,
|
|
"max_wait_seconds": 60,
|
|
}
|
|
values.update(overrides)
|
|
return MODULE.Config(**values)
|
|
|
|
|
|
def operator(name):
|
|
return {
|
|
"name": name,
|
|
"category": "ACCEPTANCE",
|
|
"description": name,
|
|
"inputs": [],
|
|
"outputs": [{"id": "out-1", "name": "out", "type": "json"}],
|
|
"hyperparams": [],
|
|
}
|
|
|
|
|
|
class XPlatformAcceptanceTest(unittest.TestCase):
|
|
def test_positive_integer_parser_rejects_lossy_values(self):
|
|
self.assertEqual(MODULE.parse_positive_int_value("XPLATFORM_MIN_VIDEO_COUNT", "42"), 42)
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "greater than 0"):
|
|
MODULE.parse_positive_int_value("XPLATFORM_MIN_VIDEO_COUNT", 0)
|
|
for value in ("01", "+1", "1.5", str(9007199254740992)):
|
|
with self.subTest(value=value):
|
|
with self.assertRaises(MODULE.AcceptanceError):
|
|
MODULE.parse_positive_int_value("XPLATFORM_MIN_VIDEO_COUNT", value)
|
|
|
|
def test_optional_positive_integer_env_rejects_lossy_values(self):
|
|
with mock.patch.dict(MODULE.os.environ, {"XPLATFORM_MIN_VIDEO_COUNT": "01"}, clear=False):
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "exact positive integer"):
|
|
MODULE.optional_positive_int_env_default("XPLATFORM_MIN_VIDEO_COUNT")
|
|
|
|
def test_validate_config_requires_explicit_acceptance_criteria(self):
|
|
config = valid_config(
|
|
min_clarity="",
|
|
rules_json="",
|
|
min_collection_count=None,
|
|
min_sampled_count=0,
|
|
)
|
|
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "XPLATFORM_MIN_CLARITY"):
|
|
MODULE.validate_config(config)
|
|
|
|
def test_validate_config_rejects_out_of_range_quality_thresholds(self):
|
|
config = valid_config(min_clarity="1.1", max_shake="2", max_hand_occlusion="-0.1")
|
|
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "XPLATFORM_MIN_CLARITY must be in the range"):
|
|
MODULE.validate_config(config)
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "XPLATFORM_MAX_SHAKE must be in the range"):
|
|
MODULE.validate_config(config)
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "XPLATFORM_MAX_HAND_OCCLUSION must be in the range"):
|
|
MODULE.validate_config(config)
|
|
|
|
def test_validate_config_rejects_non_finite_and_fractional_runtime_params(self):
|
|
config = valid_config(
|
|
min_clarity="nan",
|
|
clarity_scale="inf",
|
|
segment_duration_seconds="1.5",
|
|
frame_interval_seconds="nan",
|
|
)
|
|
|
|
with self.assertRaises(MODULE.AcceptanceError) as context:
|
|
MODULE.validate_config(config)
|
|
|
|
message = str(context.exception)
|
|
self.assertIn("XPLATFORM_MIN_CLARITY must be finite", message)
|
|
self.assertIn("XPLATFORM_CLARITY_SCALE must be finite", message)
|
|
self.assertIn("XPLATFORM_SEGMENT_DURATION_SECONDS must be an integer", message)
|
|
self.assertIn("XPLATFORM_FRAME_INTERVAL_SECONDS must be finite", message)
|
|
|
|
def test_validate_config_rejects_lossy_integer_runtime_params(self):
|
|
config = valid_config(segment_duration_seconds="01")
|
|
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "XPLATFORM_SEGMENT_DURATION_SECONDS must be an exact positive integer"):
|
|
MODULE.validate_config(config)
|
|
|
|
def test_validate_config_rejects_too_small_max_wait(self):
|
|
config = valid_config(max_wait_seconds=30)
|
|
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "XPLATFORM_MAX_WAIT_SECONDS must be greater than or equal to 60"):
|
|
MODULE.validate_config(config)
|
|
|
|
def test_normalize_sampling_rules_json_validates_rule_shape(self):
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "samplePercent"):
|
|
MODULE.normalize_sampling_rules_json(
|
|
json.dumps(
|
|
[
|
|
{
|
|
"id": "broken",
|
|
"field": "quality.clarity",
|
|
"operator": "gte",
|
|
"value": 0.3,
|
|
"samplePercent": 0,
|
|
}
|
|
]
|
|
)
|
|
)
|
|
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "samplePercent must be finite"):
|
|
MODULE.normalize_sampling_rules_json(
|
|
json.dumps(
|
|
[
|
|
{
|
|
"id": "broken",
|
|
"field": "quality.clarity",
|
|
"operator": "gte",
|
|
"value": 0.3,
|
|
"samplePercent": "nan",
|
|
}
|
|
]
|
|
)
|
|
)
|
|
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "samplePercent must be numeric"):
|
|
MODULE.normalize_sampling_rules_json(
|
|
json.dumps(
|
|
[
|
|
{
|
|
"id": "broken",
|
|
"field": "quality.clarity",
|
|
"operator": "gte",
|
|
"value": 0.3,
|
|
"samplePercent": True,
|
|
}
|
|
]
|
|
)
|
|
)
|
|
|
|
with self.assertRaisesRegex(MODULE.AcceptanceError, "duplicate id"):
|
|
MODULE.normalize_sampling_rules_json(
|
|
json.dumps(
|
|
[
|
|
{
|
|
"id": "same",
|
|
"field": "quality.clarity",
|
|
"operator": "gte",
|
|
"value": 0.3,
|
|
"samplePercent": 100,
|
|
},
|
|
{
|
|
"id": "same",
|
|
"field": "quality.shake",
|
|
"operator": "lte",
|
|
"value": 0.2,
|
|
"samplePercent": 100,
|
|
},
|
|
]
|
|
)
|
|
)
|
|
|
|
def test_validate_config_normalizes_sampling_rules(self):
|
|
config = valid_config(
|
|
rules_json=json.dumps(
|
|
[
|
|
{
|
|
"id": "clarity",
|
|
"field": "quality.clarity",
|
|
"operator": "gte",
|
|
"value": 0.3,
|
|
"samplePercent": 100,
|
|
}
|
|
],
|
|
ensure_ascii=False,
|
|
indent=2,
|
|
)
|
|
)
|
|
|
|
MODULE.validate_config(config)
|
|
|
|
self.assertEqual(json.loads(config.rules_json)[0]["id"], "clarity")
|
|
|
|
def test_normalize_sampling_rules_json_allows_exists_without_value(self):
|
|
normalized = MODULE.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_create_workflow_uses_configured_request_context(self):
|
|
config = valid_config()
|
|
operators = {
|
|
name: operator(name)
|
|
for name in (
|
|
"X 平台数据集索引",
|
|
"采集目录校验",
|
|
"视频质检",
|
|
"数据过滤",
|
|
"视频切片",
|
|
"视频标注",
|
|
"成品打包",
|
|
"抽检采样",
|
|
"结果上传",
|
|
)
|
|
}
|
|
calls = []
|
|
|
|
def fake_request(config_arg, method, path, payload=None, expected=(200, 201, 202, 204)):
|
|
self.assertIs(config_arg, config)
|
|
calls.append((method, path, payload, expected))
|
|
if path == "/api/projects":
|
|
return {"id": "project-1"}
|
|
if path == "/api/projects/project-1/workflows":
|
|
return {"id": "workflow-1"}
|
|
self.fail(f"unexpected request path: {path}")
|
|
|
|
with mock.patch.object(MODULE, "request", side_effect=fake_request):
|
|
project_id, workflow_id = MODULE.create_workflow(config, operators, None)
|
|
|
|
self.assertEqual(project_id, "project-1")
|
|
self.assertEqual(workflow_id, "workflow-1")
|
|
self.assertEqual(calls[0][0:2], ("POST", "/api/projects"))
|
|
self.assertEqual(calls[1][0:2], ("POST", "/api/projects/project-1/workflows"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|