Skip to content

Commit

Permalink
Improve code coverage (#17)
Browse files Browse the repository at this point in the history
* Add test_ticker_spin

* Test MapeElement send_interaction()

* Add test mape element __init__()

* Remove pass in `on_interaction_received()`
  • Loading branch information
imcatta1 committed Feb 1, 2019
1 parent 87d618f commit 99e9429
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
1 change: 0 additions & 1 deletion mapek_framework/src/mapek_framework/mape_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def on_interaction_received(self, interaction, payload):
This method is not supposed to be called explicitly.
The :class:`~mapek_framework.group.Group` class will do that for you.
"""
pass

def send_interaction(self, interaction, payload):
"""Send a message using an `output_interaction`."""
Expand Down
47 changes: 46 additions & 1 deletion mapek_framework/src/test_mapek_framework/test_mape_element.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import unittest
import mock
from mapek_framework.mape_element import _MapeElement, MonitorElement, AnalyzeElement, PlanElement, ExecuteElement
from mapek_framework.mape_element import _MapeElement, MonitorElement, AnalyzeElement, PlanElement, ExecuteElement
from mapek_framework import ManagedSystem, Interaction


class MyMapeElement1(_MapeElement):

input_interactions = [Interaction('ii', None)] * 3
output_interactions = [Interaction('o1', None)] * 4

def on_interaction_received(self, interaction, payload):
pass


class MyMapeElement2(_MapeElement):

def on_interaction_received(self, interaction, payload):
pass


class MyManagedSystem(ManagedSystem):
pass


class MapeElementTestCase(unittest.TestCase):
Expand All @@ -10,3 +30,28 @@ def test_mape_element_inheritance(self):
self.assertTrue(issubclass(AnalyzeElement, _MapeElement))
self.assertTrue(issubclass(PlanElement, _MapeElement))
self.assertTrue(issubclass(ExecuteElement, _MapeElement))

@mock.patch('rospy.Publisher')
@mock.patch('rospy.Subscriber')
def test_mape_element_init(self, mock_subscriber, mock_publisher):
knowledge = {}
managed_system = MyManagedSystem()

element = MyMapeElement1(knowledge, managed_system)
self.assertEqual(element.knowledge, knowledge)
self.assertEqual(element.managed_system, managed_system)
self.assertEqual(mock_subscriber.call_count, 3)
self.assertEqual(mock_publisher.call_count, 4)

def test_mape_element_send_interaction(self):
interaction = 'interaction_name'
payload = 'msg'
m1 = mock.Mock()
m2 = mock.MagicMock()
m2.__getitem__.side_effect = lambda _: m1

element = MyMapeElement2(None, None)
element.output_topics = m2
element.send_interaction(interaction, payload)
m2.__getitem__.assert_called_with(interaction)
m1.publish.assert_called_with(payload)
28 changes: 28 additions & 0 deletions mapek_framework/src/test_mapek_framework/test_ticker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import mock
from mapek_framework import Ticker


Expand All @@ -13,3 +14,30 @@ def test_ticker_init(self):
self.assertEqual(ticker_instance._node_name, node_name)
self.assertEqual(ticker_instance._topic, topic)
self.assertEqual(ticker_instance._rate, rate)

def test_ticker_spin(self):
node_name = 'foo_name'
topic = 'foo_topic'
rate = 42

def is_shutdown():
if not hasattr(is_shutdown, 'already_executed'):
is_shutdown.already_executed = True
return False

return True

ticker_instance = Ticker(node_name, topic, rate)

with \
mock.patch('rospy.is_shutdown', side_effect=is_shutdown) as mock_is_shutdown, \
mock.patch('rospy.Rate') as mock_rate, \
mock.patch('rospy.init_node') as mock_init_node, \
mock.patch('rospy.Publisher'):

ticker_instance.spin()
self.assertEqual(mock_init_node.call_count, 1)
mock_init_node.assert_called_with(node_name)
self.assertEqual(mock_rate.call_count, 1)
mock_rate.assert_called_with(rate)
self.assertEqual(mock_is_shutdown.call_count, 2)

0 comments on commit 99e9429

Please sign in to comment.