|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pydantic.v1 as pydantic |
| 4 | +import pytest |
| 5 | + |
| 6 | +from tidy3d import IndexSimulation, IndexSimulationData, Simulation, SimulationData |
| 7 | + |
| 8 | +from ..utils import SAMPLE_SIMULATIONS, run_emulated |
| 9 | + |
| 10 | +# Reusable test constants |
| 11 | +SIM_INDEX = ("sim_A", "sim_B") |
| 12 | +DATA_INDEX = ("data_A", "data_B") |
| 13 | + |
| 14 | + |
| 15 | +def make_simulations() -> tuple[Simulation, ...]: |
| 16 | + """Creates a tuple of simple, distinct Simulation objects for testing.""" |
| 17 | + sim1 = SAMPLE_SIMULATIONS["full_fdtd"] |
| 18 | + sim2 = SAMPLE_SIMULATIONS["full_fdtd"].updated_copy(run_time=2e-12) |
| 19 | + return (sim1, sim2) |
| 20 | + |
| 21 | + |
| 22 | +def make_simulation_data() -> tuple[SimulationData, ...]: |
| 23 | + """Creates a tuple of simple SimulationData objects for testing.""" |
| 24 | + sims = make_simulations() |
| 25 | + data1 = run_emulated(sims[0]) |
| 26 | + data2 = run_emulated(sims[1]) |
| 27 | + return (data1, data2) |
| 28 | + |
| 29 | + |
| 30 | +def make_index_simulation(**kwargs) -> IndexSimulation: |
| 31 | + """Factory function to create a standard IndexSimulation instance.""" |
| 32 | + return IndexSimulation(index=SIM_INDEX, simulation=make_simulations(), **kwargs) |
| 33 | + |
| 34 | + |
| 35 | +def make_index_simulation_data(**kwargs) -> IndexSimulationData: |
| 36 | + """Factory function to create a standard IndexSimulationData instance.""" |
| 37 | + return IndexSimulationData(index=DATA_INDEX, data=make_simulation_data(), **kwargs) |
| 38 | + |
| 39 | + |
| 40 | +def test_index_simulation_data_creation(): |
| 41 | + """Tests successful creation of an IndexSimulationData instance.""" |
| 42 | + sim_data = make_simulation_data() |
| 43 | + container = make_index_simulation_data() |
| 44 | + assert container.index == DATA_INDEX |
| 45 | + assert container.data == sim_data |
| 46 | + assert len(container.index) == len(container.data) |
| 47 | + |
| 48 | + |
| 49 | +def test_index_simulation_data_mismatched_length_raises_error(): |
| 50 | + """Tests that a ValueError is raised for mismatched index and data lengths.""" |
| 51 | + sim_data = make_simulation_data() |
| 52 | + with pytest.raises(pydantic.ValidationError, match="Length of 'index' and 'data'"): |
| 53 | + IndexSimulationData(index=("only_one_index",), data=sim_data) |
| 54 | + |
| 55 | + |
| 56 | +def test_index_simulation_data_getitem_success(): |
| 57 | + """Tests successful retrieval of simulation data by its string index.""" |
| 58 | + container = make_index_simulation_data() |
| 59 | + sim_data = make_simulation_data() |
| 60 | + assert container[DATA_INDEX[0]] == sim_data[0] |
| 61 | + assert container[DATA_INDEX[1]] == sim_data[1] |
| 62 | + |
| 63 | + |
| 64 | +def test_index_simulation_data_getitem_key_error(): |
| 65 | + """Tests that a KeyError is raised when retrieving a non-existent index.""" |
| 66 | + container = make_index_simulation_data() |
| 67 | + with pytest.raises(KeyError, match="Index 'not_a_real_key' not found."): |
| 68 | + _ = container["not_a_real_key"] |
0 commit comments