How to use on_my_event method in Molotov

Best Python code snippet using molotov_python

test_plugins.py

Source:test_plugins.py Github

copy

Full Screen

1from enum import Enum2from samtranslator.plugins import SamPlugins, BasePlugin, LifeCycleEvents3from unittest import TestCase4from mock import Mock, patch, call5class TestSamPluginsRegistration(TestCase):6 def setUp(self):7 # Setup the plugin to be a "subclass" of the BasePlugin8 self.mock_plugin_name = "mock_plugin"9 self.mock_plugin = Mock(spec=BasePlugin)10 self.mock_plugin.name = self.mock_plugin_name11 self.sam_plugins = SamPlugins()12 def test_register_must_work(self):13 self.sam_plugins.register(self.mock_plugin)14 self.assertEqual(self.mock_plugin, self.sam_plugins._get(self.mock_plugin_name))15 def test_register_must_raise_on_duplicate_plugin(self):16 self.sam_plugins.register(self.mock_plugin)17 with self.assertRaises(ValueError):18 self.sam_plugins.register(self.mock_plugin)19 def test_register_must_raise_on_invalid_plugin_type(self):20 # Plugin which is not an instance of BaseClass21 bad_plugin = Mock()22 bad_plugin.name = "some name"23 with self.assertRaises(ValueError):24 self.sam_plugins.register(bad_plugin)25 def test_register_must_append_plugins_to_end(self):26 plugin1 = _make_mock_plugin("plugin1")27 plugin2 = _make_mock_plugin("plugin2")28 plugin3 = _make_mock_plugin("plugin3")29 self.sam_plugins.register(plugin1)30 self.sam_plugins.register(plugin2)31 self.sam_plugins.register(plugin3)32 expected = [plugin1, plugin2, plugin3]33 self.assertEqual(expected, self.sam_plugins._plugins)34 def test_must_register_plugins_list_on_initialization(self):35 plugin1 = _make_mock_plugin("plugin1")36 plugin2 = _make_mock_plugin("plugin2")37 plugins_list = [plugin1, plugin2]38 new_sam_plugin = SamPlugins(plugins_list)39 # Also make sure the plugins are in the same order as in input40 self.assertEqual(plugins_list, new_sam_plugin._plugins)41 def test_must_register_single_plugin_on_initialization(self):42 new_sam_plugin = SamPlugins(self.mock_plugin)43 self.assertTrue(new_sam_plugin.is_registered(self.mock_plugin_name))44 def test_len_must_get_num_registered_plugins(self):45 plugin1 = _make_mock_plugin("plugin1")46 plugin2 = _make_mock_plugin("plugin2")47 self.sam_plugins.register(plugin1)48 self.sam_plugins.register(plugin2)49 self.assertEqual(2, len(self.sam_plugins))50 def test_is_registered_must_find_registered_plugins(self):51 self.sam_plugins.register(self.mock_plugin)52 self.assertTrue(self.sam_plugins.is_registered(self.mock_plugin_name))53 def test_is_registered_must_return_false_when_no_plugins_registered(self):54 # No Plugins are registered55 self.assertFalse(self.sam_plugins.is_registered(self.mock_plugin_name))56 def test_is_registered_must_return_false_for_non_existent_plugins(self):57 # Register a plugin but check with some random name58 self.sam_plugins.register(self.mock_plugin)59 self.assertFalse(self.sam_plugins.is_registered("some plugin name"))60 def test_get_must_return_a_registered_plugin(self):61 plugin1 = _make_mock_plugin("plugin1")62 plugin2 = _make_mock_plugin("plugin2")63 self.sam_plugins.register(plugin1)64 self.sam_plugins.register(plugin2)65 self.assertEqual(plugin1, self.sam_plugins._get(plugin1.name))66 self.assertEqual(plugin2, self.sam_plugins._get(plugin2.name))67 def test_get_must_handle_no_registered_plugins(self):68 # NO plugins registered69 self.assertIsNone(self.sam_plugins._get("some plugin"))70 def test_get_must_handle_non_registered_plugins(self):71 # Register one plugin, but try to retrieve some other non-existent plugin72 self.sam_plugins.register(self.mock_plugin)73 self.assertIsNone(self.sam_plugins._get("some plugin"))74class TestSamPluginsAct(TestCase):75 def setUp(self):76 self.sam_plugins = SamPlugins()77 # Create a mock of LifeCycleEvents object with ONE event called "my_event"78 self.mock_lifecycle_events = Mock(spec=LifeCycleEvents)79 self.my_event = Mock(spec=LifeCycleEvents)80 self.my_event.name = "my_event"81 self.my_event.value = "my_event"82 self.mock_lifecycle_events.my_event = self.my_event83 def test_act_must_invoke_correct_hook_method(self):84 # Setup the plugin to return a mock when the "on_" method is invoked85 plugin = _make_mock_plugin("plugin")86 hook_method = Mock()87 setattr(plugin, "on_"+self.my_event.name, hook_method)88 self.sam_plugins.register(plugin)89 # Call the act method with some arguments and verify that the hook was invoked with correct arguments90 arg1 = "foo"91 arg2 = "bar"92 kwargs1 = "kwargs1"93 kwargs2 = "kwargs2"94 self.sam_plugins.act(self.my_event, arg1, arg2, kwargs1=kwargs1, kwargs2=kwargs2)95 hook_method.assert_called_once_with(arg1, arg2, kwargs1=kwargs1, kwargs2=kwargs2)96 def test_act_must_invoke_hook_on_all_plugins(self):97 # Create three plugins, and setup hook methods on it98 plugin1 = _make_mock_plugin("plugin1"); setattr(plugin1, "on_"+self.my_event.name, Mock())99 plugin2 = _make_mock_plugin("plugin2"); setattr(plugin2, "on_"+self.my_event.name, Mock())100 plugin3 = _make_mock_plugin("plugin3"); setattr(plugin3, "on_"+self.my_event.name, Mock())101 self.sam_plugins.register(plugin1)102 self.sam_plugins.register(plugin2)103 self.sam_plugins.register(plugin3)104 # Call the act method with some arguments and verify that the hook was invoked with correct arguments105 arg1 = "foo"106 arg2 = "bar"107 kwargs1 = "kwargs1"108 kwargs2 = "kwargs2"109 self.sam_plugins.act(self.my_event, arg1, arg2, kwargs1=kwargs1, kwargs2=kwargs2)110 plugin1.on_my_event.assert_called_once_with(arg1, arg2, kwargs1=kwargs1, kwargs2=kwargs2)111 plugin2.on_my_event.assert_called_once_with(arg1, arg2, kwargs1=kwargs1, kwargs2=kwargs2)112 plugin3.on_my_event.assert_called_once_with(arg1, arg2, kwargs1=kwargs1, kwargs2=kwargs2)113 def test_act_must_invoke_plugins_in_sequence(self):114 # Create three plugins, and setup hook methods on it115 plugin1 = _make_mock_plugin("plugin1"); setattr(plugin1, "on_"+self.my_event.name, Mock())116 plugin2 = _make_mock_plugin("plugin2"); setattr(plugin2, "on_"+self.my_event.name, Mock())117 plugin3 = _make_mock_plugin("plugin3"); setattr(plugin3, "on_"+self.my_event.name, Mock())118 # Create a parent mock and attach child mocks to help assert order of the calls119 # https://stackoverflow.com/questions/32463321/how-to-assert-method-call-order-with-python-mock120 parent_mock = Mock()121 parent_mock.attach_mock(plugin1.on_my_event, "plugin1_hook")122 parent_mock.attach_mock(plugin2.on_my_event, "plugin2_hook")123 parent_mock.attach_mock(plugin3.on_my_event, "plugin3_hook")124 self.sam_plugins.register(plugin1)125 self.sam_plugins.register(plugin2)126 self.sam_plugins.register(plugin3)127 # Call the act method128 self.sam_plugins.act(self.my_event)129 # Verify calls were made in the specific sequence130 parent_mock.assert_has_calls([call.plugin1_hook(), call.plugin2_hook(), call.plugin3_hook()])131 def test_act_must_skip_if_no_plugins_are_registered(self):132 # Create three plugins, and setup hook methods on it133 plugin1 = _make_mock_plugin("plugin1"); setattr(plugin1, "on_"+self.my_event.name, Mock())134 # Don't register any plugin135 # Call the act method136 self.sam_plugins.act(self.my_event)137 plugin1.on_my_event.assert_not_called()138 def test_act_must_fail_on_invalid_event_type_string(self):139 with self.assertRaises(ValueError):140 self.sam_plugins.act("some event")141 def test_act_must_fail_on_invalid_event_type_object(self):142 with self.assertRaises(ValueError):143 self.sam_plugins.act(Mock())144 def test_act_must_fail_on_invalid_event_type_enum(self):145 class SomeEnum(Enum):146 A = 1147 with self.assertRaises(ValueError):148 self.sam_plugins.act(SomeEnum.A)149 def test_act_must_fail_on_non_existent_hook_method(self):150 # Create a plugin but setup hook method with wrong name151 plugin1 = _make_mock_plugin("plugin1")152 setattr(plugin1, "on_unknown_event", Mock())153 self.sam_plugins.register(plugin1)154 with self.assertRaises(NameError):155 self.sam_plugins.act(self.my_event)156 plugin1.on_unknown_event.assert_not_called()157 def test_act_must_raise_exceptions_raised_by_plugins(self):158 # Create a plugin but setup hook method with wrong name159 plugin1 = _make_mock_plugin("plugin1"); setattr(plugin1, "on_"+self.my_event.name, Mock())160 self.sam_plugins.register(plugin1)161 # Setup the hook to raise exception162 plugin1.on_my_event.side_effect = IOError163 with self.assertRaises(IOError):164 self.sam_plugins.act(self.my_event)165 plugin1.on_my_event.assert_called_once_with()166 def test_act_must_abort_hooks_after_exception(self):167 # ie. after a hook raises an exception, subsequent hooks must NOT be run168 # Create three plugins, and setup hook methods on it169 plugin1 = _make_mock_plugin("plugin1"); setattr(plugin1, "on_"+self.my_event.name, Mock())170 plugin2 = _make_mock_plugin("plugin2"); setattr(plugin2, "on_"+self.my_event.name, Mock())171 plugin3 = _make_mock_plugin("plugin3"); setattr(plugin3, "on_"+self.my_event.name, Mock())172 # Create a parent mock and attach child mocks to help assert order of the calls173 # https://stackoverflow.com/questions/32463321/how-to-assert-method-call-order-with-python-mock174 parent_mock = Mock()175 parent_mock.attach_mock(plugin1.on_my_event, "plugin1_hook")176 parent_mock.attach_mock(plugin2.on_my_event, "plugin2_hook")177 parent_mock.attach_mock(plugin3.on_my_event, "plugin3_hook")178 self.sam_plugins.register(plugin1)179 self.sam_plugins.register(plugin2)180 self.sam_plugins.register(plugin3)181 # setup plugin2 to raise exception182 plugin2.on_my_event.side_effect = IOError183 # Call the act method184 with self.assertRaises(IOError):185 self.sam_plugins.act(self.my_event)186 # Since Plugin2 raised the exception, plugin3's hook must NEVER be called187 parent_mock.assert_has_calls([call.plugin1_hook(), call.plugin2_hook()])188class TestBasePlugin(TestCase):189 def test_initialization_should_set_name(self):190 name = "some name"191 plugin = BasePlugin(name)192 self.assertEqual(name, plugin.name)193 def test_initialization_without_name_must_raise_exception(self):194 with self.assertRaises(ValueError):195 BasePlugin(None)196 def test_on_methods_must_not_do_anything(self):197 data_mock = Mock()198 # Simple test to verify that all methods starting with "on_" prefix must not implement any functionality199 plugin = BasePlugin("name")200 # Add every on_ method here201 plugin.on_before_transform_resource(data_mock, data_mock, data_mock)202 # `method_calls` tracks calls to the mock, its methods, attributes, and *their* methods & attributes203 self.assertEqual([], data_mock.method_calls)204def _make_mock_plugin(name):205 m = Mock(spec=BasePlugin)206 m.name = name...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Molotov automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful