Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Testing Coverage for Numba Caching #1085

Merged
merged 8 commits into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions test/test_utils.py
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)
Loading