|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import types |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from unittest import mock |
| 7 | + |
| 8 | +import qt_stubs # noqa: F401 |
| 9 | + |
| 10 | +# Provide a minimal vtk stub before importing settings |
| 11 | +vtk_stub = types.ModuleType("vtk") |
| 12 | + |
| 13 | + |
| 14 | +class DummyNamedColors: |
| 15 | + def GetColor3d(self, key): |
| 16 | + return (0.0, 0.0, 0.0) |
| 17 | + |
| 18 | + |
| 19 | +vtk_stub.vtkNamedColors = DummyNamedColors |
| 20 | +sys.modules["vtk"] = vtk_stub |
| 21 | + |
| 22 | +from src.settings import compare_files |
| 23 | + |
| 24 | + |
| 25 | +class CompareFilesTest(unittest.TestCase): |
| 26 | + def test_missing_first_file_returns_false(self): |
| 27 | + with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 28 | + temp_file.write(b"data") |
| 29 | + existing_path = temp_file.name |
| 30 | + missing_path = existing_path + "_missing" |
| 31 | + self.assertFalse(compare_files(missing_path, existing_path)) |
| 32 | + os.remove(existing_path) |
| 33 | + |
| 34 | + def test_missing_second_file_returns_false(self): |
| 35 | + with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 36 | + temp_file.write(b"data") |
| 37 | + existing_path = temp_file.name |
| 38 | + missing_path = existing_path + "_missing" |
| 39 | + self.assertFalse(compare_files(existing_path, missing_path)) |
| 40 | + os.remove(existing_path) |
| 41 | + |
| 42 | + def test_both_files_missing_returns_false(self): |
| 43 | + import uuid |
| 44 | + |
| 45 | + missing1 = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex) |
| 46 | + missing2 = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex) |
| 47 | + self.assertFalse(compare_files(missing1, missing2)) |
| 48 | + |
| 49 | + def test_file_not_found_during_read_returns_false(self): |
| 50 | + with tempfile.NamedTemporaryFile(delete=False) as f1: |
| 51 | + path1 = f1.name |
| 52 | + with tempfile.NamedTemporaryFile(delete=False) as f2: |
| 53 | + path2 = f2.name |
| 54 | + with mock.patch("builtins.open", side_effect=FileNotFoundError()): |
| 55 | + self.assertFalse(compare_files(path1, path2)) |
| 56 | + os.remove(path1) |
| 57 | + os.remove(path2) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + unittest.main() |
0 commit comments