From 03726cd5c2fce5f8e12bd9cdc1f4eed735e441ca Mon Sep 17 00:00:00 2001 From: rziemianek Date: Tue, 25 Jun 2024 12:08:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Added=20tests=20for=20converting=20?= =?UTF-8?q?to=20string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/test_sharepoint.py | 36 +++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/unit/test_sharepoint.py b/tests/unit/test_sharepoint.py index ae89543cb..f257003b5 100644 --- a/tests/unit/test_sharepoint.py +++ b/tests/unit/test_sharepoint.py @@ -3,6 +3,17 @@ import pandas as pd from viadot.sources import Sharepoint +DUMMY_CREDS = {"site": "test", "username": "test2", "password": "test"} +SAMPLE_DF = pd.DataFrame( + { + "int_col": [1, 2, 3, 4, 5, None], + "float_col": [1.1, 2.2, 3.3, 3.0, 5.5, 6.6], + "str_col": ["a", "b", "c", "d", "e", "f"], + "nan_col": [None, None, None, None, None, None], + "mixed_col": [1, "text", None, None, 4.2, "text2"], + } +) + class SharepointMock(Sharepoint): def _download_excel(self, url=None): @@ -10,9 +21,7 @@ def _download_excel(self, url=None): def test_sharepoint_default_na(): - dummy_creds = {"site": "test", "username": "test2", "password": "test"} - - s = SharepointMock(credentials=dummy_creds) + s = SharepointMock(credentials=DUMMY_CREDS) df = s.to_df(url="test", na_values=Sharepoint.DEFAULT_NA_VALUES) assert not df.empty @@ -20,12 +29,27 @@ def test_sharepoint_default_na(): def test_sharepoint_custom_na(): - dummy_creds = {"site": "test", "username": "test", "password": "test"} - - s = SharepointMock(credentials=dummy_creds) + s = SharepointMock(credentials=DUMMY_CREDS) df = s.to_df( url="test", na_values=[v for v in Sharepoint.DEFAULT_NA_VALUES if v != "NA"] ) assert not df.empty assert "NA" in list(df["col_a"]) + + +def test_sharepoint_convert_all_to_string_type(): + s = SharepointMock(credentials=DUMMY_CREDS) + converted_df = s._convert_all_to_string_type(df=SAMPLE_DF) + + assert not converted_df.empty + assert (converted_df["nan_col"] == None).all() + + +def test_sharepoint_convert_empty_columns_to_string(): + s = SharepointMock(credentials=DUMMY_CREDS) + converted_df = s._empty_column_to_string(df=SAMPLE_DF) + + assert not converted_df.empty + assert converted_df["float_col"].dtype == float + assert converted_df["nan_col"].dtype == "string"