Skip to content

Commit 2552fdb

Browse files
committed
added "test_messages_contain_object_names()" to "test_simulation.py"
1 parent a8c5f14 commit 2552fdb

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

tests/test_components/test_simulation.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3648,3 +3648,69 @@ def test_create_sim_multiphysics_with_incompatibilities():
36483648
),
36493649
],
36503650
)
3651+
3652+
3653+
def test_messages_contain_object_names():
3654+
"""Make sure that errors and warnings contain the name of the object."""
3655+
# Note: This function currently tests for out-of-bounds errors and warnings.
3656+
sim_args = {
3657+
"size": (1, 1, 1),
3658+
"grid_spec": td.GridSpec.auto(wavelength=4),
3659+
"boundary_spec": td.BoundarySpec(
3660+
x=td.Boundary.periodic(),
3661+
y=td.Boundary.periodic(),
3662+
z=td.Boundary.periodic(),
3663+
),
3664+
"run_time": 1e-12,
3665+
}
3666+
3667+
# Test 1) Create a structure lying partially outside the simulation boundary.
3668+
# Check that a warning message is generated containing the structure's `name`.
3669+
structure_args = {
3670+
"geometry": td.Box(center=(1.0, 1.0, 0.0), size=(0.5, 0.5, 0.5)),
3671+
"medium": td.Medium(permittivity=2.0),
3672+
}
3673+
td.log.set_capture(True)
3674+
# Create a simulation with this structure.
3675+
_ = td.Simulation(
3676+
structures=[td.Structure(name="structure_123", **structure_args)],
3677+
**sim_args,
3678+
)
3679+
warning_list = td.log.captured_warnings()
3680+
assert len(warning_list) >= 1
3681+
assert "structure_123" in warning_list[0]["msg"]
3682+
td.log.set_capture(False)
3683+
3684+
# Test 2) Create a source lying partially outside the simulation boundary.
3685+
# Check that an error message is generated containing the source's `name`.
3686+
source_args = {
3687+
"center": (0, -1.0, 0),
3688+
"size": (1, 0, 0.5),
3689+
"polarization": "Ex",
3690+
"source_time": td.GaussianPulse(
3691+
freq0=100e14,
3692+
fwidth=10e14,
3693+
),
3694+
}
3695+
try:
3696+
# Create a simulation with this source.
3697+
_ = td.Simulation(
3698+
sources=[td.UniformCurrentSource(name="source_123", **source_args)],
3699+
**sim_args,
3700+
)
3701+
raise AssertionError # The previous line should raise an exception.
3702+
except pydantic.ValidationError as e:
3703+
assert "source_123" in str(e)
3704+
3705+
# Test 3) Create a monitor lying partially outside the simulation boundary.
3706+
# Check that an error message is generated containing the monitor's `name`.
3707+
monitor_args = {"center": (-1.0, 0, 0), "size": (0.5, 0, 1), "freqs": [100e14]}
3708+
try:
3709+
# Create a simulation with this monitor.
3710+
_ = td.Simulation(
3711+
monitors=[td.FieldMonitor(name="monitor_123", **monitor_args)],
3712+
**sim_args,
3713+
)
3714+
raise AssertionError # The previous line should raise an exception.
3715+
except pydantic.ValidationError as e:
3716+
assert "monitor_123" in str(e)

0 commit comments

Comments
 (0)