|
| 1 | +from pathlib import Path |
1 | 2 | from unittest import mock |
| 3 | +from unittest.mock import MagicMock |
2 | 4 |
|
| 5 | +import numpy as np |
| 6 | +import PIL.Image |
| 7 | +import pytest |
| 8 | +from pytest_mock import MockerFixture |
3 | 9 | from sqlmodel import Session, select |
4 | 10 |
|
5 | | -from murfey.server.api.workflow import DCGroupParameters, register_dc_group |
| 11 | +from murfey.server.api.workflow import ( |
| 12 | + DCGroupParameters, |
| 13 | + MillingParameters, |
| 14 | + make_gif, |
| 15 | + register_dc_group, |
| 16 | +) |
6 | 17 | from murfey.util.db import DataCollectionGroup, SearchMap |
7 | 18 | from tests.conftest import ExampleVisit |
8 | 19 |
|
@@ -427,3 +438,74 @@ def test_register_dc_group_new_atlas_with_searchmaps( |
427 | 438 | murfey_db_session, |
428 | 439 | close_db=False, |
429 | 440 | ) |
| 441 | + |
| 442 | + |
| 443 | +@pytest.mark.asyncio |
| 444 | +async def test_make_gif( |
| 445 | + mocker: MockerFixture, |
| 446 | + tmp_path: Path, |
| 447 | +): |
| 448 | + # Set up test variables |
| 449 | + session_id = 10 |
| 450 | + instrument_name = "test_instrument" |
| 451 | + rsync_basepath = tmp_path / "data" |
| 452 | + visit_name = "cm12345-6" |
| 453 | + year = 2020 |
| 454 | + visit_dir = rsync_basepath / str(year) / visit_name |
| 455 | + lamella_num = 12 |
| 456 | + lamella_folder = "Lamella" |
| 457 | + if lamella_num > 1: |
| 458 | + lamella_folder += f" ({lamella_num})" |
| 459 | + raw_directory = "autotem" |
| 460 | + |
| 461 | + # Create a list of test image file paths |
| 462 | + raw_images = [ |
| 463 | + visit_dir |
| 464 | + / "autotem" |
| 465 | + / visit_name |
| 466 | + / "Sites" |
| 467 | + / lamella_folder |
| 468 | + / "DCImages/DCM_asdfjkl/asdfjkl-Polishing-dc_rescan-image-.png" |
| 469 | + ] * 5 |
| 470 | + # Mock the output of PIL.Image.open to always return a NumPY array |
| 471 | + mocker.patch( |
| 472 | + "murfey.server.api.workflow.Image.open", |
| 473 | + return_value=PIL.Image.fromarray(np.ones((512, 512), dtype=np.uint16)), |
| 474 | + ) |
| 475 | + |
| 476 | + # Create the Pydantic model |
| 477 | + params = MillingParameters( |
| 478 | + lamella_number=lamella_num, |
| 479 | + images=[str(f) for f in raw_images], |
| 480 | + raw_directory=raw_directory, |
| 481 | + ) |
| 482 | + |
| 483 | + # Mock the database query |
| 484 | + mock_db = MagicMock() |
| 485 | + mock_db.exec.return_value.one.return_value.instrument_name = instrument_name |
| 486 | + |
| 487 | + # Mock the machine config and 'get_machine_config' |
| 488 | + mock_machine_config = MagicMock() |
| 489 | + mock_machine_config.rsync_basepath = rsync_basepath |
| 490 | + mocker.patch( |
| 491 | + "murfey.server.api.workflow.get_machine_config", |
| 492 | + return_value={ |
| 493 | + instrument_name: mock_machine_config, |
| 494 | + }, |
| 495 | + ) |
| 496 | + |
| 497 | + # Create the save directory directory |
| 498 | + save_dir = visit_dir / "processed" / raw_directory |
| 499 | + save_dir.mkdir(parents=True, exist_ok=True) |
| 500 | + |
| 501 | + # Run the function and check that the expected outputs are there |
| 502 | + result = await make_gif( |
| 503 | + year=year, |
| 504 | + visit_name=visit_name, |
| 505 | + session_id=session_id, |
| 506 | + gif_params=params, |
| 507 | + db=mock_db, |
| 508 | + ) |
| 509 | + image_path = save_dir / f"lamella_{lamella_num}_milling.gif" |
| 510 | + assert image_path.exists() |
| 511 | + assert result.get("output_gif") == str(image_path) |
0 commit comments