|
| 1 | +import sys |
| 2 | + |
| 3 | +from uxarray.utils.numba import is_numba_function_cached |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +import unittest |
| 7 | +from unittest.mock import MagicMock, patch, mock_open |
| 8 | +import os |
| 9 | +import pickle |
| 10 | +import numba |
| 11 | +from uxarray.utils import computing |
| 12 | +import numpy as np |
| 13 | + |
| 14 | + |
| 15 | +class TestNumba(TestCase): |
| 16 | + @patch("os.path.isfile") |
| 17 | + @patch("builtins.open", new_callable=mock_open) |
| 18 | + def test_numba_function_cached_valid_cache(self, mock_open_func, mock_isfile): |
| 19 | + # Mock setup |
| 20 | + mock_isfile.return_value = True |
| 21 | + mock_open_func.return_value.__enter__.return_value.read = MagicMock(return_value=pickle.dumps(("stamp", None))) |
| 22 | + |
| 23 | + mock_func = MagicMock() |
| 24 | + mock_func._cache._cache_path = "/mock/cache/path" |
| 25 | + mock_func._cache._cache_file._index_name = "mock_cache.pkl" |
| 26 | + mock_func._cache._impl.locator.get_source_stamp.return_value = "stamp" |
| 27 | + |
| 28 | + # Mock pickle version load |
| 29 | + with patch("pickle.load", side_effect=[numba.__version__]): |
| 30 | + result = is_numba_function_cached(mock_func) |
| 31 | + |
| 32 | + self.assertTrue(result) |
| 33 | + |
| 34 | + @patch("os.path.isfile") |
| 35 | + @patch("builtins.open", new_callable=mock_open) |
| 36 | + def test_numba_function_cached_invalid_cache_version(self, mock_open_func, mock_isfile): |
| 37 | + # Mock setup |
| 38 | + mock_isfile.return_value = True |
| 39 | + mock_open_func.return_value.__enter__.return_value.read = MagicMock(return_value=pickle.dumps(("stamp", None))) |
| 40 | + |
| 41 | + mock_func = MagicMock() |
| 42 | + mock_func._cache._cache_path = "/mock/cache/path" |
| 43 | + mock_func._cache._cache_file._index_name = "mock_cache.pkl" |
| 44 | + mock_func._cache._impl.locator.get_source_stamp.return_value = "stamp" |
| 45 | + |
| 46 | + # Mock pickle version load with mismatched version |
| 47 | + with patch("pickle.load", side_effect=["invalid_version"]): |
| 48 | + result = is_numba_function_cached(mock_func) |
| 49 | + |
| 50 | + self.assertFalse(result) |
| 51 | + |
| 52 | + @patch("os.path.isfile") |
| 53 | + def test_numba_function_cached_no_cache_file(self, mock_isfile): |
| 54 | + # Mock setup |
| 55 | + mock_isfile.return_value = False |
| 56 | + |
| 57 | + mock_func = MagicMock() |
| 58 | + mock_func._cache._cache_path = "/mock/cache/path" |
| 59 | + mock_func._cache._cache_file._index_name = "mock_cache.pkl" |
| 60 | + |
| 61 | + result = is_numba_function_cached(mock_func) |
| 62 | + self.assertFalse(result) |
| 63 | + |
| 64 | + def test_numba_function_cached_no_cache_attribute(self): |
| 65 | + mock_func = MagicMock() |
| 66 | + del mock_func._cache # Ensure _cache attribute does not exist |
| 67 | + |
| 68 | + result = is_numba_function_cached(mock_func) |
| 69 | + self.assertTrue(result) |
| 70 | + |
| 71 | + @patch("os.path.isfile") |
| 72 | + @patch("builtins.open", new_callable=mock_open) |
| 73 | + def test_numba_function_cached_invalid_stamp(self, mock_open_func, mock_isfile): |
| 74 | + # Mock setup |
| 75 | + mock_isfile.return_value = True |
| 76 | + mock_open_func.return_value.__enter__.return_value.read = MagicMock( |
| 77 | + return_value=pickle.dumps(("invalid_stamp", None))) |
| 78 | + |
| 79 | + mock_func = MagicMock() |
| 80 | + mock_func._cache._cache_path = "/mock/cache/path" |
| 81 | + mock_func._cache._cache_file._index_name = "mock_cache.pkl" |
| 82 | + mock_func._cache._impl.locator.get_source_stamp.return_value = "stamp" |
| 83 | + |
| 84 | + # Mock pickle version load |
| 85 | + with patch("pickle.load", side_effect=[numba.__version__]): |
| 86 | + result = is_numba_function_cached(mock_func) |
| 87 | + |
| 88 | + self.assertFalse(result) |
0 commit comments