|
| 1 | +import pytest |
| 2 | + |
| 3 | +# Attempt multiple import paths to accommodate common layouts |
| 4 | +try: |
| 5 | + # e.g., src/state_manager/models/dependent_string.py |
| 6 | + from state_manager.models.dependent_string import DependentString, Dependent |
| 7 | +except Exception: |
| 8 | + try: |
| 9 | + # e.g., state_manager/dependent_string.py |
| 10 | + from state_manager.dependent_string import DependentString, Dependent |
| 11 | + except Exception: |
| 12 | + try: |
| 13 | + # e.g., state-manager/state_manager/models/dependent_string.py accessible as package |
| 14 | + from models.dependent_string import DependentString, Dependent |
| 15 | + except Exception: |
| 16 | + # Fallback: relative import if tests live alongside source |
| 17 | + from dependent_string import DependentString, Dependent # type: ignore |
| 18 | + |
| 19 | + |
| 20 | +class TestCreateDependentString: |
| 21 | + def test_no_placeholders_returns_head_only_and_empty_dependents(self): |
| 22 | + s = "plain string without placeholders" |
| 23 | + ds = DependentString.create_dependent_string(s) |
| 24 | + assert isinstance(ds, DependentString) |
| 25 | + assert ds.head == s |
| 26 | + assert ds.dependents == {} |
| 27 | + # get_identifier_field should be empty for no dependents |
| 28 | + assert ds.get_identifier_field() == [] |
| 29 | + |
| 30 | + def test_single_placeholder_happy_path(self): |
| 31 | + template = "Hello ${{ step.outputs.foo }} world" |
| 32 | + ds = DependentString.create_dependent_string(template) |
| 33 | + # Head is the prefix before the first placeholder |
| 34 | + assert ds.head == "Hello " |
| 35 | + # One dependent keyed by order 0 |
| 36 | + assert list(ds.dependents.keys()) == [0] |
| 37 | + dep = ds.dependents[0] |
| 38 | + assert isinstance(dep, Dependent) |
| 39 | + assert dep.identifier == "step" |
| 40 | + assert dep.field == "foo" |
| 41 | + assert dep.tail == " world" |
| 42 | + # Not set yet -> generate_string should raise |
| 43 | + with pytest.raises(ValueError) as exc: |
| 44 | + ds.generate_string() |
| 45 | + assert "Dependent value is not set" in str(exc.value) |
| 46 | + # After setting, generation should succeed |
| 47 | + ds.set_value("step", "foo", "BAR") |
| 48 | + assert ds.generate_string() == "Hello BAR world" |
| 49 | + |
| 50 | + def test_placeholder_at_end_results_in_empty_tail(self): |
| 51 | + template = "Hi ${{ a.outputs.x }}" |
| 52 | + ds = DependentString.create_dependent_string(template) |
| 53 | + assert ds.dependents[0].tail == "" |
| 54 | + ds.set_value("a", "x", "V") |
| 55 | + assert ds.generate_string() == "Hi V" |
| 56 | + |
| 57 | + def test_multiple_placeholders_in_order(self): |
| 58 | + template = "Start ${{ a.outputs.x }} mid ${{ b.outputs.y }} end" |
| 59 | + ds = DependentString.create_dependent_string(template) |
| 60 | + # Keys should reflect insertion order (0, 1) when sorted |
| 61 | + assert sorted(ds.dependents.keys()) == [0, 1] |
| 62 | + assert ds.dependents[0].identifier == "a" |
| 63 | + assert ds.dependents[0].field == "x" |
| 64 | + assert ds.dependents[0].tail == " mid " |
| 65 | + assert ds.dependents[1].identifier == "b" |
| 66 | + assert ds.dependents[1].field == "y" |
| 67 | + assert ds.dependents[1].tail == " end" |
| 68 | + ds.set_value("a", "x", "AX") |
| 69 | + ds.set_value("b", "y", "BY") |
| 70 | + assert ds.generate_string() == "Start AX mid BY end" |
| 71 | + |
| 72 | + def test_unclosed_placeholder_raises_value_error(self): |
| 73 | + template = "Start ${{ a.outputs.x end" |
| 74 | + with pytest.raises(ValueError) as exc: |
| 75 | + DependentString.create_dependent_string(template) |
| 76 | + msg = str(exc.value) |
| 77 | + assert "Invalid syntax string placeholder" in msg |
| 78 | + assert "'${{'" in msg or "not closed" in msg |
| 79 | + |
| 80 | + def test_invalid_placeholder_wrong_parts_count_raises(self): |
| 81 | + # Missing the third part after outputs |
| 82 | + template = "Start ${{ a.outputs }} end" |
| 83 | + with pytest.raises(ValueError) as exc: |
| 84 | + DependentString.create_dependent_string(template) |
| 85 | + assert "Invalid syntax string placeholder" in str(exc.value) |
| 86 | + |
| 87 | + def test_invalid_placeholder_wrong_keyword_raises(self): |
| 88 | + template = "Start ${{ a.outputz.x }} end" |
| 89 | + with pytest.raises(ValueError) as exc: |
| 90 | + DependentString.create_dependent_string(template) |
| 91 | + assert "Invalid syntax string placeholder" in str(exc.value) |
| 92 | + |
| 93 | + def test_placeholder_with_extra_whitespace_is_parsed(self): |
| 94 | + template = "P ${{ step . outputs . foo }} T" |
| 95 | + ds = DependentString.create_dependent_string(template) |
| 96 | + assert ds.dependents[0].identifier == "step" |
| 97 | + assert ds.dependents[0].field == "foo" |
| 98 | + ds.set_value("step", "foo", "VAL") |
| 99 | + assert ds.generate_string() == "P VAL T" |
| 100 | + |
| 101 | + |
| 102 | +class TestMappingAndSetValue: |
| 103 | + def test_get_identifier_field_returns_unique_keys(self): |
| 104 | + template = "A ${{ a.outputs.x }} B ${{ a.outputs.x }} C ${{ b.outputs.y }}" |
| 105 | + ds = DependentString.create_dependent_string(template) |
| 106 | + # Should return two unique keys: ('a','x') and ('b','y'), order not guaranteed |
| 107 | + keys = ds.get_identifier_field() |
| 108 | + assert set(keys) == {("a", "x"), ("b", "y")} |
| 109 | + |
| 110 | + def test_set_value_updates_all_matching_dependents(self): |
| 111 | + template = "A ${{ a.outputs.x }} B ${{ a.outputs.x }}" |
| 112 | + ds = DependentString.create_dependent_string(template) |
| 113 | + ds.set_value("a", "x", "V") |
| 114 | + # Both dependents should get the same value and render twice |
| 115 | + assert ds.generate_string() == "A V B V" |
| 116 | + |
| 117 | + def test_set_value_with_unknown_mapping_raises_keyerror(self): |
| 118 | + template = "A ${{ a.outputs.x }}" |
| 119 | + ds = DependentString.create_dependent_string(template) |
| 120 | + with pytest.raises(KeyError): |
| 121 | + ds.set_value("unknown", "field", "V") |
| 122 | + |
| 123 | + def test_build_mapping_is_idempotent_and_cached(self): |
| 124 | + template = "A ${{ a.outputs.x }} B ${{ b.outputs.y }}" |
| 125 | + ds = DependentString.create_dependent_string(template) |
| 126 | + # First call builds mapping |
| 127 | + keys1 = set(ds.get_identifier_field()) |
| 128 | + # Second call should not change |
| 129 | + keys2 = set(ds.get_identifier_field()) |
| 130 | + assert keys1 == keys2 == {("a", "x"), ("b", "y")} |
0 commit comments