Skip to content

Commit

Permalink
Move utils module to run with pytest
Browse files Browse the repository at this point in the history
Get rid of staticmethods

Fixed import ordering
  • Loading branch information
utkbansal committed Jun 24, 2017
1 parent d8677b6 commit f7d7cac
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ env:
- PYTHON_VERSION=2.7
- COVERALLS=false
- NOSE_FLAGS="--processes=2 --process-timeout=400 --no-open-files --with-timer --timer-top-n 50"
- NOSE_TEST_LIST="analysis auxiliary core topology utils"
- NOSE_TEST_LIST="analysis auxiliary coordinates core topology"
- PYTEST_FLAGS="--disable-pytest-warnings"
- PYTEST_LIST="testsuite/MDAnalysisTests/lib testsuite/MDAnalysisTests/formats testsuite/MDAnalysisTests/coordinates"
- PYTEST_LIST="testsuite/MDAnalysisTests/lib testsuite/MDAnalysisTests/formats testsuite/MDAnalysisTests/utils"
- NOSE_COVERAGE_FILE="nose_coverage"
- PYTEST_COVERAGE_FILE="pytest_coverage"
- MAIN_CMD="pytest ${PYTEST_LIST} ${PYTEST_FLAGS}; python ./testsuite/MDAnalysisTests/mda_nosetests ${NOSE_TEST_LIST} ${NOSE_FLAGS}"
Expand Down
Empty file.
14 changes: 7 additions & 7 deletions testsuite/MDAnalysisTests/utils/test_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
from __future__ import division, absolute_import

import MDAnalysis
import MDAnalysis.lib.distances

import numpy as np
from MDAnalysis.lib import mdamath
from MDAnalysis.tests.datafiles import PSF, DCD, TRIC
from MDAnalysisTests import parser_not_found
from nose.plugins.attrib import attr
from numpy.testing import (TestCase, dec, raises, assert_,
assert_almost_equal, assert_equal, assert_raises, )
assert_almost_equal, assert_equal, assert_raises,)

from nose.plugins.attrib import attr

from MDAnalysis.tests.datafiles import PSF, DCD, TRIC
from MDAnalysis.lib import mdamath
from MDAnalysisTests import parser_not_found

class _TestDistanceArray(TestCase):
# override backend in test classes
Expand Down Expand Up @@ -121,7 +122,6 @@ def tearDown(self):
del self.trajectory
del self.ca

@attr('issue')
def test_simple(self):
U = self.universe
self.trajectory.rewind()
Expand Down
35 changes: 23 additions & 12 deletions testsuite/MDAnalysisTests/utils/test_modelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,30 @@
#
from __future__ import absolute_import, print_function

import os

import MDAnalysis
from MDAnalysis.tests.datafiles import (
PSF,
DCD,
capping_input,
capping_output,
capping_ace,
capping_nma,
merge_protein,
merge_ligand,
merge_water
)
import MDAnalysis.core.groups
from MDAnalysis import Merge
from MDAnalysis.analysis.align import alignto
from MDAnalysis.core.groups import AtomGroup
from MDAnalysis.tests.datafiles import PSF, DCD, capping_input, capping_output, capping_ace, capping_nma, \
merge_protein, merge_ligand, merge_water
from MDAnalysisTests import parser_not_found, tempdir

from numpy.testing import (TestCase, dec, assert_equal, assert_raises, assert_,
assert_array_equal)

import os

from MDAnalysis import Merge
from MDAnalysis.analysis.align import alignto


def capping(ref, ace, nma, output):
resids = ref.select_atoms("all").resids
Expand All @@ -48,12 +59,12 @@ def capping(ref, ace, nma, output):
# TODO consider a case when the protein resid is 1 and all peptide has to be shifted by +1, put that in docs as a
# post-processing step
alignto(ace, ref, select={
"mobile": "resid {0} and backbone".format(resid_min),
"reference": "resid {0} and backbone".format(resid_min)},
"mobile": "resid {0} and backbone".format(resid_min),
"reference": "resid {0} and backbone".format(resid_min)},
strict=True)
alignto(nma, ref, select={
"mobile": "resid {0} and backbone and not (resname NMA NME)".format(resid_max),
"reference": "resid {0} and (backbone or name OT2)".format(resid_max)},
"mobile": "resid {0} and backbone and not (resname NMA NME)".format(resid_max),
"reference": "resid {0} and (backbone or name OT2)".format(resid_max)},
strict=True)

# TODO remove the Hydrogen closest to ACE's oxygen
Expand Down Expand Up @@ -152,9 +163,9 @@ def test_merge(self):
# Check that the output Universe has the same number of residues and
# segments as the starting AtomGroups
assert_equal(len(u0.residues), (len(u1.residues) + len(u2.residues) +
len(u3.residues)))
len(u3.residues)))
assert_equal(len(u0.segments), (len(u1.segments) + len(u2.segments) +
len(u3.segments)))
len(u3.segments)))

# Make sure that all the atoms in the new universe are assigned to only
# one, new Universe
Expand Down
15 changes: 10 additions & 5 deletions testsuite/MDAnalysisTests/utils/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
from __future__ import absolute_import
from six.moves import cPickle

import MDAnalysis as mda
from numpy.testing import (
TestCase,
assert_array_equal,
assert_,
assert_raises,
assert_equal
)

import gc

import MDAnalysis as mda
from MDAnalysisTests.datafiles import PDB_small
from numpy.testing import (TestCase,
assert_array_equal, assert_, assert_raises,
assert_equal)
from six.moves import cPickle


class TestAtomGroupPickle(TestCase):
Expand Down
26 changes: 14 additions & 12 deletions testsuite/MDAnalysisTests/utils/test_selections.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@

# Test the selection exporters in MDAnalysis.selections
from __future__ import absolute_import
# use StringIO and NamedStream to write to memory instead to temp files
from six.moves import StringIO

import re

import MDAnalysis
import numpy as np
from MDAnalysis.lib.util import NamedStream
from numpy.testing import TestCase, assert_equal, assert_array_equal, dec

from MDAnalysis.tests.datafiles import PSF, DCD
from MDAnalysisTests import parser_not_found
from numpy.testing import TestCase, assert_equal, assert_array_equal, dec
# use StringIO and NamedStream to write to memory instead to temp files
from six.moves import StringIO

import MDAnalysis
from MDAnalysis.lib.util import NamedStream


class _SelectionWriter(TestCase):
Expand Down Expand Up @@ -105,10 +107,10 @@ class TestSelectionWriter_Gromacs(_SelectionWriter):
filename = "CA.ndx"
ref_name = "CA_selection"
ref_indices = ndx2array(
['5 22 46 65 84 103 122 129 141 153 160 170 \n',
'177 199 206 220 237 247 264 284 303 320 335 357 \n',
]
)
[ '5 22 46 65 84 103 122 129 141 153 160 170 \n',
'177 199 206 220 237 247 264 284 303 320 335 357 \n',
]
)

def _assert_selectionstring(self):
header = self.namedfile.readline().strip()
Expand Down Expand Up @@ -192,9 +194,9 @@ class TestSelectionWriter_Jmol(_SelectionWriter):
writer = MDAnalysis.selections.jmol.SelectionWriter
filename = "CA.spt"
ref_name, ref_indices = spt2array(
('@~ca ({4 21 45 64 83 102 121 128 140 152 159 169 176 198 205 219 236'
' 246 263 283 302 319 334 356});')
)
( '@~ca ({4 21 45 64 83 102 121 128 140 152 159 169 176 198 205 219 236'
' 246 263 283 302 319 334 356});')
)

def _assert_selectionstring(self):
header, indices = spt2array(self.namedfile.readline())
Expand Down
15 changes: 9 additions & 6 deletions testsuite/MDAnalysisTests/utils/test_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
#
from __future__ import division, absolute_import

import unittest
from six.moves import range
from itertools import permutations
from unittest import TestCase

import numpy as np
from MDAnalysis.lib import transformations as t
import unittest
from numpy.testing import (assert_allclose, assert_equal, assert_almost_equal,
assert_array_equal)
from six.moves import range

from MDAnalysis.lib import transformations as t

from unittest import TestCase


"""
Testing transformations is weird because there are 2 versions of many of
Expand Down Expand Up @@ -565,7 +568,7 @@ def test_quaternion_from_matrix_2(self):
q = self.f(np.diag([1., -1., -1., 1.]))
check = (np.allclose(
q, [0, 1, 0, 0], atol=_ATOL) or np.allclose(
q, [0, -1, 0, 0], atol=_ATOL))
q, [0, -1, 0, 0], atol=_ATOL))
assert_equal(check, True)

def test_quaternion_from_matrix_3(self):
Expand Down Expand Up @@ -886,7 +889,7 @@ def test_transformations_old_module():
raise AssertionError("MDAnalysis.core.transformations not importable. "
"Only remove for 1.0")

# NOTE: removed this test with release 1.0 when we remove the stub
# NOTE: removed this test with release 1.0 when we remove the stub


def test_rotaxis_equal_vectors():
Expand Down
25 changes: 10 additions & 15 deletions testsuite/MDAnalysisTests/utils/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,27 @@


class TestDefaultUnits(TestCase):
@staticmethod
def test_length():
def test_length(self):
assert_equal(flags['length_unit'], 'Angstrom',
u"The default length unit should be Angstrom (in core.flags)")
@staticmethod
def test_time():

def test_time(self):
assert_equal(flags['time_unit'], 'ps',
u"The default length unit should be pico seconds (in core.flags)")
@staticmethod
def test_convert_gromacs_trajectories():

def test_convert_gromacs_trajectories(self):
assert_equal(flags['convert_lengths'], True,
u"The default behaviour should be to auto-convert Gromacs trajectories")


class TestUnitEncoding(TestCase):
@staticmethod
def test_unicode():
def test_unicode(self):
try:
assert_equal(units.lengthUnit_factor[u"\u212b"], 1.0)
except KeyError:
raise AssertionError("Unicode symbol for Angtrom not supported")

@staticmethod
def test_unicode_encoding_with_symbol():
def test_unicode_encoding_with_symbol(self):
try:
assert_equal(units.lengthUnit_factor[u"Å"], 1.0)
except KeyError:
Expand Down Expand Up @@ -106,14 +103,12 @@ def test_force(self):
yield self._assert_almost_equal_convert, 2.5, 'kJ/(mol*nm)', 'kJ/(mol*A)', 0.25
yield self._assert_almost_equal_convert, 1, 'kcal/(mol*Angstrom)', 'kJ/(mol*Angstrom)', 4.184

@staticmethod
def test_unit_unknown():
def test_unit_unknown(self):
nm = 12.34567
assert_raises(ValueError, units.convert, nm, 'Stone', 'nm')
assert_raises(ValueError, units.convert, nm, 'nm', 'Stone')

@staticmethod
def test_unit_unconvertable():

def test_unit_unconvertable(self):
nm = 12.34567
A = nm * 10.
assert_raises(ValueError, units.convert, A, 'A', 'ps')
Expand Down

0 comments on commit f7d7cac

Please sign in to comment.