Skip to content

Commit

Permalink
Tweaks to make the tests look nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Apr 28, 2017
1 parent 5f0ecac commit 1bb7fa6
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ install:

# command to run tests
script:
- nosetests tests
- pytest -vv --tb=native tests/

after_script:
- coveralls
5 changes: 0 additions & 5 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ To get the source source code and run the unit tests, run::
$ virtualenv --no-site-packages -p /path/to/python2.7 venv27
$ . venv27/bin/activate
$ pip install -r requirements/test.txt
$ nosetests tests

You might want to use the pytest runner::

$ pip install pytest pytest-catchlog
$ pytest -vv --tb=native tests

While 100% code coverage does not make a library bug-free, it significantly
Expand Down
30 changes: 30 additions & 0 deletions malcolm/testutil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
import functools
from mock import Mock, patch

from malcolm.core import call_with_params


class ChildTestCase(unittest.TestCase):
@patch("malcolm.modules.ca.parts.capart.CAPart.reset", Mock)
@patch("malcolm.modules.ca.parts.catoolshelper.CaToolsHelper._instance",
Mock)
def create_child_block(self, child_block, process, **params):
"""Creates an instance of child_block with CA calls mocked out.
Args:
child_block (callable): The function to call to get the block
process (Process): The process to run under
**params: Parameters to pass to child_block()
Returns:
child: The child object with an attribute mock_writes that will have
a call(attr_name, value) or call(method_name, params) for
anything the child is asked to do
"""
child = call_with_params(child_block, process, **params)
child.mock_writes = Mock(return_value=None)
for k in child._write_functions:
child._write_functions[k] = functools.partial(child.mock_writes, k)
return child

2 changes: 2 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ scanpointgenerator>=2.1.0
git+git://github.com/thomascobb/cothread
ruamel.yaml
plop==0.3.0
pytest
pytest-catchlog
20 changes: 4 additions & 16 deletions tests/test_modules/test_pmac/test_compoundmotorpart.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import unittest
import functools
from mock import Mock, patch

from malcolm.core import call_with_params, Context, Process
from malcolm.modules.pmac.parts import CompoundMotorPart
from malcolm.modules.pmac.blocks import compound_motor_block
from malcolm.testutil import ChildTestCase


class TestRawMotorPart(unittest.TestCase):
class TestRawMotorPart(ChildTestCase):

@patch("malcolm.modules.ca.parts.capart.CAPart.reset")
@patch("malcolm.modules.ca.parts.catoolshelper.CaToolsHelper._instance")
def setUp(self, catools, reset):
self.put = Mock(return_value=None)
def setUp(self):
self.process = Process("Process")
self.context = Context(self.process)
child = call_with_params(
child = self.create_child_block(
compound_motor_block, self.process, mri="my_mri", prefix="PV:PRE",
scannable="scan")
for k in child._write_functions:
child._write_functions[k] = functools.partial(self.put, k)
child.parts["maxVelocity"].attr.set_value(5.0)
child.parts["accelerationTime"].attr.set_value(0.5)
child.parts["position"].attr.set_value(12.3)
Expand All @@ -44,7 +36,3 @@ def test_report(self):
self.assertEqual(returns.max_velocity, 5.0)
self.assertEqual(returns.current_position, 12.3)
self.assertEqual(returns.scannable, "scan")


if __name__ == "__main__":
unittest.main(verbosity=2)
41 changes: 17 additions & 24 deletions tests/test_modules/test_pmac/test_pmactrajectorypart.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import unittest
import functools
from mock import Mock, call, patch, ANY

from scanpointgenerator import LineGenerator, CompoundGenerator

from malcolm.core import call_with_params, Context, Process
from malcolm.modules.pmac.parts import PmacTrajectoryPart
from malcolm.modules.pmac.infos import MotorInfo
from malcolm.modules.pmac.blocks import pmac_trajectory_block
from scanpointgenerator import LineGenerator, CompoundGenerator

from malcolm.testutil import ChildTestCase

class TestPMACTrajectoryPart(unittest.TestCase):
maxDiff = None

@patch("malcolm.modules.ca.parts.capart.CAPart.reset")
@patch("malcolm.modules.ca.parts.catoolshelper.CaToolsHelper._instance")
def setUp(self, catools, reset):
self.put = Mock(return_value=None)
class TestPMACTrajectoryPart(ChildTestCase):
def setUp(self):
self.process = Process("Process")
self.context = Context(self.process)
child = call_with_params(
self.child = self.create_child_block(
pmac_trajectory_block, self.process, mri="PMAC:TRAJ",
prefix="PV:PRE", statPrefix="PV:STAT")
for k in child._write_functions:
child._write_functions[k] = functools.partial(self.put, k)
child.parts["i10"].attr.set_value(1705244)
self.child.parts["i10"].attr.set_value(1705244)
self.o = call_with_params(
PmacTrajectoryPart, name="pmac", mri="PMAC:TRAJ")
list(self.o.create_attributes())
Expand Down Expand Up @@ -104,7 +97,7 @@ def test_validate(self):
0.2)
def test_configure(self):
self.do_configure(axes_to_scan=["x", "y"])
assert self.put.mock_calls == [
assert self.child.mock_writes.mock_calls == [
call('numPoints', 4000000),
call('cs', 'CS1'),
] + self.resolutions_and_use_calls() + [
Expand Down Expand Up @@ -141,7 +134,7 @@ def test_configure(self):
def test_2_axis_move_to_start(self):
self.do_configure(
axes_to_scan=["x", "y"], x_pos=0.0, y_pos=0.2)
assert self.put.mock_calls == [
assert self.child.mock_writes.mock_calls == [
call('numPoints', 4000000),
call('cs', 'CS1'),
] + self.resolutions_and_use_calls() + [
Expand All @@ -167,18 +160,18 @@ def test_2_axis_move_to_start(self):
0.2)
def test_update_step(self):
self.do_configure(axes_to_scan=["x", "y"])
positionsA = self.put.call_args_list[-6][0][1]
positionsA = self.child.mock_writes.call_args_list[-6][0][1]
assert len(positionsA) == 11
assert positionsA[-1] == 0.375
assert self.o.end_index == 4
assert len(self.o.completed_steps_lookup) == 11
update_completed_steps = Mock()
self.put.reset_mock()
self.child.mock_writes.reset_mock()
self.o.update_step(
3, update_completed_steps, self.context.block_view("PMAC:TRAJ"))
update_completed_steps.assert_called_once_with(1, self.o)
assert not self.o.loading
assert self.put.mock_calls == self.resolutions_and_use_calls() + [
assert self.child.mock_writes.mock_calls == self.resolutions_and_use_calls() + [
call('pointsToBuild', 5),
call('positionsA', [
0.25, 0.125, 0.0, -0.125, -0.1375]),
Expand All @@ -195,16 +188,16 @@ def test_update_step(self):
def test_run(self):
update = Mock()
self.o.run(self.context, update)
assert self.put.mock_calls == [call('executeProfile')]
assert self.child.mock_writes.mock_calls == [call('executeProfile')]

def test_multi_run(self):
self.do_configure(axes_to_scan=["x"])
self.assertEqual(self.o.completed_steps_lookup,
[0, 0, 1, 1, 2, 2, 3, 3])
self.put.reset_mock()
self.child.mock_writes.reset_mock()
self.do_configure(
axes_to_scan=["x"], completed_steps=3, x_pos=0.6375)
assert self.put.mock_calls == [
assert self.child.mock_writes.mock_calls == [
call('numPoints', 4000000),
call('cs', 'CS1'),
] + self.resolutions_and_use_calls(useB=False) + [
Expand All @@ -223,7 +216,7 @@ def test_multi_run(self):
def test_long_steps_lookup(self):
self.do_configure(
axes_to_scan=["x"], completed_steps=3, x_pos=0.62506, duration=14.0)
assert self.put.mock_calls[-6:] == [
assert self.child.mock_writes.mock_calls[-6:] == [
call('pointsToBuild', 14),
call('positionsA',
[0.625, 0.5625, 0.5, 0.4375, 0.375, 0.3125, 0.25, 0.1875,
Expand All @@ -243,7 +236,7 @@ def test_long_steps_lookup(self):
2.0)
def test_long_move(self):
self.do_configure(axes_to_scan=["x"], x_pos=-10.1375)
assert self.put.mock_calls[13:18] == [
assert self.child.mock_writes.mock_calls[13:18] == [
call('pointsToBuild', 5),
call('positionsA', [
-8.2575000000000003, -6.1774999999999984, -4.0974999999999984,
Expand Down
16 changes: 4 additions & 12 deletions tests/test_modules/test_pmac/test_rawmotorpart.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import unittest
import functools
from mock import Mock, patch

from malcolm.core import call_with_params, Context, Process
from malcolm.modules.pmac.parts import RawMotorPart
from malcolm.modules.pmac.blocks import raw_motor_block
from malcolm.testutil import ChildTestCase


class TestRawMotorPart(unittest.TestCase):
class TestRawMotorPart(ChildTestCase):

@patch("malcolm.modules.ca.parts.capart.CAPart.reset")
@patch("malcolm.modules.ca.parts.catoolshelper.CaToolsHelper._instance")
def setUp(self, catools, reset):
self.put = Mock(return_value=None)
def setUp(self):
self.process = Process("Process")
self.context = Context(self.process)
child = call_with_params(
child = self.create_child_block(
raw_motor_block, self.process, mri="mri", prefix="PV:PRE",
motorPrefix="MOT:PRE", scannable="scan")
for k in child._write_functions:
child._write_functions[k] = functools.partial(self.put, k)
child.parts["maxVelocity"].attr.set_value(5.0)
child.parts["accelerationTime"].attr.set_value(0.5)
child.parts["position"].attr.set_value(12.3)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_modules/test_pva/test_pvautil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import numpy as np

# Mock out pvaccess
sys.modules["pvaccess"] = MagicMock()
# Mock out pvaccess if it isn't there
if "pvaccess" not in sys.modules:
sys.modules["pvaccess"] = MagicMock()
import pvaccess

from malcolm.core import StringArray
from malcolm.modules.pva.controllers.pvautil import pva_structure_from_value, \
dict_to_pv_object



class PvTempObject(object):
def __init__(self, dict_in, type):
self._dict = dict_in
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_dict_to_stucture(self):
test_dict["val8"] = [pvaccess.BOOLEAN]
test_dict["val9"] = [pvaccess.LONG]
test_dict["val10"] = [pvaccess.DOUBLE]
test_val = pvaccess.PvObject(test_dict, "type1")
test_val = PvTempObject(test_dict, "type1")
self.assertEquals(val, test_val)

# Test the variant union array type
Expand All @@ -82,13 +82,13 @@ def test_dict_to_stucture(self):
]})
test_dict = OrderedDict()
test_dict["union_array"] = [()]
test_val = pvaccess.PvObject(test_dict, "")
test_val = PvTempObject(test_dict, "")
self.assertEquals(val, test_val)
val = pva_structure_from_value(
{"union_array": []})
test_dict = OrderedDict()
test_dict["union_array"] = [()]
test_val = pvaccess.PvObject(test_dict, "")
test_val = PvTempObject(test_dict, "")
self.assertEquals(val, test_val)

@patch("malcolm.modules.pva.controllers.pvautil.pvaccess.PvObject", PvTempObject)
Expand Down

0 comments on commit 1bb7fa6

Please sign in to comment.