-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Testing Coverage for Numba Caching (#1085)
* Added tests for numba and computing * Added test cases --------- Co-authored-by: Philip Chmielowiec <67855069+philipc2@users.noreply.github.com> Co-authored-by: Philip Chmielowiec <philipchmielowiec@gmail.com>
- Loading branch information
1 parent
6557e1c
commit 283febf
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import sys | ||
|
||
from uxarray.utils.numba import is_numba_function_cached | ||
from unittest import TestCase | ||
|
||
import unittest | ||
from unittest.mock import MagicMock, patch, mock_open | ||
import os | ||
import pickle | ||
import numba | ||
from uxarray.utils import computing | ||
import numpy as np | ||
|
||
|
||
class TestNumba(TestCase): | ||
@patch("os.path.isfile") | ||
@patch("builtins.open", new_callable=mock_open) | ||
def test_numba_function_cached_valid_cache(self, mock_open_func, mock_isfile): | ||
# Mock setup | ||
mock_isfile.return_value = True | ||
mock_open_func.return_value.__enter__.return_value.read = MagicMock(return_value=pickle.dumps(("stamp", None))) | ||
|
||
mock_func = MagicMock() | ||
mock_func._cache._cache_path = "/mock/cache/path" | ||
mock_func._cache._cache_file._index_name = "mock_cache.pkl" | ||
mock_func._cache._impl.locator.get_source_stamp.return_value = "stamp" | ||
|
||
# Mock pickle version load | ||
with patch("pickle.load", side_effect=[numba.__version__]): | ||
result = is_numba_function_cached(mock_func) | ||
|
||
self.assertTrue(result) | ||
|
||
@patch("os.path.isfile") | ||
@patch("builtins.open", new_callable=mock_open) | ||
def test_numba_function_cached_invalid_cache_version(self, mock_open_func, mock_isfile): | ||
# Mock setup | ||
mock_isfile.return_value = True | ||
mock_open_func.return_value.__enter__.return_value.read = MagicMock(return_value=pickle.dumps(("stamp", None))) | ||
|
||
mock_func = MagicMock() | ||
mock_func._cache._cache_path = "/mock/cache/path" | ||
mock_func._cache._cache_file._index_name = "mock_cache.pkl" | ||
mock_func._cache._impl.locator.get_source_stamp.return_value = "stamp" | ||
|
||
# Mock pickle version load with mismatched version | ||
with patch("pickle.load", side_effect=["invalid_version"]): | ||
result = is_numba_function_cached(mock_func) | ||
|
||
self.assertFalse(result) | ||
|
||
@patch("os.path.isfile") | ||
def test_numba_function_cached_no_cache_file(self, mock_isfile): | ||
# Mock setup | ||
mock_isfile.return_value = False | ||
|
||
mock_func = MagicMock() | ||
mock_func._cache._cache_path = "/mock/cache/path" | ||
mock_func._cache._cache_file._index_name = "mock_cache.pkl" | ||
|
||
result = is_numba_function_cached(mock_func) | ||
self.assertFalse(result) | ||
|
||
def test_numba_function_cached_no_cache_attribute(self): | ||
mock_func = MagicMock() | ||
del mock_func._cache # Ensure _cache attribute does not exist | ||
|
||
result = is_numba_function_cached(mock_func) | ||
self.assertTrue(result) | ||
|
||
@patch("os.path.isfile") | ||
@patch("builtins.open", new_callable=mock_open) | ||
def test_numba_function_cached_invalid_stamp(self, mock_open_func, mock_isfile): | ||
# Mock setup | ||
mock_isfile.return_value = True | ||
mock_open_func.return_value.__enter__.return_value.read = MagicMock( | ||
return_value=pickle.dumps(("invalid_stamp", None))) | ||
|
||
mock_func = MagicMock() | ||
mock_func._cache._cache_path = "/mock/cache/path" | ||
mock_func._cache._cache_file._index_name = "mock_cache.pkl" | ||
mock_func._cache._impl.locator.get_source_stamp.return_value = "stamp" | ||
|
||
# Mock pickle version load | ||
with patch("pickle.load", side_effect=[numba.__version__]): | ||
result = is_numba_function_cached(mock_func) | ||
|
||
self.assertFalse(result) |