diff --git a/tests/google_sheets/test_google_sheets.py b/tests/google_sheets/test_google_sheets.py index a65ad0588..9783cf786 100644 --- a/tests/google_sheets/test_google_sheets.py +++ b/tests/google_sheets/test_google_sheets.py @@ -5,6 +5,7 @@ from datetime import datetime from os import path +import numpy as np import pandas as pd from googleapiclient.http import HttpMock from pandas import DataFrame @@ -15,6 +16,8 @@ from toucan_connectors.google_sheets.google_sheets_connector import ( GoogleSheetsConnector, GoogleSheetsDataSource, + parse_cell_value, + serial_number_to_date, ) from toucan_connectors.json_wrapper import JsonWrapper @@ -298,3 +301,35 @@ def test_get_form(mocker): ) expected_results = ['sample data', 'animals'] assert schema['definitions']['sheet']['enum'] == expected_results + + +def test_numeric_dateformat_(): + value = 44303 # Example serial number representing a date + format_ = {'numberFormat': {'type': 'DATE'}} + result = parse_cell_value(value, format_) + assert result == serial_number_to_date(value) + + +def test_empty_string(): + value = '' + format_ = {} + result = parse_cell_value(value, format_) + assert np.isnan(result) + + +def test_other_types(): + value = 'hello' + format_ = {'numberFormat': {'type': 'DATE'}} + result = parse_cell_value(value, format_) + assert result == value + + value = 123 + format_ = {} + result = parse_cell_value(value, format_) + assert result == value + + +def test_default_format(): + value = 44303 # Example serial number representing a date + result = parse_cell_value(value) + assert result == value diff --git a/toucan_connectors/google_sheets/google_sheets_connector.py b/toucan_connectors/google_sheets/google_sheets_connector.py index 2e3496bbd..d64aa89e7 100644 --- a/toucan_connectors/google_sheets/google_sheets_connector.py +++ b/toucan_connectors/google_sheets/google_sheets_connector.py @@ -2,6 +2,7 @@ from datetime import datetime from typing import Any, Callable, Dict, List, Optional, Type +import numpy as np import pandas as pd from dateutil.relativedelta import relativedelta from google.oauth2.credentials import Credentials @@ -214,17 +215,19 @@ def serial_number_to_date(serial_number: float) -> datetime: return SERIAL_REFERENCE_DAY + relativedelta(days=int(serial_number)) -def parse_cell_value(value, format): +def parse_cell_value(value: Any, format_: dict[str, Any] | None = None) -> Any: """ Use the format (if provided) to parse the value in its intended type """ if ( - (type(value) is int or type(value) is float) - and format - and 'numberFormat' in format - and format['numberFormat']['type'] == 'DATE' + isinstance(value, (int, float)) + and format_ is not None + and format_.get('numberFormat', {}).get('type') == 'DATE' ): return serial_number_to_date(value) + elif isinstance(value, str) and value == '': + return np.nan + return value