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'])
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_to_constant_clears_python_formula(self): cell = Cell() cell.python_formula = 'something' cell.formula = 'k' self.assertEquals(cell.python_formula, None)
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_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')
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_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, '' )
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')
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)
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_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")
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_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)
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)
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)
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_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)
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)
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)
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,), {}) )
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)
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)])
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)
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_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, ), {}))
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)
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, '')
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'])
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'')
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))
def test_setting_formatted_value_to_none(self): cell = Cell() cell.formatted_value = None self.assertEquals(cell.formatted_value, '')
def test_getitem_creates_cells(self): ws = Worksheet() try: ws[1, 2].value = Cell() except KeyError: self.fail('Did not create cell on request')
def test_setting_formatted_value_to_string_passes_through(self): cell = Cell() cell.formatted_value = 'this > string' self.assertEquals(cell.formatted_value, 'this > string')
def test_setting_formula_to_string_passes_through(self): cell = Cell() cell.formula = 'this < string' self.assertEquals(cell.formula, 'this < string')
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))
def test_can_use_unicode_in_formula(self): cell = Cell() cell.formula = u'Sacr\xe9 bleu!' self.assertEquals(cell.formula, u'Sacr\xe9 bleu!')
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'>")
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_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'>")
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'')
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!')