Exemple #1
0
    def test_setting_formula_with_syntax_error_sets_appropriate_raise_in_python_formula_and_clears_dependencies(
            self):
        cell = Cell()

        cell.dependencies = 'before'
        cell.python_formula = 'before'
        cell.formula = '=#NULL!'
        self.assertEquals(
            cell.python_formula,
            '_raise(FormulaError("Error in formula at position 2: unexpected \'#NULL!\'"))'
        )
        self.assertEquals(cell.dependencies, [])

        cell.dependencies = 'before'
        cell.python_formula = 'before'
        cell.formula = '=#Invalid!'
        self.assertEquals(
            cell.python_formula,
            '_raise(FormulaError("#Invalid! cell reference in formula"))')
        self.assertEquals(cell.dependencies, [])

        cell.dependencies = 'before'
        cell.python_formula = 'before'
        cell.formula = '=#Deleted!'
        self.assertEquals(
            cell.python_formula,
            '_raise(FormulaError("#Deleted! cell reference in formula"))')
        self.assertEquals(cell.dependencies, [])
    def test_setting_formula_with_syntax_error_sets_appropriate_raise_in_python_formula_and_clears_dependencies(self):
        cell = Cell()

        cell.dependencies = 'before'
        cell.python_formula = 'before'
        cell.formula = '=#NULL!'
        self.assertEquals(
            cell.python_formula,
            '_raise(FormulaError("Error in formula at position 2: unexpected \'#NULL!\'"))'
        )
        self.assertEquals(cell.dependencies, [])

        cell.dependencies = 'before'
        cell.python_formula = 'before'
        cell.formula = '=#Invalid!'
        self.assertEquals(
            cell.python_formula,
            '_raise(FormulaError("#Invalid! cell reference in formula"))'
        )
        self.assertEquals(cell.dependencies, [])

        cell.dependencies = 'before'
        cell.python_formula = 'before'
        cell.formula = '=#Deleted!'
        self.assertEquals(
            cell.python_formula,
            '_raise(FormulaError("#Deleted! cell reference in formula"))'
        )
        self.assertEquals(cell.dependencies, [])
Exemple #3
0
    def test_setting_formula_to_constant_clears_python_formula(self):
        cell = Cell()
        cell.python_formula = 'something'

        cell.formula = 'k'

        self.assertEquals(cell.python_formula, None)
Exemple #4
0
    def test_setting_formula_parses_formula_sets_dependencies_then_sets_python_formula(
            self, mock_parser, mock_get_python_formula_from_parse_tree,
            mock_get_dependencies_from_parse_tree):
        call_order = []

        def get_add_call(name, return_value):
            def add_call(*_):
                call_order.append(name)
                return return_value

            return add_call

        mock_get_python_formula_from_parse_tree.side_effect = get_add_call(
            'formula', sentinel.formula)
        mock_get_dependencies_from_parse_tree.side_effect = get_add_call(
            'dependencies', sentinel.dependencies)

        cell = Cell()
        cell.python_formula = '=something'

        cell.formula = '=k'

        self.assertCalledOnce(mock_parser.parse, '=k')

        self.assertCalledOnce(mock_get_python_formula_from_parse_tree,
                              mock_parser.parse.return_value)
        self.assertEquals(cell._python_formula, sentinel.formula)

        self.assertCalledOnce(mock_get_dependencies_from_parse_tree,
                              mock_parser.parse.return_value)
        self.assertEquals(cell.dependencies, sentinel.dependencies)

        self.assertEquals(call_order, ['dependencies', 'formula'])
Exemple #5
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)
    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)
    def test_setting_formula_to_constant_clears_python_formula(self):
        cell = Cell()
        cell.python_formula = 'something'

        cell.formula = 'k'

        self.assertEquals(cell.python_formula, None)
Exemple #8
0
    def test_setting_python_formula_to_non_string_explodes(self):
        cell = Cell()

        with self.assertRaises(TypeError) as mngr:
            cell.python_formula = type

        self.assertEquals(str(mngr.exception),
                          'cell python_formula must be str or unicode')
    def test_setting_formula_to_none_works_and_sets_python_formula_to_none(self):
        cell = Cell()
        cell.python_formula = 'something'

        cell.formula = None

        self.assertEquals(cell.formula, None)
        self.assertEquals(cell.python_formula, None)
Exemple #10
0
    def test_setting_formula_to_none_works_and_sets_python_formula_to_none(
            self):
        cell = Cell()
        cell.python_formula = 'something'

        cell.formula = None

        self.assertEquals(cell.formula, None)
        self.assertEquals(cell.python_formula, None)
    def test_setting_python_formula_to_non_string_explodes(self):
        cell = Cell()

        with self.assertRaises(TypeError) as mngr:
            cell.python_formula = type

        self.assertEquals(
                str(mngr.exception),
                'cell python_formula must be str or unicode'
        )
    def test_recalculate_cell_should_perform_true_division(self):
        location = (1, 2)
        cell = Cell()
        cell.python_formula = '1/4'
        context = { "worksheet": { location: cell } }

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

        recalculate_cell(location, None, { location: node }, context)

        self.assertEquals(cell.value, 0.25)
    def test_recalculate_cell_evals_python_formula_in_context_and_puts_results_in_worksheet(self):
        location = (1, 2)
        cell = Cell()
        cell.python_formula = '100 + fred'
        context = { 'fred': 23, "worksheet": { location: cell } }

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

        recalculate_cell(location, None, { location: node }, context)

        self.assertEquals(cell.value, 123)
Exemple #14
0
    def test_recalculate_cell_should_perform_true_division(self):
        location = (1, 2)
        cell = Cell()
        cell.python_formula = '1/4'
        context = {"worksheet": {location: cell}}

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

        recalculate_cell(location, None, {location: node}, context)

        self.assertEquals(cell.value, 0.25)
Exemple #15
0
    def test_recalculate_cell_evals_python_formula_in_context_and_puts_results_in_worksheet(
            self):
        location = (1, 2)
        cell = Cell()
        cell.python_formula = '100 + fred'
        context = {'fred': 23, "worksheet": {location: cell}}

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

        recalculate_cell(location, None, {location: node}, context)

        self.assertEquals(cell.value, 123)
Exemple #16
0
    def test_clear_clears_stuff(self):
        cell = Cell()

        cell.value = 29
        cell.formula = 'e equals emcee squared'
        cell.python_formula = 'e equals emcee squared'
        cell.dependencies = [(1, 1), (2, 2)]
        cell.formatted_value = "wibble"
        cell.error = 'a spear'

        cell.clear()

        self.assertEquals(cell.value, undefined)
        self.assertEquals(cell.formula, None)
        self.assertEquals(cell.python_formula, None)
        self.assertEquals(Cell().dependencies, [])
        self.assertEquals(cell.formatted_value, u"")
        self.assertEquals(cell.error, None)
    def test_clear_clears_stuff(self):
        cell = Cell()

        cell.value = 29
        cell.formula = 'e equals emcee squared'
        cell.python_formula = 'e equals emcee squared'
        cell.dependencies = [(1, 1), (2, 2)]
        cell.formatted_value = "wibble"
        cell.error = 'a spear'

        cell.clear()

        self.assertEquals(cell.value, undefined)
        self.assertEquals(cell.formula, None)
        self.assertEquals(cell.python_formula, None)
        self.assertEquals(Cell().dependencies, [])
        self.assertEquals(cell.formatted_value, u"")
        self.assertEquals(cell.error, None)
    def test_setting_formula_parses_formula_sets_dependencies_then_sets_python_formula(
            self, mock_parser, mock_get_python_formula_from_parse_tree, mock_get_dependencies_from_parse_tree
    ):
        call_order = []
        def get_add_call(name, return_value):
            def add_call(*_):
                call_order.append(name)
                return return_value
            return add_call
        mock_get_python_formula_from_parse_tree.side_effect = get_add_call('formula', sentinel.formula)
        mock_get_dependencies_from_parse_tree.side_effect = get_add_call('dependencies', sentinel.dependencies)

        cell = Cell()
        cell.python_formula = '=something'

        cell.formula = '=k'

        self.assertCalledOnce(mock_parser.parse, '=k')

        self.assertCalledOnce(mock_get_python_formula_from_parse_tree,
            mock_parser.parse.return_value
        )
        self.assertEquals(
            cell._python_formula,
            sentinel.formula
        )

        self.assertCalledOnce(mock_get_dependencies_from_parse_tree,
            mock_parser.parse.return_value
        )
        self.assertEquals(
            cell.dependencies,
            sentinel.dependencies
        )

        self.assertEquals(call_order, ['dependencies', 'formula'])