コード例 #1
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)
コード例 #2
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)
コード例 #3
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)
コード例 #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'])
コード例 #5
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, [])
コード例 #6
0
    def test_eq_neq(self):
        cell1 = Cell()
        cell1.formula = '=formula'
        cell2 = Cell()
        cell2.formula = '=formula'
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        cell1.value = 10
        self.assertFalse(cell1 == cell2)
        self.assertTrue(cell1 != cell2)
        cell2.value = 10
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        cell1.formatted_value = 'formatted'
        self.assertFalse(cell1 == cell2)
        self.assertTrue(cell1 != cell2)
        cell2.formatted_value = 'formatted'
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        cell1.error = 'this error'
        self.assertFalse(cell1 == cell2)
        self.assertTrue(cell1 != cell2)
        cell2.error = 'this error'
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        self.assertFalse(cell1 == 'hello')
        self.assertTrue(cell1 != 'hello')
コード例 #7
0
    def test_recalc_cell_should_clear_cell_error_and_not_add_to_console_text_on_eval_succeeding(self):
        cell = Cell()
        cell.formula = '=123'
        cell.error = 'old error, just hanging around...'
        worksheet = Worksheet()
        location = (1, 11)
        worksheet[location] = cell

        node = Mock()
        node.parents = []
        graph = {location: node }

        context = { 'worksheet': { location: cell, } }

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

        self.assertEqual(
            worksheet[location].error,
            None
        )
        self.assertEqual(
            worksheet[location].value,
            123
        )
        self.assertEqual(
            worksheet._console_text, ''
        )
コード例 #8
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)
コード例 #9
0
    def test_setting_formatted_value_to_non_string_explodes(self):
        class TestObject(object):
            pass
        cell = Cell()

        with self.assertRaises(TypeError) as mngr:
            cell.formatted_value = TestObject()
        self.assertEquals(str(mngr.exception), 'cell formatted_value must be str or unicode')
コード例 #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)
コード例 #11
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')
コード例 #12
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)
コード例 #13
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'
        )
コード例 #14
0
    def test_clear_value_clears_value_but_not_formatted_value(self):
        cell = Cell()

        cell.value = 29
        cell.formatted_value = "wibble"

        cell.clear_value()

        self.assertEquals(cell.value, undefined)
        self.assertEquals(cell.formatted_value, "wibble")
コード例 #15
0
    def test_load_constants_should_clear_errors_for_constants(self):
        cell = Cell()
        cell.formula = "a constant"
        cell.error = 'Ohno!'
        worksheet = Worksheet()
        worksheet.A1 = cell

        load_constants(worksheet)

        self.assertIsNone(cell.error)
コード例 #16
0
    def test_setting_formatted_value_to_non_string_explodes(self):
        class TestObject(object):
            pass

        cell = Cell()

        with self.assertRaises(TypeError) as mngr:
            cell.formatted_value = TestObject()
        self.assertEquals(str(mngr.exception),
                          'cell formatted_value must be str or unicode')
コード例 #17
0
    def test_load_constants_should_clear_errors_for_constants(self):
        cell = Cell()
        cell.formula = "a constant"
        cell.error = 'Ohno!'
        worksheet = Worksheet()
        worksheet.A1 = cell

        load_constants(worksheet)

        self.assertIsNone(cell.error)
コード例 #18
0
    def test_should_not_reprocess_locations_already_in_visited_even_if_it_is_in_worksheet(
        self, mock_add_location_dependencies
    ):
        cell = Cell()
        cell.formula = 'constant'
        worksheet = Worksheet()
        worksheet[1, 2] = cell

        _generate_cell_subgraph(worksheet, sentinel.graph, (1, 2), set([(1, 2)]), [])

        self.assertFalse(mock_add_location_dependencies.called)
コード例 #19
0
    def test_should_not_reprocess_locations_already_in_visited_even_if_it_is_in_worksheet(
            self, mock_add_location_dependencies):
        cell = Cell()
        cell.formula = 'constant'
        worksheet = Worksheet()
        worksheet[1, 2] = cell

        _generate_cell_subgraph(worksheet, sentinel.graph, (1, 2),
                                set([(1, 2)]), [])

        self.assertFalse(mock_add_location_dependencies.called)
コード例 #20
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)
コード例 #21
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)
コード例 #22
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)
コード例 #23
0
    def test_run_worksheet_with_overrides(self, mock_urllib2):
        self.maxDiff = None

        cellA2 = Cell()
        cellA2.formula = '1'
        cellA2.value = 1

        cellC3 = Cell()
        cellC3.formula = '5'
        cellC3.value = 5

        cellE4 = Cell()
        cellE4.formula = '=A2 + C3'
        cellE4.value = 6

        overrides = {
            (1, 2): '11',
            (3, 3): 55,
            (4, 1): '="abc"',
            'dirigible_l337_private_key': sentinel.private_key
        }

        result_of_calculation_json = '''{
            "name": "Untitled",
            "1": {
                "2": 11
            },
            "3": {
                "3": 55
            },
            "4": {
                "1": "abc"
            },
            "5": {
                "4": 66
            }
        }'''

        mock_opener = mock_urllib2.build_opener.return_value
        mock_urlopen_file = mock_opener.open.return_value
        mock_urlopen_file.read.return_value = result_of_calculation_json

        worksheet_url = 'ws_url/'
        result = run_worksheet(worksheet_url, overrides, sentinel.private_key)

        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        self.assertCalledOnce(mock_opener.open, target_url, data=urlencode(overrides))
        self.assertEquals(type(result), Worksheet)
        expected_sheet = Worksheet()
        expected_sheet.name = 'Untitled'
        expected_sheet[1, 2].value = 11
        expected_sheet[3, 3].value = 55
        expected_sheet[4, 1].value = 'abc'
        expected_sheet[5, 4].value = 66

        self.assertEquals(result, expected_sheet)
コード例 #24
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)
コード例 #25
0
    def test_recalculate_cell_should_remove_nodes_from_dependency_graph(self):
        location = (1, 2)
        cell = Cell()
        cell.formula = '=123'
        leaf_queue = Queue()
        parent = Mock()
        parent_loc = (2, 3)
        node = Mock()
        node.parents = set([(parent_loc)])
        graph = { location: node, parent_loc: parent }
        context = { 'worksheet': { location: cell, } }

        recalculate_cell(location, leaf_queue, graph, context)

        self.assertEquals( node.remove_from_parents.call_args, (([parent], leaf_queue,), {}) )
コード例 #26
0
    def test_setitem_on_nonexistent_cell(self):
        cell_range = CellRange(self.ws, (2, 2), (10, 9))
        cell_range[2, 2].value = 'new val'
        self.assertEquals(self.ws[3, 3].value, 'new val')

        new_cell = Cell()
        cell_range[2, 3] = new_cell
        self.assertEquals(self.ws[3, 4], new_cell)
コード例 #27
0
    def test_setitem_on_locations_should_accept_cell_instances(self):
        ws = Worksheet()
        ws.to_location = Mock(return_value=(1, 2))
        cell = Cell()

        ws[3, 4] = cell

        self.assertEquals(ws.to_location.call_args_list, [(((3, 4), ), {})])
        self.assertEquals(ws.keys(), [(1, 2)])
コード例 #28
0
 def test_initialisation(self):
     self.assertEquals(Cell().formula, None)
     self.assertEquals(Cell().python_formula, None)
     self.assertEquals(Cell().dependencies, [])
     self.assertEquals(Cell().value, undefined)
     self.assertEquals(Cell().formatted_value, u'')
     self.assertEquals(Cell().error, None)
コード例 #29
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)
コード例 #30
0
    def test_recalculate_cell_should_remove_nodes_from_dependency_graph(self):
        location = (1, 2)
        cell = Cell()
        cell.formula = '=123'
        leaf_queue = Queue()
        parent = Mock()
        parent_loc = (2, 3)
        node = Mock()
        node.parents = set([(parent_loc)])
        graph = {location: node, parent_loc: parent}
        context = {
            'worksheet': {
                location: cell,
            }
        }

        recalculate_cell(location, leaf_queue, graph, context)

        self.assertEquals(node.remove_from_parents.call_args, ((
            [parent],
            leaf_queue,
        ), {}))
コード例 #31
0
    def to_cells(self, start, end):
        start_col, start_row = start
        end_col, end_row = end

        strings_dict = jsonlib.loads(self.contents_json)

        for col in xrange(0, end_col - start_col + 1):
            for row in xrange(0, end_row - start_row + 1):

                clip_loc = col % self.width, row % self.height

                clip_cell = strings_dict['%s,%s' % clip_loc]
                dest_cell = Cell()
                if clip_cell['formula']:
                    column_offset, row_offset = self._get_offset(col, row, start_col, start_row)
                    dest_cell.formula = rewrite_formula(
                        clip_cell['formula'], column_offset, row_offset,
                        self.is_cut, self.source_range
                    )

                dest_cell.formatted_value = clip_cell['formatted_value']
                dest_loc = col + start_col, row + start_row
                yield (dest_loc, dest_cell)
コード例 #32
0
    def test_sheet_to_ui_json_grid_data_should_not_include_totally_empty_cells(
            self):
        worksheet = Worksheet()
        worksheet.A1 = Cell()

        expected_json_contents = {
            'bottom': 10,
            'left': 0,
            'right': 10,
            'topmost': 0
        }
        self.assertEquals(
            json.loads(sheet_to_ui_json_grid_data(worksheet, (0, 0, 10, 10))),
            expected_json_contents)
コード例 #33
0
    def to_cells(self, start, end):
        start_col, start_row = start
        end_col, end_row = end

        strings_dict = jsonlib.loads(self.contents_json)

        for col in xrange(0, end_col - start_col + 1):
            for row in xrange(0, end_row - start_row + 1):

                clip_loc = col % self.width, row % self.height

                clip_cell = strings_dict['%s,%s' % clip_loc]
                dest_cell = Cell()
                if clip_cell['formula']:
                    column_offset, row_offset = self._get_offset(
                        col, row, start_col, start_row)
                    dest_cell.formula = rewrite_formula(
                        clip_cell['formula'], column_offset, row_offset,
                        self.is_cut, self.source_range)

                dest_cell.formatted_value = clip_cell['formatted_value']
                dest_loc = col + start_col, row + start_row
                yield (dest_loc, dest_cell)
コード例 #34
0
    def test_recalc_cell_should_clear_cell_error_and_not_add_to_console_text_on_eval_succeeding(
            self):
        cell = Cell()
        cell.formula = '=123'
        cell.error = 'old error, just hanging around...'
        worksheet = Worksheet()
        location = (1, 11)
        worksheet[location] = cell

        node = Mock()
        node.parents = []
        graph = {location: node}

        context = {
            'worksheet': {
                location: cell,
            }
        }

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

        self.assertEqual(worksheet[location].error, None)
        self.assertEqual(worksheet[location].value, 123)
        self.assertEqual(worksheet._console_text, '')
コード例 #35
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'])
コード例 #36
0
    def test_clear_value_clears_value_but_not_formatted_value(self):
        cell = Cell()

        cell.value = 29
        cell.formatted_value = "wibble"

        cell.clear_value()

        self.assertEquals(cell.value, undefined)
        self.assertEquals(cell.formatted_value, "wibble")
コード例 #37
0
def worksheet_from_json(json_string):
    #use jsonlib for read ops because of better performance
    #keep simplejson for write ops as it's more robust
    worksheet_dict = jsonlib.read(json_string)
    worksheet = Worksheet()
    for (key, value) in worksheet_dict.iteritems():
        if key == "_console_text":
            worksheet._console_text = value
        elif key == "_usercode_error":
            worksheet._usercode_error = value
        else:
            col_str, row_str = key.split(",")
            cell = Cell()
            cell._formula = value["formula"]
            cell._python_formula = value.get("python_formula")
            cell.dependencies = map(tuple, value.get("dependencies", []))
            cell.error = value.get("error")
            cell._value = value.get("value", undefined)
            cell.formatted_value = value["formatted_value"]
            worksheet[int(col_str), int(row_str)] = cell
    return worksheet
コード例 #38
0
 def test_setting_formatted_value_to_string_passes_through(self):
     cell = Cell()
     cell.formatted_value = 'this > string'
     self.assertEquals(cell.formatted_value, 'this > string')
コード例 #39
0
 def test_setting_value_sets_formatted_value_to_unicode_version(self):
     cell = Cell()
     cell._set_formatted_value = Mock()
     cell.value = sentinel.value
     self.assertCalledOnce(cell._set_formatted_value, unicode(sentinel.value))
コード例 #40
0
 def test_setting_value_to_undefined_sets_formatted_value_to_empty_string(self):
     cell = Cell()
     cell._set_formatted_value = Mock()
     cell.value = undefined
     self.assertCalledOnce(cell._set_formatted_value, u'')
コード例 #41
0
 def test_setting_formula_to_string_passes_through(self):
     cell = Cell()
     cell.formula = 'this < string'
     self.assertEquals(cell.formula, 'this < string')
コード例 #42
0
    def test_repr(self):
        cell = Cell()
        cell.formula = 'f'
        self.assertEquals(
            repr(cell),
            "<Cell formula=f value=<undefined> formatted_value=u''>")

        cell = Cell()
        cell.value = 'v'
        self.assertEquals(
            repr(cell), "<Cell formula=None value='v' formatted_value=u'v'>")

        cell = Cell()
        cell.value = 23
        self.assertEquals(
            repr(cell), "<Cell formula=None value=23 formatted_value=u'23'>")

        cell = Cell()
        cell.formula = 'f'
        cell.value = 'v'
        self.assertEquals(repr(cell),
                          "<Cell formula=f value='v' formatted_value=u'v'>")

        cell = Cell()
        cell.formula = 'f'
        cell.value = 'v'
        cell.formatted_value = u'fv'
        self.assertEquals(repr(cell),
                          "<Cell formula=f value='v' formatted_value=u'fv'>")

        cell = Cell()
        cell.formula = 'f'
        cell.value = 'v'
        cell.formatted_value = u'fv'
        cell.error = 'e'
        self.assertEquals(
            repr(cell),
            "<Cell formula=f value='v' formatted_value=u'fv' error='e'>")
コード例 #43
0
 def test_setting_value_to_ws_doesnt_die(self):
     # This is actually mostly testing that it doesn't explode
     cell = Cell()
     ws = Worksheet()
     cell.value = ws
     self.assertEquals(cell.formatted_value, unicode(ws))
コード例 #44
0
    def test_repr(self):
        cell = Cell()
        cell.formula = 'f'
        self.assertEquals(repr(cell), "<Cell formula=f value=<undefined> formatted_value=u''>")

        cell = Cell()
        cell.value = 'v'
        self.assertEquals(repr(cell), "<Cell formula=None value='v' formatted_value=u'v'>")

        cell = Cell()
        cell.value = 23
        self.assertEquals(repr(cell), "<Cell formula=None value=23 formatted_value=u'23'>")

        cell = Cell()
        cell.formula = 'f'
        cell.value = 'v'
        self.assertEquals(repr(cell), "<Cell formula=f value='v' formatted_value=u'v'>")

        cell = Cell()
        cell.formula = 'f'
        cell.value = 'v'
        cell.formatted_value = u'fv'
        self.assertEquals(repr(cell), "<Cell formula=f value='v' formatted_value=u'fv'>")

        cell = Cell()
        cell.formula = 'f'
        cell.value = 'v'
        cell.formatted_value = u'fv'
        cell.error = 'e'
        self.assertEquals(repr(cell), "<Cell formula=f value='v' formatted_value=u'fv' error='e'>")
コード例 #45
0
 def test_can_use_unicode_in_formula(self):
     cell = Cell()
     cell.formula = u'Sacr\xe9 bleu!'
     self.assertEquals(cell.formula, u'Sacr\xe9 bleu!')
コード例 #46
0
 def test_getitem_creates_cells(self):
     ws = Worksheet()
     try:
         ws[1, 2].value = Cell()
     except KeyError:
         self.fail('Did not create cell on request')
コード例 #47
0
 def test_setting_formatted_value_to_none(self):
     cell = Cell()
     cell.formatted_value = None
     self.assertEquals(cell.formatted_value, '')
コード例 #48
0
 def test_setting_value_sets_formatted_value_to_unicode_version(self):
     cell = Cell()
     cell._set_formatted_value = Mock()
     cell.value = sentinel.value
     self.assertCalledOnce(cell._set_formatted_value,
                           unicode(sentinel.value))
コード例 #49
0
    def __getitem__(self, key):
        location = self.to_location(key)
        if not location:
            raise InvalidKeyError("%r is not a valid cell location" % (key,))

        return self.setdefault(location, Cell())
コード例 #50
0
 def test_setting_value_to_undefined_sets_formatted_value_to_empty_string(
         self):
     cell = Cell()
     cell._set_formatted_value = Mock()
     cell.value = undefined
     self.assertCalledOnce(cell._set_formatted_value, u'')
コード例 #51
0
 def test_setting_formatted_value_to_none(self):
     cell = Cell()
     cell.formatted_value = None
     self.assertEquals(cell.formatted_value, '')
コード例 #52
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)
コード例 #53
0
 def test_setting_formatted_value_to_string_passes_through(self):
     cell = Cell()
     cell.formatted_value = 'this > string'
     self.assertEquals(cell.formatted_value, 'this > string')
コード例 #54
0
    def test_eq_neq(self):
        cell1 = Cell()
        cell1.formula = '=formula'
        cell2 = Cell()
        cell2.formula = '=formula'
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        cell1.value = 10
        self.assertFalse(cell1 == cell2)
        self.assertTrue(cell1 != cell2)
        cell2.value = 10
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        cell1.formatted_value = 'formatted'
        self.assertFalse(cell1 == cell2)
        self.assertTrue(cell1 != cell2)
        cell2.formatted_value = 'formatted'
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        cell1.error = 'this error'
        self.assertFalse(cell1 == cell2)
        self.assertTrue(cell1 != cell2)
        cell2.error = 'this error'
        self.assertTrue(cell1 == cell2)
        self.assertFalse(cell1 != cell2)

        self.assertFalse(cell1 == 'hello')
        self.assertTrue(cell1 != 'hello')
コード例 #55
0
 def test_setting_value_to_ws_doesnt_die(self):
     # This is actually mostly testing that it doesn't explode
     cell = Cell()
     ws = Worksheet()
     cell.value = ws
     self.assertEquals(cell.formatted_value, unicode(ws))
コード例 #56
0
    def test_run_worksheet_with_overrides(self, mock_urllib2):
        self.maxDiff = None

        cellA2 = Cell()
        cellA2.formula = '1'
        cellA2.value = 1

        cellC3 = Cell()
        cellC3.formula = '5'
        cellC3.value = 5

        cellE4 = Cell()
        cellE4.formula = '=A2 + C3'
        cellE4.value = 6

        overrides = {
            (1, 2): '11',
            (3, 3): 55,
            (4, 1): '="abc"',
            'dirigible_l337_private_key': sentinel.private_key
        }

        result_of_calculation_json = '''{
            "name": "Untitled",
            "1": {
                "2": 11
            },
            "3": {
                "3": 55
            },
            "4": {
                "1": "abc"
            },
            "5": {
                "4": 66
            }
        }'''

        mock_opener = mock_urllib2.build_opener.return_value
        mock_urlopen_file = mock_opener.open.return_value
        mock_urlopen_file.read.return_value = result_of_calculation_json

        worksheet_url = 'ws_url/'
        result = run_worksheet(worksheet_url, overrides, sentinel.private_key)

        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        self.assertCalledOnce(mock_opener.open,
                              target_url,
                              data=urlencode(overrides))
        self.assertEquals(type(result), Worksheet)
        expected_sheet = Worksheet()
        expected_sheet.name = 'Untitled'
        expected_sheet[1, 2].value = 11
        expected_sheet[3, 3].value = 55
        expected_sheet[4, 1].value = 'abc'
        expected_sheet[5, 4].value = 66

        self.assertEquals(result, expected_sheet)
コード例 #57
0
 def test_setting_formatted_value_to_unicode(self):
     cell = Cell()
     cell.formatted_value = u'Sacr\xe9 bleu!'
     self.assertEquals(cell.formatted_value, u'Sacr\xe9 bleu!')