Skip to content

Commit b8fe34b

Browse files
authored
[fmt] utils, import, viz tests (#4875)
1 parent 453be6c commit b8fe34b

19 files changed

+1068
-612
lines changed

testsuite/MDAnalysisTests/import/fork_called.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"""Tests whether os.fork() is called as a side effect when importing MDAnalysis.
2727
See PR #1794 for details."""
2828

29-
with mock.patch('os.fork') as os_dot_fork:
29+
with mock.patch("os.fork") as os_dot_fork:
3030
import MDAnalysis
31+
3132
assert not os_dot_fork.called

testsuite/MDAnalysisTests/import/test_import.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727

2828
"""Test if importing MDAnalysis has unwanted side effects (PR #1794)."""
2929

30-
@pytest.mark.skipif(os.name == 'nt',
31-
reason="fork-related import checks irrelevant on Windows")
30+
31+
@pytest.mark.skipif(
32+
os.name == "nt", reason="fork-related import checks irrelevant on Windows"
33+
)
3234
class TestMDAImport(object):
3335
# Tests concerning importing MDAnalysis.
3436
def test_os_dot_fork_not_called(self):
@@ -37,7 +39,7 @@ def test_os_dot_fork_not_called(self):
3739
# no previously imported modules interfere with it. It is therefore
3840
# offloaded to the script "fork_called.py".
3941
loc = os.path.dirname(os.path.realpath(__file__))
40-
script = os.path.join(loc, 'fork_called.py')
42+
script = os.path.join(loc, "fork_called.py")
4143
encoding = sys.stdout.encoding
4244
if encoding is None:
4345
encoding = "utf-8"
@@ -47,16 +49,17 @@ def test_os_dot_fork_not_called(self):
4749
# CalledProcessError. That error's output member then contains the
4850
# failed script's stderr and we can print it:
4951
try:
50-
out = subprocess.check_output([sys.executable, script],
51-
stderr=subprocess.STDOUT)\
52-
.decode(encoding)
52+
out = subprocess.check_output(
53+
[sys.executable, script], stderr=subprocess.STDOUT
54+
).decode(encoding)
5355
except subprocess.CalledProcessError as err:
5456
print(err.output)
55-
raise(err)
57+
raise (err)
5658

5759
def test_os_dot_fork_not_none(self):
5860
# In MDAnalysis.core.universe, os.fork is set to None prior to importing
5961
# the uuid module and restored afterwards (see PR #1794 for details).
6062
# This tests asserts that os.fork has been restored.
6163
import MDAnalysis
64+
6265
assert os.fork is not None

testsuite/MDAnalysisTests/utils/test_authors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@
2424

2525
import MDAnalysis
2626

27+
2728
def test_package_authors():
28-
assert len(MDAnalysis.__authors__) > 0, 'Could not find the list of authors'
29+
assert (
30+
len(MDAnalysis.__authors__) > 0
31+
), "Could not find the list of authors"

testsuite/MDAnalysisTests/utils/test_datafiles.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def test_failed_import(monkeypatch):
2929
# Putting this test first to avoid datafiles already being loaded
3030
errmsg = "MDAnalysisTests package not installed."
3131

32-
monkeypatch.setitem(sys.modules, 'MDAnalysisTests.datafiles', None)
32+
monkeypatch.setitem(sys.modules, "MDAnalysisTests.datafiles", None)
3333

34-
if 'MDAnalysis.tests.datafiles' in sys.modules:
35-
monkeypatch.delitem(sys.modules, 'MDAnalysis.tests.datafiles')
34+
if "MDAnalysis.tests.datafiles" in sys.modules:
35+
monkeypatch.delitem(sys.modules, "MDAnalysis.tests.datafiles")
3636

3737
with pytest.raises(ImportError, match=errmsg):
3838
import MDAnalysis.tests.datafiles
@@ -42,20 +42,35 @@ def test_import():
4242
try:
4343
import MDAnalysis.tests.datafiles
4444
except ImportError:
45-
pytest.fail("Failed to 'import MDAnalysis.tests.datafiles --- install MDAnalysisTests")
45+
pytest.fail(
46+
"Failed to 'import MDAnalysis.tests.datafiles --- install MDAnalysisTests"
47+
)
4648

4749

4850
def test_all_exports():
4951
import MDAnalysisTests.datafiles
50-
missing = [name for name in dir(MDAnalysisTests.datafiles)
51-
if
52-
not name.startswith('_') and name not in MDAnalysisTests.datafiles.__all__ and name != 'MDAnalysisTests']
52+
53+
missing = [
54+
name
55+
for name in dir(MDAnalysisTests.datafiles)
56+
if not name.startswith("_")
57+
and name not in MDAnalysisTests.datafiles.__all__
58+
and name != "MDAnalysisTests"
59+
]
5360
assert_equal(missing, [], err_msg="Variables need to be added to __all__.")
5461

5562

5663
def test_export_variables():
5764
import MDAnalysisTests.datafiles
5865
import MDAnalysis.tests.datafiles
59-
missing = [name for name in MDAnalysisTests.datafiles.__all__
60-
if name not in dir(MDAnalysis.tests.datafiles)]
61-
assert_equal(missing, [], err_msg="Variables not exported to MDAnalysis.tests.datafiles")
66+
67+
missing = [
68+
name
69+
for name in MDAnalysisTests.datafiles.__all__
70+
if name not in dir(MDAnalysis.tests.datafiles)
71+
]
72+
assert_equal(
73+
missing,
74+
[],
75+
err_msg="Variables not exported to MDAnalysis.tests.datafiles",
76+
)

testsuite/MDAnalysisTests/utils/test_duecredit.py

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,68 @@
3535
# duecredit itself is not needed in the name space but this is a
3636
# convenient way to skip all tests if duecredit is not installed
3737
# (see https://github.com/MDAnalysis/mdanalysis/issues/1906)
38-
pytest.importorskip('duecredit')
38+
pytest.importorskip("duecredit")
3939

40-
@pytest.mark.skipif((os.environ.get('DUECREDIT_ENABLE', 'yes').lower()
41-
in ('no', '0', 'false')),
42-
reason=
43-
"duecredit is explicitly disabled with DUECREDIT_ENABLE=no")
40+
41+
@pytest.mark.skipif(
42+
(
43+
os.environ.get("DUECREDIT_ENABLE", "yes").lower()
44+
in ("no", "0", "false")
45+
),
46+
reason="duecredit is explicitly disabled with DUECREDIT_ENABLE=no",
47+
)
4448
class TestDuecredit(object):
4549

4650
def test_duecredit_active(self):
4751
assert mda.due.active == True
4852

49-
@pytest.mark.parametrize("module,path,citekey", [
50-
("MDAnalysis", "MDAnalysis", "10.25080/majora-629e541a-00e"),
51-
("MDAnalysis", "MDAnalysis", "10.1002/jcc.21787"),
52-
])
53+
@pytest.mark.parametrize(
54+
"module,path,citekey",
55+
[
56+
("MDAnalysis", "MDAnalysis", "10.25080/majora-629e541a-00e"),
57+
("MDAnalysis", "MDAnalysis", "10.1002/jcc.21787"),
58+
],
59+
)
5360
def test_duecredit_collector_primary(self, module, path, citekey):
5461
assert mda.due.citations[(path, citekey)].cites_module == True
5562

5663
# note: citekeys are *all lower case*
57-
@pytest.mark.parametrize("module,path,citekey", [
58-
("MDAnalysis.analysis.psa",
59-
"pathsimanalysis.psa",
60-
"10.1371/journal.pcbi.1004568"),
61-
("MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
62-
"MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
63-
"10.1063/1.4922445"),
64-
("MDAnalysis.analysis.leaflet",
65-
"MDAnalysis.analysis.leaflet",
66-
"10.1002/jcc.21787"),
67-
("MDAnalysis.lib.qcprot",
68-
"MDAnalysis.lib.qcprot",
69-
"10.1107/s0108767305015266"),
70-
("MDAnalysis.lib.qcprot",
71-
"MDAnalysis.lib.qcprot",
72-
"qcprot2"),
73-
("MDAnalysis.analysis.encore",
74-
"MDAnalysis.analysis.encore",
75-
"10.1371/journal.pcbi.1004415"),
76-
("MDAnalysis.analysis.dssp",
77-
"MDAnalysis.analysis.dssp",
78-
"10.1002/bip.360221211")
79-
])
64+
@pytest.mark.parametrize(
65+
"module,path,citekey",
66+
[
67+
(
68+
"MDAnalysis.analysis.psa",
69+
"pathsimanalysis.psa",
70+
"10.1371/journal.pcbi.1004568",
71+
),
72+
(
73+
"MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
74+
"MDAnalysis.analysis.hydrogenbonds.hbond_autocorrel",
75+
"10.1063/1.4922445",
76+
),
77+
(
78+
"MDAnalysis.analysis.leaflet",
79+
"MDAnalysis.analysis.leaflet",
80+
"10.1002/jcc.21787",
81+
),
82+
(
83+
"MDAnalysis.lib.qcprot",
84+
"MDAnalysis.lib.qcprot",
85+
"10.1107/s0108767305015266",
86+
),
87+
("MDAnalysis.lib.qcprot", "MDAnalysis.lib.qcprot", "qcprot2"),
88+
(
89+
"MDAnalysis.analysis.encore",
90+
"MDAnalysis.analysis.encore",
91+
"10.1371/journal.pcbi.1004415",
92+
),
93+
(
94+
"MDAnalysis.analysis.dssp",
95+
"MDAnalysis.analysis.dssp",
96+
"10.1002/bip.360221211",
97+
),
98+
],
99+
)
80100
def test_duecredit_collector_analysis_modules(self, module, path, citekey):
81101
importlib.import_module(module)
82102
assert mda.due.citations[(path, citekey)].cites_module == True
@@ -85,17 +105,21 @@ def test_duecredit_mmtf(self):
85105
# doesn't trigger on import but on use of either parser or reader
86106
u = mda.Universe(MMTF)
87107

88-
assert mda.due.citations[('MDAnalysis.coordinates.MMTF',
89-
'10.1371/journal.pcbi.1005575')].cites_module
90-
assert mda.due.citations[('MDAnalysis.topology.MMTFParser',
91-
'10.1371/journal.pcbi.1005575')].cites_module
108+
assert mda.due.citations[
109+
("MDAnalysis.coordinates.MMTF", "10.1371/journal.pcbi.1005575")
110+
].cites_module
111+
assert mda.due.citations[
112+
("MDAnalysis.topology.MMTFParser", "10.1371/journal.pcbi.1005575")
113+
].cites_module
92114

93115
@pytest.mark.skipif(not HAS_H5PY, reason="h5py not installed")
94116
def test_duecredit_h5md(self):
95117
# doesn't trigger on import but on use of either reader or writer
96118
u = mda.Universe(TPR_xvf, H5MD_xvf)
97119

98-
assert mda.due.citations[('MDAnalysis.coordinates.H5MD',
99-
'10.25080/majora-1b6fd038-005')].cites_module
100-
assert mda.due.citations[('MDAnalysis.coordinates.H5MD',
101-
'10.1016/j.cpc.2014.01.018')].cites_module
120+
assert mda.due.citations[
121+
("MDAnalysis.coordinates.H5MD", "10.25080/majora-1b6fd038-005")
122+
].cites_module
123+
assert mda.due.citations[
124+
("MDAnalysis.coordinates.H5MD", "10.1016/j.cpc.2014.01.018")
125+
].cites_module

testsuite/MDAnalysisTests/utils/test_failure.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424

2525

2626
def test_failure():
27-
"""Fail if the MDA_FAILURE_TEST environment variable is set.
28-
"""
29-
if u'MDA_FAILURE_TEST' in os.environ:
27+
"""Fail if the MDA_FAILURE_TEST environment variable is set."""
28+
if "MDA_FAILURE_TEST" in os.environ:
3029
# Have a file open to trigger an output from the open_files plugin.
31-
f = open('./failure.txt', 'w')
32-
raise AssertionError("the MDA_FAILURE_TEST environment variable is set")
30+
f = open("./failure.txt", "w")
31+
raise AssertionError(
32+
"the MDA_FAILURE_TEST environment variable is set"
33+
)

testsuite/MDAnalysisTests/utils/test_imports.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
path_to_testing_modules = MDAnalysisTests.__path__[0]
3131
# Exclusion path relative to MDAnalysisTests
32-
exclusions = ['/plugins', '/data']
32+
exclusions = ["/plugins", "/data"]
3333

3434

3535
def is_excluded(path):
36-
leaf = path[len(path_to_testing_modules):]
36+
leaf = path[len(path_to_testing_modules) :]
3737
return leaf in exclusions
3838

3939

@@ -42,20 +42,26 @@ def get_file_paths():
4242
for dirpath, dirnames, files in os.walk(path_to_testing_modules):
4343
if is_excluded(dirpath):
4444
continue
45-
for f in filter(lambda x: x.endswith('.py'), files):
45+
for f in filter(lambda x: x.endswith(".py"), files):
4646
fpath = os.path.join(dirpath, f)
4747
if is_excluded(fpath):
4848
continue
4949
paths.append(fpath)
5050
return paths
5151

5252

53-
@pytest.mark.parametrize('testing_module', get_file_paths())
53+
@pytest.mark.parametrize("testing_module", get_file_paths())
5454
def test_relative_import(testing_module):
55-
with open(testing_module, 'r') as test_module_file_object:
55+
with open(testing_module, "r") as test_module_file_object:
5656
for lineno, line in enumerate(test_module_file_object, start=1):
57-
if 'from .' in line and 'import' in line \
58-
and not 'test_imports' in testing_module:
57+
if (
58+
"from ." in line
59+
and "import" in line
60+
and not "test_imports" in testing_module
61+
):
5962
raise AssertionError(
6063
"A relative import statement was found in "
61-
"module {testing_module} at linenumber {lineno}.".format(**vars()))
64+
"module {testing_module} at linenumber {lineno}.".format(
65+
**vars()
66+
)
67+
)

testsuite/MDAnalysisTests/utils/test_log.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,8 @@ def buffer():
6868

6969

7070
def _assert_in(output, string):
71-
assert string in output, "Output '{0}' does not match required format '{1}'.".format(output.replace('\r', '\\r'), string.replace('\r', '\\r'))
72-
71+
assert (
72+
string in output
73+
), "Output '{0}' does not match required format '{1}'.".format(
74+
output.replace("\r", "\\r"), string.replace("\r", "\\r")
75+
)

0 commit comments

Comments
 (0)