From 0d19bf248a96c2ba4647d7b775a11a924e883850 Mon Sep 17 00:00:00 2001 From: Bryan Date: Tue, 2 Jun 2020 00:01:44 +0800 Subject: [PATCH 1/3] add test for pandas argument and encoding --- testing/test_awswrangler/test_moto.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/testing/test_awswrangler/test_moto.py b/testing/test_awswrangler/test_moto.py index bccb200e1..87df54457 100644 --- a/testing/test_awswrangler/test_moto.py +++ b/testing/test_awswrangler/test_moto.py @@ -1,3 +1,5 @@ +from unittest.mock import ANY + import boto3 import botocore import mock @@ -8,7 +10,6 @@ import awswrangler as wr from awswrangler.exceptions import EmptyDataFrame, InvalidArgumentCombination - from ._utils import ensure_data_types, get_df_csv, get_df_list @@ -219,6 +220,23 @@ def test_csv(s3): assert len(df.columns) == 10 +@mock.patch('pandas.read_csv') +@mock.patch('s3fs.S3FileSystem.open') +def test_read_csv_pass_pandas_arguments_and_encoding_succeed(mock_open, mock_read_csv, s3): + bucket = "bucket" + key = "foo/foo.csv" + path = "s3://{}/{}".format(bucket, key) + s3_object = s3.Object(bucket, key) + s3_object.put(Body=b"foo") + + try: + wr.s3.read_csv(path=path, encoding="ISO-8859-1", sep=",", lineterminator="\r\n") + except TypeError: + pass + mock_open.assert_called_with(path='s3://bucket/foo/foo.csv', mode='r', encoding='ISO-8859-1', newline="\r\n") + mock_read_csv.assert_called_with(ANY, compression=None, encoding="ISO-8859-1", sep=",", lineterminator="\r\n") + + def test_to_csv_invalid_argument_combination_raise_when_dataset_false_succeed(s3): path = "s3://bucket/test.csv" with pytest.raises(InvalidArgumentCombination): From eca88db8997db004c4f2b30fc889a31ebdb42ea1 Mon Sep 17 00:00:00 2001 From: Bryan Date: Tue, 2 Jun 2020 00:16:37 +0800 Subject: [PATCH 2/3] fixed pylint --- testing/test_awswrangler/test_moto.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/testing/test_awswrangler/test_moto.py b/testing/test_awswrangler/test_moto.py index 87df54457..05cdd9e44 100644 --- a/testing/test_awswrangler/test_moto.py +++ b/testing/test_awswrangler/test_moto.py @@ -10,6 +10,7 @@ import awswrangler as wr from awswrangler.exceptions import EmptyDataFrame, InvalidArgumentCombination + from ._utils import ensure_data_types, get_df_csv, get_df_list @@ -220,8 +221,8 @@ def test_csv(s3): assert len(df.columns) == 10 -@mock.patch('pandas.read_csv') -@mock.patch('s3fs.S3FileSystem.open') +@mock.patch("pandas.read_csv") +@mock.patch("s3fs.S3FileSystem.open") def test_read_csv_pass_pandas_arguments_and_encoding_succeed(mock_open, mock_read_csv, s3): bucket = "bucket" key = "foo/foo.csv" @@ -233,7 +234,7 @@ def test_read_csv_pass_pandas_arguments_and_encoding_succeed(mock_open, mock_rea wr.s3.read_csv(path=path, encoding="ISO-8859-1", sep=",", lineterminator="\r\n") except TypeError: pass - mock_open.assert_called_with(path='s3://bucket/foo/foo.csv', mode='r', encoding='ISO-8859-1', newline="\r\n") + mock_open.assert_called_with(path="s3://bucket/foo/foo.csv", mode="r", encoding="ISO-8859-1", newline="\r\n") mock_read_csv.assert_called_with(ANY, compression=None, encoding="ISO-8859-1", sep=",", lineterminator="\r\n") From 5bdbf68d8470651b0c6fa67f1f1338232fe224b2 Mon Sep 17 00:00:00 2001 From: Bryan Date: Tue, 2 Jun 2020 00:53:55 +0800 Subject: [PATCH 3/3] catch the exception by pytest --- testing/test_awswrangler/test_moto.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/testing/test_awswrangler/test_moto.py b/testing/test_awswrangler/test_moto.py index 05cdd9e44..71367f730 100644 --- a/testing/test_awswrangler/test_moto.py +++ b/testing/test_awswrangler/test_moto.py @@ -230,12 +230,10 @@ def test_read_csv_pass_pandas_arguments_and_encoding_succeed(mock_open, mock_rea s3_object = s3.Object(bucket, key) s3_object.put(Body=b"foo") - try: + with pytest.raises(TypeError): wr.s3.read_csv(path=path, encoding="ISO-8859-1", sep=",", lineterminator="\r\n") - except TypeError: - pass - mock_open.assert_called_with(path="s3://bucket/foo/foo.csv", mode="r", encoding="ISO-8859-1", newline="\r\n") - mock_read_csv.assert_called_with(ANY, compression=None, encoding="ISO-8859-1", sep=",", lineterminator="\r\n") + mock_open.assert_called_with(path="s3://bucket/foo/foo.csv", mode="r", encoding="ISO-8859-1", newline="\r\n") + mock_read_csv.assert_called_with(ANY, compression=None, encoding="ISO-8859-1", sep=",", lineterminator="\r\n") def test_to_csv_invalid_argument_combination_raise_when_dataset_false_succeed(s3):