Skip to content

Commit

Permalink
Mock File Modification Time
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEAllen committed Aug 7, 2024
1 parent 4f63ace commit 1bb7d04
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions mantidimaging/eyes_tests/live_viewer_window_test.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations

from __future__ import annotations
from typing import TYPE_CHECKING
from unittest import mock

import numpy as np

import os
from mantidimaging.core.operations.loader import load_filter_packages
from mantidimaging.gui.windows.live_viewer.model import Image_Data
from mantidimaging.test_helpers.unit_test_helper import FakeFSTestCase
from pathlib import Path

from mantidimaging.eyes_tests.base_eyes import BaseEyesTest

if TYPE_CHECKING:
import time # noqa: F401


class LiveViewerWindowTest(FakeFSTestCase, BaseEyesTest):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.initial_time = 4000.0
os.environ['TZ'] = 'local_time_zone'

@classmethod
def setUpClass(cls) -> None:
super().setUpClass()
Expand All @@ -30,36 +37,42 @@ def setUp(self) -> None:
def _generate_image(self):
image = np.zeros((10, 10))
image[5, :] = np.arange(10)
os.utime(self.live_directory, (10, self.initial_time))
self.initial_time += 1000
return image

def _make_simple_dir(self, directory: Path):
file_list = [directory / f"abc_{i:06d}.tif" for i in range(5)]
if not directory.exists():
self.fs.create_dir(directory)

increment = 0
for file in file_list:
self.fs.create_file(file)
os.utime(file, (10, self.initial_time + increment))
increment += 1000

return file_list

@mock.patch('mantidimaging.gui.windows.live_viewer.model.ImageWatcher')
def test_live_view_opens_without_data(self, _mock_image_watcher):
@mock.patch("time.time", return_value=4000.0)
def test_live_view_opens_without_data(self, _mock_time, _mock_image_watcher):
self.imaging.show_live_viewer(self.live_directory)
self.check_target(widget=self.imaging.live_viewer)

@mock.patch('mantidimaging.gui.windows.live_viewer.presenter.LiveViewerWindowPresenter.load_image')
@mock.patch('mantidimaging.gui.windows.live_viewer.model.ImageWatcher')
def test_live_view_opens_with_data(self, _mock_image_watcher, mock_load_image):
@mock.patch("time.time", return_value=4000.0)
def test_live_view_opens_with_data(self, _mock_time, _mock_image_watcher, mock_load_image):
file_list = self._make_simple_dir(self.live_directory)
image_list = [Image_Data(path) for path in file_list]
mock_load_image.return_value = self._generate_image()
self.imaging.show_live_viewer(self.live_directory)
self.imaging.live_viewer.presenter.model._handle_image_changed_in_list(image_list)
self.assertEqual(self.imaging.live_viewer.label_active_filename.text(), "abc_000004.tif - 1970-01-01 02:13:20")
self.check_target(widget=self.imaging.live_viewer)

@mock.patch('mantidimaging.gui.windows.live_viewer.presenter.LiveViewerWindowPresenter.load_image')
@mock.patch('mantidimaging.gui.windows.live_viewer.model.ImageWatcher')
def test_live_view_opens_with_bad_data(self, _mock_image_watcher, mock_load_image):
@mock.patch("time.time", return_value=4000.0)
def test_live_view_opens_with_bad_data(self, _mock_time, _mock_image_watcher, mock_load_image):
file_list = self._make_simple_dir(self.live_directory)
image_list = [Image_Data(path) for path in file_list]
mock_load_image.side_effect = ValueError
Expand All @@ -69,7 +82,8 @@ def test_live_view_opens_with_bad_data(self, _mock_image_watcher, mock_load_imag

@mock.patch('mantidimaging.gui.windows.live_viewer.presenter.LiveViewerWindowPresenter.load_image')
@mock.patch('mantidimaging.gui.windows.live_viewer.model.ImageWatcher')
def test_rotate_operation_rotates_image(self, _mock_image_watcher, mock_load_image):
@mock.patch("time.time", return_value=4000.0)
def test_rotate_operation_rotates_image(self, _mock_time, _mock_image_watcher, mock_load_image):
file_list = self._make_simple_dir(self.live_directory)
image_list = [Image_Data(path) for path in file_list]
mock_load_image.return_value = self._generate_image()
Expand Down

0 comments on commit 1bb7d04

Please sign in to comment.