|
1 | 1 | import MDAnalysis as mda |
| 2 | +import filecmp |
| 3 | + |
| 4 | + |
2 | 5 | import pytest |
3 | 6 | import requests |
4 | 7 |
|
5 | 8 |
|
6 | 9 |
|
7 | | -working_PDB_ID = '1DPX' # egg white lysozyme |
8 | | -file_format="pdb" |
| 10 | +working_PDB_ID = '1DPX' # egg white lysozyme |
| 11 | +file_format = "pdb" |
9 | 12 |
|
10 | 13 |
|
| 14 | +@pytest.fixture(scope="function") |
| 15 | +def shared_cache_directory(tmp_path_factory): |
| 16 | + """Create Cache Directory""" |
| 17 | + return tmp_path_factory.mktemp("cache") |
11 | 18 |
|
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) |
15 | 19 |
|
| 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)) |
16 | 28 |
|
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) |
21 | 33 |
|
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""" |
26 | 36 |
|
| 37 | + with pytest.raises(mda.web.downloaders.FileDownloadPDBError): |
| 38 | + mda.web.PdbDownloader(PDB_ID='BananaBoat').download() |
27 | 39 |
|
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) |
34 | 47 |
|
35 | | - downloader.download(cache_path=test_directory) |
| 48 | + assert shared_cache_directory / f"{working_PDB_ID}.{file_format}" |
36 | 49 |
|
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""" |
38 | 52 |
|
| 53 | + downloader1 = mda.web.PdbDownloader(PDB_ID=working_PDB_ID, |
| 54 | + file_format=file_format) |
39 | 55 |
|
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) |
42 | 83 |
|
43 | | - with pytest.raises(mda.web.downloaders.FileDownloadPDBError): |
44 | | - mda.web.PdbDownloader(PDB_ID='BananaBoat').download() |
45 | 84 |
|
46 | | - |
47 | 85 |
|
48 | 86 |
|
49 | 87 |
|
|
0 commit comments