mock.sentinel.usercode

Here are the examples of the python api mock.sentinel.usercode taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

13 Examples 7

Example 1

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    def test_calculate_patches_sys_stdout_in_context(
        self, mock_execute_usercode
    ):
        worksheet = Worksheet()

        def check_stdout(_, context):
            self.assertEquals(type(context['sys'].stdout), MyStdout)
            self.assertEquals(context['sys'].stdout.worksheet, worksheet)
        mock_execute_usercode.side_effect = check_stdout

        calculate(worksheet, sentinel.usercode, sentinel.private_key)

        self.assertNotEqual(type(sys.stdout), MyStdout)

Example 2

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    @patch('sheet.calculate.run_worksheet')
    def test_calculate_puts_curried_run_worksheet_into_context(self, mock_run_worksheet, mock_execute_usercode):
        worksheet = Worksheet()
        calculate(worksheet, sentinel.usercode, sentinel.private_key)
        args, kwargs = mock_execute_usercode.call_args
        context = args[1]
        curried_run_worksheet = context['run_worksheet']
        self.assertEquals(mock_run_worksheet.call_args_list, [])
        curried_run_worksheet(sentinel.urls)
        self.assertCalledOnce(mock_run_worksheet, sentinel.urls, None, sentinel.private_key)

        mock_run_worksheet.reset_mock()
        curried_run_worksheet(sentinel.urls, sentinel.overrides)
        self.assertCalledOnce(mock_run_worksheet, sentinel.urls, sentinel.overrides, sentinel.private_key)

Example 3

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    def test_calculate_clears_previous_worksheet_cell_values_before_executing_usercode(
        self, mock_execute_usercode
    ):
        calls_list = []
        worksheet = Worksheet()
        worksheet.clear_values = Mock()

        worksheet.clear_values.side_effect = lambda *args : calls_list.append('clear values')
        mock_execute_usercode.side_effect  = lambda *args : calls_list.append('execute usercode')

        calculate(worksheet, sentinel.usercode, sentinel.private_key)

        self.assertEquals(calls_list, ['clear values', 'execute usercode'])

Example 4

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode', Mock())
    @patch('sheet.calculate.evaluate_formulae_in_context', Mock())
    @patch('sheet.calculate.time')
    def test_calculate_clears_previous_worksheet_console_text_and_reports_time(self, mock_time):
        recalc_times = [1.3245, 0]
        def mock_time_fn():
            return recalc_times.pop()
        mock_time.side_effect = mock_time_fn
        worksheet = Worksheet()
        worksheet._console_text = 'previous errors'
        worksheet.add_console_text = Mock()

        calculate(worksheet, sentinel.usercode, sentinel.private_key)
        expected_text = 'Took 1.32s'
        self.assertEquals(worksheet.add_console_text.call_args_list[0],
                          ((expected_text,),{'log_type':'system'})
        )

Example 5

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    @patch('sheet.calculate.evaluate_formulae_in_context')
    def test_calculate_clears_previous_worksheet_usercode_error(self, mock_evaluate_formulae_in_context, mock_execute_usercode):
        worksheet = Worksheet()
        worksheet._usercode_error = "Argh!"
        calculate(worksheet, sentinel.usercode, sentinel.private_key)
        self.assertEquals(worksheet._usercode_error, None)

Example 6

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    @patch('sheet.calculate.evaluate_formulae_in_context', Mock())
    def test_calculate_catches_usercode_exceptions(self, mock_execute_usercode):
        worksheet = Worksheet()
        def execute_usercode(_, __):
            exec('1/0\n')
        mock_execute_usercode.side_effect = execute_usercode
        try:
            calculate(worksheet, sentinel.usercode, sentinel.private_key)
        except:
            self.fail("Unhandled exception when executing broken usercode")

Example 7

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    @patch('sheet.calculate.evaluate_formulae_in_context')
    def test_calculate_catches_and_reports_exceptions_in_worksheet_usercode_error_field(self, mock_evaluate_formulae_in_context, mock_execute_usercode):
        worksheet = Worksheet()
        def execute_usercode(_, __):
            exec(
                'import sys\n'
                'def func():\n'
                '    x = my_value\n'
                'func()\n'
            )
        mock_execute_usercode.side_effect = execute_usercode
        calculate(worksheet, sentinel.usercode, sentinel.private_key)
        expected = {
            'message': 'NameError: global name \'my_value\' is not defined',
            'line': 3,
        }
        self.assertEquals(worksheet._usercode_error, expected)

Example 8

Project: dirigible-spreadsheet Source File: test_calculate.py
    def test_calculate_catches_and_reports_syntax_errors_with_special_message_in_worksheet_usercode_error_field(self):

        def patched_execute_usercode(_, __):
            exec('import sys:\nx == my_value')
        worksheet = Worksheet()

        original_execute_usercode = calculate_module.execute_usercode
        calculate_module.execute_usercode = patched_execute_usercode
        try:
            calculate(worksheet, sentinel.usercode, sentinel.private_key)
        finally:
            calculate_module.execute_usercode = original_execute_usercode

        expected = {
            'message': 'Syntax error at character 11',
            'line': 1
        }
        self.assertEquals(worksheet._usercode_error, expected)

Example 9

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    @patch('sheet.calculate.evaluate_formulae_in_context')
    def test_calculate_catches_and_reports_syntax_errors_to_console(self, mock_evaluate_formulae_in_context, mock_execute_usercode):
        worksheet = Worksheet()
        worksheet.add_console_text = Mock()

        def execute_usercode(_, __):
            exec('import sys:\nx == my_value')
        mock_execute_usercode.side_effect = execute_usercode
        calculate(worksheet, sentinel.usercode, sentinel.private_key)

        expected_error_text = 'Syntax error at character 11 (line 1)\n'
        self.assertIn(
            ((expected_error_text,), {}),
            worksheet.add_console_text.call_args_list
        )

Example 10

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.execute_usercode')
    @patch('sheet.calculate.evaluate_formulae_in_context')
    def test_calculate_should_execute_usercode_with_correct_context_and_curried_evaluate_formulae_in_context(
        self, mock_evaluate_formulae_in_context, mock_execute_usercode
    ):
        worksheet = Worksheet()
        calculate(worksheet, sentinel.usercode, sentinel.private_key)

        args, kwargs = mock_execute_usercode.call_args

        self.assertEquals(kwargs, {})

        self.assertEquals(args[0], sentinel.usercode)

        context = args[1]
        self.assertEquals(context['CellRange'], CellRange)
        self.assertEquals(context['DateTime'], DateTime)
        self.assertEquals(context['FormulaError'], FormulaError)
        self.assertEquals(context['_raise'], _raise)
        self.assertEquals(context['sys'], sys)

        self.assertEquals(context['worksheet'], worksheet)
        self.assertEquals(context['load_constants'], load_constants)
        self.assertEquals(context['undefined'], undefined)
        evaluate_formulae = context['evaluate_formulae']
        evaluate_formulae(sentinel.worksheet)
        self.assertEquals(
            mock_evaluate_formulae_in_context.call_args,
            ((sentinel.worksheet, context), {})
        )

Example 11

Project: dirigible-spreadsheet Source File: test_calculate.py
    @patch('sheet.calculate.evaluate_formulae_in_context', Mock())
    @patch('sheet.calculate.execute_usercode')
    @patch('sheet.calculate.time')
    def test_calculate_clears_previous_worksheet_console_text_and_reports_time_when_theres_an_error(self, mock_time, mock_execute_usercode):
        recalc_times = [1.3245, 0]
        def mock_time_fn():
            return recalc_times.pop()
        mock_time.side_effect = mock_time_fn
        def throw_error(_, __):
            raise Exception('argh')
        mock_execute_usercode.side_effect = throw_error
        worksheet = Worksheet()
        worksheet._console_text = 'previous errors\n'
        worksheet.add_console_text = Mock()

        calculate(worksheet, sentinel.usercode, sentinel.private_key)

        self.assertNotIn('previous errors', worksheet._console_text)
        self.assertEquals(
            worksheet.add_console_text.call_args_list[-1],
            (('Took 1.32s',),{'log_type':'system'}),
        )

Example 12

Project: dirigible-spreadsheet Source File: test_calculate.py
    def test_calculate_catches_and_reports_exceptions_to_console(self):

        def patched_execute_usercode(_, context):
            exec(
                'import sys\n'
                'def func():\n'
                '    x = my_value\n'
                'func()\n',
                context
            )
        worksheet = Worksheet()
        worksheet.add_console_text = Mock()
        original_execute_usercode = calculate_module.execute_usercode
        calculate_module.execute_usercode = patched_execute_usercode
        try:
            calculate(worksheet, sentinel.usercode, sentinel.private_key)
        finally:
            calculate_module.execute_usercode = original_execute_usercode

        expected_error_text = dedent("""
                    NameError: global name \'my_value\' is not defined
                        User code line 4
                        User code line 3, in func\n""")[1:]
        self.assertIn(
            call(expected_error_text),
            worksheet.add_console_text.call_args_list
        )

Example 13

Project: dirigible-spreadsheet Source File: test_sheet.py
    @patch('sheet.sheet.calculate_with_timeout')
    def test_calculate_calls_calculate_with_unjsonified_worksheet_and_saves_recalced_json(
        self, mock_calculate
    ):
        sheet = Sheet()
        sheet.jsonify_worksheet = Mock()
        sheet.unjsonify_worksheet = Mock()
        sheet.usercode = sentinel.usercode
        sheet.timeout_seconds = sentinel.timeout_seconds
        sheet.create_private_key = Mock()
        sheet.otp = Mock()

        sheet.calculate()

        self.assertCalledOnce(
            mock_calculate,
            sheet.unjsonify_worksheet.return_value,
            sheet.usercode,
            sheet.timeout_seconds,
            sheet.create_private_key.return_value
        )
        self.assertCalledOnce(sheet.jsonify_worksheet, sheet.unjsonify_worksheet.return_value)