Skip to content

Commit

Permalink
test: add unittests of gspread client
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Aug 19, 2024
1 parent 393722d commit 2e7c3c9
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions course_discovery/apps/course_metadata/tests/test_gspread.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest import mock

import gspread

from django.test import TestCase

from course_discovery.apps.course_metadata.gspread_client import GspreadClient
Expand Down Expand Up @@ -29,3 +31,90 @@ def test_get_worksheet_data_by_tab_id(self, _mock_gspread_connection, mock_logge
spread_sheet.worksheets.return_value = []
client.get_worksheet_data_by_tab_id(spread_sheet, '123456')
mock_logger.error.assert_called_with('[Worksheet Not Found]: No worksheet found with id: 123456')

@mock.patch('course_discovery.apps.course_metadata.gspread_client.GspreadClient.get_spread_sheet_by_key')
@mock.patch('course_discovery.apps.course_metadata.gspread_client.GspreadClient.get_worksheet_data_by_tab_id')
@mock.patch('course_discovery.apps.course_metadata.gspread_client.logger')
def test_read_data(self, _mock_logger, mock_get_worksheet_data_by_tab_id, mock_get_spread_sheet_by_key):
"""
Test read_data method of Gspread client with mock data
"""
mock_spreadsheet = mock.Mock()
mock_worksheet_data = [{'header1': 'value1', 'header2': 'value2'}]
mock_get_spread_sheet_by_key.return_value = mock_spreadsheet
mock_get_worksheet_data_by_tab_id.return_value = mock_worksheet_data

client = GspreadClient()
config = {'SHEET_ID': 'sheet_id', 'INPUT_TAB_ID': 'input_tab_id'}
result = client.read_data(config)

mock_get_spread_sheet_by_key.assert_called_once_with('sheet_id')
mock_get_worksheet_data_by_tab_id.assert_called_once_with(mock_spreadsheet, 'input_tab_id')
self.assertEqual(result, mock_worksheet_data)

@mock.patch('course_discovery.apps.course_metadata.gspread_client.ascii_uppercase',
new=list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
)
def test_write_headers(self):
"""
Test write_headers method of Gspread client
"""
mock_sheet_tab = mock.Mock()
headers = ['header1', 'header2']

client = GspreadClient()
client._write_headers(mock_sheet_tab, headers)

mock_sheet_tab.append_row.assert_called_once_with(headers)
mock_sheet_tab.format.assert_called_once_with('A1:B1', {'textFormat': {'bold': True}})

def test_write_rows(self):
"""
Test write_rows method of Gspread client
"""
mock_sheet_tab = mock.Mock()
headers = ['header1', 'header2']
csv_data = [{'header1': 'value1', 'header2': 'value2'}, {'header1': 'value3', 'header2': 'value4'}]

client = GspreadClient()
client._write_rows(mock_sheet_tab, headers, csv_data)

mock_sheet_tab.append_row.assert_any_call(['value1', 'value2'])
mock_sheet_tab.append_row.assert_any_call(['value3', 'value4'])
self.assertEqual(mock_sheet_tab.append_row.call_count, 2)

@mock.patch('course_discovery.apps.course_metadata.gspread_client.GspreadClient._get_or_create_worksheet')
@mock.patch('course_discovery.apps.course_metadata.gspread_client.GspreadClient._write_headers')
@mock.patch('course_discovery.apps.course_metadata.gspread_client.GspreadClient._write_rows')
@mock.patch('course_discovery.apps.course_metadata.gspread_client.GspreadClient.get_spread_sheet_by_key')
@mock.patch('course_discovery.apps.course_metadata.gspread_client.logger')
def test_write_data(
self,
_mock_logger,
mock_get_spread_sheet_by_key,
mock_write_rows,
mock_write_headers,
mock_get_or_create_worksheet,
):
"""
Test write_data method of Gspread client with mock data
"""
mock_spreadsheet = mock.Mock()
mock_sheet_tab = mock.Mock()
mock_get_spread_sheet_by_key.return_value = mock_spreadsheet
mock_get_or_create_worksheet.return_value = mock_sheet_tab

client = GspreadClient()
config = {"SHEET_ID": "sheet_id", "OUTPUT_TAB_ID": "output_tab_id"}
csv_headers = ["header1", "header2"]
csv_data = [{"header1": "value1", "header2": "value2"}]

client.write_data(config, csv_headers, csv_data, overwrite=True)

mock_get_spread_sheet_by_key.assert_called_once_with("sheet_id")
mock_get_or_create_worksheet.assert_called_once_with(
mock_spreadsheet, "output_tab_id", len(csv_headers) + 1, len(csv_data) + 1
)
mock_sheet_tab.clear.assert_called_once()
mock_write_headers.assert_called_once_with(mock_sheet_tab, csv_headers)
mock_write_rows.assert_called_once_with(mock_sheet_tab, csv_headers, csv_data)

0 comments on commit 2e7c3c9

Please sign in to comment.