|
| 1 | +"""Tests for the exceptions module.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +import github3.exceptions |
| 7 | +from exceptions import OptionalFileNotFoundError, check_optional_file |
| 8 | + |
| 9 | + |
| 10 | +class TestOptionalFileNotFoundError(unittest.TestCase): |
| 11 | + """Test the OptionalFileNotFoundError exception.""" |
| 12 | + |
| 13 | + def test_optional_file_not_found_error_inherits_from_not_found_error(self): |
| 14 | + """Test that OptionalFileNotFoundError inherits from github3.exceptions.NotFoundError.""" |
| 15 | + mock_resp = Mock() |
| 16 | + mock_resp.status_code = 404 |
| 17 | + error = OptionalFileNotFoundError(resp=mock_resp) |
| 18 | + self.assertIsInstance(error, github3.exceptions.NotFoundError) |
| 19 | + |
| 20 | + def test_optional_file_not_found_error_creation(self): |
| 21 | + """Test OptionalFileNotFoundError can be created.""" |
| 22 | + mock_resp = Mock() |
| 23 | + mock_resp.status_code = 404 |
| 24 | + error = OptionalFileNotFoundError(resp=mock_resp) |
| 25 | + self.assertIsInstance(error, OptionalFileNotFoundError) |
| 26 | + |
| 27 | + def test_optional_file_not_found_error_with_response(self): |
| 28 | + """Test OptionalFileNotFoundError with HTTP response.""" |
| 29 | + mock_resp = Mock() |
| 30 | + mock_resp.status_code = 404 |
| 31 | + error = OptionalFileNotFoundError(resp=mock_resp) |
| 32 | + |
| 33 | + # Should be created successfully |
| 34 | + self.assertIsInstance(error, OptionalFileNotFoundError) |
| 35 | + |
| 36 | + def test_can_catch_as_github3_not_found_error(self): |
| 37 | + """Test that OptionalFileNotFoundError can be caught as github3.exceptions.NotFoundError.""" |
| 38 | + mock_resp = Mock() |
| 39 | + mock_resp.status_code = 404 |
| 40 | + |
| 41 | + try: |
| 42 | + raise OptionalFileNotFoundError(resp=mock_resp) |
| 43 | + except github3.exceptions.NotFoundError as e: |
| 44 | + self.assertIsInstance(e, OptionalFileNotFoundError) |
| 45 | + except Exception: # pylint: disable=broad-exception-caught |
| 46 | + self.fail( |
| 47 | + "OptionalFileNotFoundError should be catchable as github3.exceptions.NotFoundError" |
| 48 | + ) |
| 49 | + |
| 50 | + def test_can_catch_specifically(self): |
| 51 | + """Test that OptionalFileNotFoundError can be caught specifically.""" |
| 52 | + mock_resp = Mock() |
| 53 | + mock_resp.status_code = 404 |
| 54 | + |
| 55 | + try: |
| 56 | + raise OptionalFileNotFoundError(resp=mock_resp) |
| 57 | + except OptionalFileNotFoundError as e: |
| 58 | + self.assertIsInstance(e, OptionalFileNotFoundError) |
| 59 | + except Exception: # pylint: disable=broad-exception-caught |
| 60 | + self.fail("OptionalFileNotFoundError should be catchable specifically") |
| 61 | + |
| 62 | + def test_optional_file_not_found_error_properties(self): |
| 63 | + """Test OptionalFileNotFoundError has expected properties.""" |
| 64 | + mock_resp = Mock() |
| 65 | + mock_resp.status_code = 404 |
| 66 | + |
| 67 | + error = OptionalFileNotFoundError(resp=mock_resp) |
| 68 | + self.assertEqual(error.code, 404) |
| 69 | + self.assertEqual(error.response, mock_resp) |
| 70 | + |
| 71 | + |
| 72 | +class TestCheckOptionalFile(unittest.TestCase): |
| 73 | + """Test the check_optional_file utility function.""" |
| 74 | + |
| 75 | + def test_check_optional_file_with_existing_file(self): |
| 76 | + """Test check_optional_file when file exists.""" |
| 77 | + mock_repo = Mock() |
| 78 | + mock_file_contents = Mock() |
| 79 | + mock_file_contents.size = 100 |
| 80 | + mock_repo.file_contents.return_value = mock_file_contents |
| 81 | + |
| 82 | + result = check_optional_file(mock_repo, "config.yml") |
| 83 | + |
| 84 | + self.assertEqual(result, mock_file_contents) |
| 85 | + mock_repo.file_contents.assert_called_once_with("config.yml") |
| 86 | + |
| 87 | + def test_check_optional_file_with_empty_file(self): |
| 88 | + """Test check_optional_file when file exists but is empty.""" |
| 89 | + mock_repo = Mock() |
| 90 | + mock_file_contents = Mock() |
| 91 | + mock_file_contents.size = 0 |
| 92 | + mock_repo.file_contents.return_value = mock_file_contents |
| 93 | + |
| 94 | + result = check_optional_file(mock_repo, "config.yml") |
| 95 | + |
| 96 | + self.assertIsNone(result) |
| 97 | + mock_repo.file_contents.assert_called_once_with("config.yml") |
| 98 | + |
| 99 | + def test_check_optional_file_with_missing_file(self): |
| 100 | + """Test check_optional_file when file doesn't exist.""" |
| 101 | + mock_repo = Mock() |
| 102 | + mock_resp = Mock() |
| 103 | + mock_resp.status_code = 404 |
| 104 | + |
| 105 | + original_error = github3.exceptions.NotFoundError(resp=mock_resp) |
| 106 | + mock_repo.file_contents.side_effect = original_error |
| 107 | + |
| 108 | + with self.assertRaises(OptionalFileNotFoundError) as context: |
| 109 | + check_optional_file(mock_repo, "missing.yml") |
| 110 | + |
| 111 | + # Check that the original exception is chained |
| 112 | + self.assertEqual(context.exception.__cause__, original_error) |
| 113 | + self.assertEqual(context.exception.response, mock_resp) |
| 114 | + mock_repo.file_contents.assert_called_once_with("missing.yml") |
| 115 | + |
| 116 | + def test_check_optional_file_can_catch_as_not_found_error(self): |
| 117 | + """Test that OptionalFileNotFoundError from check_optional_file can be caught as NotFoundError.""" |
| 118 | + mock_repo = Mock() |
| 119 | + mock_resp = Mock() |
| 120 | + mock_resp.status_code = 404 |
| 121 | + |
| 122 | + mock_repo.file_contents.side_effect = github3.exceptions.NotFoundError( |
| 123 | + resp=mock_resp |
| 124 | + ) |
| 125 | + |
| 126 | + try: |
| 127 | + check_optional_file(mock_repo, "missing.yml") |
| 128 | + except github3.exceptions.NotFoundError as e: |
| 129 | + self.assertIsInstance(e, OptionalFileNotFoundError) |
| 130 | + except Exception: # pylint: disable=broad-exception-caught |
| 131 | + self.fail( |
| 132 | + "Should be able to catch OptionalFileNotFoundError as NotFoundError" |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + unittest.main() |
0 commit comments