Skip to content

Commit

Permalink
more tests to raise coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-dot committed Sep 25, 2024
1 parent 686fc25 commit bc4cf6f
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tests/common/beamlines/test_device_instantiation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any
from unittest.mock import patch

import pytest

from dodal.beamlines import get_all_beamline_modules
from dodal.beamlines import ModuleDiscoveryError, get_all_beamline_modules
from dodal.common.beamlines import beamline_utils
from dodal.utils import BLUESKY_PROTOCOLS, make_all_devices

Expand Down Expand Up @@ -49,3 +50,33 @@ def test_devices_are_identical(RE, module_and_devices_for_beamline):
)
for device_name in devices_a.keys():
assert devices_a[device_name] is devices_b[device_name]


def test_get_all_beamline_modules_raises_module_discovery_error_on_spec_failure():
"""
Tests that get_all_beamline_modules raises ModuleDiscoveryError if module spec cannot be found.
"""

# Simulate the failure of `importlib.util.find_spec` by returning None
with patch("importlib.util.find_spec", return_value=None):
with pytest.raises(
ModuleDiscoveryError, match="Unable to find module search locations"
):
list(
get_all_beamline_modules()
) # Convert the generator to a list to trigger execution


def test_get_all_beamline_modules_raises_module_discovery_error_on_exception():
"""
Tests that get_all_beamline_modules raises ModuleDiscoveryError if an exception is raised in find_spec.
"""

# Simulate an exception being raised from `importlib.util.find_spec`
with patch("importlib.util.find_spec", side_effect=Exception("Mocked exception")):
with pytest.raises(
ModuleDiscoveryError, match="Error while finding module spec"
):
list(
get_all_beamline_modules()
) # Convert the generator to a list to trigger execution

0 comments on commit bc4cf6f

Please sign in to comment.