def test_inherit_actions(self): M1 = ManagerType('M1', (Manager,), { 'a_m1': mock.Mock(spec=ActionProperty), }) M2 = ManagerType('M2', (M1,), { 'a_m2': mock.Mock(spec=ActionProperty), }) self.assertEqual(M2.get_all_actions(), ['a_m1', 'a_m2'])
def setUp(self): @view('object') def as_object(self, resource, response): response.set_header('x-my-header', 'oh-snap') return {'a': 1} self.as_object = as_object self.manager = ManagerType('NewManager', (Manager, ), { 'as_object': as_object })
def test_class_with_inheritance(self): @view('xml') def as_xml(): pass other_manager = ManagerType('NewManager', (self.manager, ), { 'as_xml': as_xml }) self.assertEqual(other_manager.get_all_formats(), { 'object': self.as_object, 'xml': as_xml, })
class TestManagerView(unittest.TestCase): def setUp(self): @view('object') def as_object(self, resource, response): response.set_header('x-my-header', 'oh-snap') return {'a': 1} self.as_object = as_object self.manager = ManagerType('NewManager', (Manager, ), { 'as_object': as_object }) def test_class_with_views(self): self.assertEqual(self.manager.get_all_formats(), { 'object': self.as_object, }) def test_class_with_inheritance(self): @view('xml') def as_xml(): pass other_manager = ManagerType('NewManager', (self.manager, ), { 'as_xml': as_xml }) self.assertEqual(other_manager.get_all_formats(), { 'object': self.as_object, 'xml': as_xml, })
def test_manager_rf(self): rf = mock.MagicMock(spec=dict) with mock.patch('napixd.managers.base.ResourceFields') as RF: m = ManagerType('Manager', (Manager,), { 'resource_fields': rf }) self.assertEqual(m._resource_fields, RF.return_value) RF.assert_called_once_with(rf)
def setUp(self): def send_mail(self, resource, dest, subject='export'): """send a mail""" return dest, subject #Like a duck self.fn = mock.MagicMock( __name__=send_mail.__name__, __doc__=send_mail.__doc__, spec=types.FunctionType, func_code=send_mail.func_code, ) self.action = action(self.fn) self.Manager = ManagerType('MyManager', (Manager, ), { 'send_mail': self.action, 'get_resource': mock.Mock(spec=True, return_value={'mpm': 'prefork'}), })
def setUp(self): self.Manager = ManagerType('Serializable', (AttrResourceMixin, Manager), { 'resource_fields': { 'a': { 'example': 1 }, 'b': { 'example': 'abc' } } })
def implements(cls): """ If :meth:`save` is not overriden """ implemented = ManagerType.implements(cls) if cls.save == DictManager.save: implemented.discard('create_resource') implemented.discard('delete_resource') implemented.discard('modify_resource') if cls.generate_id == DictManager.generate_id and cls.generate_new_id == DictManager.generate_new_id: implemented.discard('create_resource') return implemented
class TestManagerAction(unittest.TestCase): def setUp(self): def send_mail(self, resource, dest, subject='export'): """send a mail""" return dest, subject #Like a duck self.fn = mock.MagicMock( __name__=send_mail.__name__, __doc__=send_mail.__doc__, spec=types.FunctionType, func_code=send_mail.func_code, ) self.action = action(self.fn) self.Manager = ManagerType('MyManager', (Manager, ), { 'send_mail': self.action, 'get_resource': mock.Mock(spec=True, return_value={'mpm': 'prefork'}), }) def test_class_with_actions(self): self.assertEqual(self.Manager.get_all_actions(), ['send_mail']) def test_action_class_property(self): a = self.Manager.send_mail self.assertEqual(a.__doc__, 'send a mail') self.assertEqual(a.__name__, 'send_mail') def test_action_instance_property(self): m = self.Manager(mock.Mock(), mock.Mock()) r = mock.Mock() m.send_mail(r, 123, mpm='prefork') self.fn.assert_called_once_with(m, r, 123, mpm='prefork') def test_uba_rf(self): with mock.patch('napixd.managers.actions.ResourceFieldsDict') as RFD: uba = self.action.__get__(None, mock.Mock()) RFD.assert_called_once_with(self.fn, self.action.resource_fields) self.assertEqual(uba.resource_fields, RFD.return_value)
def testFalseString(self): m = ManagerType('Manager', (Manager,), { 'managed_class': ['abc'] }) self.assertEqual(m.get_managed_classes(), [ManagedClass('abc')])
def testFalseManagedClass(self): m = ManagerType('Manager', (Manager,), { 'managed_class': [ManagedClass(self.SubManager, 'ploc')] }) self.assertEqual(m.get_managed_classes(), [ ManagedClass(self.SubManager, 'ploc')])
def testNone(self): m = ManagerType('Manager', (Manager,), { 'managed_class': None }) self.assertEqual(m.get_managed_classes(), [])
def test_implements(self): M1 = ManagerType('M1', (Manager,), { 'get_resource': mock.Mock, 'list_resource': mock.Mock, }) self.assertEqual(M1.implements(), set(['get_resource', 'list_resource']))