|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import collections.abc |
| 4 | + |
| 5 | +import pydantic.v1 as pydantic |
| 6 | +import pytest |
| 7 | + |
| 8 | +from tidy3d import SimulationMap |
| 9 | + |
| 10 | +from ..utils import SAMPLE_SIMULATIONS |
| 11 | + |
| 12 | +# Reusable test constants |
| 13 | +SIM_MAP_DATA = { |
| 14 | + "sim_A": SAMPLE_SIMULATIONS["full_fdtd"], |
| 15 | + "sim_B": SAMPLE_SIMULATIONS["full_fdtd"].updated_copy(run_time=2e-12), |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def make_simulation_map() -> SimulationMap: |
| 20 | + """Factory function to create a standard SimulationMap instance.""" |
| 21 | + return SimulationMap(keys=tuple(SIM_MAP_DATA.keys()), values=tuple(SIM_MAP_DATA.values())) |
| 22 | + |
| 23 | + |
| 24 | +def test_simulation_map_creation(): |
| 25 | + """Tests successful creation and basic properties of a SimulationMap.""" |
| 26 | + s_map = make_simulation_map() |
| 27 | + assert isinstance(s_map, collections.abc.Mapping) |
| 28 | + assert len(s_map) == len(SIM_MAP_DATA) |
| 29 | + assert s_map["sim_A"] == SIM_MAP_DATA["sim_A"] |
| 30 | + assert list(s_map.keys()) == list(SIM_MAP_DATA.keys()) |
| 31 | + |
| 32 | + |
| 33 | +def test_simulation_map_invalid_type_raises_error(): |
| 34 | + """Tests that a ValidationError is raised for incorrect value types.""" |
| 35 | + invalid_data = {"sim_A": "not a simulation"} |
| 36 | + with pytest.raises(pydantic.ValidationError): |
| 37 | + SimulationMap(keys=tuple(invalid_data.keys()), values=tuple(invalid_data.values())) |
| 38 | + |
| 39 | + |
| 40 | +def test_simulation_map_getitem_success(): |
| 41 | + """Tests successful retrieval of a simulation by its string key.""" |
| 42 | + s_map = make_simulation_map() |
| 43 | + assert s_map["sim_A"] == SIM_MAP_DATA["sim_A"] |
| 44 | + assert s_map["sim_B"] == SIM_MAP_DATA["sim_B"] |
| 45 | + |
| 46 | + |
| 47 | +def test_simulation_map_getitem_key_error(): |
| 48 | + """Tests that a KeyError is raised when retrieving a non-existent key.""" |
| 49 | + s_map = make_simulation_map() |
| 50 | + with pytest.raises(KeyError): |
| 51 | + _ = s_map["not_a_real_key"] |
0 commit comments