From c71e6ba67b449850ee9f264d8fdebc51bd19f9aa Mon Sep 17 00:00:00 2001 From: Aaron Zedwick Date: Fri, 22 Nov 2024 12:01:18 -0600 Subject: [PATCH 1/2] Added tests for numba and computing --- test/test_computing.py | 10 +++++ test/test_utils.py | 88 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 test/test_utils.py diff --git a/test/test_computing.py b/test/test_computing.py index a83d3b76c..b9ab39fbc 100644 --- a/test/test_computing.py +++ b/test/test_computing.py @@ -62,6 +62,11 @@ def test_two_prod_fma(self): self.assertEqual(y, pyfma.fma(a, b, -x)) self.assertAlmostEqual(a * b, x + y, places=15) + def test_comp_prod_fma(self): + # Test `_comp_prod_fma` + result = ac_utils._comp_prod_fma([1.1, 2.2, 3.3]) + self.assertEqual(result, 7.986000000000001) + def test_fast_two_mult(self): """Test the two_prod_fma function.""" a = 1.0 @@ -127,3 +132,8 @@ def test_two_square(self): a = 10.0 res = ac_utils._two_square(a) self.assertAlmostEqual(a * a, res[0], places=15) + + def test_norm_g(self): + # Test `_norm_g` + result = ac_utils._norm_g([1.0, 2.0, 3.0]) + self.assertEqual(result, 3.7416573867739413) diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 000000000..b52c5d32d --- /dev/null +++ b/test/test_utils.py @@ -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) From fa841f4fbbd78ade20905afd544ec86372db19c6 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick Date: Tue, 26 Nov 2024 19:31:21 -0600 Subject: [PATCH 2/2] Added test cases --- test/test_grid.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/test_grid.py b/test/test_grid.py index 81db37193..15404875a 100644 --- a/test/test_grid.py +++ b/test/test_grid.py @@ -74,7 +74,6 @@ def test_encode_as(self): self.grid_RLL1deg.encode_as("Exodus") self.grid_RLL10deg_CSne4.encode_as("Exodus") - def test_init_verts(self): """Create a uxarray grid from multiple face vertices with duplicate nodes and saves a ugrid file. @@ -258,6 +257,20 @@ def test_grid_properties(self): self.assertEqual(n_faces, grid_geoflow.n_face) self.assertEqual(n_face_nodes, grid_geoflow.n_max_face_nodes) + # Test max properties + self.assertEqual(self.grid_CSne30.n_max_face_faces, self.grid_CSne30.face_face_connectivity.shape[1]) + + # Building `edge_edge_connectivity` not yet supported, nor do any grids currently have this natively + # self.assertEqual(self.grid_CSne30.n_max_edge_edges, self.grid_CSne30.edge_edge_connectivity.shape[1]) + # Building also not supported for node_edge + # self.assertEqual(self.grid_CSne30.n_max_node_edges, self.grid_CSne30.node_edge_connectivity.shape[1]) + self.assertEqual(self.grid_CSne30.n_max_face_nodes, self.grid_CSne30.face_node_connectivity.shape[1]) + self.assertEqual(self.grid_CSne30.n_max_face_edges, self.grid_CSne30.face_edge_connectivity.shape[1]) + self.assertEqual(self.grid_CSne30.n_max_node_faces, self.grid_CSne30.node_face_connectivity.shape[1]) + + n_nodes_per_face = self.grid_CSne30.n_nodes_per_face + self.grid_CSne30.n_nodes_per_face = n_nodes_per_face + def test_read_shpfile(self): """Reads a shape file and write ugrid file.""" with self.assertRaises(ValueError): @@ -269,6 +282,14 @@ def test_read_scrip(self): # Test read from scrip and from ugrid for grid class grid_CSne8 = ux.open_grid(gridfile_CSne8) # tests from scrip + def test_value_errors(self): + """Tests that the proper value errors are raised when incorrect inputs are used""" + + # Create invalid grid + invalid_grid = xr.Dataset() + + self.assertRaises(RuntimeError, ux.open_grid, invalid_grid) + class TestOperators(TestCase): grid_CSne30_01 = ux.open_grid(gridfile_CSne30)