From 46c4a33e23fa3ba616854b848024d98878a6c58f Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Mon, 21 Oct 2024 10:43:53 +0100 Subject: [PATCH] get test to pass --- .../devices/areadetector/plugins/MJPG_async.py | 9 +-------- .../oav/snapshots/snapshot_with_beam_centre.py | 3 --- tests/devices/unit_tests/oav/test_snapshots.py | 14 +++++++------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/dodal/devices/areadetector/plugins/MJPG_async.py b/src/dodal/devices/areadetector/plugins/MJPG_async.py index b03144b519..dd759cdd6b 100644 --- a/src/dodal/devices/areadetector/plugins/MJPG_async.py +++ b/src/dodal/devices/areadetector/plugins/MJPG_async.py @@ -42,10 +42,8 @@ async def _save_image(self, image: Image.Image): directory and filename signals. The full resultant path is put on the \ last_saved_path signal """ - print("HERE 5.5") filename_str = await self.filename.get_value() directory_str = await self.directory.get_value() - print("HERE 6") path = Path(f"{directory_str}/{filename_str}.png").as_posix() if not Path(directory_str).is_dir(): @@ -53,13 +51,11 @@ async def _save_image(self, image: Image.Image): Path(directory_str).mkdir(parents=True) LOGGER.info(f"Saving image to {path}") - print("HERE 7") buffer = BytesIO() image.save(buffer, format="png") async with aiofiles.open(path, "wb") as fh: await fh.write(buffer.getbuffer()) - # image.save(path) await self.last_saved_path.set(path, wait=True) @AsyncStatus.wrap @@ -78,12 +74,9 @@ async def trigger(self): f"OAV responded with {response.status}: {response.reason}." ) try: - print("HERE 1") data = await response.read() - print("HERE1.5") with Image.open(BytesIO(data)) as image: - print("HERE 2") - await self.post_processing(image) + await self.post_processing(image) except Exception as e: LOGGER.warning(f"Failed to create snapshot. \n {e}") diff --git a/src/dodal/devices/oav/snapshots/snapshot_with_beam_centre.py b/src/dodal/devices/oav/snapshots/snapshot_with_beam_centre.py index 044456af6a..a96318459e 100644 --- a/src/dodal/devices/oav/snapshots/snapshot_with_beam_centre.py +++ b/src/dodal/devices/oav/snapshots/snapshot_with_beam_centre.py @@ -24,12 +24,9 @@ def __init__( super().__init__(prefix, name) async def post_processing(self, image: Image.Image): - print("HERE 3") beam_x = await self.beam_centre_i.get_value() beam_y = await self.beam_centre_j.get_value() - print("HERE 4") SnapshotWithBeamCentre.draw_crosshair(image, beam_x, beam_y) - print("HERE 5") await self._save_image(image) diff --git a/tests/devices/unit_tests/oav/test_snapshots.py b/tests/devices/unit_tests/oav/test_snapshots.py index c87fd5e6d4..e8f5543825 100644 --- a/tests/devices/unit_tests/oav/test_snapshots.py +++ b/tests/devices/unit_tests/oav/test_snapshots.py @@ -28,17 +28,16 @@ async def snapshot() -> SnapshotWithBeamCentre: "dodal.devices.areadetector.plugins.MJPG_async.ClientSession.get", autospec=True, ) -# @patch("dodal.devices.areadetector.plugins.MJPG_async.aiofiles", autospec=True) +@patch("dodal.devices.areadetector.plugins.MJPG_async.aiofiles", autospec=True) async def test_given_snapshot_triggered_then_crosshair_drawn_and_file_saved( - # mock_aiofiles, - mock_get, patch_image_draw, patch_image, mock_mkdir, snapshot + mock_aiofiles, mock_get, patch_image_draw, patch_image, mock_mkdir, snapshot ): mock_get.return_value.__aenter__.return_value = (mock_response := AsyncMock()) mock_response.ok = MagicMock(return_value=True) mock_response.read.return_value = (test_data := b"TEST") - # mock_open = mock_aiofiles.open - # mock_open.return_value.__aenter__.return_value = (mock_file := AsyncMock()) + mock_aio_open = mock_aiofiles.open + mock_aio_open.return_value.__aenter__.return_value = (mock_file := AsyncMock()) patch_line = MagicMock() patch_image_draw.Draw.return_value.line = patch_line @@ -52,5 +51,6 @@ async def test_given_snapshot_triggered_then_crosshair_drawn_and_file_saved( await snapshot.trigger() assert len(patch_line.mock_calls) == 2 - # mock_open.assert_called_once_with("/tmp/file.png", "wb") - # mock_file.write.assert_called_once_with(test_data) + assert await snapshot.last_saved_path.get_value() == "/tmp/test.png" + mock_aio_open.assert_called_once_with("/tmp/test.png", "wb") + mock_file.write.assert_called_once()