improve coverage of tests

This commit is contained in:
lee-t
2025-05-19 21:57:54 -04:00
committed by Peter Cock
parent 377635ed52
commit 28f18b2d09
2 changed files with 26 additions and 8 deletions

View File

@ -290,7 +290,9 @@ class PDBList:
file_type = (
"pdb"
if file_format == "pdb"
else "mmCIF" if file_format == "mmCif" else "XML"
else "mmCIF"
if file_format == "mmCif"
else "XML"
)
url = (
self.pdb_server
@ -338,7 +340,7 @@ class PDBList:
urlretrieve(url, filename)
except OSError as e:
print(
f"Desired structure not found or doesn't exist:"
f"Desired structure not found or download failed."
f" '{pdb_code}': {str(e)}"
)
return None

View File

@ -160,18 +160,34 @@ class TestPDBListGetStructure(unittest.TestCase):
)
def test_retrieve_pdb_file_not_existing(self):
"""Tests retrieving a non-existent molecule returns None."""
"""Tests retrieving a non-existent molecule - returns None and prints error message."""
with self.make_temp_directory(os.getcwd()) as tmp:
pdblist = PDBList(pdb=tmp)
result = pdblist.retrieve_pdb_file("zzzz", file_format="pdb")
self.assertIsNone(result)
with unittest.patch("sys.stderr") as mock_stderr:
result = pdblist.retrieve_pdb_file("zzzz", file_format="pdb")
self.assertIsNone(result)
self.assertTrue(
any(
"Desired structure not found or download failed." in str(call)
for call in mock_stderr.write.call_args_list
),
"Error message not printed for non-existent molecule",
)
def test_retrieve_pdb_file_bad_url(self):
"""Tests retrieving with bad server URL returns None."""
"""Tests retrieving with bad server URL - returns None and prints error message."""
with self.make_temp_directory(os.getcwd()) as tmp:
pdblist = PDBList(server=" http://something.wrong ", pdb=tmp)
result = pdblist.retrieve_pdb_file("127d", file_format="pdb")
self.assertIsNone(result)
with unittest.patch("sys.stderr") as mock_stderr:
result = pdblist.retrieve_pdb_file("127d", file_format="pdb")
self.assertIsNone(result)
self.assertTrue(
any(
"Desired structure not found or download failed." in str(call)
for call in mock_stderr.write.call_args_list
),
"Error message not printed for bad server URL",
)
class TestPDBListGetAssembly(unittest.TestCase):