Skip to content

Commit

Permalink
Make GSheetsHook return an empty list when there are no values (#27261)
Browse files Browse the repository at this point in the history
Return empty list when there are no values
  • Loading branch information
alexandermalyga committed Oct 26, 2022
1 parent c8b2737 commit 7653c61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion airflow/providers/google/suite/hooks/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_values(
.execute(num_retries=self.num_retries)
)

return response["values"]
return response.get("values", [])

def batch_get_values(
self,
Expand Down
22 changes: 22 additions & 0 deletions tests/providers/google/suite/hooks/test_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ def test_get_values(self, get_conn):
dateTimeRenderOption=DATE_TIME_RENDER_OPTION,
)

@mock.patch("airflow.providers.google.suite.hooks.sheets.GSheetsHook.get_conn")
def test_get_values_empty(self, get_conn):
get_method = get_conn.return_value.spreadsheets.return_value.values.return_value.get
execute_method = get_method.return_value.execute
execute_method.return_value = {}
result = self.hook.get_values(
spreadsheet_id=SPREADSHEET_ID,
range_=RANGE_,
major_dimension=MAJOR_DIMENSION,
value_render_option=VALUE_RENDER_OPTION,
date_time_render_option=DATE_TIME_RENDER_OPTION,
)
assert result == []
execute_method.assert_called_once_with(num_retries=NUM_RETRIES)
get_method.assert_called_once_with(
spreadsheetId=SPREADSHEET_ID,
range=RANGE_,
majorDimension=MAJOR_DIMENSION,
valueRenderOption=VALUE_RENDER_OPTION,
dateTimeRenderOption=DATE_TIME_RENDER_OPTION,
)

@mock.patch("airflow.providers.google.suite.hooks.sheets.GSheetsHook.get_conn")
def test_batch_get_values(self, get_conn):
batch_get_method = get_conn.return_value.spreadsheets.return_value.values.return_value.batchGet
Expand Down

0 comments on commit 7653c61

Please sign in to comment.