def test_mystdout_pushes_print_commands_to_worksheet(self):
     ws = Worksheet()
     mso = MyStdout(ws)
     mso.write('weeeeeee!')
     ws2 = Worksheet()
     ws2.add_console_text('weeeeeee!', log_type='output')
     self.assertEquals(ws._console_text, ws2._console_text)
Beispiel #2
0
 def test_mystdout_pushes_print_commands_to_worksheet(self):
     ws = Worksheet()
     mso = MyStdout(ws)
     mso.write('weeeeeee!')
     ws2 = Worksheet()
     ws2.add_console_text('weeeeeee!', log_type='output')
     self.assertEquals(ws._console_text, ws2._console_text)
    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
        )
    def test_recalc_cell_catches_cell_errors_and_adds_them_to_console(self):
        cell = Cell()
        cell.formula = "=123"
        cell.python_formula = '_raise(Exception("OMGWTFBBQ"))'
        cell.error = 'old error, just hanging around...'
        worksheet = Worksheet()
        location = (1, 11)
        worksheet[location] = cell

        # Mocked out to avoid explosion -- tested in another unit test.
        node = Mock()
        node.parents = []
        graph = {location: node }

        context = { 'worksheet': worksheet, "_raise": _raise }
        worksheet.add_console_text = Mock()

        recalculate_cell(location, Mock(), graph, context)

        self.assertEqual(
            worksheet[location].error,
            'Exception: OMGWTFBBQ'
        )

        expected_error_text = "Exception: OMGWTFBBQ\n    Formula '%s' in A11\n" % (
                cell.formula)

        self.assertCalledOnce(worksheet.add_console_text, expected_error_text)

        self.assertEquals(worksheet[location].value, undefined)
Beispiel #5
0
    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'
            }),
        )
Beispiel #6
0
    def test_recalc_cell_catches_cell_errors_and_adds_them_to_console(self):
        cell = Cell()
        cell.formula = "=123"
        cell.python_formula = '_raise(Exception("OMGWTFBBQ"))'
        cell.error = 'old error, just hanging around...'
        worksheet = Worksheet()
        location = (1, 11)
        worksheet[location] = cell

        # Mocked out to avoid explosion -- tested in another unit test.
        node = Mock()
        node.parents = []
        graph = {location: node}

        context = {'worksheet': worksheet, "_raise": _raise}
        worksheet.add_console_text = Mock()

        recalculate_cell(location, Mock(), graph, context)

        self.assertEqual(worksheet[location].error, 'Exception: OMGWTFBBQ')

        expected_error_text = "Exception: OMGWTFBBQ\n    Formula '%s' in A11\n" % (
            cell.formula)

        self.assertCalledOnce(worksheet.add_console_text, expected_error_text)

        self.assertEquals(worksheet[location].value, undefined)
Beispiel #7
0
    def test_append_console_text(self):
        ws = Worksheet()
        ws.add_console_text('a first error')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>')

        ws.add_console_text('a second error\noh noez!')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>')

        ws.add_console_text('not an error', log_type='output')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>'
            '<span class="console_output_text">not an error</span>')

        ws.add_console_text('A system timing report, for example :-)',
                            log_type='system')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>'
            '<span class="console_output_text">not an error</span>'
            '<span class="console_system_text">A system timing report, for example :-)</span>'
        )

        ws.add_console_text('<b></b>', log_type='output')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>'
            '<span class="console_output_text">not an error</span>'
            '<span class="console_system_text">A system timing report, for example :-)</span>'
            '<span class="console_output_text">&lt;b&gt;&lt;/b&gt;</span>')
    def test_append_console_text(self):
        ws = Worksheet()
        ws.add_console_text('a first error')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>')

        ws.add_console_text('a second error\noh noez!')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>')

        ws.add_console_text('not an error', log_type='output')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>'
            '<span class="console_output_text">not an error</span>')

        ws.add_console_text('A system timing report, for example :-)', log_type='system')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>'
            '<span class="console_output_text">not an error</span>'
            '<span class="console_system_text">A system timing report, for example :-)</span>')

        ws.add_console_text('<b></b>', log_type='output')
        self.assertEquals(
            ws._console_text,
            '<span class="console_error_text">a first error</span>'
            '<span class="console_error_text">a second error\n'
            'oh noez!</span>'
            '<span class="console_output_text">not an error</span>'
            '<span class="console_system_text">A system timing report, for example :-)</span>'
            '<span class="console_output_text">&lt;b&gt;&lt;/b&gt;</span>')
    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
        )
    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'})
        )
Beispiel #11
0
    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)
Beispiel #12
0
    def test_report_cell_error(self):
        worksheet = Worksheet()
        worksheet.add_console_text = Mock()
        worksheet[1, 2].formula = '=A1'

        report_cell_error(worksheet, (1, 2), ZeroDivisionError('hello'))

        self.assertEqual(worksheet[(1, 2)].error, 'ZeroDivisionError: hello')
        self.assertEqual(worksheet[(1, 2)].value, undefined)
        expected_error_text = dedent('''
            ZeroDivisionError: hello
                Formula '=A1' in A2
            ''')[1:]
        self.assertEqual(
            worksheet.add_console_text.call_args_list,
            [((expected_error_text, ), {})],
        )
Beispiel #13
0
    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'
                          }))
    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'}),
        )
Beispiel #15
0
    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)
    def test_report_cell_error(self):
        worksheet = Worksheet()
        worksheet.add_console_text = Mock()
        worksheet[1, 2].formula = '=A1'

        report_cell_error(worksheet, (1, 2), ZeroDivisionError('hello'))

        self.assertEqual(
            worksheet[(1, 2)].error,
            'ZeroDivisionError: hello'
            )
        self.assertEqual(
            worksheet[(1, 2)].value,
            undefined
            )
        expected_error_text = dedent('''
            ZeroDivisionError: hello
                Formula '=A1' in A2
            ''')[1:]
        self.assertEqual(
            worksheet.add_console_text.call_args_list,
            [((expected_error_text,), {})],
            )