Example #1
0
    def test_run_worksheet_with_error(self, mock_urllib2):
        mock_opener = mock_urllib2.build_opener.return_value
        mock_urlopen_file = mock_opener.open.return_value
        mock_urlopen_file.read.return_value = '{ "usercode_error" : { "message": "error", "line": "line_no" } }'

        with self.assertRaises(Exception) as mngr:
            run_worksheet('worksheet_url', None, sentinel.private_key)
        self.assertEquals(str(mngr.exception), 'run_worksheet: error')
    def test_run_worksheet_with_error(self, mock_urllib2):
        mock_opener = mock_urllib2.build_opener.return_value
        mock_urlopen_file = mock_opener.open.return_value
        mock_urlopen_file.read.return_value = '{ "usercode_error" : { "message": "error", "line": "line_no" } }'

        with self.assertRaises(Exception) as mngr:
            run_worksheet('worksheet_url', None, sentinel.private_key)
        self.assertEquals(str(mngr.exception), 'run_worksheet: error')
    def test_run_worksheet_passes_private_key_in_params(self, mock_api_json_to_worksheet, mock_urllib2):
        worksheet_url = 'ws_url/'
        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        mock_api_json_to_worksheet.return_value = Worksheet()

        mock_opener = mock_urllib2.build_opener.return_value
        mock_urlopen_file = mock_opener.open.return_value
        mock_urlopen_file.read.return_value = sentinel.worksheet_json

        run_worksheet(worksheet_url, None, sentinel.private_key)

        self.assertCalledOnce(mock_opener.open, target_url, data=urlencode({'dirigible_l337_private_key': sentinel.private_key}))
Example #4
0
    def test_run_worksheet_with_overrides(self, mock_api_json_to_worksheet,
                                          mock_urllib2, mock_urlencode):
        overrides = {'a1': 55}
        str_overrides = {
            'a1': '55',
            'dirigible_l337_private_key': sentinel.private_key
        }
        mock_api_json_to_worksheet.return_value = Worksheet()
        worksheet_url = 'ws_url/'
        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        result = run_worksheet(worksheet_url, overrides, sentinel.private_key)

        self.assertCalledOnce(mock_urllib2.build_opener)
        mock_opener = mock_urllib2.build_opener.return_value

        self.assertCalledOnce(mock_urlencode, str_overrides)
        encoded_overrides = mock_urlencode.return_value

        self.assertCalledOnce(mock_opener.open,
                              target_url,
                              data=encoded_overrides)
        mock_urlopen_file = mock_opener.open.return_value

        self.assertCalledOnce(mock_api_json_to_worksheet,
                              mock_urlopen_file.read.return_value)
        self.assertEquals(result, mock_api_json_to_worksheet.return_value)
Example #5
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)
Example #6
0
    def test_run_worksheet_passes_private_key_in_params(
            self, mock_api_json_to_worksheet, mock_urllib2):
        worksheet_url = 'ws_url/'
        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        mock_api_json_to_worksheet.return_value = Worksheet()

        mock_opener = mock_urllib2.build_opener.return_value
        mock_urlopen_file = mock_opener.open.return_value
        mock_urlopen_file.read.return_value = sentinel.worksheet_json

        run_worksheet(worksheet_url, None, sentinel.private_key)

        self.assertCalledOnce(mock_opener.open,
                              target_url,
                              data=urlencode({
                                  'dirigible_l337_private_key':
                                  sentinel.private_key
                              }))
    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_run_worksheet_no_overrides(self, mock_api_json_to_worksheet, mock_urllib2):
        mock_api_json_to_worksheet.return_value = Worksheet()
        worksheet_url = 'ws_url/'
        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        result = run_worksheet(worksheet_url, None, sentinel.private_key)

        self.assertCalledOnce(mock_urllib2.build_opener)
        mock_opener = mock_urllib2.build_opener.return_value

        self.assertCalledOnce(mock_opener.open, target_url, data=urlencode({'dirigible_l337_private_key': sentinel.private_key}))
        mock_urlopen_file = mock_opener.open.return_value

        self.assertCalledOnce(mock_api_json_to_worksheet, mock_urlopen_file.read.return_value)
        self.assertEquals(result, mock_api_json_to_worksheet.return_value)
    def test_run_worksheet_with_overrides(self, mock_api_json_to_worksheet, mock_urllib2, mock_urlencode):
        overrides = {'a1': 55}
        str_overrides = {'a1': '55', 'dirigible_l337_private_key': sentinel.private_key}
        mock_api_json_to_worksheet.return_value = Worksheet()
        worksheet_url = 'ws_url/'
        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        result = run_worksheet(worksheet_url, overrides, sentinel.private_key)

        self.assertCalledOnce(mock_urllib2.build_opener)
        mock_opener = mock_urllib2.build_opener.return_value

        self.assertCalledOnce(mock_urlencode, str_overrides)
        encoded_overrides = mock_urlencode.return_value

        self.assertCalledOnce(mock_opener.open, target_url, data=encoded_overrides)
        mock_urlopen_file = mock_opener.open.return_value

        self.assertCalledOnce(mock_api_json_to_worksheet, mock_urlopen_file.read.return_value)
        self.assertEquals(result, mock_api_json_to_worksheet.return_value)
Example #10
0
    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)
Example #11
0
    def test_run_worksheet_no_overrides(self, mock_api_json_to_worksheet,
                                        mock_urllib2):
        mock_api_json_to_worksheet.return_value = Worksheet()
        worksheet_url = 'ws_url/'
        target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION)
        result = run_worksheet(worksheet_url, None, sentinel.private_key)

        self.assertCalledOnce(mock_urllib2.build_opener)
        mock_opener = mock_urllib2.build_opener.return_value

        self.assertCalledOnce(mock_opener.open,
                              target_url,
                              data=urlencode({
                                  'dirigible_l337_private_key':
                                  sentinel.private_key
                              }))
        mock_urlopen_file = mock_opener.open.return_value

        self.assertCalledOnce(mock_api_json_to_worksheet,
                              mock_urlopen_file.read.return_value)
        self.assertEquals(result, mock_api_json_to_worksheet.return_value)
    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)