|
1 | 1 | import contextlib |
2 | | -from io import StringIO |
| 2 | +from io import BufferedReader, StringIO, TextIOWrapper |
3 | 3 | from tempfile import NamedTemporaryFile |
4 | 4 | from unittest import IsolatedAsyncioTestCase, mock |
5 | 5 |
|
@@ -110,24 +110,55 @@ def test_banner_did(self): |
110 | 110 | def test_load_resource(self): |
111 | 111 | # Testing local file access |
112 | 112 | with mock.patch("builtins.open", mock.MagicMock()) as mock_open: |
113 | | - test_module.load_resource("abc", encoding="utf-8") |
| 113 | + # First call succeeds |
| 114 | + file_handle = mock.MagicMock(spec=TextIOWrapper) |
| 115 | + mock_open.return_value = file_handle |
| 116 | + result = test_module.load_resource("abc", encoding="utf-8") |
| 117 | + mock_open.assert_called_once_with("abc", encoding="utf-8") |
| 118 | + assert result == file_handle # Verify the returned file handle |
| 119 | + |
| 120 | + mock_open.reset_mock() |
| 121 | + # Simulate IOError on second call |
114 | 122 | mock_open.side_effect = IOError("insufficient privilege") |
115 | 123 | # load_resource should absorb IOError |
116 | | - test_module.load_resource("abc", encoding="utf-8") |
| 124 | + result = test_module.load_resource("abc", encoding="utf-8") |
| 125 | + mock_open.assert_called_once_with("abc", encoding="utf-8") |
| 126 | + assert result is None |
117 | 127 |
|
118 | 128 | # Testing package resource access with encoding (text mode) |
119 | | - with mock.patch( |
120 | | - "importlib.resources.open_binary", mock.MagicMock() |
121 | | - ) as mock_open_binary, mock.patch( |
| 129 | + with mock.patch("importlib.resources.files") as mock_files, mock.patch( |
122 | 130 | "io.TextIOWrapper", mock.MagicMock() |
123 | 131 | ) as mock_text_io_wrapper: |
124 | | - test_module.load_resource("abc:def", encoding="utf-8") |
125 | | - mock_open_binary.assert_called_once_with("abc", "def") |
126 | | - mock_text_io_wrapper.assert_called_once() |
| 132 | + # Setup the mocks |
| 133 | + mock_resource_path = mock.MagicMock() |
| 134 | + mock_files.return_value.joinpath.return_value = mock_resource_path |
| 135 | + mock_resource_handle = mock.MagicMock(spec=BufferedReader) |
| 136 | + mock_resource_path.open.return_value = mock_resource_handle |
| 137 | + mock_text_io_wrapper.return_value = mock.MagicMock(spec=TextIOWrapper) |
| 138 | + |
| 139 | + result = test_module.load_resource("abc:def", encoding="utf-8") |
| 140 | + |
| 141 | + # Assertions |
| 142 | + mock_files.assert_called_once_with("abc") |
| 143 | + mock_files.return_value.joinpath.assert_called_once_with("def") |
| 144 | + mock_resource_path.open.assert_called_once_with("rb") |
| 145 | + mock_text_io_wrapper.assert_called_once_with( |
| 146 | + mock_resource_handle, encoding="utf-8" |
| 147 | + ) |
| 148 | + assert result is mock_text_io_wrapper.return_value |
127 | 149 |
|
128 | 150 | # Testing package resource access without encoding (binary mode) |
129 | | - with mock.patch( |
130 | | - "importlib.resources.open_binary", mock.MagicMock() |
131 | | - ) as mock_open_binary: |
132 | | - test_module.load_resource("abc:def", encoding=None) |
133 | | - mock_open_binary.assert_called_once_with("abc", "def") |
| 151 | + with mock.patch("importlib.resources.files") as mock_files: |
| 152 | + # Setup the mocks |
| 153 | + mock_resource_path = mock.MagicMock() |
| 154 | + mock_files.return_value.joinpath.return_value = mock_resource_path |
| 155 | + mock_resource_handle = mock.MagicMock(spec=BufferedReader) |
| 156 | + mock_resource_path.open.return_value = mock_resource_handle |
| 157 | + |
| 158 | + result = test_module.load_resource("abc:def", encoding=None) |
| 159 | + |
| 160 | + # Assertions |
| 161 | + mock_files.assert_called_once_with("abc") |
| 162 | + mock_files.return_value.joinpath.assert_called_once_with("def") |
| 163 | + mock_resource_path.open.assert_called_once_with("rb") |
| 164 | + assert result == mock_resource_handle # Verify the returned binary stream |
0 commit comments