Skip to content

Commit 5a08eda

Browse files
seismanweiji14
andauthored
Turn the big test into a class with multiple sub-tests
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
1 parent 829f8b4 commit 5a08eda

File tree

1 file changed

+54
-32
lines changed

1 file changed

+54
-32
lines changed

pygmt/tests/test_clib_loading.py

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,18 @@ def test_load_libgmt_with_a_bad_library_path(monkeypatch):
8989
assert check_libgmt(load_libgmt()) is None
9090

9191

92-
def test_load_libgmt_with_broken_libraries(monkeypatch):
92+
class TestLibgmtBrokenLibs:
9393
"""
94-
Test load_libgmt still works when a broken library is found.
94+
Test that load_libgmt still works when a broken library is found.
9595
"""
96+
9697
# load the GMT library before mocking the ctypes.CDLL function
9798
loaded_libgmt = load_libgmt()
99+
invalid_path = "/invalid/path/to/libgmt.so"
100+
faked_libgmt1 = FakedLibGMT("/path/to/faked/libgmt1.so")
101+
faked_libgmt2 = FakedLibGMT("/path/to/faked/libgmt2.so")
98102

99-
def mock_ctypes_cdll_return(libname):
103+
def _mock_ctypes_cdll_return(self, libname):
100104
"""
101105
Mock the return value of ctypes.CDLL.
102106
@@ -119,54 +123,72 @@ def mock_ctypes_cdll_return(libname):
119123
# raise OSError like the original ctypes.CDLL
120124
raise OSError(f"Unable to find '{libname}'")
121125
# libname is a loaded GMT library
122-
return loaded_libgmt
126+
return self.loaded_libgmt
123127

124-
with monkeypatch.context() as mpatch:
125-
# pylint: disable=protected-access
126-
# mock the ctypes.CDLL function using mock_ctypes_cdll_return()
127-
mpatch.setattr(ctypes, "CDLL", mock_ctypes_cdll_return)
128+
@pytest.fixture
129+
def mock_ctypes(self, monkeypatch):
130+
monkeypatch.setattr(ctypes, "CDLL", self._mock_ctypes_cdll_return)
128131

129-
faked_libgmt1 = FakedLibGMT("/path/to/faked/libgmt1.so")
130-
faked_libgmt2 = FakedLibGMT("/path/to/faked/libgmt2.so")
132+
def test_two_broken_libraries(self, mock_ctypes):
133+
"""
134+
Case 1: two broken libraries.
131135
132-
# case 1: two broken libraries
133-
# Raise the GMTCLibNotFoundError exception
134-
# The error message should contain information of both libraries
135-
lib_fullnames = [faked_libgmt1, faked_libgmt2]
136+
Raise the GMTCLibNotFoundError exception. Error message should contain
137+
information of both libraries that failed to load properly.
138+
"""
139+
lib_fullnames = [self.faked_libgmt1, self.faked_libgmt2]
136140
msg_regex = (
137-
fr"Error loading GMT shared library at '{faked_libgmt1._name}'.\n"
138-
fr"Error loading '{faked_libgmt1._name}'. Couldn't access.*\n"
139-
fr"Error loading GMT shared library at '{faked_libgmt2._name}'.\n"
140-
fr"Error loading '{faked_libgmt2._name}'. Couldn't access.*"
141+
fr"Error loading GMT shared library at '{self.faked_libgmt1._name}'.\n"
142+
fr"Error loading '{self.faked_libgmt1._name}'. Couldn't access.*\n"
143+
fr"Error loading GMT shared library at '{self.faked_libgmt2._name}'.\n"
144+
f"Error loading '{self.faked_libgmt2._name}'. Couldn't access.*"
141145
)
142146
with pytest.raises(GMTCLibNotFoundError, match=msg_regex):
143147
load_libgmt(lib_fullnames=lib_fullnames)
144148

145-
# case 2: broken library + invalid path
146-
lib_fullnames = [faked_libgmt1, "/invalid/path/to/libgmt.so"]
149+
def test_load_brokenlib_invalidpath(self, mock_ctypes):
150+
"""
151+
Case 2: broken library + invalid path.
152+
153+
Raise the GMTCLibNotFoundError exception. Error message should contain
154+
information of one library that failed to load and one invalid path.
155+
"""
156+
lib_fullnames = [self.faked_libgmt1, self.invalid_path]
147157
msg_regex = (
148-
fr"Error loading GMT shared library at '{faked_libgmt1._name}'.\n"
149-
fr"Error loading '{faked_libgmt1._name}'. Couldn't access.*\n"
150-
"Error loading GMT shared library at '/invalid/path/to/libgmt.so'.\n"
151-
"Unable to find '/invalid/path/to/libgmt.so'"
158+
fr"Error loading GMT shared library at '{self.faked_libgmt1._name}'.\n"
159+
fr"Error loading '{self.faked_libgmt1._name}'. Couldn't access.*\n"
160+
fr"Error loading GMT shared library at '{self.invalid_path}'.\n"
161+
f"Unable to find '{self.invalid_path}'"
152162
)
153163
with pytest.raises(GMTCLibNotFoundError, match=msg_regex):
154164
load_libgmt(lib_fullnames=lib_fullnames)
155165

156-
# case 3: broken library + invalid path + working library
157-
lib_fullnames = [faked_libgmt1, "/invalid/path/to/libgmt.so", loaded_libgmt]
166+
def test_brokenlib_invalidpath_workinglib(self, mock_ctypes):
167+
"""
168+
Case 3: broken library + invalid path + working library.
169+
"""
170+
lib_fullnames = [self.faked_libgmt1, self.invalid_path, self.loaded_libgmt]
158171
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
159172

160-
# case 4: invalid path + broken library + working library
161-
lib_fullnames = ["/invalid/path/to/libgmt.so", faked_libgmt1, loaded_libgmt]
173+
def test_invalidpath_brokenlib_workinglib(self, mock_ctypes):
174+
"""
175+
Case 4: invalid path + broken library + working library.
176+
"""
177+
lib_fullnames = [self.invalid_path, self.faked_libgmt1, self.loaded_libgmt]
162178
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
163179

164-
# case 5: working library + broken library + invalid path
165-
lib_fullnames = [loaded_libgmt, faked_libgmt1, "/invalid/path/to/libgmt.so"]
180+
def test_workinglib_brokenlib_invalidpath(self, mock_ctypes):
181+
"""
182+
Case 5: working library + broken library + invalid path.
183+
"""
184+
lib_fullnames = [self.loaded_libgmt, self.faked_libgmt1, self.invalid_path]
166185
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
167186

168-
# case 6: repeating broken libraries + working library
169-
lib_fullnames = [faked_libgmt1, faked_libgmt1, loaded_libgmt]
187+
def test_brokenlib_brokenlib_workinglib(self, mock_ctypes):
188+
"""
189+
Case 6: repeating broken libraries + working library.
190+
"""
191+
lib_fullnames = [self.faked_libgmt1, self.faked_libgmt1, self.loaded_libgmt]
170192
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
171193

172194

0 commit comments

Comments
 (0)