|
| 1 | +import pytest |
| 2 | +from acidwatch_api.models.base import BaseAdapter |
| 3 | +from acidwatch_api.models.datamodel import Phase |
| 4 | + |
| 5 | + |
| 6 | +class DummyAdapter(BaseAdapter): |
| 7 | + model_id = "dummy" |
| 8 | + display_name = "Dummy" |
| 9 | + description = "test" |
| 10 | + category = "ChemicalEquilibrium" |
| 11 | + valid_substances = ["CO2", "HCl"] |
| 12 | + |
| 13 | + async def run(self): |
| 14 | + return [] |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def adapter(): |
| 19 | + a = DummyAdapter(parameters=None, jwt_token=None) |
| 20 | + return a |
| 21 | + |
| 22 | + |
| 23 | +class TestSetConcentrations: |
| 24 | + def test_filters_to_valid_substances(self, adapter): |
| 25 | + adapter.set_concentrations({"CO2": 1.0, "HCl": 2.0, "H2O": 3.0}) |
| 26 | + assert adapter.concentrations == {"CO2": 1.0, "HCl": 2.0} |
| 27 | + |
| 28 | + def test_defaults_missing_valid_substances_to_zero(self, adapter): |
| 29 | + adapter.set_concentrations({"CO2": 1.0, "H2O": 3.0}) |
| 30 | + assert adapter.concentrations == {"CO2": 1.0, "HCl": 0.0} |
| 31 | + |
| 32 | + |
| 33 | +class TestPassthroughConcentrations: |
| 34 | + @pytest.mark.parametrize( |
| 35 | + "input_concs,expected", |
| 36 | + [ |
| 37 | + ({"CO2": 1.0, "HCl": 2.0}, {}), |
| 38 | + ({"CO2": 1.0, "H2O": 3.0}, {"H2O": 3.0}), |
| 39 | + ({"H2O": 3.0, "NaCl": 5.0}, {"H2O": 3.0, "NaCl": 5.0}), |
| 40 | + ], |
| 41 | + ids=[ |
| 42 | + "all_handled", |
| 43 | + "one_unhandled", |
| 44 | + "all_unhandled", |
| 45 | + ], |
| 46 | + ) |
| 47 | + def test_returns_unhandled_components(self, adapter, input_concs, expected): |
| 48 | + adapter.set_concentrations(input_concs) |
| 49 | + assert adapter.passthrough_concentrations == expected |
| 50 | + |
| 51 | + |
| 52 | +class TestMergePassthrough: |
| 53 | + def test_no_passthrough_returns_phases_unchanged(self, adapter): |
| 54 | + adapter.set_concentrations({"CO2": 1.0, "HCl": 2.0}) |
| 55 | + phases = [Phase(kind="co2-rich", fraction=1.0, concentrations={"CO2": 0.5})] |
| 56 | + result = adapter.merge_passthrough(phases) |
| 57 | + assert result is phases |
| 58 | + |
| 59 | + @pytest.mark.parametrize( |
| 60 | + "phase_kind,expect_merge", |
| 61 | + [ |
| 62 | + ("co2-rich", True), |
| 63 | + ("aqueous", False), |
| 64 | + ], |
| 65 | + ) |
| 66 | + def test_only_merges_into_co2_rich_phases(self, adapter, phase_kind, expect_merge): |
| 67 | + adapter.set_concentrations({"CO2": 1.0, "H2O": 3.0}) |
| 68 | + phases = [Phase(kind=phase_kind, fraction=1.0, concentrations={"CO2": 0.5})] |
| 69 | + result = adapter.merge_passthrough(phases) |
| 70 | + if expect_merge: |
| 71 | + assert result[0].concentrations == {"CO2": 0.5, "H2O": 3.0} |
| 72 | + else: |
| 73 | + assert result[0].concentrations == {"CO2": 0.5} |
| 74 | + |
| 75 | + def test_model_output_takes_precedence_over_passthrough(self, adapter): |
| 76 | + adapter.set_concentrations({"CO2": 1.0, "H2O": 3.0, "NaCl": 7.0}) |
| 77 | + phases = [ |
| 78 | + Phase( |
| 79 | + kind="co2-rich", fraction=1.0, concentrations={"CO2": 0.5, "NaCl": 9.0} |
| 80 | + ) |
| 81 | + ] |
| 82 | + result = adapter.merge_passthrough(phases) |
| 83 | + assert result[0].concentrations == {"CO2": 0.5, "H2O": 3.0, "NaCl": 9.0} |
| 84 | + |
| 85 | + def test_multiple_phases(self, adapter): |
| 86 | + adapter.set_concentrations({"CO2": 1.0, "H2O": 3.0}) |
| 87 | + phases = [ |
| 88 | + Phase(kind="co2-rich", fraction=0.7, concentrations={"CO2": 0.5}), |
| 89 | + Phase(kind="aqueous", fraction=0.3, concentrations={"CO2": 0.1}), |
| 90 | + ] |
| 91 | + result = adapter.merge_passthrough(phases) |
| 92 | + assert result[0].concentrations == {"CO2": 0.5, "H2O": 3.0} |
| 93 | + assert result[1].concentrations == {"CO2": 0.1} |
0 commit comments