Skip to content

Commit

Permalink
Adding tests for the execute method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoramite committed Aug 28, 2017
1 parent 9cba646 commit 5a94113
Showing 1 changed file with 75 additions and 12 deletions.
87 changes: 75 additions & 12 deletions grow/preprocessors/google_drive_test.py
Expand Up @@ -64,6 +64,71 @@ def test_convert_rows_to_mapping(self):
result = preprocessor._convert_rows_to_mapping(rows)
self.assertDictEqual(expected, result)

@mock.patch.object(google_drive.BaseGooglePreprocessor, 'create_service')
def test_sheets_export_path_list(self, mock_service_sheets):
path = '/content/pages/sheet.yaml'
config = google_drive.GoogleSheetsPreprocessor.Config(
id='A1B2C3D4E5F6', format='list', gid=765, path=path)
preprocessor = google_drive.GoogleSheetsPreprocessor(self.pod, config)
mock_sheets_service = self._setup_mocks(sheets_get={
'spreadsheetId': 'A1B2C3D4E5F6',
'sheets': [{
'properties': {
'title': 'sheet1',
'sheetId': 765,
'gridProperties': {
'columnCount': 4,
'rowCount': 1000
},
},
}]
}, sheets_values={
'values': [
['id', 'name', 'age', '_comment'],
['1', 'Jim', 27, 'commenting'],
['2', 'Sue', 23, 'something'],
],
})
mock_service_sheets.return_value = mock_sheets_service['service']
preprocessor.execute(config)
formatted_data = [{'age': 27, 'id': '1', 'name': 'Jim'},
{'age': 23, 'id': '2', 'name': 'Sue'}]
result = self.pod.read_yaml(path)
self.assertEqual(formatted_data, result)

@mock.patch.object(google_drive.BaseGooglePreprocessor, 'create_service')
def test_sheets_export_collection_string(self, mock_service_sheets):
config = google_drive.GoogleSheetsPreprocessor.Config(
id='A1B2C3D4E5F6', format='string', collection='/content/pages/')
preprocessor = google_drive.GoogleSheetsPreprocessor(self.pod, config)
mock_sheets_service = self._setup_mocks(sheets_get={
'spreadsheetId': 'A1B2C3D4E5F6',
'sheets': [{
'properties': {
'title': 'sheet1',
'sheetId': 765,
'gridProperties': {
'columnCount': 2,
'rowCount': 1000
},
},
}]
}, sheets_values={
'values': [
['id', 'name'],
['key', 'value'],
['other_key', 'foo'],
],
})
mock_service_sheets.return_value = mock_sheets_service['service']
preprocessor.execute(config)
formatted_data = {
'key': 'value',
'other_key': 'foo',
}
result = self.pod.read_yaml('/content/pages/sheet1.yaml')
self.assertEqual(formatted_data, result)

@mock.patch.object(google_drive.BaseGooglePreprocessor, 'create_service')
def test_sheets_download_list(self, mock_service_sheets):
preprocessor = google_drive.GoogleSheetsPreprocessor
Expand Down Expand Up @@ -264,7 +329,6 @@ def test_sheets_download_empty(self, mock_service_sheets):
765: []
}, gid_to_data)


def test_format_content(self):
rows = [
['key', 'value'],
Expand All @@ -284,9 +348,8 @@ def test_format_content(self):
result = google_drive.GoogleSheetsPreprocessor.format_content(
content=content, path=path, format_as='map')
self.assertEqual(content_dict, result)
serialized_result = \
google_drive.GoogleSheetsPreprocessor.serialize_content(
formatted_data=result, path=path)
serialized_result = google_drive.GoogleSheetsPreprocessor.serialize_content(
formatted_data=result, path=path)
expected = yaml.safe_dump(content_dict, default_flow_style=False)
self.assertEqual(expected, serialized_result)

Expand All @@ -296,9 +359,8 @@ def test_format_content(self):
result = google_drive.GoogleSheetsPreprocessor.format_content(
content=content, path=path, existing_data=existing_data,
format_as='map', key_to_update='address')
serialized_result = \
google_drive.GoogleSheetsPreprocessor.serialize_content(
formatted_data=result, path=path)
serialized_result = google_drive.GoogleSheetsPreprocessor.serialize_content(
formatted_data=result, path=path)
expected = copy.deepcopy(content_dict)
expected['address'] = {
'color': 'red',
Expand All @@ -313,9 +375,8 @@ def test_format_content(self):
content=content, path=path, format_as='map')
self.assertEqual(content_dict, result)

serialized_result = \
google_drive.GoogleSheetsPreprocessor.serialize_content(
formatted_data=result, path=path)
serialized_result = google_drive.GoogleSheetsPreprocessor.serialize_content(
formatted_data=result, path=path)
expected = json.dumps(content_dict)
self.assertEqual(expected, serialized_result)

Expand Down Expand Up @@ -376,8 +437,10 @@ def test_get_edit_url(self):

def test_parse_path(self):
preprocessor = google_drive.GoogleSheetsPreprocessor
self.assertEqual(('/path/a.yaml', None), preprocessor.parse_path('/path/a.yaml'))
self.assertEqual(['/path/a.yaml', 'b'], preprocessor.parse_path('/path/a.yaml:b'))
self.assertEqual(('/path/a.yaml', None),
preprocessor.parse_path('/path/a.yaml'))
self.assertEqual(['/path/a.yaml', 'b'],
preprocessor.parse_path('/path/a.yaml:b'))


if __name__ == '__main__':
Expand Down

0 comments on commit 5a94113

Please sign in to comment.