How to use the pysoa.test.compatibility.mock.MagicMock function in pysoa

To help you get started, we’ve selected a few pysoa examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eventbrite / pysoa / tests / unit / server / test_types.py View on Github external
def test_call_local_action_other_request_details(self):
        action = mock.MagicMock()
        action.return_value.return_value = ActionResponse(action='another_foo', errors=[Error('BAR', 'Bar error')])

        server = mock.MagicMock()
        server.settings = {'a_setting': 'a_value'}
        server.action_class_map = {'another_foo': action}

        r = EnrichedActionRequest(action='foo', body={'bar': 'baz'}, context={'auth': 'abc123'}, control={'speed': 5})
        r._server = server

        with pytest.raises(ActionError) as error_context:
            r.call_local_action('another_foo', {'height': '10m'})

        assert error_context.value.errors[0].code == 'BAR'

        action.assert_called_once_with(server.settings)
        assert action.return_value.call_count == 1

        other_r = action.return_value.call_args[0][0]
        assert other_r is not r
github eventbrite / pysoa / tests / unit / server / test_standalone.py View on Github external
def test_only_forking_not_limited(self, mock_cpu_count, mock_process):
        server_getter = mock.MagicMock()

        mock_cpu_count.return_value = 2

        sys.argv = ['/path/to/example_service/standalone.py', '-f', '10', '--no-respawn']

        prev_sigint = prev_sigterm = prev_sighup = None
        try:
            prev_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
            prev_sigterm = signal.signal(signal.SIGTERM, signal.SIG_IGN)
            prev_sighup = signal.signal(signal.SIGHUP, signal.SIG_IGN)

            processes = [mock.MagicMock() for _ in range(0, 10)]
            mock_process.side_effect = processes

            standalone.simple_main(server_getter)  # type: ignore
github eventbrite / pysoa / tests / unit / server / test_standalone.py View on Github external
def test_only_forking_limited(self, mock_cpu_count, mock_process):
        server_getter = mock.MagicMock()

        mock_cpu_count.return_value = 1

        sys.argv = ['/path/to/example_service/standalone.py', '-f', '10', '--no-respawn']

        prev_sigint = prev_sigterm = prev_sighup = None
        try:
            prev_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
            prev_sigterm = signal.signal(signal.SIGTERM, signal.SIG_IGN)
            prev_sighup = signal.signal(signal.SIGHUP, signal.SIG_IGN)

            processes = [mock.MagicMock() for _ in range(0, 5)]
            mock_process.side_effect = processes

            standalone.simple_main(server_getter)  # type: ignore

            server_getter.assert_called_once_with()
            self.assertFalse(server_getter.return_value.main.called)

            self.assertEqual(5, mock_process.call_count)
            i = 1
            for call in mock_process.call_args_list:
                self.assertEqual(server_getter.return_value.main, call[1]['target'])
                self.assertEqual('pysoa-worker-{}'.format(i), call[1]['name'])
                self.assertEqual((i, ), call[1]['args'])
                i += 1

            for i, process in enumerate(processes):
github eventbrite / pysoa / tests / unit / server / test_standalone.py View on Github external
def test_only_file_watcher_argument_no_values(self, mock_get_reloader):
        server_getter = mock.MagicMock()

        sys.argv = ['/path/to/example_service/standalone.py', '--use-file-watcher']

        standalone.simple_main(server_getter)  # type: ignore

        server_getter.assert_called_once_with()
        self.assertFalse(server_getter.return_value.main.called)

        assert mock_get_reloader.call_count == 1
        assert mock_get_reloader.call_args_list[0][0][0] in ('', 'pytest', 'coverage')
        assert mock_get_reloader.call_args_list[0][0][1] is None
        assert mock_get_reloader.call_args_list[0][1]['signal_forks'] is False

        self.assertEqual(1, mock_get_reloader.return_value.main.call_count)
        self.assertEqual(
            server_getter.return_value,
github eventbrite / pysoa / tests / unit / server / test_autoreloader.py View on Github external
def test_main_no_args(self):
        reloader = MockReloader('example_service.standalone', ['pysoa'])

        mock_func = mock.MagicMock()

        # noinspection PyUnresolvedReferences
        with mock.patch.object(target=reloader, attribute='watch_and_reload') as mock_watch_and_reload:
            reloader.main(mock_func)

            mock_watch_and_reload.assert_called_once_with(mock_func, (), {})

        self.assertFalse(mock_func.called)
github eventbrite / pysoa / tests / unit / server / test_standalone.py View on Github external
def test_only_forking_not_limited(self, mock_cpu_count, mock_process):
        server_getter = mock.MagicMock()

        mock_cpu_count.return_value = 2

        sys.argv = ['/path/to/example_service/standalone.py', '-f', '10', '--no-respawn']

        prev_sigint = prev_sigterm = prev_sighup = None
        try:
            prev_sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
            prev_sigterm = signal.signal(signal.SIGTERM, signal.SIG_IGN)
            prev_sighup = signal.signal(signal.SIGHUP, signal.SIG_IGN)

            processes = [mock.MagicMock() for _ in range(0, 10)]
            mock_process.side_effect = processes

            standalone.simple_main(server_getter)  # type: ignore

            server_getter.assert_called_once_with()
            self.assertFalse(server_getter.return_value.main.called)

            self.assertEqual(10, mock_process.call_count)
            i = 1
            for call in mock_process.call_args_list:
                self.assertEqual(server_getter.return_value.main, call[1]['target'])
                self.assertEqual('pysoa-worker-{}'.format(i), call[1]['name'])
                self.assertEqual((i, ), call[1]['args'])
                i += 1

            for i, process in enumerate(processes):
github eventbrite / pysoa / tests / unit / server / test_standalone.py View on Github external
def test_file_watcher_argument_some_values_with_forking(self, mock_get_reloader):
        server_getter = mock.MagicMock()

        sys.argv = ['/path/to/example_service/standalone.py', '--use-file-watcher', 'pysoa', '-f', '5']

        standalone.simple_main(server_getter)  # type: ignore

        server_getter.assert_called_once_with()
        self.assertFalse(server_getter.return_value.main.called)

        assert mock_get_reloader.call_count == 1
        assert mock_get_reloader.call_args_list[0][0][0] in ('', 'pytest', 'coverage')
        assert mock_get_reloader.call_args_list[0][0][1] == ['pysoa']
        assert mock_get_reloader.call_args_list[0][1]['signal_forks'] is True

        self.assertEqual(1, mock_get_reloader.return_value.main.call_count)
        self.assertEqual(5, mock_get_reloader.return_value.main.call_args_list[0][0][1][0].fork_processes)
        self.assertEqual(
github eventbrite / pysoa / tests / unit / server / test_types.py View on Github external
def test_call_local_action_no_action(self):
        server = mock.MagicMock()
        server.action_class_map = {'unused_foo': mock.MagicMock()}

        r = EnrichedActionRequest(action='foo', body={'bar': 'baz'})
        r._server = server

        with pytest.raises(ActionError) as error_context:
            r.call_local_action('other_foo', {'color': 'red'})

        assert error_context.value.errors[0].code == 'UNKNOWN'
        assert error_context.value.errors[0].field == 'action'

        response = r.call_local_action('other_foo', {'color': 'red'}, raise_action_errors=False)
        assert response.action == 'other_foo'
        assert response.errors
        assert response.errors[0].code == 'UNKNOWN'
        assert response.errors[0].field == 'action'