Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest Style: auxiliary/base.py #1492

Merged
merged 4 commits into from
Jul 18, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions testsuite/MDAnalysisTests/auxiliary/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
from six.moves import range
import pytest
import numpy as np
from numpy.testing import (assert_equal, assert_raises, assert_almost_equal,
raises)
from numpy.testing import assert_almost_equal


import MDAnalysis as mda

from MDAnalysisTests.datafiles import (COORDINATES_XTC, COORDINATES_TOPOLOGY)

@raises(ValueError)

@pytest.mark.raises(exception=ValueError)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the pytest.raises context manager

def test_get_bad_auxreader_format_raises_ValueError():
# should raise a ValueError when no AuxReaders with match the specified format
mda.auxiliary.core.get_auxreader_for(format='bad-format')
Expand Down Expand Up @@ -132,16 +133,13 @@ def format_data(self, data):
class BaseAuxReaderTest(object):

def test_n_steps(self, ref, reader):
assert_equal(len(reader), ref.n_steps,
"number of steps does not match")
assert len(reader) == ref.n_steps, "number of steps does not match"

def test_dt(self, ref, reader):
assert_equal(reader.dt, ref.dt,
"dt does not match")
assert reader.dt == ref.dt, "dt does not match"

def test_initial_time(self, ref, reader):
assert_equal(reader.initial_time, ref.initial_time,
"initial time does not match")
assert reader.initial_time == ref.initial_time, "initial time does not match"

def test_first_step(self, ref, reader):
# on first loading we should start at step 0
Expand Down Expand Up @@ -173,7 +171,8 @@ def test_next_past_last_step_raises_StopIteration(self, ref, reader):
# should take us to the last step
reader[-1]
# if we try to move to next step from here, should raise StopIteration
assert_raises(StopIteration, reader.next)
with pytest.raises(StopIteration):
reader.next

def test_move_to_invalid_step_raises_IndexError(self, ref, reader):
# last step is number n_steps -1 ; if we try move to step number
Expand Down Expand Up @@ -228,17 +227,15 @@ def test_time_selector(self, ref):
time_selector = ref.time_selector)
# time should still match reference time for each step
for i, val in enumerate(reader):
assert_equal(val.time, ref.select_time_ref[i],
"time for step {} does not match".format(i))
assert val.time == ref.select_time_ref[i], "time for step {} does not match".format(i)

def test_data_selector(self, ref):
# reload reader, passing in a data selector
reader = ref.reader(ref.testdata,
data_selector=ref.data_selector)
# data should match reference data for each step
for i, val in enumerate(reader):
assert_equal(val.data, ref.select_data_ref[i],
"data for step {0} does not match".format(i))
assert val.data == ref.select_data_ref[i], "data for step {0} does not match".format(i)

def test_no_constant_dt(self, ref):
## assume we can select time...
Expand All @@ -248,8 +245,7 @@ def test_no_constant_dt(self, ref):
constant_dt=False)
# time should match reference for selecting time, for each step
for i, val in enumerate(reader):
assert_equal(val.time, ref.select_time_ref[i],
"data for step {} does not match".format(i))
assert val.time == ref.select_time_ref[i], "data for step {} does not match".format(i)

def test_update_ts_without_auxname_raises_ValueError(self, ref):
# reload reader without auxname
Expand Down Expand Up @@ -319,7 +315,7 @@ def test_read_higher_freq_timestep(self, ref, reader):
def test_get_auxreader_for(self, ref, reader):
# check guesser gives us right reader
reader = mda.auxiliary.core.get_auxreader_for(ref.testdata)
assert_equal(reader, ref.reader)
assert reader == ref.reader

def test_iterate_through_trajectory(self, ref):
# add to trajectory
Expand All @@ -329,9 +325,8 @@ def test_iterate_through_trajectory(self, ref):
# trajectory here has same dt, offset; so there's a direct correspondence
# between frames and steps
for i, ts in enumerate(u.trajectory):
assert_equal(ts.aux.test, ref.auxsteps[i].data,
"representative value does not match when iterating through "
"all trajectory timesteps")
assert ts.aux.test == ref.auxsteps[i].data, "representative value does not match when iterating through "\
"all trajectory timesteps"
u.trajectory.close()

def test_iterate_as_auxiliary_from_trajectory(self, ref):
Expand All @@ -342,22 +337,19 @@ def test_iterate_as_auxiliary_from_trajectory(self, ref):
# trahectory here has same dt, offset, so there's a direct correspondence
# between frames and steps, and iter_as_aux will run through all frames
for i, ts in enumerate(u.trajectory.iter_as_aux('test')):
assert_equal(ts.aux.test, ref.auxsteps[i].data,
"representative value does not match when iterating through "
"all trajectory timesteps")
assert ts.aux.test == ref.auxsteps[i].data, "representative value does not match when iterating through "\
"all trajectory timesteps"
u.trajectory.close()

def test_get_description(self, ref, reader):
description = reader.get_description()
for attr in ref.description:
assert_equal(description[attr], ref.description[attr],
"'Description' does not match for {}".format(attr))
assert description[attr] == ref.description[attr], "'Description' does not match for {}".format(attr)

def test_load_from_description(self, reader):
description = reader.get_description()
new = mda.auxiliary.core.auxreader(**description)
assert_equal(new, reader,
"AuxReader reloaded from description does not match")
assert new == reader, "AuxReader reloaded from description does not match"


def assert_auxstep_equal(A, B):
Expand Down