Skip to content

Commit

Permalink
get test to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
noemifrisina committed Oct 21, 2024
1 parent 51d45f7 commit 46c4a33
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
9 changes: 1 addition & 8 deletions src/dodal/devices/areadetector/plugins/MJPG_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,20 @@ 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():
LOGGER.info(f"Snapshot folder {directory_str} does not exist, creating...")
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
Expand All @@ -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}")

Expand Down
3 changes: 0 additions & 3 deletions src/dodal/devices/oav/snapshots/snapshot_with_beam_centre.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 7 additions & 7 deletions tests/devices/unit_tests/oav/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

0 comments on commit 46c4a33

Please sign in to comment.