Skip to content

Commit

Permalink
Add decorator function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Jun 8, 2016
1 parent fd5d4c1 commit 194a00d
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions tests/test_core/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,8 @@
require("mock")
from mock import Mock, patch, call, MagicMock

from malcolm.core.method import Method, takes, returns, MapMeta


class DummyClass(object):

def __init__(self):
pass

@takes()
def say_hello(self, name):
"""Say hello"""
print("Hello" + name)

@returns()
def say_goodbye(self, name):
"""Say goodbye"""
print("Goodbye" + name)
from malcolm.core.method import Method, takes, returns
from malcolm.core.mapmeta import OPTIONAL, REQUIRED


class TestMethod(unittest.TestCase):
Expand Down Expand Up @@ -205,20 +190,63 @@ def test_from_dict_deserialize(self, mock_mapmeta):
self.assertEqual(m.defaults, defaults)

@patch("malcolm.core.method.MapMeta")
def test_decorators(self, map_meta_mock):
def test_takes_given_optional(self, map_meta_mock):
m1 = MagicMock()
m2 = MagicMock()
map_meta_mock.side_effect = [m1, m2]
map_meta_mock.return_value = m1
a1 = MagicMock()
a1.name = "name"

dummy = DummyClass()
@takes(a1, OPTIONAL)
def say_hello(name):
"""Say hello"""
print("Hello" + name)

self.assertTrue(hasattr(say_hello, "Method"))
self.assertEqual(m1, say_hello.Method.takes)
m1.add_element.assert_called_once_with(a1, False)
self.assertEqual(0, len(say_hello.Method.defaults))

@patch("malcolm.core.method.MapMeta")
def test_takes_given_defaults(self, map_meta_mock):
m1 = MagicMock()
map_meta_mock.return_value = m1
a1 = MagicMock()
a1.name = "name"

self.assertTrue(hasattr(dummy.say_hello, "Method"))
self.assertIsInstance(dummy.say_hello.Method.takes, MapMeta)
# self.assertEqual(m1, dummy.say_hello.Method.takes)
self.assertTrue(hasattr(dummy.say_goodbye, "Method"))
self.assertIsInstance(dummy.say_goodbye.Method.returns, MapMeta)
# self.assertEqual(m2, dummy.say_hello.Method.returns)
@takes(a1, "User")
def say_hello(name):
"""Say hello"""
print("Hello" + name)

self.assertTrue(hasattr(say_hello, "Method"))
self.assertEqual(m1, say_hello.Method.takes)
m1.add_element.assert_called_once_with(a1, False)
self.assertEqual("User", say_hello.Method.defaults[a1.name])

@patch("malcolm.core.method.MapMeta")
def test_returns_given_valid_sets(self, map_meta_mock):
m1 = MagicMock()
map_meta_mock.return_value = m1
a1 = MagicMock()
a1.name = "name"

@returns(a1, REQUIRED)
def return_hello(name):
"""Return hello"""
return "Hello" + name

self.assertTrue(hasattr(return_hello, "Method"))
self.assertEqual(m1, return_hello.Method.returns)
m1.add_element.assert_called_once_with(a1, True)

@patch("malcolm.core.method.MapMeta")
def test_returns_not_given_req_or_opt_raises(self, _):

with self.assertRaises(ValueError):
@returns(MagicMock(), "Raise Error")
def return_hello(name):
"""Return hello"""
return "Hello" + name

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 194a00d

Please sign in to comment.