|
| 1 | +################################################################################# |
| 2 | +# FOQUS Copyright (c) 2012 - 2025, by the software owners: Oak Ridge Institute |
| 3 | +# for Science and Education (ORISE), TRIAD National Security, LLC., Lawrence |
| 4 | +# Livermore National Security, LLC., The Regents of the University of |
| 5 | +# California, through Lawrence Berkeley National Laboratory, Battelle Memorial |
| 6 | +# Institute, Pacific Northwest Division through Pacific Northwest National |
| 7 | +# Laboratory, Carnegie Mellon University, West Virginia University, Boston |
| 8 | +# University, the Trustees of Princeton University, The University of Texas at |
| 9 | +# Austin, URS Energy & Construction, Inc., et al. All rights reserved. |
| 10 | +# |
| 11 | +# Please see the file LICENSE.md for full copyright and license information, |
| 12 | +# respectively. This file is also available online at the URL |
| 13 | +# "https://github.com/CCSI-Toolset/FOQUS". |
| 14 | +################################################################################# |
| 15 | +import os |
| 16 | +import tempfile |
| 17 | +import unittest |
| 18 | +from unittest.mock import Mock, patch |
| 19 | + |
| 20 | +from foqus_lib.framework.uq.Common import Common |
| 21 | + |
| 22 | + |
| 23 | +class TestCommon(unittest.TestCase): |
| 24 | + """Test cases for the Common class""" |
| 25 | + |
| 26 | + def test_get_file_name_root(self): |
| 27 | + """Test extracting filename root""" |
| 28 | + self.assertEqual(Common.getFileNameRoot("test.dat"), "test") |
| 29 | + self.assertEqual(Common.getFileNameRoot("/path/to/file.psuade"), "file") |
| 30 | + self.assertEqual(Common.getFileNameRoot("complex.file.name.txt"), "complex") |
| 31 | + |
| 32 | + def test_get_local_filename(self): |
| 33 | + """Test generating local filename""" |
| 34 | + result = Common.getLocalFileName("/tmp", "test.dat", "_processed") |
| 35 | + expected = "/tmp" + os.path.sep + "test_processed" |
| 36 | + self.assertEqual(result, expected) |
| 37 | + |
| 38 | + @patch("os.path.exists") |
| 39 | + @patch("os.mkdir") |
| 40 | + @patch("os.listdir") |
| 41 | + @patch("os.unlink") |
| 42 | + def test_init_folder(self, mock_unlink, mock_listdir, mock_mkdir, mock_exists): |
| 43 | + """Test folder initialization""" |
| 44 | + # Test creating new folder |
| 45 | + mock_exists.return_value = False |
| 46 | + Common.initFolder("/test/path") |
| 47 | + mock_mkdir.assert_called_once_with("/test/path") |
| 48 | + |
| 49 | + # Test cleaning existing folder |
| 50 | + mock_exists.return_value = True |
| 51 | + mock_listdir.return_value = ["file1.txt", "file2.dat"] |
| 52 | + with patch("os.path.isfile", return_value=True): |
| 53 | + Common.initFolder("/test/path", deleteFiles=True) |
| 54 | + self.assertEqual(mock_unlink.call_count, 2) |
| 55 | + |
| 56 | + @patch("foqus_lib.framework.uq.Common.usePyside", False) |
| 57 | + @patch("builtins.print") |
| 58 | + def test_show_error_no_pyside(self, mock_print): |
| 59 | + """Test error display without PyQt""" |
| 60 | + Common.showError("Test error message") |
| 61 | + mock_print.assert_called_once_with("Test error message") |
| 62 | + |
| 63 | + def test_get_user_regression_output_name(self): |
| 64 | + """Test getting user regression output name""" |
| 65 | + # Create a mock data object |
| 66 | + mock_data = Mock() |
| 67 | + |
| 68 | + # Test case 1: When labels contain the modified name (with underscore) |
| 69 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as f: |
| 70 | + f.write("# Test regression file\n") |
| 71 | + f.write("labels = ['node_output1', 'node_output2']\n") |
| 72 | + temp_file1 = f.name |
| 73 | + |
| 74 | + try: |
| 75 | + mock_data.getNamesIncludeNodes.return_value = False |
| 76 | + result = Common.getUserRegressionOutputName( |
| 77 | + "node.output1", temp_file1, mock_data |
| 78 | + ) |
| 79 | + self.assertEqual(result, "node_output1") # Should return the modified name |
| 80 | + |
| 81 | + # Even when getNamesIncludeNodes is True, if the modified name is found in labels, |
| 82 | + # it should still return the modified name |
| 83 | + mock_data.getNamesIncludeNodes.return_value = True |
| 84 | + result = Common.getUserRegressionOutputName( |
| 85 | + "node.output1", temp_file1, mock_data |
| 86 | + ) |
| 87 | + self.assertEqual( |
| 88 | + result, "node_output1" |
| 89 | + ) # Should still return modified name because it's in labels |
| 90 | + finally: |
| 91 | + os.unlink(temp_file1) |
| 92 | + |
| 93 | + # Test case 2: When data has names that include nodes but labels don't match |
| 94 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as f: |
| 95 | + f.write("# Test regression file\n") |
| 96 | + f.write( |
| 97 | + "labels = ['other_output1', 'other_output2']\n" |
| 98 | + ) # Different labels that don't match |
| 99 | + temp_file2 = f.name |
| 100 | + |
| 101 | + try: |
| 102 | + mock_data.getNamesIncludeNodes.return_value = True |
| 103 | + result = Common.getUserRegressionOutputName( |
| 104 | + "node.output1", temp_file2, mock_data |
| 105 | + ) |
| 106 | + self.assertEqual(result, "output1") # Should strip the node part |
| 107 | + |
| 108 | + # Test case 3: When data doesn't include nodes and no match in labels |
| 109 | + mock_data.getNamesIncludeNodes.return_value = False |
| 110 | + result = Common.getUserRegressionOutputName( |
| 111 | + "node.output1", temp_file2, mock_data |
| 112 | + ) |
| 113 | + self.assertEqual(result, "node.output1") # Should return original name |
| 114 | + finally: |
| 115 | + os.unlink(temp_file2) |
| 116 | + |
| 117 | + def test_get_user_regression_output_name_no_labels(self): |
| 118 | + """Test getting user regression output name when no labels line exists""" |
| 119 | + mock_data = Mock() |
| 120 | + mock_data.getNamesIncludeNodes.return_value = True |
| 121 | + |
| 122 | + # Create a temporary regression file without labels |
| 123 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as f: |
| 124 | + f.write("# Test regression file\n") |
| 125 | + f.write("# No labels line here\n") |
| 126 | + temp_file = f.name |
| 127 | + |
| 128 | + try: |
| 129 | + result = Common.getUserRegressionOutputName( |
| 130 | + "node.output1", temp_file, mock_data |
| 131 | + ) |
| 132 | + self.assertEqual( |
| 133 | + result, "output1" |
| 134 | + ) # Should strip node part when names include nodes |
| 135 | + finally: |
| 136 | + os.unlink(temp_file) |
0 commit comments