Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing in MJPG stream and fix system test #777

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dodal/devices/areadetector/plugins/MJPG.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import threading
from abc import ABC, abstractmethod
from io import BytesIO
from pathlib import Path

import requests
Expand Down Expand Up @@ -70,7 +71,7 @@ def get_snapshot():
try:
response = requests.get(url_str, stream=True)
response.raise_for_status()
with Image.open(response.raw) as image:
with Image.open(BytesIO(response.content)) as image:
self.post_processing(image)
st.set_finished()
except requests.HTTPError as e:
Expand Down
3 changes: 2 additions & 1 deletion tests/devices/system_tests/test_oav_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ def take_snapshot_with_grid(oav: OAV, snapshot_filename, snapshot_directory):
yield from bps.trigger(oav.grid_snapshot, wait=True)


# We need to find a better way of integrating this, see https://github.com/DiamondLightSource/mx-bluesky/issues/183
@pytest.mark.skip(reason="Don't want to actually take snapshots during testing.")
def test_grid_overlay(RE: RunEngine):
beamline = "BL03I"
oav_params = OAVConfigParams(ZOOM_LEVELS_XML, DISPLAY_CONFIGURATION)
oav = OAV(name="oav", prefix=f"{beamline}-DI-OAV-01", params=oav_params)
oav = OAV(name="oav", prefix=f"{beamline}", params=oav_params)
snapshot_filename = "snapshot"
snapshot_directory = "."
RE(take_snapshot_with_grid(oav, snapshot_filename, snapshot_directory))
Expand Down
1 change: 1 addition & 0 deletions tests/devices/unit_tests/areadetector/plugins/test_MJPG.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_given_snapshot_triggered_then_crosshair_drawn(
patch_requests, patch_image_draw, patch_os, patch_image
):
patch_line = MagicMock()
patch_requests.get.return_value.content = b""
params = OAVConfigParams(ZOOM_LEVELS_XML, DISPLAY_CONFIGURATION)
params.update_on_zoom(1.0, 100, 100)

Expand Down
26 changes: 15 additions & 11 deletions tests/devices/unit_tests/test_oav.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def fake_oav() -> OAV:
return fake_oav


@pytest.fixture
def mock_get_with_valid_response():
patcher = patch("requests.get")
mock_get = patcher.start()
mock_get.return_value.content = b""
yield mock_get
patcher.stop()


@patch("requests.get")
def test_snapshot_trigger_handles_request_with_bad_status_code_correctly(
mock_get, fake_oav: OAV
Expand All @@ -63,22 +72,20 @@ def test_snapshot_trigger_handles_request_with_bad_status_code_correctly(
st.wait()


@patch("requests.get")
@patch("dodal.devices.areadetector.plugins.MJPG.Image")
@patch("dodal.devices.areadetector.plugins.MJPG.os", new=MagicMock())
def test_snapshot_trigger_loads_correct_url(
mock_image: MagicMock, mock_get: MagicMock, fake_oav: OAV
mock_image: MagicMock, mock_get_with_valid_response: MagicMock, fake_oav: OAV
):
st = fake_oav.grid_snapshot.trigger()
st.wait()
mock_get.assert_called_once_with("http://test.url", stream=True)
mock_get_with_valid_response.assert_called_once_with("http://test.url", stream=True)


@patch("requests.get")
@patch("dodal.devices.areadetector.plugins.MJPG.Image.open")
@patch("dodal.devices.areadetector.plugins.MJPG.os", new=MagicMock())
def test_snapshot_trigger_saves_to_correct_file(
mock_open: MagicMock, mock_get, fake_oav
mock_open: MagicMock, mock_get_with_valid_response, fake_oav
):
image = Image.open("test")
mock_open.return_value.__enter__.return_value = image
Expand All @@ -93,23 +100,21 @@ def test_snapshot_trigger_saves_to_correct_file(
assert calls_to_save == expected_calls_to_save


@patch("requests.get")
@patch("dodal.devices.areadetector.plugins.MJPG.Image.open")
@patch("dodal.devices.areadetector.plugins.MJPG.os")
def test_given_directory_not_existing_when_snapshot_triggered_then_directory_created(
mock_os, mock_open: MagicMock, mock_get, fake_oav
mock_os, mock_open: MagicMock, mock_get_with_valid_response, fake_oav
):
mock_os.path.isdir.return_value = False
st = fake_oav.grid_snapshot.trigger()
st.wait()
mock_os.mkdir.assert_called_once_with("test directory")


@patch("requests.get")
@patch("dodal.devices.areadetector.plugins.MJPG.Image.open")
@patch("dodal.devices.areadetector.plugins.MJPG.os", new=MagicMock())
def test_snapshot_trigger_applies_current_microns_per_pixel_to_snapshot(
mock_open: MagicMock, mock_get, fake_oav
mock_open: MagicMock, mock_get_with_valid_response, fake_oav
):
image = Image.open("test") # type: ignore
mock_open.return_value.__enter__.return_value = image
Expand All @@ -123,7 +128,6 @@ def test_snapshot_trigger_applies_current_microns_per_pixel_to_snapshot(
assert fake_oav.grid_snapshot.microns_per_pixel_y.get() == expected_mpp_y


@patch("requests.get")
@patch("dodal.devices.areadetector.plugins.MJPG.Image.open")
@patch("dodal.devices.oav.grid_overlay.add_grid_overlay_to_image")
@patch("dodal.devices.oav.grid_overlay.add_grid_border_overlay_to_image")
Expand All @@ -132,7 +136,7 @@ def test_correct_grid_drawn_on_image(
mock_border_overlay: MagicMock,
mock_grid_overlay: MagicMock,
mock_open: MagicMock,
mock_get: MagicMock,
mock_get_with_valid_response: MagicMock,
fake_oav: OAV,
):
st = fake_oav.grid_snapshot.trigger()
Expand Down
Loading