def test_empty_worksheet_from_json(self): worksheet = worksheet_from_json( json.dumps( { "_console_text": "", "_usercode_error": None, } ) ) self.assertEquals(worksheet._console_text, "") self.assertEquals(worksheet._usercode_error, None) self.assertIsNotNone(worksheet._console_lock)
def test_run_worksheet_should_return_worksheet_with_calculated_values_only( self, mock_urllib2): self.maxDiff = None original_sheet = Worksheet() original_sheet.A2.formula = '1' original_sheet.A2.value = 1 original_sheet.C3.formula = '5' original_sheet.C3.value = 5 original_sheet.E4.formula = '=A2 + C3' original_sheet.E4.value = 6 expected_sheet = Worksheet() expected_sheet.name = 'Untitled' for (col, row), cell in original_sheet.items(): expected_sheet[col, row].value = cell.value foreign_sheet = Sheet() foreign_sheet.owner = User(username='******', password='******') foreign_sheet.owner.save() foreign_sheet.contents_json = worksheet_to_json(original_sheet) foreign_sheet.calculate() mock_opener = mock_urllib2.build_opener.return_value mock_urlopen_file = mock_opener.open.return_value mock_urlopen_file.read.return_value = _sheet_to_value_only_json( foreign_sheet.name, worksheet_from_json(foreign_sheet.contents_json)) worksheet_url = 'ws_url/' result = run_worksheet(worksheet_url, None, sentinel.private_key) target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION) self.assertCalledOnce(mock_opener.open, target_url, data=urlencode({ 'dirigible_l337_private_key': sentinel.private_key })) self.assertEquals(type(result), Worksheet) self.assertEquals(result, expected_sheet)
def test_run_worksheet_should_return_worksheet_with_calculated_values_only(self, mock_urllib2): self.maxDiff = None original_sheet = Worksheet() original_sheet.A2.formula = '1' original_sheet.A2.value = 1 original_sheet.C3.formula = '5' original_sheet.C3.value = 5 original_sheet.E4.formula = '=A2 + C3' original_sheet.E4.value = 6 expected_sheet = Worksheet() expected_sheet.name = 'Untitled' for (col, row), cell in original_sheet.items(): expected_sheet[col, row].value = cell.value foreign_sheet = Sheet() foreign_sheet.owner = User(username='******', password='******') foreign_sheet.owner.save() foreign_sheet.contents_json = worksheet_to_json(original_sheet) foreign_sheet.calculate() mock_opener = mock_urllib2.build_opener.return_value mock_urlopen_file = mock_opener.open.return_value mock_urlopen_file.read.return_value = _sheet_to_value_only_json( foreign_sheet.name, worksheet_from_json(foreign_sheet.contents_json) ) worksheet_url = 'ws_url/' result = run_worksheet(worksheet_url, None, sentinel.private_key) target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION) self.assertCalledOnce(mock_opener.open, target_url, data=urlencode({'dirigible_l337_private_key': sentinel.private_key})) self.assertEquals(type(result), Worksheet) self.assertEquals(result, expected_sheet)
def test_worksheet_from_json_uses_json(self, mock_json): mock_json.loads.return_value = {} worksheet_from_json('{}') self.assertCalledOnce(mock_json.loads, '{}')
def test_worksheet_with_data_from_json(self): worksheet = worksheet_from_json( json.dumps( { "2,29" : { "formula" : "a formula", "value" : 56, "dependencies" : [[4, 3], [2, 1]], "formatted_value": "fifty-six", "error": "b0rken" }, "3,29" : { "formula" : "another formula", "value" : ["value", "is", "a", "list"], "formatted_value": "[the same list]" }, "4,29" : { "formula" : None, "formatted_value": "The formatted object", }, "5,29" : { "formula" : "=2 + 4", "python_formula" : "2 + 4", "value" : 6, "formatted_value": "six", }, "6,29" : { "formula" : "=I don't have a python formula. I don't want one.", "value" : 7, "formatted_value": "seven", }, "_console_text": "The console text", "_usercode_error": { "message": "The usercode error", "line": 23 }, } ) ) self.assertEquals(worksheet.B29.formula, "a formula") self.assertEquals(worksheet.B29.python_formula, None) self.assertEquals(worksheet.B29.dependencies, [(4, 3), (2, 1)]) self.assertEquals(worksheet.B29.value, 56) self.assertEquals(worksheet.B29.formatted_value, "fifty-six") self.assertEquals(worksheet.B29.error, "b0rken") self.assertEquals(worksheet.C29.formula, "another formula") self.assertEquals(worksheet.C29.python_formula, None) self.assertEquals(worksheet.C29.value, ["value", "is", "a", "list"]) self.assertEquals(worksheet.C29.formatted_value, "[the same list]") self.assertEquals(worksheet.D29.formula, None) self.assertEquals(worksheet.D29.python_formula, None) self.assertEquals(worksheet.D29.value, undefined) self.assertEquals(worksheet.D29.formatted_value, "The formatted object") self.assertEquals(worksheet.E29.formula, "=2 + 4") self.assertEquals(worksheet.E29.python_formula, "2 + 4") self.assertEquals(worksheet.E29.value, 6) self.assertEquals(worksheet.E29.formatted_value, "six") self.assertEquals(worksheet.F29.formula, "=I don't have a python formula. I don't want one.") self.assertEquals(worksheet.F29.python_formula, None) self.assertEquals(worksheet.F29.value, 7) self.assertEquals(worksheet.F29.formatted_value, "seven") self.assertEquals(worksheet._console_text, "The console text") self.assertEquals(worksheet._usercode_error, { "message": "The usercode error", "line": 23 }) self.assertIsNotNone(worksheet._console_lock)