|
| 1 | +import pytest |
| 2 | +from pydantic import Field |
| 3 | + |
| 4 | +from libecalc.presentation.yaml.definition_resolver import resolve_definitions |
| 5 | +from libecalc.presentation.yaml.yaml_types import YamlBase |
| 6 | +from libecalc.presentation.yaml.yaml_types.process.yaml_process_pipeline import ( |
| 7 | + YamlProcessPipeline, |
| 8 | +) |
| 9 | +from libecalc.presentation.yaml.yaml_types.process.yaml_process_references import DefinitionReference |
| 10 | +from libecalc.presentation.yaml.yaml_types.process.yaml_process_units import ( |
| 11 | + YamlLiquidRemoverDefinition, |
| 12 | + YamlPressureDropperDefinition, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestResolveDefinitions: |
| 17 | + def test_resolves_string_reference(self): |
| 18 | + pipeline = YamlProcessPipeline.model_validate( |
| 19 | + { |
| 20 | + "NAME": "p", |
| 21 | + "PROCESS_UNITS": [{"TARGET": "my_dropper"}], |
| 22 | + } |
| 23 | + ) |
| 24 | + defs = { |
| 25 | + "my_dropper": YamlPressureDropperDefinition.model_validate( |
| 26 | + {"TYPE": "PRESSURE_DROPPER", "PRESSURE_DROP": "5"} |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + resolved = resolve_definitions(pipeline, defs) |
| 31 | + |
| 32 | + assert isinstance(resolved.process_units[0].target, YamlPressureDropperDefinition) |
| 33 | + assert resolved.process_units[0].target.pressure_drop == "5" |
| 34 | + |
| 35 | + def test_preserves_inline_definition(self): |
| 36 | + pipeline = YamlProcessPipeline.model_validate( |
| 37 | + { |
| 38 | + "NAME": "p", |
| 39 | + "PROCESS_UNITS": [ |
| 40 | + {"TARGET": {"TYPE": "LIQUID_REMOVER"}}, |
| 41 | + ], |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + resolved = resolve_definitions(pipeline, {}) |
| 46 | + |
| 47 | + assert isinstance(resolved.process_units[0].target, YamlLiquidRemoverDefinition) |
| 48 | + |
| 49 | + def test_missing_reference_raises_key_error(self): |
| 50 | + pipeline = YamlProcessPipeline.model_validate( |
| 51 | + { |
| 52 | + "NAME": "p", |
| 53 | + "PROCESS_UNITS": [{"TARGET": "nonexistent"}], |
| 54 | + } |
| 55 | + ) |
| 56 | + |
| 57 | + with pytest.raises(KeyError, match="nonexistent"): |
| 58 | + resolve_definitions(pipeline, {}) |
| 59 | + |
| 60 | + def test_resolves_multiple_references(self): |
| 61 | + pipeline = YamlProcessPipeline.model_validate( |
| 62 | + { |
| 63 | + "NAME": "p", |
| 64 | + "PROCESS_UNITS": [ |
| 65 | + {"TARGET": "dropper"}, |
| 66 | + {"TARGET": "remover"}, |
| 67 | + ], |
| 68 | + } |
| 69 | + ) |
| 70 | + defs = { |
| 71 | + "dropper": YamlPressureDropperDefinition.model_validate({"TYPE": "PRESSURE_DROPPER", "PRESSURE_DROP": "3"}), |
| 72 | + "remover": YamlLiquidRemoverDefinition.model_validate({"TYPE": "LIQUID_REMOVER"}), |
| 73 | + } |
| 74 | + |
| 75 | + resolved = resolve_definitions(pipeline, defs) |
| 76 | + |
| 77 | + assert isinstance(resolved.process_units[0].target, YamlPressureDropperDefinition) |
| 78 | + assert isinstance(resolved.process_units[1].target, YamlLiquidRemoverDefinition) |
| 79 | + |
| 80 | + def test_mixed_inline_and_reference(self): |
| 81 | + pipeline = YamlProcessPipeline.model_validate( |
| 82 | + { |
| 83 | + "NAME": "p", |
| 84 | + "PROCESS_UNITS": [ |
| 85 | + {"TARGET": "dropper"}, |
| 86 | + {"TARGET": {"TYPE": "LIQUID_REMOVER"}}, |
| 87 | + ], |
| 88 | + } |
| 89 | + ) |
| 90 | + defs = { |
| 91 | + "dropper": YamlPressureDropperDefinition.model_validate({"TYPE": "PRESSURE_DROPPER", "PRESSURE_DROP": "1"}), |
| 92 | + } |
| 93 | + |
| 94 | + resolved = resolve_definitions(pipeline, defs) |
| 95 | + |
| 96 | + assert isinstance(resolved.process_units[0].target, YamlPressureDropperDefinition) |
| 97 | + assert isinstance(resolved.process_units[1].target, YamlLiquidRemoverDefinition) |
| 98 | + |
| 99 | + def test_no_references_returns_equal_model(self): |
| 100 | + pipeline = YamlProcessPipeline.model_validate( |
| 101 | + { |
| 102 | + "NAME": "p", |
| 103 | + "PROCESS_UNITS": [ |
| 104 | + {"TARGET": {"TYPE": "LIQUID_REMOVER"}}, |
| 105 | + ], |
| 106 | + } |
| 107 | + ) |
| 108 | + |
| 109 | + resolved = resolve_definitions(pipeline, {}) |
| 110 | + |
| 111 | + assert resolved == pipeline |
| 112 | + |
| 113 | + def test_preserves_instance_name(self): |
| 114 | + pipeline = YamlProcessPipeline.model_validate( |
| 115 | + { |
| 116 | + "NAME": "p", |
| 117 | + "PROCESS_UNITS": [{"TARGET": "dropper", "NAME": "stage1"}], |
| 118 | + } |
| 119 | + ) |
| 120 | + defs = { |
| 121 | + "dropper": YamlPressureDropperDefinition.model_validate({"TYPE": "PRESSURE_DROPPER", "PRESSURE_DROP": "5"}), |
| 122 | + } |
| 123 | + |
| 124 | + resolved = resolve_definitions(pipeline, defs) |
| 125 | + |
| 126 | + assert resolved.process_units[0].name == "stage1" |
| 127 | + |
| 128 | + def test_non_definition_strings_are_not_resolved(self): |
| 129 | + """Fields typed as plain str (not DefinitionReference unions) should be left alone.""" |
| 130 | + pipeline = YamlProcessPipeline.model_validate( |
| 131 | + { |
| 132 | + "NAME": "my_pipeline_name", |
| 133 | + "PROCESS_UNITS": [{"TARGET": {"TYPE": "LIQUID_REMOVER"}}], |
| 134 | + } |
| 135 | + ) |
| 136 | + |
| 137 | + resolved = resolve_definitions(pipeline, {"my_pipeline_name": "should_not_replace"}) |
| 138 | + |
| 139 | + assert resolved.name == "my_pipeline_name" |
| 140 | + |
| 141 | + def test_resolves_references_in_dict_values(self): |
| 142 | + """DefinitionReference inside dict values should be resolved.""" |
| 143 | + |
| 144 | + class Inner(YamlBase): |
| 145 | + target: YamlPressureDropperDefinition | DefinitionReference |
| 146 | + |
| 147 | + class Outer(YamlBase): |
| 148 | + items: dict[str, Inner] = Field(default_factory=dict) |
| 149 | + |
| 150 | + outer = Outer.model_validate({"ITEMS": {"a": {"TARGET": "dropper"}}}) |
| 151 | + defs = { |
| 152 | + "dropper": YamlPressureDropperDefinition.model_validate({"TYPE": "PRESSURE_DROPPER", "PRESSURE_DROP": "2"}), |
| 153 | + } |
| 154 | + |
| 155 | + resolved = resolve_definitions(outer, defs) |
| 156 | + |
| 157 | + assert isinstance(resolved.items["a"].target, YamlPressureDropperDefinition) |
0 commit comments