Skip to content

Commit cc4398f

Browse files
committed
Refactored Tests -- put them in classes!
1 parent 3116310 commit cc4398f

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

testsuite/MDAnalysisTests/web/test_web.py

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,87 @@
11
import MDAnalysis as mda
2+
import filecmp
3+
4+
25
import pytest
36
import requests
47

58

69

7-
working_PDB_ID = '1DPX' # egg white lysozyme
8-
file_format="pdb"
10+
working_PDB_ID = '1DPX' # egg white lysozyme
11+
file_format = "pdb"
912

1013

14+
@pytest.fixture(scope="function")
15+
def shared_cache_directory(tmp_path_factory):
16+
"""Create Cache Directory"""
17+
return tmp_path_factory.mktemp("cache")
1118

12-
def test_fetch_pdb_base_functionality():
13-
"""Check default parameter of mda.fetch_pdb"""
14-
assert isinstance(mda.fetch_pdb(PDB_ID=working_PDB_ID), mda.Universe)
1519

20+
class Test_Pdb_Downloader_BaseFunctionality():
21+
"""Test Public API of Pdb_Downloader()"""
22+
23+
def test_base_functionality(self):
24+
"""Test keywords in convert_to_universe()"""
25+
downloader = mda.web.PdbDownloader(PDB_ID=working_PDB_ID).download()
26+
27+
assert(isinstance(downloader.convert_to_universe(in_memory=True), mda.Universe))
1628

17-
def test_fetch_pdb_timeout():
18-
"""Checks requests timeout Exception for fetch_pdb and PdbDownloader"""
19-
with pytest.raises(requests.exceptions.ConnectTimeout):
20-
mda.fetch_pdb(PDB_ID=working_PDB_ID, timeout=0.000001)
29+
def test_timeout(self):
30+
"""Checks requests timeout Exception for fetch_pdb and PdbDownloader"""
31+
with pytest.raises(requests.exceptions.ConnectTimeout):
32+
mda.web.PdbDownloader(PDB_ID=working_PDB_ID).download(timeout=0.00000001)
2133

22-
def test_fetch_pdb_keywords():
23-
"""Check if keywords could be passed to Universe consturctors"""
24-
assert isinstance(mda.fetch_pdb(working_PDB_ID, download_path=None, timeout=None, in_memory=True),
25-
mda.Universe)
34+
def test_invalid_id(self):
35+
"""Test invalid id for PdbDownloader"""
2636

37+
with pytest.raises(mda.web.downloaders.FileDownloadPDBError):
38+
mda.web.PdbDownloader(PDB_ID='BananaBoat').download()
2739

28-
def test_file_namecache(tmp_path):
29-
"""Test that cache is saved as ID.file_format"""
30-
test_directory = tmp_path
31-
32-
downloader = mda.web.PdbDownloader(PDB_ID=working_PDB_ID,
33-
file_format=file_format)
40+
class Test_Pdb_Downloader_Cache():
41+
### Cache Test underneath
42+
def test_file_name_cache(self, shared_cache_directory):
43+
"""Test that cache is saved as ID.file_format"""
44+
downloader = mda.web.PdbDownloader(PDB_ID=working_PDB_ID)
45+
46+
downloader.download(cache_path=shared_cache_directory)
3447

35-
downloader.download(cache_path=test_directory)
48+
assert shared_cache_directory / f"{working_PDB_ID}.{file_format}"
3649

37-
assert test_directory / f"{working_PDB_ID}.{file_format}"
50+
def test_loading_cache(self, shared_cache_directory):
51+
"""Test that PdbDownloader.download() is reading cache"""
3852

53+
downloader1 = mda.web.PdbDownloader(PDB_ID=working_PDB_ID,
54+
file_format=file_format)
3955

40-
def test_invalid_id():
41-
"""Test invalid id for PdbDownloader and fetch_pdb"""
56+
downloader1.download(cache_path=shared_cache_directory)
57+
f1 = shared_cache_directory / f"{working_PDB_ID}.{file_format}"
58+
59+
downloader2 = mda.web.PdbDownloader(PDB_ID=working_PDB_ID,
60+
file_format=file_format)
61+
downloader2.download(cache_path=shared_cache_directory)
62+
63+
f2 = shared_cache_directory / f"{working_PDB_ID}.{file_format}"
64+
65+
assert filecmp.cmp(f1, f2, shallow=False) == True
66+
67+
class Test_Fetch_Pdb():
68+
"""Tests Public API of MDAnalysis.fetch_pdb()"""
69+
70+
def test_base_functionality(self):
71+
"""Check default parameter of mda.fetch_pdb"""
72+
assert isinstance(mda.fetch_pdb(PDB_ID=working_PDB_ID), mda.Universe)
73+
74+
def test_timeout(self):
75+
"""Checks requests timeout Exception for fetch_pdb and PdbDownloader"""
76+
with pytest.raises(requests.exceptions.ConnectTimeout):
77+
mda.fetch_pdb(PDB_ID=working_PDB_ID, timeout=0.00000000001)
78+
79+
def test_universe_keywords(self):
80+
"""Check if keywords could be passed to Universe constructors"""
81+
assert isinstance(mda.fetch_pdb(working_PDB_ID, download_path=None, timeout=None, in_memory=True),
82+
mda.Universe)
4283

43-
with pytest.raises(mda.web.downloaders.FileDownloadPDBError):
44-
mda.web.PdbDownloader(PDB_ID='BananaBoat').download()
4584

46-
4785

4886

4987

0 commit comments

Comments
 (0)