Skip to content
Snippets Groups Projects
test_rq_json_loader.py 4.62 KiB
Newer Older
Simon Wittl's avatar
Simon Wittl committed
import pytest
import numpy as np
from pathlib import Path
from unittest.mock import MagicMock, patch

# Importing symbols from loader module
from rq_controller.common import PyProjection, PyProjectionGeometry, PyRegionOfIntrest, PyVolume
import pyometiff

from rq_controller.common.io.rq_json import RqJsonLoader


class MockOMETIFFReader:
    def __init__(self, data):
        self.data = data

    def read(self):
        return self.data, None, None


@pytest.fixture
def rq_json_loader() -> RqJsonLoader:
    return RqJsonLoader()


@pytest.fixture
def mock_load_json():
    # Mocking the load_json method to avoid actual file operations
    with patch.object(RqJsonLoader, 'load_json') as mock:
        mock.return_value = {
            'focal_spot_mm': np.array([1.0, 1.1, 1.2]),
            'detector_postion_mm': np.array([2.0, 2.1, 2.2]),
            'detector_orientation_quad': np.array([3.0, 3.1, 3.2]),
            'frame_id': 'frame1',
            'focal_spot_orientation_quad': np.array([4.0, 4.1, 4.2]),
            'detector_heigth_mm': 5.0,
            'detector_width_mm': 6.0,
            'voltage_kv': 7.0,
            'current_ua': 8.0,
            'exposure_time_ms': 9.0,
            'center_points_mm': [10.0, 11.0, 12.0],
            'dimensions_mm': [13.0, 14.0, 15.0],
            'resolution_mm': [16.0, 17.0, 18.0]
        }
        yield mock


def test_initialization(rq_json_loader):
    assert rq_json_loader.porjection_geometry_suffix == '.geom-json'
    assert rq_json_loader.projection_suffix == '.tif'
    assert rq_json_loader.region_of_intrest_suffix == '.roi-json'
    assert rq_json_loader.volume_suffix == '.ome.tiff'


def test_load_projection_geometry(rq_json_loader, mock_load_json):
    load_path = Path('/fake/path.tif')
    projection_geometry = rq_json_loader.load_projection_geometry(load_path)
    assert isinstance(projection_geometry, PyProjectionGeometry)
    # Add more assertions as needed based on your implementation


def test_load_projection(rq_json_loader, mock_load_json):
    load_path = Path('/fake/path.tif')
    projection = rq_json_loader.load_projection(load_path)
    assert isinstance(projection, PyProjection)
    # Add more assertions as needed based on your implementation


def test_load_region_of_intrest(rq_json_loader, mock_load_json):
    load_path = Path('/fake/path.roi-json')
    region_of_intrest = rq_json_loader.load_region_of_intrest(load_path)
    assert isinstance(region_of_intrest, PyRegionOfIntrest)
    # Add more assertions as needed based on your implementation


def test_load_volume(rq_json_loader, mock_load_json):
    load_path = Path('/fake/path.ome.tiff')
    with patch('pyometiff.OMETIFFReader', MockOMETIFFReader):
        volume = rq_json_loader.load_volume(load_path)
    assert isinstance(volume, PyVolume)
    # Add more assertions as needed based on your implementation


def test_load_projection_geometry_raises_not_implemented(rq_json_loader):
    load_path = Path('/fake/path.tif')
    with pytest.raises(NotImplementedError):
        rq_json_loader.load_projection_geometry(load_path)


def test_load_projection_raises_not_implemented(rq_json_loader):
    load_path = Path('/fake/path.tif')
    with pytest.raises(NotImplementedError):
        rq_json_loader.load_projection(load_path)


def test_load_region_of_intrest_raises_not_implemented(rq_json_loader):
    load_path = Path('/fake/path.roi-json')
    with pytest.raises(NotImplementedError):
        rq_json_loader.load_region_of_intrest(load_path)


def test_load_volume_raises_not_implemented(rq_json_loader):
    load_path = Path('/fake/path.ome.tiff')
    with pytest.raises(NotImplementedError):
        rq_json_loader.load_volume(load_path)


def test_load_json(rq_json_loader):
    load_path = Path('/fake/path.json')
    data_dict = {
        'focal_spot_mm': np.array([1.0]),
        'detector_postion_mm': np.array([2.0]),
        'detector_orientation_quad': np.array([3.0]),
        'frame_id': 'frame1',
        'focal_spot_orientation_quad': np.array([4.0])
    }
    with patch('builtins.open', MagicMock(return_value=MagicMock(spec=open))):
        with patch('json.load', MagicMock(return_value=data_dict)):
            result = rq_json_loader.load_json(load_path)
    assert result == data_dict


def test_load_json_file_not_found(rq_json_loader):
    load_path = Path('/fake/path.json')
    with pytest.raises(FileNotFoundError):
        rq_json_loader.load_json(load_path)


def test_load_volume_invalid_dtype(rq_json_loader, mock_load_json):
    load_path = Path('/fake/path.ome.tiff')
    with patch('pyometiff.OMETIFFReader', MockOMETIFFReader):
        with pytest.raises(ValueError):
            rq_json_loader.load_volume(load_path)