|
| 1 | +"""Tests for SFN from_deployment parameter extraction logic.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +def _extract_param_info(env_vars): |
| 8 | + """ |
| 9 | + Mirrors the parameter extraction logic in |
| 10 | + StepFunctionsDeployedFlow.from_deployment: given the Environment list |
| 11 | + from ContainerOverrides, return param_info dict. |
| 12 | + """ |
| 13 | + param_info = {} |
| 14 | + try: |
| 15 | + env_dict = {item.get("Name"): item.get("Value") for item in env_vars} |
| 16 | + raw = env_dict.get("METAFLOW_DEFAULT_PARAMETERS") |
| 17 | + if raw: |
| 18 | + for pname, pvalue in json.loads(raw).items(): |
| 19 | + if isinstance(pvalue, bool): |
| 20 | + ptype = "bool" |
| 21 | + elif isinstance(pvalue, int): |
| 22 | + ptype = "int" |
| 23 | + elif isinstance(pvalue, float): |
| 24 | + ptype = "float" |
| 25 | + else: |
| 26 | + ptype = "str" |
| 27 | + param_info[pname] = { |
| 28 | + "name": pname, |
| 29 | + "python_var_name": pname, |
| 30 | + "type": ptype, |
| 31 | + "description": "", |
| 32 | + "is_required": False, |
| 33 | + } |
| 34 | + except (KeyError, json.JSONDecodeError, TypeError): |
| 35 | + pass |
| 36 | + return param_info |
| 37 | + |
| 38 | + |
| 39 | +def test_valid_param_extraction(): |
| 40 | + env = [ |
| 41 | + { |
| 42 | + "Name": "METAFLOW_DEFAULT_PARAMETERS", |
| 43 | + "Value": json.dumps( |
| 44 | + {"name": "alice", "count": 5, "rate": 3.14, "verbose": True} |
| 45 | + ), |
| 46 | + } |
| 47 | + ] |
| 48 | + info = _extract_param_info(env) |
| 49 | + assert info["name"]["type"] == "str" |
| 50 | + assert info["count"]["type"] == "int" |
| 51 | + assert info["rate"]["type"] == "float" |
| 52 | + assert info["verbose"]["type"] == "bool" |
| 53 | + assert all(not v["is_required"] for v in info.values()) |
| 54 | + |
| 55 | + |
| 56 | +def test_bool_detected_before_int(): |
| 57 | + """bool is a subclass of int in Python; ensure bool wins.""" |
| 58 | + env = [{"Name": "METAFLOW_DEFAULT_PARAMETERS", "Value": json.dumps({"flag": True})}] |
| 59 | + assert _extract_param_info(env)["flag"]["type"] == "bool" |
| 60 | + |
| 61 | + |
| 62 | +def test_malformed_json_returns_empty(): |
| 63 | + env = [{"Name": "METAFLOW_DEFAULT_PARAMETERS", "Value": "{bad json!}"}] |
| 64 | + assert _extract_param_info(env) == {} |
| 65 | + |
| 66 | + |
| 67 | +def test_missing_env_var_returns_empty(): |
| 68 | + env = [{"Name": "OTHER_VAR", "Value": "x"}] |
| 69 | + assert _extract_param_info(env) == {} |
| 70 | + |
| 71 | + |
| 72 | +def test_empty_env_list_returns_empty(): |
| 73 | + assert _extract_param_info([]) == {} |
0 commit comments